forked from Cpasjuste/uae4all2
-
Notifications
You must be signed in to change notification settings - Fork 10
/
filesys.cpp
executable file
·3465 lines (3029 loc) · 92 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 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 "config.h"
#include "thread.h"
#include "options.h"
#include "uae.h"
#include "memory-uae.h"
#include "custom.h"
#include "events.h"
//#include "newcpu.h"
#include "m68k/m68k_intrf.h"
#include "filesys.h"
#include "autoconf.h"
#include "fsusage.h"
#include "native2amiga.h"
#include "scsidev.h"
#include "fsdb.h"
#include "gui.h"
#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 long dos_errno(void)
{
int e = errno;
switch (e) {
case ENOMEM: return ERROR_NO_FREE_STORE;
case EEXIST: return ERROR_OBJECT_EXISTS;
case EACCES: return ERROR_WRITE_PROTECTED;
case ENOENT: return ERROR_OBJECT_NOT_AROUND;
case ENOTDIR: return ERROR_OBJECT_WRONG_TYPE;
case ENOSPC: return ERROR_DISK_IS_FULL;
case EBUSY: return ERROR_OBJECT_IN_USE;
case EISDIR: return ERROR_OBJECT_WRONG_TYPE;
#if defined(ETXTBSY)
case ETXTBSY: return ERROR_OBJECT_IN_USE;
#endif
#if defined(EROFS)
case EROFS: return ERROR_DISK_WRITE_PROTECTED;
#endif
#if defined(ENOTEMPTY)
#if ENOTEMPTY != EEXIST
case ENOTEMPTY: return ERROR_DIRECTORY_NOT_EMPTY;
#endif
#endif
default:
TRACE(("Unimplemented error %s\n", strerror(e)));
return ERROR_NOT_IMPLEMENTED;
}
}
/*
* 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
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 devno;
struct hardfiledata hf;
/* Threading stuff */
smp_comm_pipe *unit_pipe, *back_pipe;
uae_thread_id tid;
struct _unit *volatile self;
/* Reset handling */
uae_sem_t reset_sync_sem;
int reset_state;
} UnitInfo;
#define MAX_UNITS 20
struct uaedev_mount_info {
int num_units;
UnitInfo ui[MAX_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)
{
return mountinfo->ui[unit_no].volname == 0;
}
static void close_filesys_unit (UnitInfo *uip)
{
if (uip->hf.fd != 0)
fclose (uip->hf.fd);
if (uip->volname != 0)
free (uip->volname);
if (uip->devname != 0)
free (uip->devname);
if (uip->rootdir != 0)
free (uip->rootdir);
if (uip->unit_pipe)
free (uip->unit_pipe);
if (uip->back_pipe)
free (uip->back_pipe);
uip->unit_pipe = 0;
uip->back_pipe = 0;
uip->hf.fd = 0;
uip->volname = 0;
uip->devname = 0;
uip->rootdir = 0;
}
char *get_filesys_unit (struct uaedev_mount_info *mountinfo, int nr,
char **volname, char **rootdir, int *readonly,
int *secspertrack, int *surfaces, int *reserved,
int *cylinders, int *size, int *blocksize)
{
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;
*rootdir = uip->rootdir ? my_strdup (uip->rootdir) : 0;
*readonly = uip->readonly;
*secspertrack = uip->hf.secspertrack;
*surfaces = uip->hf.surfaces;
*reserved = uip->hf.reservedblocks;
*size = uip->hf.size;
*cylinders = uip->hf.nrcyls;
*blocksize = uip->hf.blocksize;
return 0;
}
static char *set_filesys_unit_1 (struct uaedev_mount_info *mountinfo, int nr,
char *volname, char *rootdir, int readonly,
int secspertrack, int surfaces, int reserved,
int blocksize)
{
UnitInfo *ui = mountinfo->ui + nr;
if (nr >= mountinfo->num_units)
return "No slot allocated for this unit";
ui->hf.fd = 0;
ui->hf.size = 0;
ui->hf.nrcyls = 0;
ui->hf.secspertrack = 0;
ui->hf.surfaces = 0;
ui->hf.reservedblocks = 0;
ui->hf.blocksize = 0;
ui->devname = 0;
ui->volname = 0;
ui->rootdir = 0;
ui->unit_pipe = 0;
ui->back_pipe = 0;
if (volname != 0) {
ui->volname = my_strdup (volname);
ui->hf.fd = 0;
} else {
ui->volname = 0;
ui->hf.fd = fopen (rootdir, "r+b");
if (ui->hf.fd == 0) {
readonly = 1;
ui->hf.fd = fopen (rootdir, "rb");
}
if (ui->hf.fd == 0)
return "Hardfile not found";
if (secspertrack < 1 || secspertrack > 32767
|| surfaces < 1 || surfaces > 1023
|| reserved < 0 || reserved > 1023
|| (blocksize & (blocksize - 1)) != 0)
{
return "Bad hardfile geometry";
}
fseek (ui->hf.fd, 0, SEEK_END);
ui->hf.size = ftell (ui->hf.fd);
ui->hf.secspertrack = secspertrack;
ui->hf.surfaces = surfaces;
ui->hf.reservedblocks = reserved;
ui->hf.nrcyls = (secspertrack * surfaces
? (ui->hf.size / blocksize) / (secspertrack * surfaces)
: 0);
ui->hf.blocksize = blocksize;
}
ui->self = 0;
ui->reset_state = FS_STARTUP;
ui->rootdir = my_strdup (rootdir);
ui->readonly = readonly;
ui->devno = 0;
ui->tid = NULL;
ui->reset_sync_sem = NULL;
return 0;
}
char *set_filesys_unit (struct uaedev_mount_info *mountinfo, int nr,
char *volname, char *rootdir, int readonly,
int secspertrack, int surfaces, int reserved,
int blocksize)
{
UnitInfo ui = mountinfo->ui[nr];
char *result = set_filesys_unit_1 (mountinfo, nr, volname, rootdir, readonly,
secspertrack, surfaces, reserved, blocksize);
if (result)
mountinfo->ui[nr] = ui;
else
close_filesys_unit (&ui);
return result;
}
char *add_filesys_unit (struct uaedev_mount_info *mountinfo,
char *volname, char *rootdir, int readonly,
int secspertrack, int surfaces, int reserved,
int blocksize)
{
char *retval;
int nr = mountinfo->num_units;
UnitInfo *uip = mountinfo->ui + nr;
if (nr >= MAX_UNITS)
return "Maximum number of file systems mounted";
mountinfo->num_units++;
retval = set_filesys_unit_1 (mountinfo, nr, volname, rootdir, readonly,
secspertrack, surfaces, reserved, blocksize);
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 bytes", num,
uip[num].rootdir, uip[num].hf.size);
return 0;
}
struct uaedev_mount_info *alloc_mountinfo (void)
{
UnitInfo *ui;
struct uaedev_mount_info *info;
info = (struct uaedev_mount_info *)xmalloc (sizeof *info);
/* memset (info, 0xaa, sizeof *info);*/
info->num_units = 0;
ui = info->ui;
ui->hf.fd = 0;
ui->hf.size = 0;
ui->hf.nrcyls = 0;
ui->hf.secspertrack = 0;
ui->hf.surfaces = 0;
ui->hf.reservedblocks = 0;
ui->hf.blocksize = 0;
ui->devname = 0;
ui->volname = 0;
ui->rootdir = 0;
ui->unit_pipe = 0;
ui->back_pipe = 0;
ui->volname = 0;
ui->hf.size = 0;
ui->hf.secspertrack = 0;
ui->hf.surfaces = 0;
ui->hf.reservedblocks = 0;
ui->hf.nrcyls = 0;
ui->hf.blocksize = 0;
ui->self = 0;
ui->reset_state = 0;
ui->rootdir = 0;
ui->readonly = 0;
ui->devno = 0;
ui->tid = NULL;
ui->reset_sync_sem = NULL;
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->rootdir)
uip->rootdir = my_strdup (uip->rootdir);
#if !(defined __PSP2__)
// Broken in latest VitaSDK 20240214 (fdopen returns 0), causing crashes with hdf
if (uip->hf.fd)
uip->hf.fd = fdopen ( dup (fileno (uip->hf.fd)), uip->readonly ? "rb" : "r+b");
#endif
}
return i2;
}
void free_mountinfo (struct uaedev_mount_info *mip)
{
int i;
for (i = 0; i < mip->num_units; i++)
close_filesys_unit (mip->ui + i);
free (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 */
#ifdef __64BIT__
#define DOS_TRUE ((uint32_t)-1L)
#else
#define DOS_TRUE ((unsigned long)-1L)
#endif
#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_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_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;
} Key;
/* Since ACTION_EXAMINE_NEXT is so braindamaged, we have to keep
* some of these around
*/
#define EXKEYS 100
#define MAX_AINO_HASH 128
/* 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;
} 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 *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)
{
}
static void de_recycle_aino (Unit *unit, a_inode *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;
if (aino->comment)
free (aino->comment);
free (aino->nname);
free (aino->aname);
free (aino);
}
static void recycle_aino (Unit *unit, a_inode *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 > 500 + unit->total_locked_ainos) {
/* Reap a few. */
int i = 0;
while (i < 50) {
a_inode *parent = unit->rootnode.prev->parent;
a_inode **aip, *old_prev;
aip = &parent->child;
if (! parent->locked_children)
for (;;) {
a_inode *aino = *aip;
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. */
old_prev = unit->rootnode.prev;
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 != old_prev
&& unit->rootnode.prev->parent == parent);
}
}
/* 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;
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);
free (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)
{
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->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;
return a;
}
char *build_nname (const char *d, const char *n)
{
char dsep[2] = { FSDB_DIR_SEPARATOR, '\0' };
char *p = (char *) xmalloc (strlen (d) + strlen (n) + 2);
strcpy (p, d);
strcat (p, dsep);
strcat (p, n);
return p;
}
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;
}
/* 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;
char *p = 0;
*modified_rel = 0;
/* If we have a mapping of some other aname to "rel", we must pretend
* it does not exist.
* This can happen for example if an Amiga program creates a
* file called ".". We can't represent this in our filesystem,
* so we create a special file "uae_xxx" and record the mapping
* aname "." -> nname "uae_xxx" in the database. Then, the Amiga
* program looks up "uae_xxx" (yes, it's contrived). The filesystem
* should not make the uae_xxx file visible to the Amiga side. */
if (fsdb_used_as_nname (base, rel))
return 0;
/* A file called "." (or whatever else is invalid on this filesystem)
* does not exist, as far as the Amiga side is concerned. */
if (fsdb_name_invalid (rel))
return 0;
/* See if we have a file that has the same name as the aname we are
* looking for. */
found = fsdb_search_dir (base->nname, rel);
if (found == 0)
return found;
if (found == rel)
return build_nname (base->nname, rel);
*modified_rel = found;
return build_nname (base->nname, found);
}
static char *create_nname (Unit *unit, a_inode *base, char *rel)
{
char *p;
/* We are trying to create a file called REL. */
/* If the name is used otherwise in the directory (or globally), we
* need a new unique nname. */
if (fsdb_name_invalid (rel) || fsdb_used_as_nname (base, rel)) {
oh_dear:
p = fsdb_create_unique_nname (base, rel);
return p;
}
p = build_nname (base->nname, rel);
/* Delete this code once we know everything works. */
if (access (p, R_OK) >= 0 || errno != ENOENT) {
write_log ("Filesystem in trouble... please report.\n");
free (p);
goto oh_dear;
}
return p;
}
/*
* This gets called if an ACTION_EXAMINE_NEXT happens and we hit an object
* for which we know the name on the native filesystem, but no corresponding
* Amiga filesystem name.
* @@@ For DOS filesystems, it might make sense to declare the new name
* "weak", so that it can get overriden by a subsequent call to get_nname().
* That way, if someone does "dir :" and there is a file "foobar.inf", and
* someone else tries to open "foobar.info", get_nname() could maybe made to
* figure out that this is supposed to be the file "foobar.inf".
* DOS sucks...
*/
static char *get_aname (Unit *unit, a_inode *base, char *rel)
{
return my_strdup (rel);
}
static void init_child_aino (Unit *unit, a_inode *base, a_inode *aino)
{
aino->uniq = ++unit->a_uniq;
if (unit->a_uniq == 0xFFFFFFFF) {
write_log ("Running out of a_inodes (prepare for big trouble)!\n");
}
aino->shlock = 0;
aino->elock = 0;
aino->dirty = 0;
aino->deleted = 0;
/* For directories - this one isn't being ExNext()ed yet. */
aino->locked_children = 0;
aino->exnext_count = 0;
/* But the parent might be. */
if (base->exnext_count) {
unit->total_locked_ainos++;
base->locked_children++;
}
/* Update tree structure */
aino->parent = base;
aino->child = 0;
aino->sibling = base->child;
base->child = aino;
aino->next = aino->prev = 0;
}
static a_inode *new_child_aino (Unit *unit, a_inode *base, char *rel)
{
char *modified_rel;
char *nn;
a_inode *aino;
TRACE(("new_child_aino %s, %s\n", base->aname, rel));
aino = fsdb_lookup_aino_aname (base, rel);
if (aino == 0) {
nn = get_nname (unit, base, rel, &modified_rel);
if (nn == 0)
return 0;
aino = (a_inode *) xcalloc (sizeof (a_inode), 1);
if (aino == 0)
return 0;
aino->aname = modified_rel ? modified_rel : my_strdup (rel);
aino->nname = nn;