forked from Parchive/par2cmdline
-
Notifications
You must be signed in to change notification settings - Fork 2
/
diskfile.cpp
1124 lines (918 loc) · 24.6 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
#include "par2cmdline.h"
#ifdef _MSC_VER
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#endif
#if defined(__FreeBSD_kernel__)
#include <sys/disk.h>
#define BLKGETSIZE64 DIOCGMEDIASIZE
#endif
#ifdef WIN32
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#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;
}
DiskFile::~DiskFile(void)
{
if (hFile != INVALID_HANDLE_VALUE)
::CloseHandle(hFile);
}
bool DiskFile::CreateParentDirectory(string _pathname)
{
// do we have a path separator in the filename ?
string::size_type where;
if (string::npos != (where = _pathname.find_last_of('/')) ||
string::npos != (where = _pathname.find_last_of('\\')))
{
string path = filename.substr(0, where);
struct stat st;
if (stat(path.c_str(), &st) == 0)
return true; // let the caller deal with non-directories
if (!DiskFile::CreateParentDirectory(path))
return false;
if (!CreateDirectory(path.c_str(), NULL))
{
DWORD error = ::GetLastError();
cerr << "Could not create the " << path << " directory: " << ErrorMessage(error) << endl;
return false;
}
}
return true;
}
// Create new file on disk and make sure that there is enough
// space on disk for it.
bool DiskFile::Create(string _filename, u64 _filesize)
{
assert(hFile == INVALID_HANDLE_VALUE);
filename = _filename;
filesize = _filesize;
if (!DiskFile::CreateParentDirectory(filename))
return false;
// Create the file
hFile = ::CreateFileA(_filename.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 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(_filename.c_str());
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(_filename.c_str());
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;
}
// Open the file
bool DiskFile::Open(string _filename, u64 _filesize)
{
assert(hFile == INVALID_HANDLE_VALUE);
filename = _filename;
filesize = _filesize;
hFile = ::CreateFileA(_filename.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 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;
}
}
string DiskFile::GetCanonicalPathname(string filename)
{
char fullname[MAX_PATH];
char *filepart;
// Resolve a relative path to a full path
int length = ::GetFullPathName(filename.c_str(), sizeof(fullname), fullname, &filepart);
if (length <= 0 || sizeof(fullname) < length)
return filename;
// Make sure the drive letter is upper case.
fullname[0] = toupper(fullname[0]);
// Translate all /'s to \'s
char *current = strchr(fullname, '/');
while (current)
{
*current++ = '\\';
current = strchr(current, '/');
}
// Copy the root directory to the output string
string longname(fullname, 3);
// Start processing at the first path component
current = &fullname[3];
char *limit = &fullname[length];
// Process until we reach the end of the full name
while (current < limit)
{
char *tail;
// Find the next \, or the end of the string
(tail = strchr(current, '\\')) || (tail = limit);
*tail = 0;
// Create a wildcard to search for the path
string wild = longname + 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 += current;
break;
}
::FindClose(hFind);
// Copy the component found to the output
longname += finddata.cFileName;
current = tail + 1;
// If we have not reached the end of the name, add a "\"
if (current < limit)
longname += '\\';
}
return longname;
}
list<string>* DiskFile::FindFiles(string path, string wildcard, bool recursive)
{
// check path, if not ending with path separator, add one
char pathend = *path.rbegin();
if (pathend != '\\')
{
path += '\\';
}
list<string> *matches = new list<string>;
wildcard = path + wildcard;
WIN32_FIND_DATA fd;
HANDLE h = ::FindFirstFile(wildcard.c_str(), &fd);
if (h != INVALID_HANDLE_VALUE)
{
do
{
if (0 == (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
matches->push_back(path + fd.cFileName);
}
else if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (fd.cFileName[0] == '.') {
continue;
}
list<string> *dirmatches;
string nwwildcard="*";
dirmatches = DiskFile::FindFiles(fd.cFileName, nwwildcard, true);
matches->merge(*dirmatches);
}
} while (::FindNextFile(h, &fd));
::FindClose(h);
}
return matches;
}
u64 DiskFile::GetFileSize(string filename)
{
struct _stati64 st;
if ((0 == _stati64(filename.c_str(), &st)) && (0 != (st.st_mode & S_IFREG)))
{
return st.st_size;
}
else
{
return 0;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#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;
}
DiskFile::~DiskFile(void)
{
if (file != 0)
fclose(file);
}
bool DiskFile::CreateParentDirectory(string _pathname)
{
// do we have a path separator in the filename ?
string::size_type where;
if (string::npos != (where = _pathname.find_last_of('/')) ||
string::npos != (where = _pathname.find_last_of('\\')))
{
string path = filename.substr(0, where);
struct stat st;
if (stat(path.c_str(), &st) == 0)
return true; // let the caller deal with non-directories
if (!DiskFile::CreateParentDirectory(path))
return false;
if (mkdir(path.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH))
{
cerr << "Could not create the " << path << " directory: " << strerror(errno) << endl;
return false;
}
}
return true;
}
// Create new file on disk and make sure that there is enough
// space on disk for it.
bool DiskFile::Create(string _filename, u64 _filesize)
{
assert(file == 0);
filename = _filename;
filesize = _filesize;
if (!DiskFile::CreateParentDirectory(filename))
return false;
file = fopen(_filename.c_str(), "wb");
if (file == 0)
{
cerr << "Could not create " << _filename << ": " << strerror(errno) << 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))
{
cerr << "Could not set end of file of " << _filename << ": " << strerror(errno) << endl;
fclose(file);
file = 0;
::remove(filename.c_str());
return false;
}
if (1 != fwrite(&_filesize, 1, 1, file))
{
cerr << "Could not set end of file of " << _filename << ": " << strerror(errno) << endl;
fclose(file);
file = 0;
::remove(filename.c_str());
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(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 << ": " << strerror(errno) << 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 << ": " << strerror(errno) << endl;
return false;
}
offset += length;
if (filesize < offset)
{
filesize = offset;
}
return true;
}
// Open the file
bool DiskFile::Open(string _filename, u64 _filesize)
{
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 << ": " << strerror(errno) << 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 << ": " << strerror(errno) << 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
#ifdef PATH_MAX
char curdir[PATH_MAX];
if (0 == getcwd(curdir, sizeof(curdir)))
#else
// Avoid unconditional use of PATH_MAX (not defined on hurd)
char *curdir = get_current_dir_name();
if (curdir == NULL)
#endif
{
return filename;
}
// Allocate a work buffer and copy the resulting full path into it.
char *work = new char[strlen(curdir) + filename.size() + 2];
strcpy(work, curdir);
#ifndef PATH_MAX
free(curdir);
#endif
if (work[strlen(work)-1] != '/')
strcat(work, "/");
strcat(work, filename.c_str());
char *in = work;
char *out = work;
while (*in)
{
if (*in == '/')
{
if (in[1] == '.' && in[2] == '/')
{
// skip the input past /./
in += 2;
}
else if (in[1] == '.' && in[2] == '.' && in[3] == '/')
{
// backtrack the output if /../ was found on the input
in += 3;
if (out > work)
{
do
{
out--;
} while (out > work && *out != '/');
}
}
else
{
*out++ = *in++;
}
}
else
{
*out++ = *in++;
}
}
*out = 0;
string result = work;
delete [] work;
return result;
}
list<string>* DiskFile::FindFiles(string path, string wildcard, bool recursive)
{
// check path, if not ending with path separator, add one
char pathend = *path.rbegin();
if (pathend != '/')
{
path += '/';
}
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)
{
struct stat st;
string fn = path + name;
if (stat(fn.c_str(), &st) == 0)
{
if (S_ISDIR(st.st_mode) &&
recursive == true)
{
list<string> *dirmatches;
string nwwildcard="*";
dirmatches = DiskFile::FindFiles(fn, nwwildcard, true);
matches->merge(*dirmatches);
}
else if (S_ISREG(st.st_mode))
{
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())
{
struct stat st;
string fn = path + name;
if (stat(fn.c_str(), &st) == 0)
{
if (S_ISDIR(st.st_mode) &&
recursive == true)
{
list<string> *dirmatches;
string nwwildcard="*";
dirmatches = DiskFile::FindFiles(fn, nwwildcard, true);
matches->merge(*dirmatches);
}
else if (S_ISREG(st.st_mode))
{
matches->push_back(path + name);
}
}
}
}
}
}
closedir(dirp);
}
}
else
{
struct stat st;
string fn = path + wildcard;
if (stat(fn.c_str(), &st) == 0)
{
if (S_ISDIR(st.st_mode) &&
recursive == true)
{
list<string> *dirmatches;
string nwwildcard="*";
dirmatches = DiskFile::FindFiles(fn, nwwildcard, true);
matches->merge(*dirmatches);
}
else if (S_ISREG(st.st_mode))
{
matches->push_back(path + wildcard);
}
}
}
return matches;
}
u64 DiskFile::GetFileSize(string filename)
{
struct stat st;
if ((0 == stat(filename.c_str(), &st)) && (0 != (st.st_mode & S_IFREG)))
{
return st.st_size;
}
else
{
return 0;
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#endif
bool DiskFile::Open(void)
{
string _filename = filename;
return Open(_filename);
}
bool DiskFile::Open(string _filename)
{
return Open(_filename, GetFileSize(_filename));
}
// 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(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;
}
}
void DiskFile::SplitRelativeFilename(string filename, string basepath, string &name)
{
name = filename;
name.erase(0, basepath.length());
}
bool DiskFile::FileExists(string filename)
{
struct stat st;
return ((0 == stat(filename.c_str(), &st)) && (0 != (st.st_mode & S_IFREG)));
}
// Take a filename from a PAR2 file and replace any characters
// which would be illegal for a file on disk
string DiskFile::TranslateFilename(string filename)
{
string result;
string::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 '*':
case ':':
case '<':
case '>':
case '?':
case '|':
ok = false;
}
}
#else
if (ch < 32)
{
ok = false;
}
#endif
// replace unix / to windows \ or windows \ to unix /
#ifdef WIN32
if (ch == '/') {
ch = '\\';
}
#else
if (ch == '\\') {
ch = '/';
}
#endif
if (ok)
{
result += ch;
}
else
{
// convert problem characters to hex
result += ((ch >> 4) < 10) ? (ch >> 4) + '0' : (ch >> 4) + 'A'-10;
result += ((ch & 0xf) < 10) ? (ch & 0xf) + '0' : (ch & 0xf) + 'A'-10;
}