forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filesys.cpp
10603 lines (9588 loc) · 285 KB
/
filesys.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
/*
* UAE - The Un*x Amiga Emulator
*
* Unix file system handler for AmigaDOS
*
* Copyright 1996 Ed Hanway
* Copyright 1996, 1997 Bernd Schmidt
*
* Version 0.4: 970308
*
* Based on example code (c) 1988 The Software Distillery
* and published in Transactor for the Amiga, Volume 2, Issues 2-5.
* (May - August 1989)
*
* Known limitations:
* Does not support several (useless) 2.0+ packet types.
* May not return the correct error code in some cases.
* Does not check for sane values passed by AmigaDOS. May crash the emulation
* if passed garbage values.
* Could do tighter checks on malloc return values.
* Will probably fail spectacularly in some cases if the filesystem is
* modified at the same time by another process while UAE is running.
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "threaddep/thread.h"
#include "options.h"
#include "traps.h"
#include "uae.h"
#include "memory.h"
#include "custom.h"
#include "events.h"
#include "newcpu.h"
#include "filesys.h"
#include "autoconf.h"
#include "fsusage.h"
#include "native2amiga.h"
#include "scsidev.h"
#include "uaeserial.h"
#include "fsdb.h"
#include "zfile.h"
#include "zarchive.h"
#include "gui.h"
#include "gayle.h"
#include "idecontrollers.h"
#include "savestate.h"
#include "a2091.h"
#include "ncr_scsi.h"
#include "cdtv.h"
#include "sana2.h"
#include "bsdsocket.h"
#include "uaeresource.h"
#include "inputdevice.h"
#include "clipboard.h"
#include "consolehook.h"
#include "blkdev.h"
#include "isofs_api.h"
#include "scsi.h"
#include "uaenative.h"
#include "tabletlibrary.h"
#include "cia.h"
#include "picasso96.h"
#include "cpuboard.h"
#include "rommgr.h"
#include "debug.h"
#include "debugmem.h"
#ifdef RETROPLATFORM
#include "rp.h"
#endif
#define TRACING_ENABLED 1
int log_filesys = 0;
#define TRAPMD 1
#if TRACING_ENABLED
#if 0
#define TRACE(x) if (log_filesys > 0 && (unit->volflags & MYVOLUMEINFO_CDFS)) { write_log x; }
#else
#define TRACE(x) if (log_filesys > 0) { write_log x; }
#endif
#define TRACEI(x) if (log_filesys > 0) { write_log x; }
#define TRACE2(x) if (log_filesys >= 2) { write_log x; }
#define TRACE3(x) if (log_filesys >= 3) { write_log x; }
#define DUMPLOCK(c,u,x) dumplock(c,u,x)
#else
#define TRACE(x)
#define DUMPLOCK(c,u,x)
#define TRACE2(x)
#define TRACE3(x)
#endif
#define KS12_BOOT_HACK 1
#define UNIT_LED(unit) ((unit)->ui.unit_type == UNIT_CDFS ? LED_CD : LED_HD)
#define SE2_MIN_FILE_LENGTH 32
struct ShellExecute2
{
uae_u32 size;
char *file, *parms, *currentdir;
char *fileparms;
uae_u32 stack;
uae_s32 priority;
uae_u32 flags;
uae_u32 id;
uae_u32 binsize;
uae_u8 *bin;
char *tmpout;
uaecptr aoutbuf;
char *binname;
int aoutlen;
uaecptr process;
uaecptr buffer;
uae_u32 exitcode;
shellexecute2_callback cb;
char *outbuf;
int outlen;
void *userdata;
int state;
};
#define SHELLEXEC_MAX 16
static struct ShellExecute2 shellexecute2[SHELLEXEC_MAX];
static volatile int shellexecute2_queued;
static uae_u32 filesys_shellexecute2_process(int mode, TrapContext *ctx);
static void filesys_shellexecute2_run_queue(void);
static void shellexecute2_free(struct ShellExecute2 *se2);
static int bootrom_header;
static uae_u32 dlg (uae_u32 a)
{
return (dbg (a + 0) << 24) | (dbg (a + 1) << 16) | (dbg (a + 2) << 8) | (dbg (a + 3) << 0);
}
static void aino_test (a_inode *aino)
{
#ifdef AINO_DEBUG
a_inode *aino2 = aino, *aino3;
for (;;) {
if (!aino || !aino->next)
return;
if ((aino->checksum1 ^ aino->checksum2) != 0xaaaa5555) {
write_log (_T("PANIC: corrupted or freed but used aino detected!"), aino);
}
aino3 = aino;
aino = aino->next;
if (aino->prev != aino3) {
write_log (_T("PANIC: corrupted aino linking!\n"));
break;
}
if (aino == aino2) break;
}
#endif
}
static void aino_test_init (a_inode *aino)
{
#ifdef AINO_DEBUG
aino->checksum1 = (uae_u32)aino;
aino->checksum2 = aino->checksum1 ^ 0xaaaa5555;
#endif
}
#define UAEFS_VERSION "UAE fs 0.6"
uaecptr filesys_initcode, filesys_initcode_ptr, filesys_initcode_real;
static uaecptr bootrom_start;
static uae_u32 fsdevname, fshandlername, filesys_configdev;
static uae_u32 cdfs_devname, cdfs_handlername;
static uaecptr afterdos_name, afterdos_id, afterdos_initcode;
static uaecptr keymaphook_name, keymaphook_id, keymaphook_initcode;
static uaecptr shell_execute_data, shell_execute_process;
static int filesys_in_interrupt;
static uae_u32 mountertask;
static int automountunit = -1;
static int autocreatedunit;
static int cd_unit_offset, cd_unit_number;
static uaecptr ROM_filesys_doio, ROM_filesys_doio_original;
static uaecptr ROM_filesys_putmsg, ROM_filesys_putmsg_original;
static uaecptr ROM_filesys_putmsg_return;
static uaecptr ROM_filesys_hack_remove;
static smp_comm_pipe shellexecute_pipe;
static uae_u32 segtrack_mode = 0;
static bool gayle_ide_in_use;
#define FS_STARTUP 0
#define FS_GO_DOWN 1
#define DEVNAMES_PER_HDF 32
#define UNIT_FILESYSTEM 0
#define UNIT_CDFS 1
typedef struct {
int unit_type;
int open; // >0 start as filesystem, <0 = allocated but do not start
TCHAR *devname; /* device name, e.g. UAE0: */
uaecptr devname_amiga;
uaecptr startup;
uaecptr devicenode;
uaecptr parmpacket;
TCHAR *volname; /* volume name, e.g. CDROM, WORK, etc. */
int volflags; /* volume flags, readonly, stream uaefsdb support */
TCHAR *rootdir; /* root native directory/hdf. empty drive if invalid path */
TCHAR *rootdirdiff; /* "diff" file/directory */
bool readonly; /* disallow write access? */
bool locked; /* action write protect */
bool unknown_media; /* ID_UNREADABLE_DISK */
int bootpri; /* boot priority. -128 = no autoboot, -129 = no mount */
int devno;
bool wasisempty; /* if true, this unit was created empty */
int canremove; /* if >0, this unit can be safely ejected and remounted */
bool configureddrive; /* if true, this is drive that was manually configured */
bool inject_icons; /* inject icons if directory filesystem */
struct hardfiledata hf;
struct zvolume *zarchive;
/* Threading stuff */
smp_comm_pipe *volatile unit_pipe, *volatile back_pipe;
uae_thread_id tid;
struct _unit *self;
/* Reset handling */
uae_sem_t reset_sync_sem;
volatile int reset_state;
/* RDB stuff */
uaecptr rdb_devname_amiga[DEVNAMES_PER_HDF];
int rdb_lowcyl;
int rdb_highcyl;
int rdb_cylblocks;
uae_u8 *rdb_filesysstore;
int rdb_filesyssize;
TCHAR *filesysdir;
/* filesystem seglist */
uaecptr filesysseg;
uae_u32 rdb_dostype;
/* CDFS */
bool cd_open;
int cddevno;
void *cdfs_superblock;
} UnitInfo;
struct uaedev_mount_info {
UnitInfo ui[MAX_FILESYSTEM_UNITS];
};
static struct uaedev_mount_info mountinfo;
/* minimal AmigaDOS definitions */
/* field offsets in DosPacket */
#define dp_Type 8
#define dp_Res1 12
#define dp_Res2 16
#define dp_Arg1 20
#define dp_Arg2 24
#define dp_Arg3 28
#define dp_Arg4 32
#define dp_Arg5 36
#define DP64_INIT -3L
#define dp64_Type 8
#define dp64_Res0 12
#define dp64_Res2 16
#define dp64_Res1 24
#define dp64_Arg1 32
#define dp64_Arg2 40
#define dp64_Arg3 48
#define dp64_Arg4 52
#define dp64_Arg5 56
#define dp_Max 60
/* result codes */
#define DOS_TRUE ((uae_u32)-1L)
#define DOS_FALSE (0L)
/* DirEntryTypes */
#define ST_PIPEFILE -5
#define ST_LINKFILE -4
#define ST_FILE -3
#define ST_ROOT 1
#define ST_USERDIR 2
#define ST_SOFTLINK 3
#define ST_LINKDIR 4
#if 1
#define MAXFILESIZE32 (0xffffffff)
#else
/* technically correct but most native
* filesystems don't enforce it
*/
#define MAXFILESIZE32 (0x7fffffff)
#endif
#define MAXFILESIZE32_2G (0x7fffffff)
/* Passed as type to Lock() */
#define SHARED_LOCK -2 /* File is readable by others */
#define ACCESS_READ -2 /* Synonym */
#define EXCLUSIVE_LOCK -1 /* No other access allowed */
#define ACCESS_WRITE -1 /* Synonym */
/* packet types */
#define ACTION_CURRENT_VOLUME 7
#define ACTION_LOCATE_OBJECT 8
#define ACTION_RENAME_DISK 9
#define ACTION_FREE_LOCK 15
#define ACTION_DELETE_OBJECT 16
#define ACTION_RENAME_OBJECT 17
#define ACTION_MORE_CACHE 18
#define ACTION_COPY_DIR 19
#define ACTION_SET_PROTECT 21
#define ACTION_CREATE_DIR 22
#define ACTION_EXAMINE_OBJECT 23
#define ACTION_EXAMINE_NEXT 24
#define ACTION_DISK_INFO 25
#define ACTION_INFO 26
#define ACTION_FLUSH 27
#define ACTION_SET_COMMENT 28
#define ACTION_PARENT 29
#define ACTION_SET_DATE 34
#define ACTION_FIND_WRITE 1004
#define ACTION_FIND_INPUT 1005
#define ACTION_FIND_OUTPUT 1006
#define ACTION_END 1007
#define ACTION_SEEK 1008
#define ACTION_WRITE_PROTECT 1023
#define ACTION_IS_FILESYSTEM 1027
#define ACTION_READ 'R'
#define ACTION_WRITE 'W'
/* 2.0+ packet types */
#define ACTION_INHIBIT 31
#define ACTION_SET_FILE_SIZE 1022
#define ACTION_LOCK_RECORD 2008
#define ACTION_FREE_RECORD 2009
#define ACTION_SAME_LOCK 40
#define ACTION_CHANGE_MODE 1028
#define ACTION_FH_FROM_LOCK 1026
#define ACTION_COPY_DIR_FH 1030
#define ACTION_PARENT_FH 1031
#define ACTION_EXAMINE_ALL 1033
#define ACTION_EXAMINE_FH 1034
#define ACTION_EXAMINE_ALL_END 1035
#define ACTION_FORMAT 1020
#define ACTION_IS_FILESYSTEM 1027
#define ACTION_ADD_NOTIFY 4097
#define ACTION_REMOVE_NOTIFY 4098
#define ACTION_READ_LINK 1024
/* OS4 64-bit filesize packets */
#define ACTION_FILESYSTEM_ATTR 3005
#define ACTION_CHANGE_FILE_POSITION64 8001
#define ACTION_GET_FILE_POSITION64 8002
#define ACTION_CHANGE_FILE_SIZE64 8003
#define ACTION_GET_FILE_SIZE64 8004
/* MOS 64-bit filesize packets */
#define ACTION_SEEK64 26400
#define ACTION_SET_FILE_SIZE64 26401
#define ACTION_LOCK_RECORD64 26402
#define ACTION_FREE_RECORD64 26403
#define ACTION_QUERY_ATTR 26407
#define ACTION_EXAMINE_OBJECT64 26408
#define ACTION_EXAMINE_NEXT64 26409
#define ACTION_EXAMINE_FH64 26410
/* not supported */
#define ACTION_MAKE_LINK 1021
#define DISK_TYPE_DOS 0x444f5300 /* DOS\0 */
#define DISK_TYPE_DOS_FFS 0x444f5301 /* DOS\1 */
#define CDFS_DOSTYPE 0x43440000 /* CDxx */
typedef struct _dpacket {
uaecptr packet_addr;
uae_u8 *packet_data;
uae_u8 packet_array[dp_Max];
bool need_flush;
} dpacket;
typedef struct {
uae_u32 uniq;
/* The directory we're going through. */
a_inode *aino;
/* The file we're going to look up next. */
a_inode *curr_file;
} ExamineKey;
struct lockrecord
{
struct lockrecord *next;
dpacket *packet;
uae_u64 pos;
uae_u64 len;
uae_u32 mode;
uae_u32 timeout;
uae_u32 msg;
};
typedef struct key {
struct key *next;
a_inode *aino;
uae_u32 uniq;
struct fs_filehandle *fd;
uae_u64 file_pos;
int dosmode;
int createmode;
int notifyactive;
struct lockrecord *record;
} Key;
typedef struct notify {
struct notify *next;
uaecptr notifyrequest;
TCHAR *fullname;
TCHAR *partname;
} Notify;
typedef struct exallkey {
uae_u32 id;
struct fs_dirhandle *dirhandle;
TCHAR *fn;
uaecptr control;
} ExAllKey;
/* Since ACTION_EXAMINE_NEXT is so braindamaged, we have to keep
* some of these around
*/
#define EXKEYS 128
#define EXALLKEYS 100
#define MAX_AINO_HASH 128
#define NOTIFY_HASH_SIZE 127
/* handler state info */
typedef struct _unit {
struct _unit *next;
/* Amiga stuff */
uaecptr dosbase;
/* volume points to our IO board, always 1:1 mapping */
uaecptr volume;
uaecptr port; /* Our port */
uaecptr locklist;
/* Native stuff */
uae_s32 unit; /* unit number */
UnitInfo ui; /* unit startup info */
TCHAR tmpbuf3[256];
/* Dummy message processing */
uaecptr dummy_message;
volatile unsigned int cmds_sent;
volatile unsigned int cmds_complete;
volatile unsigned int cmds_acked;
/* ExKeys */
ExamineKey examine_keys[EXKEYS];
int next_exkey;
unsigned int total_locked_ainos;
/* ExAll */
ExAllKey exalls[EXALLKEYS];
int exallid;
/* Keys */
struct key *keys;
struct lockrecord *waitingrecords;
a_inode rootnode;
unsigned int aino_cache_size;
a_inode *aino_hash[MAX_AINO_HASH];
unsigned int nr_cache_hits;
unsigned int nr_cache_lookups;
struct notify *notifyhash[NOTIFY_HASH_SIZE];
int volflags;
uae_u32 lockkey;
bool inhibited;
bool canremovable;
/* increase when media is changed.
* used to detect if cached aino is valid
*/
int mountcount;
int mount_changed;
void *cdfs_superblock;
TCHAR *mount_volume;
TCHAR *mount_rootdir;
bool mount_readonly;
int mount_flags;
int reinsertdelay;
TCHAR *newvolume;
TCHAR *newrootdir;
bool newreadonly;
int newflags;
} Unit;
int nr_units (void)
{
int cnt = 0;
for (int i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
if (mountinfo.ui[i].open > 0)
cnt++;
}
return cnt;
}
int nr_directory_units (struct uae_prefs *p)
{
int cnt = 0;
if (p) {
for (int i = 0; i < p->mountitems; i++) {
if (p->mountconfig[i].ci.controller_type == HD_CONTROLLER_TYPE_UAE)
cnt++;
}
} else {
for (int i = 0; i < MAX_FILESYSTEM_UNITS; i++) {
if (mountinfo.ui[i].open > 0 && mountinfo.ui[i].hf.ci.controller_type == HD_CONTROLLER_TYPE_UAE)
cnt++;
}
}
return cnt;
}
static int is_virtual (int unit_no)
{
int t = is_hardfile (unit_no);
return t == FILESYS_VIRTUAL || t == FILESYS_CD;
}
int is_hardfile (int unit_no)
{
if (mountinfo.ui[unit_no].volname || mountinfo.ui[unit_no].wasisempty || mountinfo.ui[unit_no].unknown_media) {
if (unit_no >= cd_unit_offset && unit_no < cd_unit_offset + cd_unit_number)
return FILESYS_CD;
return FILESYS_VIRTUAL;
}
if (mountinfo.ui[unit_no].hf.ci.sectors == 0) {
if (mountinfo.ui[unit_no].hf.flags & 1)
return FILESYS_HARDDRIVE;
return FILESYS_HARDFILE_RDB;
}
return FILESYS_HARDFILE;
}
static void close_filesys_unit (UnitInfo *uip)
{
if (!uip->open)
return;
if (uip->hf.handle_valid)
hdf_close (&uip->hf);
if (uip->volname != 0)
xfree (uip->volname);
if (uip->devname != 0)
xfree (uip->devname);
if (uip->rootdir != 0)
xfree (uip->rootdir);
if (uip->unit_pipe)
xfree (uip->unit_pipe);
if (uip->back_pipe)
xfree (uip->back_pipe);
if (uip->cd_open) {
sys_command_close (uip->cddevno);
isofs_unmount (uip->cdfs_superblock);
}
uip->unit_pipe = 0;
uip->back_pipe = 0;
uip->hf.handle_valid = 0;
uip->volname = 0;
uip->devname = 0;
uip->rootdir = 0;
uip->open = 0;
uip->cd_open = 0;
}
static uaedev_config_data *getuci (struct uaedev_config_data *uci, int nr)
{
return &uci[nr];
}
static UnitInfo *getuip (struct uae_prefs *p, int index)
{
if (index < 0)
return NULL;
index = p->mountconfig[index].configoffset;
if (index < 0)
return NULL;
return &mountinfo.ui[index];
}
static int getuindex (struct uae_prefs *p, int index)
{
if (index < 0)
return -1;
return p->mountconfig[index].unitnum;
}
int get_filesys_unitconfig (struct uae_prefs *p, int index, struct mountedinfo *mi)
{
UnitInfo *ui = getuip (p, index);
struct uaedev_config_data *uci = &p->mountconfig[index];
UnitInfo uitmp;
TCHAR filepath[MAX_DPATH];
memset (mi, 0, sizeof (struct mountedinfo));
memset (&uitmp, 0, sizeof uitmp);
_tcscpy(mi->rootdir, uci->ci.rootdir);
if (!ui) {
ui = &uitmp;
if (uci->ci.type == UAEDEV_DIR) {
cfgfile_resolve_path_out_load(uci->ci.rootdir, filepath, MAX_DPATH, PATH_DIR);
_tcscpy(mi->rootdir, filepath);
mi->ismounted = 1;
if (filepath[0] == 0)
return FILESYS_VIRTUAL;
if (my_existsfile (filepath)) {
mi->ismedia = 1;
return FILESYS_VIRTUAL;
}
if (my_getvolumeinfo (filepath) < 0)
return -1;
mi->ismedia = true;
return FILESYS_VIRTUAL;
} else if (uci->ci.type == UAEDEV_HDF) {
cfgfile_resolve_path_out_load(uci->ci.rootdir, filepath, MAX_DPATH, PATH_HDF);
_tcscpy(mi->rootdir, filepath);
ui->hf.ci.readonly = true;
ui->hf.ci.blocksize = uci->ci.blocksize;
int err = hdf_open (&ui->hf, filepath);
if (err <= 0) {
mi->ismedia = false;
mi->ismounted = true;
mi->error = err;
if (uci->ci.reserved == 0 && uci->ci.sectors == 0 && uci->ci.surfaces == 0) {
if (ui->hf.flags & 1)
return FILESYS_HARDDRIVE;
return FILESYS_HARDFILE_RDB;
}
return -1;
}
mi->ismedia = true;
if (ui->hf.drive_empty)
mi->ismedia = 0;
hdf_close (&ui->hf);
} else if (uci->ci.type == UAEDEV_CD) {
cfgfile_resolve_path_out_load(uci->ci.rootdir, filepath, MAX_DPATH, PATH_CD);
_tcscpy(mi->rootdir, filepath);
struct device_info di;
ui->hf.ci.readonly = true;
ui->hf.ci.blocksize = uci->ci.blocksize;
mi->size = -1;
mi->ismounted = true;
if (blkdev_get_info (p, ui->hf.ci.device_emu_unit, &di)) {
mi->ismedia = di.media_inserted != 0;
_tcscpy (mi->rootdir, di.label);
}
#if 0
if (ui->hf.ci.cd_emu_unit == 0)
_tcscpy (mi->rootdir, _T("CD"));
else
_stprintf (mi->rootdir, _T("CD %d"), ui->hf.ci.cd_emu_unit);
#endif
}
} else if (uci->ci.type != UAEDEV_TAPE) {
if (ui->hf.ci.controller_type == HD_CONTROLLER_TYPE_UAE) { // what is this? || (ui->controller && p->cs_ide)) {
mi->ismounted = 1;
if (uci->ci.type == UAEDEV_HDF)
mi->ismedia = ui->hf.drive_empty ? false : true;
else
mi->ismedia = true;
}
}
if (uci->ci.type == UAEDEV_TAPE) {
cfgfile_resolve_path_out_load(uci->ci.rootdir, filepath, MAX_DPATH, PATH_TAPE);
_tcscpy(mi->rootdir, filepath);
struct device_info di;
int unitnum = getuindex (p, index);
mi->size = -1;
mi->ismounted = false;
if (unitnum >= 0) {
mi->ismounted = true;
if (tape_get_info (unitnum, &di)) {
mi->ismedia = di.media_inserted != 0;
_tcscpy (mi->rootdir, di.label);
}
} else {
struct scsi_data_tape *tape;
unitnum = 0;
tape = tape_alloc (unitnum, filepath, uci->ci.readonly);
if (tape) {
if (tape_get_info (unitnum, &di)) {
mi->ismedia = di.media_inserted != 0;
_tcscpy (mi->rootdir, di.label);
}
tape_free (tape);
}
}
return FILESYS_TAPE;
}
if (mi->size < 0)
return -1;
mi->size = ui->hf.virtsize;
if (uci->ci.highcyl) {
uci->ci.cyls = mi->nrcyls = uci->ci.highcyl;
} else {
uci->ci.cyls = mi->nrcyls = (int)(uci->ci.sectors * uci->ci.surfaces ? (ui->hf.virtsize / uci->ci.blocksize) / (uci->ci.sectors * uci->ci.surfaces) : 0);
}
if (uci->ci.type == UAEDEV_DIR)
return FILESYS_VIRTUAL;
if (uci->ci.reserved == 0 && uci->ci.sectors == 0 && uci->ci.surfaces == 0) {
if (ui->hf.flags & 1)
return FILESYS_HARDDRIVE;
return FILESYS_HARDFILE_RDB;
}
return FILESYS_HARDFILE;
}
static void stripsemicolon (TCHAR *s)
{
if (!s)
return;
while (uaetcslen(s) > 0 && s[uaetcslen(s) - 1] == ':')
s[uaetcslen(s) - 1] = 0;
}
static void stripspace (TCHAR *s)
{
if (!s)
return;
for (int i = 0; i < uaetcslen (s); i++) {
if (s[i] == ' ')
s[i] = '_';
}
}
static void striplength (TCHAR *s, int len)
{
if (!s)
return;
if (uaetcslen (s) <= len)
return;
s[len] = 0;
}
static void fixcharset(TCHAR *s)
{
char tmp[MAX_DPATH];
if (!s)
return;
ua_fs_copy(tmp, MAX_DPATH - 1, s, '_');
au_fs_copy(s, uaestrlen(tmp) + 1, tmp);
}
TCHAR *validatevolumename (TCHAR *s, const TCHAR *def)
{
stripsemicolon (s);
fixcharset (s);
striplength (s, 30);
if (uaetcslen(s) == 0 && def) {
xfree(s);
s = my_strdup(def);
}
return s;
}
TCHAR *validatedevicename (TCHAR *s, const TCHAR *def)
{
stripsemicolon (s);
stripspace (s);
fixcharset (s);
striplength (s, 30);
if (uaetcslen(s) == 0 && def) {
xfree(s);
s = my_strdup(def);
}
return s;
}
TCHAR *filesys_createvolname (const TCHAR *volname, const TCHAR *rootdir, struct zvolume *zv, const TCHAR *def)
{
TCHAR *nvol = NULL;
int i, archivehd;
TCHAR *p = NULL;
TCHAR path[MAX_DPATH];
cfgfile_resolve_path_out_load(rootdir, path, MAX_DPATH, PATH_DIR);
archivehd = -1;
if (my_existsfile (path))
archivehd = 1;
else if (my_existsdir (path))
archivehd = 0;
if (zv && zv->volumename && uaetcslen(zv->volumename) > 0) {
nvol = my_strdup(zv->volumename);
nvol = validatevolumename (nvol, def);
return nvol;
}
if ((!volname || uaetcslen (volname) == 0) && path && archivehd >= 0) {
p = my_strdup (path);
for (i = uaetcslen (p) - 1; i >= 0; i--) {
TCHAR c = p[i];
if (c == ':' || c == '/' || c == '\\') {
if (i == uaetcslen (p) - 1)
continue;
if (!_tcscmp (p + i, _T(":\\"))) {
xfree (p);
p = xmalloc (TCHAR, 10);
p[0] = path[0];
p[1] = 0;
i = 0;
} else {
i++;
}
break;
}
}
if (i >= 0)
nvol = my_strdup (p + i);
}
if (!nvol && archivehd >= 0) {
if (volname && uaetcslen (volname) > 0)
nvol = my_strdup (volname);
else
nvol = my_strdup (def);
}
if (!nvol) {
if (volname && uaetcslen (volname))
nvol = my_strdup (volname);
else
nvol = my_strdup (_T(""));
}
nvol = validatevolumename (nvol, def);
xfree (p);
return nvol;
}
static int set_filesys_volume (const TCHAR *rootdir, int *flags, bool *readonly, bool *emptydrive, struct zvolume **zvp)
{
*emptydrive = 0;
if (my_existsfile (rootdir)) {
struct zvolume *zv;
zv = zfile_fopen_archive (rootdir);
if (!zv) {
error_log (_T("'%s' is not a supported archive file."), rootdir);
return -1;
}
*zvp = zv;
*flags = MYVOLUMEINFO_ARCHIVE;
*readonly = 1;
} else {
*flags = my_getvolumeinfo (rootdir);
if (*flags < 0) {
if (rootdir && rootdir[0])
error_log (_T("directory '%s' not found, mounting as empty drive."), rootdir);
*emptydrive = 1;
*flags = 0;
} else if ((*flags) & MYVOLUMEINFO_READONLY) {
error_log (_T("'%s' set to read-only."), rootdir);
*readonly = 1;
}
}
return 1;
}
void uci_set_defaults (struct uaedev_config_info *uci, bool rdb)
{
memset (uci, 0, sizeof (struct uaedev_config_info));
if (!rdb) {
uci->sectors = 32;
uci->reserved = 2;
uci->surfaces = 1;
}
uci->blocksize = 512;
uci->maxtransfer = 0x7fffffff;
uci->mask = 0xffffffff;
uci->bufmemtype = 1;
uci->buffers = 50;
uci->stacksize = 4000;
uci->bootpri = 0;
uci->priority = 10;
uci->sectorsperblock = 1;
uci->device_emu_unit = -1;
}
static void get_usedblocks(struct fs_usage *fsu, bool fs, int *pblocksize, uae_s64 *pnumblocks, uae_s64 *pinuse, bool reduce)
{
uae_s64 numblocks = 0, inuse;
int blocksize = *pblocksize;
if (fs && currprefs.filesys_limit) {
if (fsu->total > (uae_s64)currprefs.filesys_limit * 1024) {
uae_s64 oldtotal = fsu->total;
fsu->total = currprefs.filesys_limit * 1024;
fsu->avail = ((fsu->avail / 1024) * (fsu->total / 1024)) / (oldtotal / 1024);
fsu->avail *= 1024;
}
}
if (reduce) {
while (blocksize < 32768 || numblocks == 0) {
numblocks = fsu->total / blocksize;
if (numblocks <= 10)
numblocks = 10;
// Value that does not overflow when multiplied by 100 (uses 128 to keep it simple)
if (numblocks < 0x02000000)
break;
blocksize *= 2;
}
}
numblocks = fsu->total / blocksize;
inuse = (numblocks * blocksize - fsu->avail) / blocksize;
if (inuse > numblocks)
inuse = numblocks;
if (pnumblocks)
*pnumblocks = numblocks;
if (pinuse)
*pinuse = inuse;
if (pblocksize)
*pblocksize = blocksize;
}
static bool get_blocks(const TCHAR *rootdir, int unit, int flags, int *pblocksize, uae_s64 *pnumblocks, uae_s64 *pinuse, bool reduce)
{
struct fs_usage fsu;
int ret;
bool fs = false;
int blocksize;
blocksize = 512;
if (flags & MYVOLUMEINFO_ARCHIVE) {
ret = zfile_fs_usage_archive(rootdir, 0, &fsu);
fs = true;
} else {
ret = get_fs_usage(rootdir, 0, &fsu);
fs = true;
}
if (ret)
return false;
get_usedblocks(&fsu, fs, &blocksize, pnumblocks, pinuse, reduce);
if (pblocksize)
*pblocksize = blocksize;
return ret == 0;
}
static int set_filesys_unit_1 (int nr, struct uaedev_config_info *ci, bool custom)
{
UnitInfo *ui;
int i;
bool emptydrive = false;
bool iscd;
struct uaedev_config_info c;
memcpy (&c, ci, sizeof (struct uaedev_config_info));
if (nr < 0) {
for (nr = 0; nr < MAX_FILESYSTEM_UNITS; nr++) {
if (!mountinfo.ui[nr].open)
break;
}
if (nr == MAX_FILESYSTEM_UNITS) {
error_log (_T("No slot allocated for this unit"));
return -1;
}
}
if (ci->controller_type != HD_CONTROLLER_TYPE_UAE || ci->type == UAEDEV_TAPE) {
ui = &mountinfo.ui[nr];
memset (ui, 0, sizeof (UnitInfo));
memcpy (&ui->hf.ci, &c, sizeof (struct uaedev_config_info));
ui->readonly = c.readonly;
ui->unit_type = -1;
ui->open = -1;
return nr;
}
iscd = nr >= cd_unit_offset && nr < cd_unit_offset + cd_unit_number;
if (!custom)
cfgfile_resolve_path_load(c.rootdir, MAX_DPATH, iscd ? PATH_CD : (ci->volname[0] ? PATH_DIR : PATH_HDF));