-
Notifications
You must be signed in to change notification settings - Fork 2
/
diskfile.cpp
1123 lines (885 loc) · 24.8 KB
/
diskfile.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
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This file is part of par2cmdline (a PAR 2.0 compatible file verification and
// repair tool). See http://parchive.sourceforge.net for details of PAR 2.0.
//
// Copyright (c) 2003 Peter Brian Clements
//
// par2cmdline is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// par2cmdline is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Modifications for concurrent processing, async I/O, Unicode support, and
// hierarchial directory support are Copyright (c) 2007-2008 Vincent Tan.
// Search for "#if WANT_CONCURRENT" for concurrent code.
// Concurrent processing utilises Intel Thread Building Blocks 2.0,
// Copyright (c) 2007 Intel Corp.
#include "par2cmdline.h"
#ifdef _MSC_VER
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#endif
#ifdef WIN32
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <tchar.h>
#define OffsetType __int64
#define MaxOffset 0x7fffffffffffffffI64
#define LengthType unsigned int
#define MaxLength 0xffffffffUL
DiskFile::DiskFile(void)
{
//filename;
filesize = 0;
offset = 0;
hFile = INVALID_HANDLE_VALUE;
exists = false;
blockcount = 0;
}
DiskFile::~DiskFile(void)
{
if (hFile != INVALID_HANDLE_VALUE)
::CloseHandle(hFile);
}
// Create new file on disk and make sure that there is enough
// space on disk for it.
bool DiskFile::Create(const string &_filename, u64 _filesize, bool async)
{
assert(hFile == INVALID_HANDLE_VALUE);
filename = _filename;
filesize = _filesize;
// Create the file
hFile = ::CreateFile(utf8_string_to_native_char_array(_filename),
GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW,
#if HAVE_ASYNC_IO
async ? FILE_FLAG_OVERLAPPED : 0,
#else
0,
#endif
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
DWORD error = ::GetLastError();
cerr << "Could not create \"" << _filename << "\": " << ErrorMessage(error) << endl;
return false;
}
if (filesize > 0)
{
// Seek to the end of the file
LONG lowoffset = ((LONG*)&filesize)[0];
LONG highoffset = ((LONG*)&filesize)[1];
if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, lowoffset, &highoffset, FILE_BEGIN))
{
DWORD error = ::GetLastError();
cerr << "Could not set size of \"" << _filename << "\": " << ErrorMessage(error) << endl;
::CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
::DeleteFile(utf8_string_to_native_char_array(_filename));
return false;
}
// Set the end of the file
if (!::SetEndOfFile(hFile))
{
DWORD error = ::GetLastError();
cerr << "Could not set size of \"" << _filename << "\": " << ErrorMessage(error) << endl;
::CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
::DeleteFile(utf8_string_to_native_char_array(_filename));
return false;
}
}
offset = filesize;
exists = true;
return true;
}
// Write some data to disk
bool DiskFile::Write(u64 _offset, const void *buffer, size_t length)
{
assert(hFile != INVALID_HANDLE_VALUE);
if (offset != _offset)
{
LONG lowoffset = ((LONG*)&_offset)[0];
LONG highoffset = ((LONG*)&_offset)[1];
// Seek to the required offset
if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, lowoffset, &highoffset, FILE_BEGIN))
{
DWORD error = ::GetLastError();
cerr << "Could not write " << (u64)length << " bytes to \"" << filename << "\" at offset " << _offset << ": " << ErrorMessage(error) << endl;
return false;
}
offset = _offset;
}
if (length > MaxLength)
{
cerr << "Could not write " << (u64)length << " bytes to \"" << filename << "\" at offset " << _offset << ": " << "Write too long" << endl;
return false;
}
DWORD write = (LengthType)length;
DWORD wrote;
// Write the data
if (!::WriteFile(hFile, buffer, write, &wrote, NULL))
{
DWORD error = ::GetLastError();
cerr << "Could not write " << (u64)length << " bytes to \"" << filename << "\" at offset " << _offset << ": " << ErrorMessage(error) << endl;
return false;
}
offset += length;
if (filesize < offset)
{
filesize = offset;
}
return true;
}
#if HAVE_ASYNC_IO
bool DiskFile::ReadAsync(aiocb_type& cb, u64 offset, void *buffer, size_t length) {
assert(INVALID_HANDLE_VALUE != hFile);
return cb.read(hFile, length, buffer, (off_t) offset);
}
bool DiskFile::WriteAsync(aiocb_type& cb, u64 offset, const void *buffer, size_t length) {
assert(INVALID_HANDLE_VALUE != hFile);
return cb.write(hFile, length, buffer, (off_t) offset);
}
#endif
// Open the file
bool DiskFile::Open(const string &_filename, u64 _filesize, bool async)
{
assert(hFile == INVALID_HANDLE_VALUE);
filename = _filename;
filesize = _filesize;
hFile = ::CreateFile(utf8_string_to_native_char_array(_filename), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING,
#if HAVE_ASYNC_IO
async ? FILE_FLAG_OVERLAPPED : 0,
#else
0,
#endif
NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
DWORD error = ::GetLastError();
switch (error)
{
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
break;
default:
cerr << "Could not open \"" << _filename << "\": " << ErrorMessage(error) << endl;
}
return false;
}
offset = 0;
exists = true;
return true;
}
// Read some data from disk
bool DiskFile::Read(u64 _offset, void *buffer, size_t length)
{
assert(hFile != INVALID_HANDLE_VALUE);
if (offset != _offset)
{
LONG lowoffset = ((LONG*)&_offset)[0];
LONG highoffset = ((LONG*)&_offset)[1];
// Seek to the required offset
if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, lowoffset, &highoffset, FILE_BEGIN))
{
DWORD error = ::GetLastError();
cerr << "Could not read " << (u64)length << " bytes from \"" << filename << "\" at offset " << _offset << ": " << ErrorMessage(error) << endl;
return false;
}
offset = _offset;
}
if (length > MaxLength)
{
cerr << "Could not read " << (u64)length << " bytes from \"" << filename << "\" at offset " << _offset << ": " << "Read too long" << endl;
return false;
}
DWORD want = (LengthType)length;
DWORD got;
// Read the data
if (!::ReadFile(hFile, buffer, want, &got, NULL))
{
DWORD error = ::GetLastError();
cerr << "Could not read " << (u64)length << " bytes from \"" << filename << "\" at offset " << _offset << ": " << ErrorMessage(error) << endl;
return false;
}
offset += length;
return true;
}
void DiskFile::Close(void)
{
if (hFile != INVALID_HANDLE_VALUE)
{
::CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;
}
}
#ifdef UNICODE
typedef wstring TSTRING;
#else
typedef string TSTRING;
#endif
static TSTRING& substitute(TSTRING& s, TCHAR from, TCHAR to) {
for(TSTRING::size_type pos = 0;;) {
pos = s.find(from, pos);
if (TSTRING::npos == pos)
break;
s[pos] = to;
}
return s;
}
string DiskFile::GetCanonicalPathname(string filename)
{
// Resolve a relative path to a full path
TCHAR *filepart = NULL;
DWORD length = ::GetFullPathName(utf8_string_to_native_char_array(filename), 0, NULL, &filepart);
if (0 == length)
return filename; // ???what went wrong???
TSTRING fullname(length, _TEXT(' '));
length = ::GetFullPathName(utf8_string_to_native_char_array(filename),
(DWORD) fullname.length(), &fullname[0], &filepart);
if (0 == length)
return filename; // ???what went wrong???
// Translate all /'s to \'s
substitute(fullname, _TEXT('/'), _TEXT('\\'));
// 2013/12/29 added handling of UNC paths
string::size_type current;
if (0 == fullname.find(_TEXT("\\\\?\\UNC\\")))
current = 8;
else if (0 == fullname.find(_TEXT("\\\\")))
current = 2;
else {
// Make sure the drive letter is upper case.
const TCHAR c = fullname[0];
if (!isalpha(c) || ':' != fullname[1] || '\\' != fullname[2]) {
current = 0;
} else {
if (islower(c))
fullname[0] = toupper(c);
current = 3;
}
}
// Copy the root directory to the output string
TSTRING longname(fullname, 0, current);
// Start processing at the first path component
// Process until we reach the end of the full name
TSTRING::size_type len = fullname.length();
while (current < len) {
// Find the next \, or the end of the string
TSTRING::size_type pos = fullname.find('\\', current);
if (TSTRING::npos == pos)
pos = len;
// Create a wildcard to search for the path
TSTRING wild = longname + fullname.substr(current, pos-current);
WIN32_FIND_DATA finddata;
HANDLE hFind = ::FindFirstFile(wild.c_str(), &finddata);
if (hFind == INVALID_HANDLE_VALUE)
{
// If the component was not found then just copy the rest of the path to the
// output buffer verbatim.
longname += fullname.substr(current);
break;
}
::FindClose(hFind);
// Copy the component found to the output
longname += finddata.cFileName;
current = pos;
if (current < len) {
current += 1; // skip '\\'
// If we have not reached the end of the name, add a "\"
longname += '\\';
}
}
return native_char_array_to_utf8_string(longname.c_str());
}
list<string>* DiskFile::FindFiles(string path, string wildcard)
{
list<string> *matches = new list<string>;
wildcard = path + wildcard;
WIN32_FIND_DATA fd;
HANDLE h = ::FindFirstFile(utf8_string_to_native_char_array(wildcard), &fd);
if (h != INVALID_HANDLE_VALUE)
{
do
{
if (0 == (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
matches->push_back(path + native_char_array_to_utf8_string(fd.cFileName));
}
} while (::FindNextFile(h, &fd));
::FindClose(h);
}
return matches;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#else // !WIN32
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifdef HAVE_FSEEKO
# define OffsetType off_t
# define MaxOffset ((off_t)0x7fffffffffffffffULL)
# define fseek fseeko
#else
# if _FILE_OFFSET_BITS == 64
# define OffsetType unsigned long long
# define MaxOffset 0x7fffffffffffffffULL
# else
# define OffsetType long
# define MaxOffset 0x7fffffffUL
# endif
#endif
#define LengthType unsigned int
#define MaxLength 0xffffffffUL
DiskFile::DiskFile(void)
{
//filename;
filesize = 0;
offset = 0;
file = 0;
exists = false;
blockcount = 0;
}
DiskFile::~DiskFile(void)
{
if (file != 0)
fclose(file);
}
// Create new file on disk and make sure that there is enough
// space on disk for it.
bool DiskFile::Create(const string &_filename, u64 _filesize, bool /* async */)
{
assert(file == 0);
filename = _filename;
filesize = _filesize;
file = fopen(_filename.c_str(), "wb");
if (file == 0)
{
cerr << "Could not create: " << _filename << endl;
return false;
}
if (_filesize > (u64)MaxOffset)
{
cerr << "Requested file size for " << _filename << " is too large." << endl;
return false;
}
if (_filesize > 0)
{
if (fseek(file, (OffsetType)_filesize-1, SEEK_SET))
{
fclose(file);
file = 0;
::remove(filename.c_str());
cerr << "Could not set end of file: " << _filename << endl;
return false;
}
if (1 != fwrite(&_filesize, 1, 1, file))
{
fclose(file);
file = 0;
::remove(filename.c_str());
cerr << "Could not set end of file: " << _filename << endl;
return false;
}
#if HAVE_ASYNC_IO
// 20090202 bug fix: async writes to the newly created file can
// fail for the last byte because the file's state hasn't been
// updated. This problem definitely occurs under Mac OS X
// 10.4/10.5, so fix this by flushing the new file.
// Closing and then re-opening the new file also works.
if (true) {
if (fflush(file)) {
fclose(file);
file = 0;
::remove(filename.c_str());
cerr << "Could not flush newly created file: " << _filename << endl;
return false;
}
} else {
if (fclose(file)) {
file = 0;
::remove(filename.c_str());
cerr << "Could not close created file: " << _filename << endl;
return false;
}
file = fopen(_filename.c_str(), "r+b"); // do NOT truncate file
if (file == 0) {
cerr << "Could not re-open newly created file: " << _filename << endl;
return false;
}
}
#endif
}
offset = filesize;
exists = true;
return true;
}
// Write some data to disk
bool DiskFile::Write(u64 _offset, const void *buffer, size_t length)
{
assert(file != 0);
if (offset != _offset)
{
if (_offset > (u64)MaxOffset)
{
cerr << "Could not write " << (u64)length << " bytes to " << filename << " at offset " << _offset << endl;
return false;
}
if (fseek(file, (OffsetType)_offset, SEEK_SET))
{
cerr << "Could not write " << (u64)length << " bytes to " << filename << " at offset " << _offset << endl;
return false;
}
offset = _offset;
}
if (length > MaxLength)
{
cerr << "Could not write " << (u64)length << " bytes to " << filename << " at offset " << _offset << endl;
return false;
}
if (1 != fwrite(buffer, (LengthType)length, 1, file))
{
cerr << "Could not write " << (u64)length << " bytes to " << filename << " at offset " << _offset << endl;
return false;
}
offset += length;
if (filesize < offset)
{
filesize = offset;
}
return true;
}
#if HAVE_ASYNC_IO
bool DiskFile::ReadAsync(aiocb_type& cb, u64 offset, void *buffer, size_t length) {
assert(NULL != file);
return cb.read(fileno(file), length, buffer, (off_t) offset);
}
bool DiskFile::WriteAsync(aiocb_type& cb, u64 offset, const void *buffer, size_t length) {
assert(NULL != file);
return cb.write(fileno(file), length, buffer, (off_t) offset);
}
#endif
// Open the file
bool DiskFile::Open(const string &_filename, u64 _filesize, bool /* async */)
{
assert(file == 0);
filename = _filename;
filesize = _filesize;
if (_filesize > (u64)MaxOffset)
{
cerr << "File size for " << _filename << " is too large." << endl;
return false;
}
file = fopen(filename.c_str(), "rb");
if (file == 0)
{
return false;
}
offset = 0;
exists = true;
return true;
}
// Read some data from disk
bool DiskFile::Read(u64 _offset, void *buffer, size_t length)
{
assert(file != 0);
if (offset != _offset)
{
if (_offset > (u64)MaxOffset)
{
cerr << "Could not read " << (u64)length << " bytes from " << filename << " at offset " << _offset << endl;
return false;
}
if (fseek(file, (OffsetType)_offset, SEEK_SET))
{
cerr << "Could not read " << (u64)length << " bytes from " << filename << " at offset " << _offset << endl;
return false;
}
offset = _offset;
}
if (length > MaxLength)
{
cerr << "Could not read " << (u64)length << " bytes from " << filename << " at offset " << _offset << endl;
return false;
}
if (1 != fread(buffer, (LengthType)length, 1, file))
{
cerr << "Could not read " << (u64)length << " bytes from " << filename << " at offset " << _offset << endl;
return false;
}
offset += length;
return true;
}
void DiskFile::Close(void)
{
if (file != 0)
{
fclose(file);
file = 0;
}
}
// Attempt to get the full pathname of the file
string DiskFile::GetCanonicalPathname(string filename)
{
// Is the supplied path already an absolute one
if (filename.size() == 0 || filename[0] == '/')
return filename;
// Get the current directory
char *curdir = getcwd(NULL, 0);
if (NULL == curdir)
return filename;
string fullpath_s(curdir);
free(curdir);
fullpath_s.push_back('/');
fullpath_s.append(filename);
#if HAVE_REALPATH
char *fullpath = realpath(fullpath_s.c_str(), NULL);
if (NULL == fullpath)
return filename;
fullpath_s = fullpath;
free(fullpath);
return fullpath_s;
#else
string fullpath;
for(const char *in = fullpath_s.c_str(); *in;) {
if (*in == '/') {
if (in[1] == '.' && in[2] == '/')
in += 2; // skip the input past /./
else if (in[1] == '.' && in[2] == '.' && in[3] == '/') {
// backtrack the output if /../ was found on the input
in += 3;
if (!fullpath.empty())
do
fullpath.erase(--fullpath.end());
while (!fullpath.empty() && *--fullpath.end() != '/');
} else if (in[1] == '/') // collapse "//" to "/"
in++;
else
fullpath.push_back(*in++); // fullpath.push_back('/'), in++;
} else
fullpath.push_back(*in++);
}
return fullpath;
#endif
}
list<string>* DiskFile::FindFiles(string path, string wildcard)
{
list<string> *matches = new list<string>;
string::size_type where;
if ((where = wildcard.find_first_of('*')) != string::npos ||
(where = wildcard.find_first_of('?')) != string::npos)
{
string front = wildcard.substr(0, where);
bool multiple = wildcard[where] == '*';
string back = wildcard.substr(where+1);
DIR *dirp = opendir(path.c_str());
if (dirp != 0)
{
struct dirent *d;
while ((d = readdir(dirp)) != 0)
{
string name = d->d_name;
if (name == "." || name == "..")
continue;
if (multiple)
{
if (name.size() >= wildcard.size() &&
name.substr(0, where) == front &&
name.substr(name.size()-back.size()) == back)
{
matches->push_back(path + name);
}
}
else
{
if (name.size() == wildcard.size())
{
string::const_iterator pw = wildcard.begin();
string::const_iterator pn = name.begin();
while (pw != wildcard.end())
{
if (*pw != '?' && *pw != *pn)
break;
++pw;
++pn;
}
if (pw == wildcard.end())
{
matches->push_back(path + name);
}
}
}
}
closedir(dirp);
}
}
else
{
struct stat st;
string fn = path + wildcard;
if (stat(fn.c_str(), &st) == 0)
{
matches->push_back(path + wildcard);
}
}
return matches;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif
bool DiskFile::Open(bool async)
{
return Open(filename, async);
}
bool DiskFile::Open(const string &_filename, bool async)
{
return Open(_filename, GetFileSize(_filename), async);
}
// Delete the file
bool DiskFile::Delete(void)
{
#ifdef WIN32
assert(hFile == INVALID_HANDLE_VALUE);
#else
assert(file == 0);
#endif
if (filename.size() > 0 && 0 == unlink(filename.c_str()))
{
return true;
}
else
{
cerr << "Cannot delete " << filename << endl;
return false;
}
}
//string DiskFile::GetPathFromFilename(string filename)
//{
// string::size_type where;
//
// if (string::npos != (where = filename.find_last_of('/')) ||
// string::npos != (where = filename.find_last_of('\\')))
// {
// return filename.substr(0, where+1);
// }
// else
// {
// return "." PATHSEP;
// }
//}
void DiskFile::SplitFilename(const string &filename, string &path, string &name)
{
string::size_type where;
if (string::npos != (where = filename.find_last_of('/')) ||
string::npos != (where = filename.find_last_of('\\')))
{
path = filename.substr(0, where+1);
name = filename.substr(where+1);
}
else
{
path = "." PATHSEP;
name = filename;
}
}
#define SUPPORT_BLOCK_DEVICES 0
#if SUPPORT_BLOCK_DEVICES
#include <sys/ioctl.h>
#include <sys/mount.h>
#endif
bool DiskFile::FileExists(const string &filename)
{
struct_stat st;
#if SUPPORT_BLOCK_DEVICES
return ((0 == stat(filename.c_str(), &st)) &&
(0 != (st.st_mode & (S_IFREG|S_IFBLK))));
#else
return ((0 == stat(utf8_string_to_native_char_array(filename), &st)) && (0 != (st.st_mode & S_IFREG)));
#endif
}
u64 DiskFile::GetFileSize(const string &filename)
{
struct_stat st;
if (0 == stat(utf8_string_to_native_char_array(filename), &st)) {
if (st.st_mode & S_IFREG)
{
return st.st_size;
}
#if SUPPORT_BLOCK_DEVICES
else if (st.st_mode & S_IFBLK)
{
FILE *fp;
u64 size = 0;
if ( NULL != (fp = fopen(filename.c_str(), "rb")) )
{
ioctl(fileno(fp), BLKGETSIZE64, &size);
fclose(fp);
}
return size;
}
#endif
}
return 0;
}
// Take a filename from a PAR2 file and replace any characters
// which would be illegal for a file on disk
string DiskFile::TranslateFilename(const string &filename)
{
string result;
string::const_iterator p = filename.begin();
while (p != filename.end())
{
unsigned char ch = *p;
bool ok = true;
#ifdef WIN32
if (ch < 32)
{
ok = false;
}
else
{
switch (ch)
{
case '/':
case '\\': {
CommandLine* cl = CommandLine::get();
if (cl && !cl->GetBaseDirectory().empty()) {
ch = '\\'; // if '/' was stored in the par2 file, change it to '\\'
break;
}
}
case '"':
case '*':
case ':':
case '<':
case '>':
case '?':
case '|':
ok = false;
}
}
#else
if (ch < 32)
{
ok = false;
}
else if ('/' == ch || '\\' == ch)
{
CommandLine* cl = CommandLine::get();
if (cl && !cl->GetBaseDirectory().empty())
ch = '/'; // if '\\' was stored in the par2 file, change it to '/'
else
ok = false;
}
#endif
if (ok)
{
result += ch;
}
else
{