-
Notifications
You must be signed in to change notification settings - Fork 405
/
Copy pathdrive_physfs.cpp
991 lines (900 loc) · 31.2 KB
/
drive_physfs.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
/*
* Copyright (C) 2002-2021 The DOSBox Team
*
* This program 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.
*
* This program 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Heavy improvements by the DOSBox-X Team
* With major works from joncampbell123 and Wengier
*/
#include "dosbox.h"
#include "dos_inc.h"
#include "drives.h"
#include "logging.h"
#if !defined(WIN32)
#include <sys/statvfs.h>
#endif
#include "../libs/physfs/physfs.h"
#include "../libs/physfs/physfs.c"
#include "../libs/physfs/physfs_archiver_7z.c"
#include "../libs/physfs/physfs_archiver_dir.c"
#include "../libs/physfs/physfs_archiver_grp.c"
#include "../libs/physfs/physfs_archiver_hog.c"
#include "../libs/physfs/physfs_archiver_iso9660.c"
#include "../libs/physfs/physfs_archiver_mvl.c"
#include "../libs/physfs/physfs_archiver_qpak.c"
#include "../libs/physfs/physfs_archiver_slb.c"
#include "../libs/physfs/physfs_archiver_unpacked.c"
#include "../libs/physfs/physfs_archiver_vdf.c"
#include "../libs/physfs/physfs_archiver_wad.c"
#include "../libs/physfs/physfs_archiver_zip.c"
#include "../libs/physfs/physfs_byteorder.c"
#include "../libs/physfs/physfs_platform_posix.c"
#include "../libs/physfs/physfs_platform_unix.c"
#include "../libs/physfs/physfs_platform_windows.c"
#include "../libs/physfs/physfs_platform_winrt.cpp"
#include "../libs/physfs/physfs_platform_haiku.cpp"
#include "../libs/physfs/physfs_unicode.c"
extern int lfn_filefind_handle;
extern uint16_t ldid[256];
extern std::string ldir[256];
extern bool rsize;
extern unsigned long totalc, freec;
int MSCDEX_RemoveDrive(char driveLetter);
int MSCDEX_AddDrive(char driveLetter, const char* physicalPath, uint8_t& subUnit);
bool MSCDEX_HasMediaChanged(uint8_t subUnit);
bool MSCDEX_GetVolumeName(uint8_t subUnit, char* name);
bool isDBCSCP();
char * DBCS_upcase(char * str);
PHYSFS_sint64 PHYSFS_fileLength(const char *name) {
PHYSFS_file *f = PHYSFS_openRead(name);
if (f == NULL) return 0;
PHYSFS_sint64 size = PHYSFS_fileLength(f);
PHYSFS_close(f);
return size;
}
/* Need to strip "/.." components and transform '\\' to '/' for physfs */
static char *normalize(char * name, const char *basedir) {
int last = (int)(strlen(name)-1);
strreplace_dbcs(name,'\\','/');
while (last >= 0 && name[last] == '/') name[last--] = 0;
if (last > 0 && name[last] == '.' && name[last-1] == '/') name[last-1] = 0;
if (last > 1 && name[last] == '.' && name[last-1] == '.' && name[last-2] == '/') {
name[last-2] = 0;
char *slash = strrchr(name,'/');
if (slash) *slash = 0;
}
if (strlen(basedir) > strlen(name)) { strcpy(name,basedir); strreplace_dbcs(name,'\\','/'); }
last = (int)(strlen(name)-1);
while (last >= 0 && name[last] == '/') name[last--] = 0;
if (name[0] == 0) name[0] = '/';
//LOG_MSG("File access: %s",name);
return name;
}
class physfsFile : public DOS_File {
public:
physfsFile(const char* name, PHYSFS_file * handle,uint16_t devinfo, const char* physname, bool write);
bool Read(uint8_t * data,uint16_t * size) override;
bool Write(const uint8_t * data,uint16_t * size) override;
bool Seek(uint32_t * pos,uint32_t type) override;
bool prepareRead();
bool prepareWrite();
bool Close() override;
uint16_t GetInformation(void) override;
bool UpdateDateTimeFromHost(void) override;
private:
PHYSFS_file * fhandle;
enum { READ,WRITE } last_action;
uint16_t info;
char pname[CROSS_LEN];
};
bool physfsDrive::AllocationInfo(uint16_t * _bytes_sector,uint8_t * _sectors_cluster,uint16_t * _total_clusters,uint16_t * _free_clusters) {
const char *wdir = PHYSFS_getWriteDir();
if (wdir) {
bool res=false;
#if defined(WIN32)
long unsigned int dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwTotalClusters;
uint8_t drive=strlen(wdir)>1&&wdir[1]==':'?toupper(wdir[0])-'A'+1:0;
if (drive>26) drive=0;
char root[4]="A:\\";
root[0]='A'+drive-1;
res = GetDiskFreeSpace(drive?root:NULL, &dwSectPerClust, &dwBytesPerSect, &dwFreeClusters, &dwTotalClusters);
if (res) {
unsigned long total = dwTotalClusters * dwSectPerClust;
int ratio = total > 2097120 ? 64 : (total > 1048560 ? 32 : (total > 524280 ? 16 : (total > 262140 ? 8 : (total > 131070 ? 4 : (total > 65535 ? 2 : 1)))));
*_bytes_sector = (uint16_t)dwBytesPerSect;
*_sectors_cluster = ratio;
*_total_clusters = total > 4194240? 65535 : (uint16_t)(dwTotalClusters * dwSectPerClust / ratio);
*_free_clusters = dwFreeClusters ? (total > 4194240? 61440 : (uint16_t)(dwFreeClusters * dwSectPerClust / ratio)) : 0;
if (rsize) {
totalc=dwTotalClusters * dwSectPerClust / ratio;
freec=dwFreeClusters * dwSectPerClust / ratio;
}
#else
struct statvfs stat;
res = statvfs(wdir, &stat) == 0;
if (res) {
int ratio = stat.f_blocks / 65536, tmp=ratio;
*_bytes_sector = 512;
*_sectors_cluster = stat.f_frsize/512 > 64? 64 : stat.f_frsize/512;
if (ratio>1) {
if (ratio * (*_sectors_cluster) > 64) tmp = (*_sectors_cluster+63)/(*_sectors_cluster);
*_sectors_cluster = ratio * (*_sectors_cluster) > 64? 64 : ratio * (*_sectors_cluster);
ratio = tmp;
}
*_total_clusters = stat.f_blocks > 65535? 65535 : stat.f_blocks;
*_free_clusters = stat.f_bavail > 61440? 61440 : stat.f_bavail;
if (rsize) {
totalc=stat.f_blocks;
freec=stat.f_bavail;
if (ratio>1) {
totalc/=ratio;
freec/=ratio;
}
}
#endif
} else {
// 512*32*32765==~500MB total size
// 512*32*16000==~250MB total free size
*_bytes_sector = 512;
*_sectors_cluster = 32;
*_total_clusters = 32765;
*_free_clusters = 16000;
}
} else {
*_bytes_sector=allocation.bytes_sector;
*_sectors_cluster=allocation.sectors_cluster;
*_total_clusters=allocation.total_clusters;
*_free_clusters=0;
}
return true;
}
bool physfsDrive::FileExists(const char* name) {
char newname[CROSS_LEN];
strcpy(newname,basedir);
strcat(newname,name);
CROSS_FILENAME(newname);
dirCache.ExpandName(newname);
normalize(newname,basedir);
bool result = PHYSFS_exists(newname);
if(result) {
PHYSFS_Stat statbuf;
BAIL_IF_ERRPASS(!PHYSFS_stat(newname, &statbuf), false);
result = (statbuf.filetype != PHYSFS_FILETYPE_DIRECTORY);
}
return result;
}
bool physfsDrive::FileStat(const char* name, FileStat_Block * const stat_block) {
char newname[CROSS_LEN];
strcpy(newname,basedir);
strcat(newname,name);
CROSS_FILENAME(newname);
dirCache.ExpandName(newname);
normalize(newname,basedir);
PHYSFS_Stat statbuf;
BAIL_IF_ERRPASS(!PHYSFS_stat(newname, &statbuf), false);
time_t mytime = statbuf.modtime;
/* Convert the stat to a FileStat */
struct tm *time;
if((time=localtime(&mytime))!=nullptr) {
stat_block->time=DOS_PackTime((uint16_t)time->tm_hour,(uint16_t)time->tm_min,(uint16_t)time->tm_sec);
stat_block->date=DOS_PackDate((uint16_t)(time->tm_year+1900),(uint16_t)(time->tm_mon+1),(uint16_t)time->tm_mday);
} else {
stat_block->time=DOS_PackTime(0,0,0);
stat_block->date=DOS_PackDate(1980,1,1);
}
stat_block->size=(uint32_t)PHYSFS_fileLength(newname);
return true;
}
struct opendirinfo {
char dir[CROSS_LEN];
char **files;
int pos;
};
/* helper functions for drive cache */
bool physfsDrive::isdir(const char *name) {
char myname[CROSS_LEN];
strcpy(myname,name);
normalize(myname,basedir);
PHYSFS_Stat statbuf;
BAIL_IF_ERRPASS(!PHYSFS_stat(myname, &statbuf), false);
return (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
}
void *physfsDrive::opendir(const char *name) {
char myname[CROSS_LEN];
strcpy(myname,name);
normalize(myname,basedir);
PHYSFS_Stat statbuf;
BAIL_IF_ERRPASS(!PHYSFS_stat(myname, &statbuf), NULL);
if(statbuf.filetype != PHYSFS_FILETYPE_DIRECTORY) return NULL;
struct opendirinfo *oinfo = (struct opendirinfo *)malloc(sizeof(struct opendirinfo));
strcpy(oinfo->dir, myname);
oinfo->files = PHYSFS_enumerateFiles(myname);
if (oinfo->files == NULL) {
const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
const char* errorMessage = err ? PHYSFS_getErrorByCode(err) : "Unknown error";
LOG_MSG("PHYSFS: nothing found for %s (%s)",myname,errorMessage);
free(oinfo);
return NULL;
}
oinfo->pos = (myname[4] == 0?0:-2);
return (void *)oinfo;
}
void physfsDrive::closedir(void *handle) {
struct opendirinfo *oinfo = (struct opendirinfo *)handle;
if (handle == NULL) return;
if (oinfo->files != NULL) PHYSFS_freeList(oinfo->files);
free(oinfo);
}
bool physfsDrive::read_directory_first(void* dirp, char* entry_name, char* entry_sname, bool& is_directory) {
return read_directory_next(dirp, entry_name, entry_sname, is_directory);
}
bool physfsDrive::read_directory_next(void* dirp, char* entry_name, char* entry_sname, bool& is_directory) {
struct opendirinfo *oinfo = (struct opendirinfo *)dirp;
if (!oinfo) return false;
if (oinfo->pos == -2) {
oinfo->pos++;
safe_strncpy(entry_name,".",CROSS_LEN);
safe_strncpy(entry_sname,".",DOS_NAMELENGTH+1);
is_directory = true;
return true;
}
if (oinfo->pos == -1) {
oinfo->pos++;
safe_strncpy(entry_name,"..",CROSS_LEN);
safe_strncpy(entry_sname,"..",DOS_NAMELENGTH+1);
is_directory = true;
return true;
}
if (!oinfo->files || !oinfo->files[oinfo->pos]) return false;
safe_strncpy(entry_name,oinfo->files[oinfo->pos++],CROSS_LEN);
*entry_sname=0;
is_directory = isdir(strlen(oinfo->dir)?(std::string(oinfo->dir)+"/"+std::string(entry_name)).c_str():entry_name);
return true;
}
static uint8_t physfs_used = 0;
physfsDrive::physfsDrive(const char driveLetter, const char * startdir,uint16_t _bytes_sector,uint8_t _sectors_cluster,uint16_t _total_clusters,uint16_t _free_clusters,uint8_t _mediaid,int &error,std::vector<std::string> &options)
:localDrive(startdir,_bytes_sector,_sectors_cluster,_total_clusters,_free_clusters,_mediaid,options)
{
this->driveLetter = driveLetter;
this->mountarc = "";
char mp[] = "A_DRIVE";
mp[0] = driveLetter;
char newname[CROSS_LEN+1];
strcpy(newname,startdir);
CROSS_FILENAME(newname);
if (!physfs_used) {
PHYSFS_init("");
PHYSFS_permitSymbolicLinks(1);
}
physfs_used++;
char *lastdir = newname;
char *dir = strchr(lastdir+(((lastdir[0]|0x20) >= 'a' && (lastdir[0]|0x20) <= 'z')?2:0),':');
while (dir) {
*dir++ = 0;
if((lastdir == newname) && !strchr(dir+(((dir[0]|0x20) >= 'a' && (dir[0]|0x20) <= 'z')?2:0),':')) {
// If the first parameter is a directory, the next one has to be the archive file,
// do not confuse it with basedir if trailing : is not there!
int tmp = (int)(strlen(dir)-1);
dir[tmp++] = ':';
dir[tmp++] = CROSS_FILESPLIT;
dir[tmp] = '\0';
}
if (*lastdir) {
if(PHYSFS_mount(lastdir, mp, true) == 0) {
const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
const char* errorMessage = err ? PHYSFS_getErrorByCode(err) : "Unknown error";
LOG_MSG("PHYSFS couldn't mount '%s': %s", lastdir, errorMessage);
}
else {
if (mountarc.size()) mountarc+=", ";
mountarc+= lastdir;
}
}
lastdir = dir;
dir = strchr(lastdir+(((lastdir[0]|0x20) >= 'a' && (lastdir[0]|0x20) <= 'z')?2:0),':');
}
error = 0;
if (!mountarc.size()) {error = 10;return;}
strcpy(basedir,"\\");
strcat(basedir,mp);
strcat(basedir,"\\");
allocation.bytes_sector=_bytes_sector;
allocation.sectors_cluster=_sectors_cluster;
allocation.total_clusters=_total_clusters;
allocation.free_clusters=_free_clusters;
allocation.mediaid=_mediaid;
dirCache.SetBaseDir(basedir, this);
}
physfsDrive::~physfsDrive(void) {
if(!physfs_used) {
LOG_MSG("PHYSFS invalid reference count!");
return;
}
physfs_used--;
if(!physfs_used) {
LOG_MSG("PHYSFS calling PHYSFS_deinit()");
PHYSFS_deinit();
}
}
const char *physfsDrive::getOverlaydir(void) {
static const char *overlaydir = PHYSFS_getWriteDir();
return overlaydir;
}
bool physfsDrive::setOverlaydir(const char * name) {
char newname[CROSS_LEN+1];
strcpy(newname,name);
CROSS_FILENAME(newname);
const char *oldwrite = PHYSFS_getWriteDir();
if (oldwrite) oldwrite = strdup(oldwrite);
if (!PHYSFS_setWriteDir(newname)) {
if (oldwrite)
PHYSFS_setWriteDir(oldwrite);
return false;
} else {
if (oldwrite) PHYSFS_unmount(oldwrite);
PHYSFS_mount(newname, NULL, 0);
dirCache.EmptyCache();
}
if (oldwrite) free((char *)oldwrite);
return true;
}
const char *physfsDrive::GetInfo() {
sprintf(info,"PhysFS directory %s", mountarc.c_str());
return info;
}
bool physfsDrive::FileOpen(DOS_File * * file,const char * name,uint32_t flags) {
char newname[CROSS_LEN];
strcpy(newname,basedir);
strcat(newname,name);
CROSS_FILENAME(newname);
dirCache.ExpandName(newname);
normalize(newname,basedir);
PHYSFS_file * hand;
if (!PHYSFS_exists(newname)) return false;
if ((flags&0xf) == OPEN_READ) {
hand = PHYSFS_openRead(newname);
} else {
/* open for reading, deal with writing later */
hand = PHYSFS_openRead(newname);
}
if (!hand) {
if((flags&0xf) != OPEN_READ) {
PHYSFS_file *hmm = PHYSFS_openRead(newname);
if (hmm) {
PHYSFS_close(hmm);
LOG_MSG("Warning: file %s exists and failed to open in write mode.",newname);
}
}
return false;
}
*file=new physfsFile(name,hand,0x2,newname,false);
(*file)->flags=flags; //for the inheritance flag and maybe check for others.
return true;
}
bool physfsDrive::FileCreate(DOS_File * * file,const char * name,uint16_t attributes) {
(void)attributes;//UNUSED
if (!getOverlaydir()) {
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
char newname[CROSS_LEN];
strcpy(newname,basedir);
strcat(newname,name);
CROSS_FILENAME(newname);
dirCache.ExpandName(newname);
normalize(newname,basedir);
/* Test if file exists, don't add to dirCache then */
bool existing_file=PHYSFS_exists(newname);
char *slash = strrchr(newname,'/');
if (slash && slash != newname) {
*slash = 0;
PHYSFS_Stat statbuf;
BAIL_IF_ERRPASS(!PHYSFS_stat(newname, &statbuf), false);
if (statbuf.filetype != PHYSFS_FILETYPE_DIRECTORY) return false;
PHYSFS_mkdir(newname);
*slash = '/';
}
PHYSFS_file * hand=PHYSFS_openWrite(newname);
if (!hand){
const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
const char* errorMessage = err ? PHYSFS_getErrorByCode(err) : "Unknown error";
LOG_MSG("Warning: file creation failed: %s (%s)",newname,errorMessage);
return false;
}
/* Make the 16 bit device information */
*file=new physfsFile(name,hand,0x2,newname,true);
(*file)->flags=OPEN_READWRITE;
if(!existing_file) {
strcpy(newname,basedir);
strcat(newname,name);
CROSS_FILENAME(newname);
dirCache.AddEntry(newname, true);
dirCache.EmptyCache();
}
return true;
}
bool physfsDrive::FileUnlink(const char * name) {
if (!getOverlaydir()) {
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
char newname[CROSS_LEN];
strcpy(newname,basedir);
strcat(newname,name);
CROSS_FILENAME(newname);
dirCache.ExpandName(newname);
normalize(newname,basedir);
if (PHYSFS_delete(newname)) {
CROSS_FILENAME(newname);
dirCache.DeleteEntry(newname);
dirCache.EmptyCache();
return true;
};
return false;
}
bool physfsDrive::RemoveDir(const char * dir) {
if (!getOverlaydir()) {
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
char newdir[CROSS_LEN];
strcpy(newdir,basedir);
strcat(newdir,dir);
CROSS_FILENAME(newdir);
dirCache.ExpandName(newdir);
normalize(newdir,basedir);
PHYSFS_Stat statbuf;
BAIL_IF_ERRPASS(!PHYSFS_stat(newdir, &statbuf), false);
if ((statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY) && PHYSFS_delete(newdir)) {
CROSS_FILENAME(newdir);
dirCache.DeleteEntry(newdir,true);
dirCache.EmptyCache();
return true;
}
return false;
}
bool physfsDrive::MakeDir(const char * dir) {
if (!getOverlaydir()) {
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
char newdir[CROSS_LEN];
strcpy(newdir,basedir);
strcat(newdir,dir);
CROSS_FILENAME(newdir);
dirCache.ExpandName(newdir);
normalize(newdir,basedir);
if (PHYSFS_mkdir(newdir)) {
CROSS_FILENAME(newdir);
dirCache.CacheOut(newdir,true);
return true;
}
return false;
}
bool physfsDrive::TestDir(const char * dir) {
char newdir[CROSS_LEN];
strcpy(newdir,basedir);
strcat(newdir,dir);
CROSS_FILENAME(newdir);
dirCache.ExpandName(newdir);
normalize(newdir,basedir);
PHYSFS_Stat statbuf;
BAIL_IF_ERRPASS(!PHYSFS_stat(newdir, &statbuf), false);
return (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY);
}
bool physfsDrive::Rename(const char * oldname,const char * newname) {
if (!getOverlaydir()) {
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
char newold[CROSS_LEN];
strcpy(newold,basedir);
strcat(newold,oldname);
CROSS_FILENAME(newold);
dirCache.ExpandName(newold);
normalize(newold,basedir);
char newnew[CROSS_LEN];
strcpy(newnew,basedir);
strcat(newnew,newname);
CROSS_FILENAME(newnew);
dirCache.ExpandName(newnew);
normalize(newnew,basedir);
const char *dir=PHYSFS_getRealDir(newold);
if (dir && !strcmp(getOverlaydir(), dir)) {
char fullold[CROSS_LEN], fullnew[CROSS_LEN];
strcpy(fullold, dir);
strcat(fullold, newold);
strcpy(fullnew, dir);
strcat(fullnew, newnew);
if (rename(fullold, fullnew)==0) {
dirCache.EmptyCache();
return true;
}
} else LOG_MSG("PHYSFS: rename not supported (%s -> %s)",newold,newnew); // yuck. physfs doesn't have "rename".
return false;
}
bool physfsDrive::SetFileAttr(const char * name,uint16_t attr) {
(void)name;//UNUSED
(void)attr;//UNUSED
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
bool physfsDrive::GetFileAttr(const char * name,uint16_t * attr) {
char newname[CROSS_LEN];
strcpy(newname,basedir);
strcat(newname,name);
CROSS_FILENAME(newname);
dirCache.ExpandName(newname);
normalize(newname,basedir);
char *last = strrchr(newname,'/');
if (last == NULL) last = newname-1;
*attr = 0;
if (!PHYSFS_exists(newname)) return false;
*attr=DOS_ATTR_ARCHIVE;
PHYSFS_Stat statbuf;
BAIL_IF_ERRPASS(!PHYSFS_stat(newname, &statbuf), false);
if(statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY)
*attr |= DOS_ATTR_DIRECTORY;
return true;
}
bool physfsDrive::GetFileAttrEx(char* name, struct stat *status) {
return localDrive::GetFileAttrEx(name,status);
}
unsigned long physfsDrive::GetCompressedSize(char* name) {
return localDrive::GetCompressedSize(name);
}
#if defined (WIN32)
HANDLE physfsDrive::CreateOpenFile(const char* name) {
return localDrive::CreateOpenFile(name);
}
unsigned long physfsDrive::GetSerial() {
return localDrive::GetSerial();
}
#endif
bool physfsDrive::FindNext(DOS_DTA & dta) {
char * dir_ent, *ldir_ent;
char full_name[CROSS_LEN], lfull_name[LFN_NAMELENGTH+1];
uint8_t srch_attr;char srch_pattern[LFN_NAMELENGTH+1];
uint8_t find_attr;
dta.GetSearchParams(srch_attr,srch_pattern,false);
uint16_t id = lfn_filefind_handle>=LFN_FILEFIND_MAX?dta.GetDirID():ldid[lfn_filefind_handle];
again:
if (!dirCache.FindNext(id,dir_ent,ldir_ent)) {
if (lfn_filefind_handle<LFN_FILEFIND_MAX) {
ldid[lfn_filefind_handle]=0;
ldir[lfn_filefind_handle]="";
}
DOS_SetError(DOSERR_NO_MORE_FILES);
return false;
}
if(!WildFileCmp(dir_ent,srch_pattern)&&!LWildFileCmp(ldir_ent,srch_pattern)) goto again;
strcpy(full_name,lfn_filefind_handle>=LFN_FILEFIND_MAX?srchInfo[id].srch_dir:ldir[lfn_filefind_handle].c_str());
strcpy(lfull_name,full_name);
strcat(full_name,dir_ent);
strcat(lfull_name,ldir_ent);
//GetExpandName might indirectly destroy dir_ent (by caching in a new directory
//and due to its design dir_ent might be lost.)
//Copying dir_ent first
dirCache.ExpandName(lfull_name);
normalize(lfull_name,basedir);
PHYSFS_Stat statbuf;
BAIL_IF_ERRPASS(!PHYSFS_stat(lfull_name, &statbuf), false);
if (statbuf.filetype == PHYSFS_FILETYPE_DIRECTORY) find_attr=DOS_ATTR_DIRECTORY|DOS_ATTR_ARCHIVE;
else find_attr=DOS_ATTR_ARCHIVE;
if (~srch_attr & find_attr & (DOS_ATTR_DIRECTORY | DOS_ATTR_HIDDEN | DOS_ATTR_SYSTEM)) goto again;
/*file is okay, setup everything to be copied in DTA Block */
char find_name[DOS_NAMELENGTH_ASCII], lfind_name[LFN_NAMELENGTH+1];
uint16_t find_date,find_time;uint32_t find_size,find_hsize=0;
find_size=(uint32_t)PHYSFS_fileLength(lfull_name);
time_t mytime = statbuf.modtime;
struct tm *time;
if((time=localtime(&mytime))!=nullptr){
find_date=DOS_PackDate((uint16_t)(time->tm_year+1900),(uint16_t)(time->tm_mon+1),(uint16_t)time->tm_mday);
find_time=DOS_PackTime((uint16_t)time->tm_hour,(uint16_t)time->tm_min,(uint16_t)time->tm_sec);
} else {
find_time=6;
find_date=4;
}
if(strlen(dir_ent)<DOS_NAMELENGTH_ASCII){
strcpy(find_name,dir_ent);
if (IS_PC98_ARCH || isDBCSCP())
DBCS_upcase(find_name);
else
upcase(find_name);
}
strcpy(lfind_name,ldir_ent);
lfind_name[LFN_NAMELENGTH]=0;
dta.SetResult(find_name,lfind_name,find_size,find_hsize,find_date,find_time,find_attr);
return true;
}
bool physfsDrive::FindFirst(const char * _dir,DOS_DTA & dta,bool /*fcb_findfirst*/) {
return localDrive::FindFirst(_dir,dta);
}
bool physfsDrive::isRemote(void) {
return localDrive::isRemote();
}
bool physfsDrive::isRemovable(void) {
return true;
}
Bits physfsDrive::UnMount(void) {
char mp[] = "A_DRIVE";
mp[0] = driveLetter;
PHYSFS_unmount(mp);
delete this;
return 0;
}
bool physfsFile::Read(uint8_t * data,uint16_t * size) {
if ((this->flags & 0xf) == OPEN_WRITE) { // check if file opened in write-only mode
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
if (last_action==WRITE) prepareRead();
last_action=READ;
const PHYSFS_sint64 mysize = PHYSFS_readBytes(fhandle, data, *size);
//LOG_MSG("Read %i bytes (wanted %i) at %i of %s (%s)",(int)mysize,(int)*size,(int)PHYSFS_tell(fhandle),name,PHYSFS_getLastError());
*size = (uint16_t)mysize;
return true;
}
bool physfsFile::Write(const uint8_t * data,uint16_t * size) {
if (!PHYSFS_getWriteDir() || (this->flags & 0xf) == OPEN_READ) { // check if file opened in read-only mode
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
if (last_action==READ) prepareWrite();
last_action=WRITE;
if (*size==0) {
if (PHYSFS_tell(fhandle) == 0) {
if (PHYSFS_close(PHYSFS_openWrite(pname))) {
//LOG_MSG("Truncate %s (%s)",name,PHYSFS_getLastError());
return true;
}
else
return false;
} else {
LOG_MSG("PHYSFS TODO: truncate not yet implemented (%s at %i)",pname,(int)PHYSFS_tell(fhandle));
return false;
}
} else {
PHYSFS_sint64 mysize = PHYSFS_writeBytes(fhandle, data, *size);
//LOG_MSG("Wrote %i bytes (wanted %i) at %i of %s (%s)",(int)mysize,(int)*size,(int)PHYSFS_tell(fhandle),name,PHYSFS_getLastError());
*size = (uint16_t)mysize;
return true;
}
}
bool physfsFile::Seek(uint32_t * pos,uint32_t type) {
PHYSFS_sint64 mypos = (int32_t)*pos;
switch (type) {
case DOS_SEEK_SET:break;
case DOS_SEEK_CUR:mypos += PHYSFS_tell(fhandle); break;
case DOS_SEEK_END:mypos += PHYSFS_fileLength(fhandle); break;
default:
//TODO Give some doserrorcode;
return false;//ERROR
}
if (!PHYSFS_seek(fhandle,mypos)) {
// Out of file range, pretend everything's ok
// and move file pointer top end of file... ?! (Black Thorne)
PHYSFS_seek(fhandle,PHYSFS_fileLength(fhandle));
};
//LOG_MSG("Seek to %i (%i at %x) of %s (%s)",(int)mypos,(int)*pos,type,name,PHYSFS_getLastError());
*pos=(uint32_t)PHYSFS_tell(fhandle);
return true;
}
bool physfsFile::prepareRead() {
PHYSFS_uint64 pos = PHYSFS_tell(fhandle);
PHYSFS_close(fhandle);
fhandle = PHYSFS_openRead(pname);
PHYSFS_seek(fhandle, pos);
return true; //LOG_MSG("Goto read (%s at %i)",pname,PHYSFS_tell(fhandle));
}
#ifndef WIN32
#include <fcntl.h>
#include <errno.h>
#endif
bool physfsFile::prepareWrite() {
const char *wdir = PHYSFS_getWriteDir();
if (wdir == NULL) {
LOG_MSG("PHYSFS could not fulfill write request: no write directory set.");
return false;
}
//LOG_MSG("Goto write (%s at %i)",pname,PHYSFS_tell(fhandle));
const char *fdir = PHYSFS_getRealDir(pname);
PHYSFS_uint64 pos = PHYSFS_tell(fhandle);
char *slash = strrchr(pname,'/');
if (slash && slash != pname) {
*slash = 0;
PHYSFS_mkdir(pname);
*slash = '/';
}
if (strcmp(fdir,wdir)) { /* we need COW */
//LOG_MSG("COW",pname,PHYSFS_tell(fhandle));
PHYSFS_file *whandle = PHYSFS_openWrite(pname);
if (whandle == NULL) {
const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
const char* errorMessage = err ? PHYSFS_getErrorByCode(err) : "Unknown error";
LOG_MSG("PHYSFS copy-on-write failed: %s.",errorMessage);
return false;
}
char buffer[65536];
PHYSFS_sint64 size;
PHYSFS_seek(fhandle, 0);
while ((size = PHYSFS_readBytes(fhandle,buffer,65536)) > 0) {
if (PHYSFS_writeBytes(whandle, buffer, size) != size) {
const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
const char* errorMessage = err ? PHYSFS_getErrorByCode(err) : "Unknown error";
LOG_MSG("PHYSFS copy-on-write failed: %s.",errorMessage);
PHYSFS_close(whandle);
return false;
}
}
PHYSFS_seek(whandle, pos);
PHYSFS_close(fhandle);
fhandle = whandle;
} else { // megayuck - physfs on posix platforms uses O_APPEND. We illegally access the fd directly and clear that flag.
//LOG_MSG("noCOW",pname,PHYSFS_tell(fhandle));
PHYSFS_close(fhandle);
fhandle = PHYSFS_openAppend(pname);
PHYSFS_seek(fhandle, pos);
}
return true;
}
bool physfsFile::Close() {
// only close if one reference left
if (refCtr==1) {
PHYSFS_close(fhandle);
fhandle = nullptr;
open = false;
};
return true;
}
uint16_t physfsFile::GetInformation(void) {
return info;
}
physfsFile::physfsFile(const char* _name, PHYSFS_file * handle,uint16_t devinfo, const char* physname, bool write) {
fhandle=handle;
info=devinfo;
strcpy(pname,physname);
PHYSFS_Stat statbuf;
if(!PHYSFS_stat(pname, &statbuf)) return;
time_t mytime = statbuf.modtime;
/* Convert the stat to a FileStat */
struct tm *time;
if((time=localtime(&mytime))!=nullptr) {
this->time=DOS_PackTime((uint16_t)time->tm_hour,(uint16_t)time->tm_min,(uint16_t)time->tm_sec);
this->date=DOS_PackDate((uint16_t)(time->tm_year+1900),(uint16_t)(time->tm_mon+1),(uint16_t)time->tm_mday);
} else {
this->time=DOS_PackTime(0,0,0);
this->date=DOS_PackDate(1980,1,1);
}
attr=DOS_ATTR_ARCHIVE;
last_action=(write?WRITE:READ);
open=true;
name=nullptr;
SetName(_name);
}
bool physfsFile::UpdateDateTimeFromHost(void) {
if(!open) return false;
PHYSFS_Stat statbuf;
BAIL_IF_ERRPASS(!PHYSFS_stat(pname, &statbuf), false);
time_t mytime = statbuf.modtime;
/* Convert the stat to a FileStat */
struct tm *time;
if((time=localtime(&mytime))!=nullptr) {
this->time=DOS_PackTime((uint16_t)time->tm_hour,(uint16_t)time->tm_min,(uint16_t)time->tm_sec);
this->date=DOS_PackDate((uint16_t)(time->tm_year+1900),(uint16_t)(time->tm_mon+1),(uint16_t)time->tm_mday);
} else {
this->time=DOS_PackTime(0,0,0);
this->date=DOS_PackDate(1980,1,1);
}
return true;
}
physfscdromDrive::physfscdromDrive(const char driveLetter, const char * startdir,uint16_t _bytes_sector,uint8_t _sectors_cluster,uint16_t _total_clusters,uint16_t _free_clusters,uint8_t _mediaid, int& error, std::vector<std::string> &options)
:physfsDrive(driveLetter,startdir,_bytes_sector,_sectors_cluster,_total_clusters,_free_clusters,_mediaid,error,options)
{
// Init mscdex
if (!mountarc.size()) {error = 10;return;}
error = MSCDEX_AddDrive(driveLetter,startdir,subUnit);
this->driveLetter = driveLetter;
// Get Volume Label
char name[32];
if (MSCDEX_GetVolumeName(subUnit,name)) dirCache.SetLabel(name,true,true);
}
const char *physfscdromDrive::GetInfo() {
sprintf(info,"PhysFS CDRom %s", mountarc.c_str());
return info;
}
bool physfscdromDrive::FileOpen(DOS_File * * file,const char * name,uint32_t flags)
{
if ((flags&0xf)==OPEN_READWRITE) {
flags &= ~OPEN_READWRITE;
} else if ((flags&0xf)==OPEN_WRITE) {
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
return physfsDrive::FileOpen(file,name,flags);
}
bool physfscdromDrive::FileCreate(DOS_File * * file,const char * name,uint16_t attributes)
{
(void)file;//UNUSED
(void)name;//UNUSED
(void)attributes;//UNUSED
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
bool physfscdromDrive::FileUnlink(const char * name)
{
(void)name;//UNUSED
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
bool physfscdromDrive::RemoveDir(const char * dir)
{
(void)dir;//UNUSED
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
bool physfscdromDrive::MakeDir(const char * dir)
{
(void)dir;//UNUSED
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
bool physfscdromDrive::Rename(const char * oldname,const char * newname)
{
(void)oldname;//UNUSED
(void)newname;//UNUSED
DOS_SetError(DOSERR_ACCESS_DENIED);
return false;
}
bool physfscdromDrive::GetFileAttr(const char * name,uint16_t * attr)
{
bool result = physfsDrive::GetFileAttr(name,attr);
if (result) *attr |= DOS_ATTR_READ_ONLY;
return result;
}
bool physfscdromDrive::FindFirst(const char * _dir,DOS_DTA & dta,bool fcb_findfirst)
{
(void)fcb_findfirst;//UNUSED
// If media has changed, reInit drivecache.
if (MSCDEX_HasMediaChanged(subUnit)) {
dirCache.EmptyCache();
// Get Volume Label
char name[32];
if (MSCDEX_GetVolumeName(subUnit,name)) dirCache.SetLabel(name,true,true);
}
return physfsDrive::FindFirst(_dir,dta);
}
void physfscdromDrive::SetDir(const char* path)
{
// If media has changed, reInit drivecache.
if (MSCDEX_HasMediaChanged(subUnit)) {
dirCache.EmptyCache();
// Get Volume Label
char name[32];
if (MSCDEX_GetVolumeName(subUnit,name)) dirCache.SetLabel(name,true,true);
}
physfsDrive::SetDir(path);
}
bool physfscdromDrive::isRemote(void) {
return true;
}
bool physfscdromDrive::isRemovable(void) {
return true;
}
Bits physfscdromDrive::UnMount(void) {
if(MSCDEX_RemoveDrive(driveLetter))
return physfsDrive::UnMount();
return 2;
}