-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCcpFileUtils.cpp
More file actions
747 lines (645 loc) · 14.7 KB
/
CcpFileUtils.cpp
File metadata and controls
747 lines (645 loc) · 14.7 KB
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
// Copyright © 2014 CCP ehf.
#include "include/CcpFileUtils.h"
#include "include/StringConversions.h"
#include "include/CcpSecureCrt.h"
#include <stdlib.h>
#ifdef _WIN32
int ConvertShareMode( CcpShareMode shareMode )
{
int shflag = 0;
switch( shareMode )
{
case CCP_SM_NOSHARING:
shflag = _SH_DENYRW;
break;
case CCP_SM_READSHARING:
shflag = _SH_DENYWR;
break;
case CCP_SM_RWSHARING:
shflag = _SH_DENYNO;
break;
}
return shflag;
}
int ConvertOpenMode( CcpOpenMode mode )
{
int oflag = 0;
switch( mode )
{
case CCP_OM_READONLY:
oflag = _O_RDONLY;
break;
case CCP_OM_READWRITE:
oflag = _O_CREAT | _O_RDWR;
break;
}
return oflag;
}
int CcpOpenFile( const wchar_t* filename, CcpOpenMode mode, CcpShareMode shareMode )
{
int oflag = ConvertOpenMode( mode );
int shflag = ConvertShareMode( shareMode );
int fd;
_wsopen_s( &fd, filename, oflag | _O_BINARY, shflag, _S_IREAD | _S_IWRITE );
return fd;
}
int CcpCreateFile( const wchar_t* filename, CcpShareMode shareMode )
{
int fd;
int shflag = ConvertShareMode( shareMode );
_wsopen_s( &fd, filename, _O_CREAT | _O_TRUNC | O_RDWR | _O_BINARY, shflag, _S_IREAD | _S_IWRITE );
return fd;
}
void CcpCloseFile( int fd )
{
_close( fd );
}
ssize_t CcpReadFromFile( int fd, void* buf, size_t numBytes )
{
return _read( fd, buf, (unsigned int)numBytes );
}
ssize_t CcpWriteToFile( int fd, const void* buf, size_t numBytes )
{
return _write( fd, buf, (unsigned int)numBytes );
}
off_t CcpLseek( int fd, off_t offset, int whence )
{
return _lseek( fd, offset, whence );
}
off_t CcpTell( int fd )
{
return _tell( fd );
}
#else
int ConvertShareMode( CcpShareMode shareMode )
{
int shflag = 0;
#ifndef __ANDROID__
switch( shareMode )
{
case CCP_SM_NOSHARING:
shflag = O_EXLOCK;
break;
case CCP_SM_READSHARING:
shflag = O_SHLOCK;
break;
case CCP_SM_RWSHARING:
shflag = 0;
break;
}
#endif
return shflag;
}
int ConvertOpenMode( CcpOpenMode mode )
{
int oflag = 0;
switch( mode )
{
case CCP_OM_READONLY:
oflag = O_RDONLY;
break;
case CCP_OM_READWRITE:
oflag = O_CREAT | O_RDWR;
break;
}
return oflag;
}
int CcpOpenFile( const wchar_t* filename, CcpOpenMode mode, CcpShareMode shareMode )
{
int oflag = ConvertOpenMode( mode );
int shflag = ConvertShareMode( shareMode );
int fd = open( CW2A( filename ), oflag | shflag, S_IRUSR | S_IWUSR );
return fd;
}
int CcpCreateFile( const wchar_t* filename )
{
int fd = open( CW2A( filename ), O_CREAT | O_TRUNC | O_RDWR, S_IRUSR | S_IWUSR );
return fd;
}
int CcpCreateFile( const wchar_t* filename, CcpShareMode shareMode )
{
int shflag = ConvertShareMode( shareMode );
int fd = open( CW2A( filename ), O_CREAT | O_TRUNC | O_RDWR | shflag, S_IRUSR | S_IWUSR );
return fd;
}
void CcpCloseFile( int fd )
{
close( fd );
}
ssize_t CcpReadFromFile( int fd, void* buf, size_t numBytes )
{
return read( fd, buf, (unsigned int)numBytes );
}
ssize_t CcpWriteToFile( int fd, const void* buf, size_t numBytes )
{
return write( fd, buf, (unsigned int)numBytes );
}
off_t CcpLseek( int fd, off_t offset, int whence )
{
return lseek( fd, offset, whence );
}
off_t CcpTell( int fd )
{
return lseek( fd, 0, SEEK_CUR );
}
#endif
bool CcpRenameFile( const std::wstring& src, const std::wstring& dst )
{
#ifdef _WIN32
wchar_t srcBuffer[CCP_MAX_PATH];
wchar_t dstBuffer[CCP_MAX_PATH];
GetFullPathNameW( src.c_str(), CCP_MAX_PATH, srcBuffer, NULL );
GetFullPathNameW( dst.c_str(), CCP_MAX_PATH, dstBuffer, NULL );
if( !MoveFileW( srcBuffer, dstBuffer ) )
{
return false;
}
#else
if( rename( CW2A( src.c_str() ), CW2A( dst.c_str() ) ) )
{
return false;
}
#endif
return true;
}
#if defined(_WIN32)
#else
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
#endif
#ifdef _WIN32
std::wstring CcpGetAbsolutePath( const std::wstring& name )
{
std::wstring absName;
wchar_t buffer[CCP_MAX_PATH];
DWORD len = GetFullPathNameW( name.c_str(), CCP_MAX_PATH, buffer, NULL );
if( len > 0 )
{
absName = buffer;
std::replace( absName.begin(), absName.end(), L'\\', L'/' );
}
return absName;
}
// Returns true iff name is the path of an existing file.
bool CcpIsPathExistingFile(const std::wstring& name)
{
DWORD attr = GetFileAttributesW(name.c_str());
if (attr != INVALID_FILE_ATTRIBUTES)
{
return true;
}
return false;
}
// Returns true iff name is the path of an existing file.
// adjustedName is the absolute path to the file
bool CcpIsPathExistingFile( const std::wstring& name, std::wstring& absName )
{
wchar_t buffer[CCP_MAX_PATH];
DWORD len = GetFullPathNameW( name.c_str(), CCP_MAX_PATH, buffer, NULL );
if( len > 0 )
{
absName = buffer;
std::replace( absName.begin(), absName.end(), L'\\', L'/' );
DWORD attr = GetFileAttributesW( buffer );
if( attr != INVALID_FILE_ATTRIBUTES )
{
return true;
}
}
return false;
}
bool CcpIsPathDirectory( const wchar_t* name )
{
DWORD attr = GetFileAttributesW( name );
if( attr == INVALID_FILE_ATTRIBUTES )
{
return false;
}
else
{
return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
}
bool CcpIsPathRelative( const wchar_t* name )
{
return PathIsRelativeW( name ) != 0;
}
void CcpGetDirectoryContents( const std::wstring& candidate, std::set<std::wstring>& tmpResults )
{
std::wstring folderWithWildcard = candidate;
folderWithWildcard += L"\\*";
WIN32_FIND_DATAW findFileData;
HANDLE h = FindFirstFileW( folderWithWildcard.c_str(), &findFileData );
if( h != INVALID_HANDLE_VALUE )
{
BOOL isMore = TRUE;
do
{
bool isSpecial = (wcscmp( findFileData.cFileName, L"." ) == 0);
isSpecial = isSpecial || (wcscmp( findFileData.cFileName, L".." ) == 0);
if( !isSpecial )
{
std::wstring name = findFileData.cFileName;
for( std::wstring::iterator it = name.begin(); it != name.end(); ++it )
{
*it = tolower( *it );
}
tmpResults.insert( name );
}
isMore = FindNextFileW( h, &findFileData );
}
while( isMore );
}
}
std::wstring CcpGetCurrentWorkingDirectory()
{
std::wstring result;
wchar_t cwdBuffer[CCP_MAX_PATH];
int cdRes = GetCurrentDirectoryW( CCP_MAX_PATH, cwdBuffer );
if( cdRes > 0 )
{
result = cwdBuffer;
std::replace( result.begin(), result.end(), L'\\', L'/' );
}
else
{
CCP_LOGERR( "Couldn't get current directory (%d, %d)", cdRes, GetLastError() );
}
return result;
}
void CcpRemoveFile( const std::wstring& filename )
{
BOOL res = DeleteFileW( filename.c_str() );
if(res == FALSE)
{
CCP_LOGERR("Couldn't delete file %S - %d", filename.c_str(), GetLastError());
}
}
std::wstring CcpExecutablePath()
{
std::vector<wchar_t> tmp(CCP_MAX_PATH);
while( GetModuleFileNameW(NULL, &tmp[0], (DWORD)tmp.size()) == tmp.size() )
{
tmp.resize(tmp.size()*2);
}
return &tmp[0];
}
#else
namespace
{
void AbsPath( const char* path, char* realPath )
{
char src[PATH_MAX];
if( path[0] != '/' )
{
// If not an absolute path, we need to prepend current
// working directory
if( !getcwd( src, sizeof( src ) ) )
{
strcpy( realPath, path );
return;
}
auto len = strlen( src );
if( len > 0 && path[0] != 0 && src[len - 1] != '/' )
{
src[len] = '/';
src[len + 1] = 0;
}
strcat( src, path );
}
else
{
strcpy( src, path );
}
// Collect leading slashes: there can be at most 2 of them.
// All the others should be eaten up
auto start = src;
size_t initialSlashes = 0;
while( *start == '/' )
{
++initialSlashes;
++start;
if( initialSlashes == 2 )
{
break;
}
}
// Split the path by slashes
auto end = start;
std::vector<std::pair<char*, char*>> comps;
while( true )
{
if( *end == '/' || *end == 0 )
{
// Empty comopnents (like in //) and . are ignored
if( start != end && ( end - start != 1 || *start != '.' ) )
{
// If the component is .., pop the previous component
if( end - start == 2 && start[0] == '.' && start[1] == '.' )
{
if( !comps.empty() )
{
comps.pop_back();
}
}
else
{
comps.push_back( std::make_pair( start, end ) );
}
}
if( *end == 0 )
{
break;
}
else
{
start = end + 1;
}
}
++end;
}
// Put initial slashes back in
end = realPath;
for( size_t i = 0; i < initialSlashes; ++i )
{
*end = '/';
++end;
}
// Join path components
for( size_t i = 0; i < comps.size(); ++i )
{
if( i )
{
*end = '/';
++end;
}
auto len = comps[i].second - comps[i].first;
strncpy( end, comps[i].first, len );
end += len;
}
*end = 0;
}
}
std::wstring CcpGetAbsolutePath( const std::wstring& name )
{
if( name.empty() )
{
return L"";
}
auto nameA = CW2A( name.c_str() );
char buffer[PATH_MAX];
strcpy_s( buffer, nameA );
AbsPath( CW2A( name.c_str() ), buffer );
std::wstring absName = std::wstring( CA2W( buffer ) );
if( CcpIsPathDirectory( absName.c_str() ) && !absName.empty() && absName[absName.length() - 1] != L'/' )
{
absName += L"/";
}
return absName;
}
void BreakPathIntoComponents( const std::string& path, std::vector<std::string>& components )
{
size_t endPos = 0;
size_t startPos = path.find_first_of( '/', endPos );
if( startPos == std::string::npos )
{
components.push_back( path );
return;
}
while( startPos != std::string::npos )
{
++startPos;
endPos = path.find_first_of( '/', startPos );
size_t len;
if( endPos == std::string::npos )
{
len = endPos;
}
else
{
len = endPos - startPos;
}
std::string comp = path.substr( startPos, len );
components.push_back( comp );
startPos = endPos;
}
}
// Removes any '.' entries, and collapses '..' entries with the entry preceding it.
// For example, '/root/a/b/../.././c' becomes '/root/c'.
void CollapseRelativePaths( std::vector<std::string>& components )
{
for( auto it = components.begin(); it != components.end(); )
{
if( *it == "." )
{
it = components.erase( it );
}
else
{
auto next = it + 1;
if( next != components.end() )
{
if( *next == ".." )
{
components.erase( next );
components.erase( it );
it = components.begin();
}
else
{
++it;
}
}
else
{
++it;
}
}
}
}
void GetPossibleCasings( const std::string& path, const std::string& nextComponent, std::vector<std::string>& possibleCasings )
{
DIR* dpdf = opendir( path.c_str() );
if( dpdf )
{
struct dirent entry;
struct dirent* result;
int resultCode = readdir_r( dpdf, &entry, &result );
while( (resultCode == 0) && result )
{
if( strcasecmp( nextComponent.c_str(), result->d_name ) == 0 )
{
possibleCasings.push_back( result->d_name );
}
resultCode = readdir_r( dpdf, &entry, &result );
}
closedir( dpdf );
}
}
// Return value indicates whether search should continue or not.
// If true, continue searching.
// If false, searching is over. In this case, the result string contains the string if found, otherwise it is empty
// meaning the search was exhausted without finding a match.
bool FindCasingCorrectedPath( const std::string& path, const std::vector<std::string>& components, size_t compIx, std::string& result )
{
std::vector<std::string> possibleCasings;
GetPossibleCasings( path, components[compIx], possibleCasings );
for( auto it = possibleCasings.begin(); it != possibleCasings.end(); ++it )
{
std::string candidate = path;
if( compIx > 0 )
{
candidate += '/';
}
candidate += *it;
if( compIx == components.size() - 1 )
{
// We're at a leaf node
struct stat statData;
int statResult = stat( candidate.c_str(), &statData );
if( statResult == 0 )
{
result = candidate;
return false;
}
else
{
return true;
}
}
else
{
bool isMore = FindCasingCorrectedPath( candidate, components, compIx + 1, result );
if( !isMore )
{
return false;
}
}
}
// Returning true here to keep the search going - we want to force the search to all leaf nodes
return true;
}
bool CcpIsPathExistingFile( const std::wstring& name, std::wstring& adjustedName )
{
std::string nameA = (const char*)CW2A( name.c_str() );
struct stat statData;
int statResult = stat( nameA.c_str(), &statData );
if( statResult != 0 )
{
// Path not found - try to correct casing
std::vector<std::string> components;
BreakPathIntoComponents( nameA, components );
CollapseRelativePaths( components );
std::string root = "/";
std::string adjustedNameA;
FindCasingCorrectedPath( root, components, 0, adjustedNameA );
if( !adjustedNameA.empty() )
{
adjustedName = CA2W( adjustedNameA.c_str() );
return true;
}
else
{
return false;
}
}
else
{
adjustedName = name;
return true;
}
}
bool CcpIsPathExistingFile( const std::wstring& name )
{
std::wstring adjustedName;
return CcpIsPathExistingFile(name, adjustedName);
}
bool CcpIsPathDirectory( const wchar_t* name )
{
std::string nameA = (const char*)CW2A( name );
struct stat statData;
int statResult = stat( nameA.c_str(), &statData );
if( statResult == 0 )
{
return S_ISDIR( statData.st_mode );
}
else
{
return false;
}
}
bool CcpIsPathRelative( const wchar_t* name )
{
return name && name[0] != L'/';
}
void CcpGetDirectoryContents( const std::wstring& candidate, std::set<std::wstring>& tmpResults )
{
std::string folderName = (const char*)CW2A( candidate.c_str() );
DIR* dpdf = opendir( folderName.c_str() );
if( dpdf )
{
struct dirent entry;
struct dirent* result;
int resultCode = readdir_r( dpdf, &entry, &result );
while( (resultCode == 0) && result )
{
bool isSpecial = (strcmp( result->d_name, "." ) == 0);
isSpecial = isSpecial || (strcmp( result->d_name, ".." ) == 0);
if( !isSpecial )
{
std::wstring name = (const wchar_t*)CA2W( result->d_name );
for( std::wstring::iterator it = name.begin(); it != name.end(); ++it )
{
*it = tolower( *it );
}
tmpResults.insert( name );
}
resultCode = readdir_r( dpdf, &entry, &result );
}
closedir( dpdf );
}
}
std::wstring CcpGetCurrentWorkingDirectory()
{
std::wstring result;
static const int BUFFER_SIZE = 1024;
char cwdBuffer[BUFFER_SIZE];
if( getcwd( cwdBuffer, BUFFER_SIZE ) )
{
result = (const wchar_t*)CA2W( cwdBuffer );
}
else
{
CCP_LOGERR( "Couldn't get current directory (%d)", errno );
}
return result;
}
void CcpRemoveFile( const std::wstring& filename )
{
std::string name = (const char*)CW2A( filename.c_str() );
remove( name.c_str() );
}
std::wstring CcpExecutablePath()
{
#ifdef __APPLE__
std::vector<char> tmp(CCP_MAX_PATH);
uint32_t size = uint32_t( tmp.size() );
if( _NSGetExecutablePath( &tmp[0], &size ) < 0 )
{
tmp.resize( size );
_NSGetExecutablePath( &tmp[0], &size );
}
char actualpath [PATH_MAX];
char* path = realpath(&tmp[0], actualpath);
return std::wstring( CA2W( actualpath ) );
#else
static_assert( false, "CcpExecutablePath is not implemented" );
#endif
}
#endif