1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
// Copyright (c) 2012-2017 VideoStitch SAS
// Copyright (c) 2018 stitchEm
#include "gpu/testing.hpp"
#include "libvideostitch/dirutils.hpp"
#include <cstdio>
#include <iostream>
#include <fstream>
#include <string.h>
#include <iostream>
#include <sys/types.h> // For stat().
#include <sys/stat.h> // For stat().
#if defined(_MSC_VER)
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>
#include <algorithm>
#include <direct.h>
#include <io.h> // For access().
#include <Lmcons.h>
#else
#include <dirent.h>
#endif
namespace VideoStitch {
namespace Testing {
bool createDirectory(const std::string& absolutePath) {
int err = 0;
#if defined(_MSC_VER)
err = _mkdir(absolutePath.c_str()); // can be used on Windows
#else
err = mkdir(absolutePath.c_str(), 0700); // can be used on non-Windows
#endif
return err == 0;
}
Status deleteFile(const std::string& fileName) {
int ret = remove(fileName.c_str());
if (ret != 0) {
return {Origin::Output, ErrType::RuntimeError,
"Error deleting file " + fileName + ", " + std::string(strerror(errno))};
}
return Status::OK();
}
// delete directory, included files and subfolders
Status deleteDir(const std::string& directory) {
// check if directory exists
if (!Util::directoryExists(directory)) {
return Status::OK();
}
#if defined(_MSC_VER)
WIN32_FIND_DATA file_data;
HANDLE hFind = INVALID_HANDLE_VALUE;
std::string dirWin = directory;
std::replace(dirWin.begin(), dirWin.end(), '/', '\\');
dirWin += '\\';
// delete all files and subfolders
if ((hFind = FindFirstFile((dirWin + "*.*").c_str(), &file_data)) != INVALID_HANDLE_VALUE) {
do {
const std::string fileName = file_data.cFileName;
const std::string fullFileName = dirWin + fileName;
const bool isDirectory = (file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
if (fileName.compare(".") == 0 || fileName.compare("..") == 0) {
continue;
}
// check if it is a directory
if (isDirectory) {
// delete directory
std::cout << "removing " << std::string(file_data.cFileName) << " <DIR>" << std::endl;
FAIL_CAUSE(deleteDir(fullFileName), Origin::Output, ErrType::RuntimeError, "Error deleting " + fullFileName);
} else {
// delete file
std::cout << "removing " << fullFileName << std::endl;
FAIL_RETURN(deleteFile(fullFileName));
}
} while (FindNextFile(hFind, &file_data));
FindClose(hFind);
}
// finally delete directory
if (RemoveDirectory(dirWin.c_str()) == FALSE) {
return {Origin::Output, ErrType::RuntimeError, "Error removing directory " + dirWin};
}
#else
DIR* dir;
struct dirent* ent;
struct stat st;
dir = opendir(directory.c_str());
if (dir != NULL) {
while (dir != NULL && (ent = readdir(dir)) != NULL) {
const std::string file_name = ent->d_name;
const std::string full_file_name = directory + "/" + file_name;
if (file_name.compare(".") == 0 || file_name.compare("..") == 0) {
continue;
}
if (stat(full_file_name.c_str(), &st) == -1) {
continue;
}
const bool is_directory = (st.st_mode & S_IFDIR) != 0;
if (is_directory) {
// delete directory
std::cout << "removing " << std::string(full_file_name) << " <DIR>" << std::endl;
FAIL_CAUSE(deleteDir(full_file_name), Origin::Output, ErrType::RuntimeError,
"Error deleting " + full_file_name);
} else {
// delete file
std::cout << "removing " << full_file_name << std::endl;
FAIL_RETURN(deleteFile(full_file_name));
}
}
closedir(dir);
}
int rmRet = -1;
const int MAX_RETRY = 5;
int retry = 0;
do {
rmRet = rmdir(directory.c_str());
} while (rmRet != 0 && retry++ < MAX_RETRY);
if (rmRet != 0) {
return {Origin::Output, ErrType::RuntimeError,
"Error removing directory " + directory + ", " + std::string(strerror(errno))};
}
#endif
return Status::OK();
}
void testGenericDataFolder() {
Status ret;
// get default VideoStitch data path, creating it if not yet created
PotentialValue<std::string> potPath = Util::getVSDataLocation();
ENSURE(potPath.ok(), potPath.ok() ? "" : potPath.status().getErrorMessage().c_str());
// make sub dir
std::string testFolder = potPath.value() + "/testFolder";
if (!Util::directoryExists(testFolder)) {
ENSURE(createDirectory(testFolder), "Error creating default VideoStitch data test subfolder");
}
// create test file
const std::string& testFilePath = testFolder + "/test.txt";
std::ofstream ofs;
ofs.open(testFilePath.c_str());
ENSURE(ofs.is_open(), "error creating a file in default VideoStitch data location");
ofs << "Writing some data\n";
ofs.close();
// load test file
std::ifstream ifs;
ifs.open(testFilePath.c_str());
ENSURE(ifs.is_open(), "error loading test file in default VideoStitch data location");
ifs.close();
// delete test file
const int err = remove(testFilePath.c_str());
ENSURE(err == 0, "error deleting test file in default VideoStitch data location");
// verify it has been deleted
struct stat buffer;
ENSURE(stat(testFilePath.c_str(), &buffer) != 0);
// delete VideoStitch data folder
ret = deleteDir(potPath.value());
ENSURE(ret.ok(), ret.ok() ? "" : ret.getErrorMessage().c_str());
}
void testGenericCacheFolder() {
Status ret;
// get default VideoStitch cache path, creating it if not yet created
PotentialValue<std::string> potPath = Util::getVSCacheLocation();
ENSURE(potPath.ok(), potPath.ok() ? "" : potPath.status().getErrorMessage().c_str());
// make sub dir
std::string testFolder = potPath.value() + "/testFolder";
if (!Util::directoryExists(testFolder)) {
ENSURE(createDirectory(testFolder), "Error creating cache test subfolder");
}
// create test file
const std::string& testFilePath = testFolder + "/test.txt";
std::ofstream ofs;
ofs.open(testFilePath.c_str());
ENSURE(ofs.is_open(), "error creating a file in default VideoStitch cache location");
ofs << "Writing some data\n";
ofs.close();
// load test file
std::ifstream ifs;
ifs.open(testFilePath.c_str());
ENSURE(ifs.is_open(), "error loading test file in default VideoStitch cache location");
ifs.close();
// delete test file
const int err = remove(testFilePath.c_str());
ENSURE(err == 0, "error deleting test file in default VideoStitch cache location");
// verify it has been deleted
struct stat buffer;
ENSURE(stat(testFilePath.c_str(), &buffer) != 0);
// delete VideoStitch cache folder
ret = deleteDir(potPath.value());
ENSURE(ret.ok(), ret.ok() ? "" : ret.getErrorMessage().c_str());
}
} // namespace Testing
} // namespace VideoStitch
int main() {
VideoStitch::Testing::initTest();
VideoStitch::Testing::testGenericDataFolder();
VideoStitch::Testing::testGenericCacheFolder();
return 0;
}