-
Notifications
You must be signed in to change notification settings - Fork 290
/
filesystem.cpp
466 lines (348 loc) · 10.8 KB
/
filesystem.cpp
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/*
* Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include "filesystem.h"
#include "alphanum.h"
#include "Process.h"
#include <sys/stat.h>
#include <algorithm>
#include <strings.h>
#include <string>
#include <fstream>
#include <streambuf>
#include <glob.h>
#include "logging.h"
// absolutePath
std::string absolutePath( const std::string& relative_path )
{
if( relative_path.size() != 0 )
{
const char first_char = relative_path[0];
if( first_char == '/' || first_char == '\\' || first_char == '~' )
return relative_path;
}
return pathJoin(Process::GetWorkingDir(), relative_path);
}
// locateFile
std::string locateFile( const std::string& path )
{
std::vector<std::string> locations;
return locateFile(path, locations);
}
// locateFile
std::string locateFile( const std::string& path, std::vector<std::string>& locations )
{
// check the given path first
if( fileExists(path.c_str()) )
return path;
// add standard search locations
locations.push_back(Process::GetExecutableDir());
locations.push_back("/usr/local/bin/");
locations.push_back("/usr/local/");
locations.push_back("/opt/");
locations.push_back("images/");
locations.push_back("/usr/local/bin/images/");
// check each location until the file is found
const size_t numLocations = locations.size();
for( size_t n=0; n < numLocations; n++ )
{
const std::string str = pathJoin(locations[n], path);
if( fileExists(str.c_str()) )
return str;
}
return "";
}
// loadFile
size_t loadFile( const std::string& path, void** bufferOut )
{
// determine the file size
const size_t file_size = fileSize(path);
if( file_size == 0 )
return 0;
// allocate memory to hold the file
void* buffer = (void*)malloc(file_size);
if( !buffer )
{
LogError("failed to allocate %zu bytes to read %s\n", file_size, path.c_str());
return 0;
}
// read the file
FILE* file = fopen(path.c_str(), "rb");
if( !file )
{
LogError("failed to open %s\n", path.c_str());
free(buffer);
return 0;
}
// read the serialized engine into memory
const size_t bytes_read = fread(buffer, 1, file_size, file);
if( bytes_read != file_size )
{
LogError("only read %zu of %zu bytes from %s\n", bytes_read, file_size, path.c_str());
free(buffer);
return 0;
}
fclose(file);
if( bufferOut != NULL )
*bufferOut = buffer;
return bytes_read;
}
// readFile
std::string readFile( const std::string& path )
{
// https://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html
std::ifstream in(path, std::ios::in | std::ios::binary);
if( !in )
{
LogError("failed to find/open file %s\n", path.c_str());
return std::string();
}
const std::string contents((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
if( contents.length() == 0 )
{
LogWarning("file was empty - %s\n", path.c_str());
}
return contents;
}
// listDir
bool listDir( const std::string& path_in, std::vector<std::string>& output, uint32_t mask )
{
std::string path = path_in;
if( path.size() == 0 )
return false;
// add a wildcard under directories, otherwise just the dir will be returned
const bool pathIsDir = fileIsType(path, FILE_DIR|FILE_LINK);
if( pathIsDir )
path = pathJoin(path, "*");
// glob the files - https://www.man7.org/linux/man-pages/man3/glob.3.html
glob_t globList;
const int result = glob(path.c_str(), GLOB_PERIOD|GLOB_MARK|GLOB_BRACE|GLOB_TILDE_CHECK, NULL, &globList);
if( result != 0 )
{
if( result == GLOB_NOSPACE )
{
LogError("listDir('%s') - ran out of memory\n", path.c_str());
}
else if( result == GLOB_ABORTED )
{
LogError("listDir('%s') - aborted due to read error or permissions\n", path.c_str());
}
else if( result == GLOB_NOMATCH )
{
const char firstChar = path[0];
// if nothing was found and a full path wasn't specified, try the exe path
if( firstChar != '.' && firstChar != '/' && firstChar != '\\' && firstChar != '*' && firstChar != '?' && firstChar != '~' )
return listDir(pathJoin(Process::GetExecutableDir(), path), output, mask);
else
LogError("listDir('%s') - found no matches\n", path.c_str());
}
return false;
}
// populate the output vector, and filter by file type
for( size_t n=0; n < globList.gl_pathc; n++ )
{
// if there's a type mask, check that it matches
if( mask != 0 && !fileIsType(globList.gl_pathv[n], mask) )
continue;
output.push_back(globList.gl_pathv[n]);
}
globfree(&globList);
// sort list alphanumerically (glob actually already does this)
std::sort(output.begin(), output.end(), doj::alphanum_less<std::string>());
if( output.size() == 0 )
{
LogError("%s didn't match any files\n", path.c_str());
return false;
}
return true;
}
// fileType
uint32_t fileType( const std::string& path )
{
if( path.size() == 0 )
return FILE_MISSING;
struct stat fileStat;
const int result = stat(path.c_str(), &fileStat);
if( result == -1 )
{
//printf("%s does not exist.\n", path.c_str());
return FILE_MISSING;
}
if( S_ISREG(fileStat.st_mode) )
return FILE_REGULAR;
else if( S_ISDIR(fileStat.st_mode) )
return FILE_DIR;
else if( S_ISLNK(fileStat.st_mode) )
return FILE_LINK;
else if( S_ISCHR(fileStat.st_mode) )
return FILE_CHAR;
else if( S_ISBLK(fileStat.st_mode) )
return FILE_BLOCK;
else if( S_ISFIFO(fileStat.st_mode) )
return FILE_FIFO;
else if( S_ISSOCK(fileStat.st_mode) )
return FILE_SOCKET;
return FILE_MISSING;
}
// fileIsType
bool fileIsType( const std::string& path, uint32_t mask )
{
if( path.size() == 0 )
return false;
const uint32_t type = fileType(path);
if( type == FILE_MISSING )
return false;
if( mask == 0 )
return true;
if( (type & mask) != type )
return false;
return true;
}
// fileExists
bool fileExists( const std::string& path, uint32_t mask )
{
return fileIsType(path, mask);
}
// fileSize
size_t fileSize( const std::string& path )
{
if( path.size() == 0 )
return 0;
struct stat fileStat;
const int result = stat(path.c_str(), &fileStat);
if( result == -1 )
{
LogError("%s does not exist.\n", path.c_str());
return 0;
}
//printf("%s size %zu bytes\n", path, (size_t)fileStat.st_size);
return fileStat.st_size;
}
// splitPath
std::pair<std::string, std::string> splitPath( const std::string& path )
{
const std::string::size_type slashIdx = path.find_last_of("/");
const std::string ext = fileExtension(path);
if( slashIdx == std::string::npos )
{
if( ext.length() > 0 )
return std::pair<std::string, std::string>("", path);
else
return std::pair<std::string, std::string>(path, "");
}
return std::pair<std::string, std::string>(path.substr(0, slashIdx + 1), path.substr(slashIdx + 1));
}
// pathFilename
std::string pathFilename( const std::string& path )
{
const std::string::size_type slashIdx = path.find_last_of("/");
if( slashIdx == std::string::npos )
return path;
return path.substr(slashIdx + 1);
}
// pathDir
std::string pathDir( const std::string& path )
{
const std::string::size_type slashIdx = path.find_last_of("/");
if( slashIdx == std::string::npos || slashIdx == 0 )
return path;
return path.substr(0, slashIdx + 1);
}
// pathJoin
std::string pathJoin( const std::string& a, const std::string& b )
{
if( a.size() == 0 )
return b;
if( b.size() == 0 )
return a;
// check if there is already a path separator at the end
const char lastChar = a[a.size()-1];
if( lastChar == '/' || lastChar == '\\' )
return a + b;
return a + "/" + b;
}
// fileExtension
std::string fileExtension( const std::string& path )
{
const std::string::size_type dotIdx = path.find_last_of(".");
if( dotIdx == std::string::npos )
return "";
std::string ext = path.substr(dotIdx + 1);
transform(ext.begin(), ext.end(), ext.begin(), tolower);
return ext;
}
// fileHasExtension
bool fileHasExtension( const std::string& path, const std::string& extension )
{
std::vector<std::string> extensions;
extensions.push_back(extension);
return fileHasExtension(path, extensions);
}
// fileHasExtension
bool fileHasExtension( const std::string& path, const char** extensions )
{
if( !extensions )
return false;
std::vector<std::string> extList;
uint32_t extCount = 0;
while(true)
{
if( !extensions[extCount] )
break;
extList.push_back(extensions[extCount]);
extCount++;
}
return fileHasExtension(path, extList);
}
// fileHasExtension
bool fileHasExtension( const std::string& path, const std::vector<std::string>& extensions )
{
const std::string pathExtension = fileExtension(path);
const size_t numExtensions = extensions.size();
if( pathExtension.size() == 0 )
return false;
if( numExtensions == 0 )
return false;
for( size_t n=0; n < numExtensions; n++ )
{
if( extensions[n].size() == 0 )
continue;
if( strcasecmp(pathExtension.c_str(), extensions[n].c_str()) == 0 )
return true;
}
return false;
}
// fileRemoveExtension
std::string fileRemoveExtension( const std::string& filename )
{
const std::string::size_type dotIdx = filename.find_last_of(".");
const std::string::size_type slashIdx = filename.find_last_of("/");
if( dotIdx == std::string::npos )
return filename;
if( slashIdx != std::string::npos && dotIdx < slashIdx )
return filename;
return filename.substr(0, dotIdx);
}
// fileChangeExtension
std::string fileChangeExtension(const std::string& filename, const std::string& newExtension)
{
return fileRemoveExtension(filename).append(newExtension);
}