-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilesys.c
executable file
·4513 lines (4019 loc) · 133 KB
/
filesys.c
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 ACTION_INHIBIT (big deal).
* Does not support several 2.0+ packet types.
* Does not support removable volumes.
* 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 "uae.h"
#include "memory.h"
#include "custom.h"
#include "events.h"
#include "newcpu.h"
#include "filesys.h"
#include "autoconf.h"
#include "traps.h"
#include "fsusage.h"
#include "native2amiga.h"
#include "scsidev.h"
#include "fsdb.h"
#include "zfile.h"
#include "gui.h"
//ric
#ifdef FILESYS
#define TRACING_ENABLED 0
#if TRACING_ENABLED
#define TRACE(x) do { write_log x; } while(0)
#define DUMPLOCK(u,x) dumplock(u,x)
#else
#define TRACE(x)
#define DUMPLOCK(u,x)
#endif
static void xfree (void *p)
{
free (p);
}
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 ("PANIC: corrupted or freed but used aino detected!", aino);
}
aino3 = aino;
aino = aino->next;
if (aino->prev != aino3) {
write_log ("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
}
/*
* This _should_ be no issue for us, but let's rather use a guaranteed
* thread safe function if we have one.
* This used to be broken in glibc versions <= 2.0.1 (I think). I hope
* no one is using this these days.
* Michael Krause says it's also broken in libc5. ARRRGHHHHHH!!!!
*/
#if 0 && defined HAVE_READDIR_R
static struct dirent *my_readdir (DIR *dirstream, struct dirent *space)
{
struct dirent *loc;
if (readdir_r (dirstream, space, &loc) == 0) {
/* Success */
return loc;
}
return 0;
}
#else
#define my_readdir(dirstream, space) readdir(dirstream)
#endif
uaecptr filesys_initcode;
static uae_u32 fsdevname, filesys_configdev;
#define FS_STARTUP 0
#define FS_GO_DOWN 1
#define DEVNAMES_PER_HDF 32
typedef struct {
char *devname; /* device name, e.g. UAE0: */
uaecptr devname_amiga;
uaecptr startup;
char *volname; /* volume name, e.g. CDROM, WORK, etc. */
char *rootdir; /* root unix directory */
int readonly; /* disallow write access? */
int bootpri; /* boot priority */
int devno;
struct hardfiledata hf;
/* Threading stuff */
smp_comm_pipe *volatile unit_pipe, *volatile back_pipe;
uae_thread_id tid;
struct _unit *self;
/* Reset handling */
volatile 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;
char *filesysdir;
} UnitInfo;
struct uaedev_mount_info {
int num_units;
UnitInfo ui[MAX_FILESYSTEM_UNITS];
};
static struct uaedev_mount_info *current_mountinfo;
int nr_units (struct uaedev_mount_info *mountinfo)
{
return mountinfo->num_units;
}
int is_hardfile (struct uaedev_mount_info *mountinfo, int unit_no)
{
if (mountinfo->ui[unit_no].volname)
return FILESYS_VIRTUAL;
if (mountinfo->ui[unit_no].hf.secspertrack == 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->hf.handle != 0)
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);
uip->unit_pipe = 0;
uip->back_pipe = 0;
uip->hf.handle = 0;
uip->volname = 0;
uip->devname = 0;
uip->rootdir = 0;
}
const char *get_filesys_unit (struct uaedev_mount_info *mountinfo, int nr,
char **devname, char **volname, char **rootdir, int *readonly,
int *secspertrack, int *surfaces, int *reserved,
int *cylinders, uae_u64 *size, int *blocksize, int *bootpri, char **filesysdir)
{
UnitInfo *uip = mountinfo->ui + nr;
if (nr >= mountinfo->num_units)
return "No slot allocated for this unit";
*volname = uip->volname ? my_strdup (uip->volname) : 0;
if (uip->devname == 0 || strlen(uip->devname) == 0) {
*devname = xmalloc (10);
sprintf (*devname, "DH%d", nr);
} else {
*devname = my_strdup (uip->devname);
}
*rootdir = uip->rootdir ? my_strdup (uip->rootdir) : 0;
*readonly = uip->readonly;
*secspertrack = uip->hf.secspertrack;
*surfaces = uip->hf.surfaces;
*reserved = uip->hf.reservedblocks;
*cylinders = uip->hf.nrcyls;
*blocksize = uip->hf.blocksize;
*size = uip->hf.size;
*bootpri = uip->bootpri;
if (filesysdir)
*filesysdir = uip->filesysdir ? my_strdup (uip->filesysdir) : 0;
return 0;
}
static const char *set_filesys_unit_1 (struct uaedev_mount_info *mountinfo, int nr,
char *devname, char *volname, char *rootdir, int readonly,
int secspertrack, int surfaces, int reserved,
int blocksize, int bootpri, char *filesysdir)
{
UnitInfo *ui = mountinfo->ui + nr;
int v;
static char errmsg[1024];
if (nr >= mountinfo->num_units)
return "No slot allocated for this unit";
ui->devname = 0;
ui->volname = 0;
ui->rootdir = 0;
ui->unit_pipe = 0;
ui->back_pipe = 0;
ui->hf.handle = 0;
ui->bootpri = 0;
ui->filesysdir = 0;
if (volname != 0) {
struct stat statbuf;
memset (&statbuf, 0, sizeof (statbuf));
ui->volname = my_strdup (volname);
#ifdef WIN32
v = isspecialdrive (rootdir);
if (v < 0) {
sprintf (errmsg, "invalid drive '%s'", rootdir);
return errmsg;
}
if (v == 0) {
#endif
if (stat (rootdir, &statbuf) < 0) {
sprintf (errmsg, "directory '%s' not found", rootdir);
return errmsg;
}
#ifdef WIN32
if (!(statbuf.st_mode & FILEFLAG_WRITE)) {
write_log ("'%s' set to read-only\n", rootdir);
readonly = 1;
}
#else
/* Check if the filesystem which contains rootdir is read-only */
if (filesys_is_readonly (rootdir) && !readonly) {
write_log ("Mounting '%s' as read-only\n", rootdir);
readonly = 1;
}
#endif
#ifdef WIN32
}
#endif
} else {
ui->hf.secspertrack = secspertrack;
ui->hf.surfaces = surfaces;
ui->hf.reservedblocks = reserved;
ui->hf.blocksize = blocksize;
ui->volname = 0;
ui->hf.readonly = readonly;
if (!hdf_open (&ui->hf, rootdir) && !readonly) {
write_log ("Failed to open hardfile with write permission. Trying as read-only\n");
ui->hf.readonly = readonly = 1;
hdf_open (&ui->hf, rootdir);
}
ui->hf.readonly = readonly;
if (ui->hf.handle == 0)
return "Hardfile not found";
if ((ui->hf.blocksize & (ui->hf.blocksize - 1)) != 0 || ui->hf.blocksize == 0)
return "Bad blocksize";
if ((ui->hf.secspertrack || ui->hf.surfaces || ui->hf.reservedblocks) &&
(ui->hf.secspertrack < 1 || ui->hf.surfaces < 1 || ui->hf.surfaces > 1023 ||
ui->hf.reservedblocks < 0 || ui->hf.reservedblocks > 1023) != 0)
return "Bad hardfile geometry";
if (ui->hf.blocksize > ui->hf.size || ui->hf.size == 0)
return "Hardfile too small";
ui->hf.nrcyls = (int)(ui->hf.secspertrack * ui->hf.surfaces ? (ui->hf.size / ui->hf.blocksize) / (ui->hf.secspertrack * ui->hf.surfaces) : 0);
}
ui->self = 0;
ui->reset_state = FS_STARTUP;
ui->rootdir = my_strdup (rootdir);
if (devname !=0 && strlen (devname) != 0)
ui->devname = my_strdup (devname);
if (filesysdir)
ui->filesysdir = my_strdup (filesysdir);
ui->readonly = readonly;
if (bootpri < -128) bootpri = -128;
if (bootpri > 127) bootpri = 127;
ui->bootpri = bootpri;
return 0;
}
const char *set_filesys_unit (struct uaedev_mount_info *mountinfo, int nr,
char *devname, char *volname, char *rootdir, int readonly,
int secspertrack, int surfaces, int reserved,
int blocksize, int bootpri, char *filesysdir)
{
const char *result;
UnitInfo ui = mountinfo->ui[nr];
hdf_close (&ui.hf);
result = set_filesys_unit_1 (mountinfo, nr, devname, volname, rootdir, readonly,
secspertrack, surfaces, reserved, blocksize, bootpri, filesysdir);
if (result)
mountinfo->ui[nr] = ui;
else
close_filesys_unit (&ui);
return result;
}
const char *add_filesys_unit (struct uaedev_mount_info *mountinfo,
char *devname, char *volname, char *rootdir, int readonly,
int secspertrack, int surfaces, int reserved,
int blocksize, int bootpri, char *filesysdir)
{
const char *retval;
int nr = mountinfo->num_units;
UnitInfo *uip = mountinfo->ui + nr;
if (nr >= MAX_FILESYSTEM_UNITS)
return "Maximum number of file systems mounted";
mountinfo->num_units++;
retval = set_filesys_unit_1 (mountinfo, nr, devname, volname, rootdir, readonly,
secspertrack, surfaces, reserved, blocksize, bootpri, filesysdir);
if (retval)
mountinfo->num_units--;
return retval;
}
int kill_filesys_unit (struct uaedev_mount_info *mountinfo, int nr)
{
UnitInfo *uip = mountinfo->ui;
if (nr >= mountinfo->num_units || nr < 0)
return -1;
close_filesys_unit (mountinfo->ui + nr);
mountinfo->num_units--;
for (; nr < mountinfo->num_units; nr++) {
uip[nr] = uip[nr+1];
}
return 0;
}
int move_filesys_unit (struct uaedev_mount_info *mountinfo, int nr, int to)
{
UnitInfo tmpui;
UnitInfo *uip = mountinfo->ui;
if (nr >= mountinfo->num_units || nr < 0
|| to >= mountinfo->num_units || to < 0
|| to == nr)
return -1;
tmpui = uip[nr];
if (to > nr) {
int i;
for (i = nr; i < to; i++)
uip[i] = uip[i + 1];
} else {
int i;
for (i = nr; i > to; i--)
uip[i] = uip[i - 1];
}
uip[to] = tmpui;
return 0;
}
int sprintf_filesys_unit (struct uaedev_mount_info *mountinfo, char *buffer, int num)
{
UnitInfo *uip = mountinfo->ui;
if (num >= mountinfo->num_units)
return -1;
if (uip[num].volname != 0)
sprintf (buffer, "(DH%d:) Filesystem, %s: %s %s", num, uip[num].volname,
uip[num].rootdir, uip[num].readonly ? "ro" : "");
else
sprintf (buffer, "(DH%d:) Hardfile, \"%s\", size %d Mbytes", num,
uip[num].rootdir, uip[num].hf.size / (1024 * 1024));
return 0;
}
void write_filesys_config (struct uaedev_mount_info *mountinfo,
const char *unexpanded, const char *default_path, FILE *f)
{
UnitInfo *uip = mountinfo->ui;
int i;
for (i = 0; i < mountinfo->num_units; i++) {
char *str;
str = cfgfile_subst_path (default_path, unexpanded, uip[i].rootdir);
if (uip[i].volname != 0) {
fprintf (f, "filesystem2=%s,%s:%s:%s,%d\n", uip[i].readonly ? "ro" : "rw",
uip[i].devname ? uip[i].devname : "", uip[i].volname, str, uip[i].bootpri);
fprintf (f, "filesystem=%s,%s:%s\n", uip[i].readonly ? "ro" : "rw",
uip[i].volname, str);
} else {
fprintf (f, "hardfile2=%s,%s:%s,%d,%d,%d,%d,%d,%s\n",
uip[i].readonly ? "ro" : "rw",
uip[i].devname ? uip[i].devname : "", str,
uip[i].hf.secspertrack, uip[i].hf.surfaces, uip[i].hf.reservedblocks, uip[i].hf.blocksize,
uip[i].bootpri,uip[i].filesysdir ? uip[i].filesysdir : "");
fprintf (f, "hardfile=%s,%d,%d,%d,%d,%s\n",
uip[i].readonly ? "ro" : "rw", uip[i].hf.secspertrack,
uip[i].hf.surfaces, uip[i].hf.reservedblocks, uip[i].hf.blocksize, str);
}
xfree (str);
}
}
struct uaedev_mount_info *alloc_mountinfo (void)
{
struct uaedev_mount_info *info;
info = malloc (sizeof *info);
memset (info, 0, sizeof *info);
info->num_units = 0;
return info;
}
struct uaedev_mount_info *dup_mountinfo (struct uaedev_mount_info *mip)
{
int i;
struct uaedev_mount_info *i2 = alloc_mountinfo ();
memcpy (i2, mip, sizeof *i2);
for (i = 0; i < i2->num_units; i++) {
UnitInfo *uip = i2->ui + i;
if (uip->volname)
uip->volname = my_strdup (uip->volname);
if (uip->devname)
uip->devname = my_strdup (uip->devname);
if (uip->rootdir)
uip->rootdir = my_strdup (uip->rootdir);
if (uip->hf.handle)
hdf_dup (&uip->hf, uip->hf.handle);
}
return i2;
}
void free_mountinfo (struct uaedev_mount_info *mip)
{
int i;
if (!mip)
return;
for (i = 0; i < mip->num_units; i++)
close_filesys_unit (mip->ui + i);
xfree (mip);
}
struct hardfiledata *get_hardfile_data (int nr)
{
UnitInfo *uip = current_mountinfo->ui;
if (nr < 0 || nr >= current_mountinfo->num_units || uip[nr].volname != 0)
return 0;
return &uip[nr].hf;
}
/* 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
/* result codes */
#define DOS_TRUE ((unsigned int)-1L)
#define DOS_FALSE (0L)
/* 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_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_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_FH 1034
#define ACTION_EXAMINE_ALL 1033
#define ACTION_MAKE_LINK 1021
#define ACTION_READ_LINK 1024
#define ACTION_FORMAT 1020
#define ACTION_IS_FILESYSTEM 1027
#define ACTION_ADD_NOTIFY 4097
#define ACTION_REMOVE_NOTIFY 4098
#define DISK_TYPE 0x444f5301 /* DOS\1 */
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;
typedef struct key {
struct key *next;
a_inode *aino;
uae_u32 uniq;
int fd;
off_t file_pos;
int dosmode;
int createmode;
int notifyactive;
} Key;
typedef struct notify {
struct notify *next;
uaecptr notifyrequest;
char *fullname;
char *partname;
} Notify;
/* Since ACTION_EXAMINE_NEXT is so braindamaged, we have to keep
* some of these around
*/
#define EXKEYS 100
#define MAX_AINO_HASH 128
#define NOTIFY_HASH_SIZE 127
/* handler state info */
typedef struct _unit {
struct _unit *next;
/* Amiga stuff */
uaecptr dosbase;
uaecptr volume;
uaecptr port; /* Our port */
uaecptr locklist;
/* Native stuff */
uae_s32 unit; /* unit number */
UnitInfo ui; /* unit startup info */
char 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 long total_locked_ainos;
/* Keys */
struct key *keys;
uae_u32 key_uniq;
uae_u32 a_uniq;
a_inode rootnode;
unsigned long aino_cache_size;
a_inode *aino_hash[MAX_AINO_HASH];
unsigned long nr_cache_hits;
unsigned long nr_cache_lookups;
struct notify *notifyhash[NOTIFY_HASH_SIZE];
} Unit;
typedef uae_u8 *dpacket;
#define PUT_PCK_RES1(p,v) do { do_put_mem_long ((uae_u32 *)((p) + dp_Res1), (v)); } while (0)
#define PUT_PCK_RES2(p,v) do { do_put_mem_long ((uae_u32 *)((p) + dp_Res2), (v)); } while (0)
#define GET_PCK_TYPE(p) ((uae_s32)(do_get_mem_long ((uae_u32 *)((p) + dp_Type))))
#define GET_PCK_RES1(p) ((uae_s32)(do_get_mem_long ((uae_u32 *)((p) + dp_Res1))))
#define GET_PCK_RES2(p) ((uae_s32)(do_get_mem_long ((uae_u32 *)((p) + dp_Res2))))
#define GET_PCK_ARG1(p) ((uae_s32)(do_get_mem_long ((uae_u32 *)((p) + dp_Arg1))))
#define GET_PCK_ARG2(p) ((uae_s32)(do_get_mem_long ((uae_u32 *)((p) + dp_Arg2))))
#define GET_PCK_ARG3(p) ((uae_s32)(do_get_mem_long ((uae_u32 *)((p) + dp_Arg3))))
#define GET_PCK_ARG4(p) ((uae_s32)(do_get_mem_long ((uae_u32 *)((p) + dp_Arg4))))
static char *char1 (uaecptr addr)
{
static char buf[1024];
unsigned int i = 0;
do {
buf[i] = get_byte(addr);
addr++;
} while (buf[i++] && i < sizeof(buf));
return buf;
}
static char *bstr1 (uaecptr addr)
{
static char buf[256];
int i;
int n = get_byte(addr);
addr++;
for (i = 0; i < n; i++, addr++)
buf[i] = get_byte(addr);
buf[i] = 0;
return buf;
}
static char *bstr (Unit *unit, uaecptr addr)
{
int i;
int n = get_byte(addr);
addr++;
for (i = 0; i < n; i++, addr++)
unit->tmpbuf3[i] = get_byte(addr);
unit->tmpbuf3[i] = 0;
return unit->tmpbuf3;
}
static char *bstr_cut (Unit *unit, uaecptr addr)
{
char *p = unit->tmpbuf3;
int i, colon_seen = 0;
int n = get_byte (addr);
addr++;
for (i = 0; i < n; i++, addr++) {
uae_u8 c = get_byte(addr);
unit->tmpbuf3[i] = c;
if (c == '/' || (c == ':' && colon_seen++ == 0))
p = unit->tmpbuf3 + i + 1;
}
unit->tmpbuf3[i] = 0;
return p;
}
static Unit *units = 0;
static int unit_num = 0;
static Unit*
find_unit (uaecptr port)
{
Unit* u;
for (u = units; u; u = u->next)
if (u->port == port)
break;
return u;
}
static void prepare_for_open (char *name)
{
#if 0
struct stat statbuf;
int mode;
if (-1 == stat (name, &statbuf))
return;
mode = statbuf.st_mode;
mode |= S_IRUSR;
mode |= S_IWUSR;
mode |= S_IXUSR;
chmod (name, mode);
#endif
}
static void de_recycle_aino (Unit *unit, a_inode *aino)
{
aino_test (aino);
if (aino->next == 0 || aino == &unit->rootnode)
return;
aino->next->prev = aino->prev;
aino->prev->next = aino->next;
aino->next = aino->prev = 0;
unit->aino_cache_size--;
}
static void dispose_aino (Unit *unit, a_inode **aip, a_inode *aino)
{
int hash = aino->uniq % MAX_AINO_HASH;
if (unit->aino_hash[hash] == aino)
unit->aino_hash[hash] = 0;
if (aino->dirty && aino->parent)
fsdb_dir_writeback (aino->parent);
*aip = aino->sibling;
xfree (aino->aname);
if (aino->comment)
xfree (aino->comment);
xfree (aino->nname);
xfree (aino);
}
static void recycle_aino (Unit *unit, a_inode *new_aino)
{
aino_test (new_aino);
if (new_aino->dir || new_aino->shlock > 0
|| new_aino->elock || new_aino == &unit->rootnode)
/* Still in use */
return;
TRACE (("Recycling; cache size %d, total_locked %d\n",
unit->aino_cache_size, unit->total_locked_ainos));
if (unit->aino_cache_size > 5000 + unit->total_locked_ainos) {
/* Reap a few. */
int i = 0;
while (i < 50) {
a_inode *parent = unit->rootnode.prev->parent;
a_inode **aip;
aip = &parent->child;
aino_test (parent);
if (! parent->locked_children) {
for (;;) {
a_inode *aino = *aip;
aino_test (aino);
if (aino == 0)
break;
/* Not recyclable if next == 0 (i.e., not chained into
recyclable list), or if parent directory is being
ExNext()ed. */
if (aino->next == 0) {
aip = &aino->sibling;
} else {
if (aino->shlock > 0 || aino->elock)
write_log ("panic: freeing locked a_inode!\n");
de_recycle_aino (unit, aino);
dispose_aino (unit, aip, aino);
i++;
}
}
}
/* In the previous loop, we went through all children of one
parent. Re-arrange the recycled list so that we'll find a
different parent the next time around. */
do {
unit->rootnode.next->prev = unit->rootnode.prev;
unit->rootnode.prev->next = unit->rootnode.next;
unit->rootnode.next = unit->rootnode.prev;
unit->rootnode.prev = unit->rootnode.prev->prev;
unit->rootnode.prev->next = unit->rootnode.next->prev = &unit->rootnode;
} while (unit->rootnode.prev->parent == parent);
}
#if 0
{
char buffer[40];
sprintf (buffer, "%d ainos reaped.\n", i);
TRACE ((buffer));
}
#endif
}
aino_test (new_aino);
/* Chain it into circular list. */
new_aino->next = unit->rootnode.next;
new_aino->prev = &unit->rootnode;
new_aino->prev->next = new_aino;
new_aino->next->prev = new_aino;
aino_test (new_aino->next);
aino_test (new_aino->prev);
unit->aino_cache_size++;
}
static void update_child_names (Unit *unit, a_inode *a, a_inode *parent)
{
int l0 = strlen (parent->nname) + 2;
while (a != 0) {
char *name_start;
char *new_name;
char dirsep[2] = { FSDB_DIR_SEPARATOR, '\0' };
a->parent = parent;
name_start = strrchr (a->nname, FSDB_DIR_SEPARATOR);
if (name_start == 0) {
write_log ("malformed file name");
}
name_start++;
new_name = (char *)xmalloc (strlen (name_start) + l0);
strcpy (new_name, parent->nname);
strcat (new_name, dirsep);
strcat (new_name, name_start);
xfree (a->nname);
a->nname = new_name;
if (a->child)
update_child_names (unit, a->child, a);
a = a->sibling;
}
}
static void move_aino_children (Unit *unit, a_inode *from, a_inode *to)
{
aino_test (from);
aino_test (to);
to->child = from->child;
from->child = 0;
update_child_names (unit, to->child, to);
}
static void delete_aino (Unit *unit, a_inode *aino)
{
a_inode **aip;
TRACE(("deleting aino %x\n", aino->uniq));
aino_test (aino);
aino->dirty = 1;
aino->deleted = 1;
de_recycle_aino (unit, aino);
/* If any ExKeys are currently pointing at us, advance them. */
if (aino->parent->exnext_count > 0) {
int i;
TRACE(("entering exkey validation\n"));
for (i = 0; i < EXKEYS; i++) {
ExamineKey *k = unit->examine_keys + i;
if (k->uniq == 0)
continue;
if (k->aino == aino->parent) {
TRACE(("Same parent found for %d\n", i));
if (k->curr_file == aino) {
k->curr_file = aino->sibling;
TRACE(("Advancing curr_file\n"));
}
}
}
}
aip = &aino->parent->child;
while (*aip != aino && *aip != 0)
aip = &(*aip)->sibling;
if (*aip != aino) {
write_log ("Couldn't delete aino.\n");
return;
}
dispose_aino (unit, aip, aino);
}
static a_inode *lookup_sub (a_inode *dir, uae_u32 uniq)
{
a_inode **cp = &dir->child;
a_inode *c, *retval;
for (;;) {
c = *cp;
if (c == 0)
return 0;
if (c->uniq == uniq) {
retval = c;
break;
}
if (c->dir) {
a_inode *a = lookup_sub (c, uniq);
if (a != 0) {
retval = a;
break;
}
}
cp = &c->sibling;
}
if (! dir->locked_children) {
/* Move to the front to speed up repeated lookups. Don't do this if
an ExNext is going on in this directory, or we'll terminally
confuse it. */
*cp = c->sibling;
c->sibling = dir->child;
dir->child = c;
}
return retval;
}
static a_inode *lookup_aino (Unit *unit, uae_u32 uniq)
{
a_inode *a;
int hash = uniq % MAX_AINO_HASH;
if (uniq == 0)
return &unit->rootnode;
a = unit->aino_hash[hash];
if (a == 0 || a->uniq != uniq)
a = lookup_sub (&unit->rootnode, uniq);
else
unit->nr_cache_hits++;
unit->nr_cache_lookups++;
unit->aino_hash[hash] = a;
aino_test (a);
return a;
}
char *build_nname (const char *d, const char *n)
{
unsigned int d_len = strlen (d);
char *p = xmalloc (d_len + strlen (n) + 2);
strcpy (p, d);
#ifdef TARGET_AMIGAOS
if (d_len && p[d_len-1] != ':' && p[d_len-1] != FSDB_DIR_SEPARATOR)
#endif
p[d_len++] = FSDB_DIR_SEPARATOR;
strcpy (&p[d_len], n);
return p;
}
#if 0
char *build_aname (const char *d, const char *n)
{
char *p = (char *) xmalloc (strlen (d) + strlen (n) + 2);
strcpy (p, d);
strcat (p, "/");
strcat (p, n);
return p;
}
#endif
/* This gets called to translate an Amiga name that some program used to
* a name that we can use on the native filesystem. */
static char *get_nname (Unit *unit, a_inode *base, char *rel,
char **modified_rel)
{
char *found;
aino_test (base);
*modified_rel = 0;
/* If we have a mapping of some other aname to "rel", we must pretend