|
|
1.1 root 1: /*
2:
1.1.1.3 root 3: Copyright 1991,1992 Eric R. Smith.
4:
1.1.1.5 ! root 5: Copyright 1992,1993,1994 Atari Corporation.
1.1.1.3 root 6:
7: All rights reserved.
1.1 root 8:
9: */
10:
11:
12:
13: /* a VERY simple tosfs.c
14:
15: * this one is extremely brain-damaged, but will serve OK for a
16:
17: * skeleton in which to put a "real" tosfs.c
18:
19: */
20:
21:
22:
23: #include "mint.h"
24:
25:
26:
1.1.1.3 root 27: /* if NEWWAY is defined, tosfs uses the new dup_cookie/release_cookie
28:
29: * protocol to keep track of file cookies, instead of the old
30:
31: * method of "timing"
32:
33: */
34:
35: /* #define NEWWAY */
36:
37: #if 0
38:
39: #define COOKIE_DB(x) DEBUG(x)
40:
41: #else
42:
43: #define COOKIE_DB(x)
44:
45: #endif
46:
47:
48:
49: /* if RO_FASCISM is defined, the read/write modes are enforced. This is
50:
51: * a Good Thing, not fascist at all. Ask Allan Pratt why he chose
52:
53: * that name sometime.
54:
55: */
56:
57: #define RO_FASCISM
58:
59:
60:
61: /* temporary code for debugging Falcon media change bug */
62:
63: #if 0
64:
65: #define MEDIA_DB(x) DEBUG(x)
66:
67: #else
68:
69: #define MEDIA_DB(x)
70:
71: #endif
72:
73:
74:
1.1 root 75: /* search mask for anything OTHER THAN a volume label */
76:
77: #define FILEORDIR 0x37
78:
79:
80:
81: char tmpbuf[PATH_MAX+1];
82:
83:
84:
1.1.1.2 root 85: static long ARGS_ON_STACK tos_root P_((int drv, fcookie *fc));
1.1 root 86:
1.1.1.2 root 87: static long ARGS_ON_STACK tos_lookup P_((fcookie *dir, const char *name, fcookie *fc));
1.1 root 88:
1.1.1.2 root 89: static long ARGS_ON_STACK tos_getxattr P_((fcookie *fc, XATTR *xattr));
1.1 root 90:
1.1.1.2 root 91: static long ARGS_ON_STACK tos_chattr P_((fcookie *fc, int attrib));
1.1 root 92:
1.1.1.2 root 93: static long ARGS_ON_STACK tos_chown P_((fcookie *fc, int uid, int gid));
1.1 root 94:
1.1.1.2 root 95: static long ARGS_ON_STACK tos_chmode P_((fcookie *fc, unsigned mode));
1.1 root 96:
1.1.1.2 root 97: static long ARGS_ON_STACK tos_mkdir P_((fcookie *dir, const char *name, unsigned mode));
1.1 root 98:
1.1.1.2 root 99: static long ARGS_ON_STACK tos_rmdir P_((fcookie *dir, const char *name));
1.1 root 100:
1.1.1.2 root 101: static long ARGS_ON_STACK tos_remove P_((fcookie *dir, const char *name));
1.1 root 102:
1.1.1.3 root 103: static long ARGS_ON_STACK tos_getname P_((fcookie *root, fcookie *dir,
104:
105: char *pathname, int size));
1.1 root 106:
1.1.1.2 root 107: static long ARGS_ON_STACK tos_rename P_((fcookie *olddir, char *oldname,
1.1 root 108:
109: fcookie *newdir, const char *newname));
110:
1.1.1.2 root 111: static long ARGS_ON_STACK tos_opendir P_((DIR *dirh, int flags));
1.1 root 112:
1.1.1.2 root 113: static long ARGS_ON_STACK tos_readdir P_((DIR *dirh, char *nm, int nmlen, fcookie *));
1.1 root 114:
1.1.1.2 root 115: static long ARGS_ON_STACK tos_rewinddir P_((DIR *dirh));
1.1 root 116:
1.1.1.2 root 117: static long ARGS_ON_STACK tos_closedir P_((DIR *dirh));
1.1 root 118:
1.1.1.2 root 119: static long ARGS_ON_STACK tos_pathconf P_((fcookie *dir, int which));
1.1 root 120:
1.1.1.2 root 121: static long ARGS_ON_STACK tos_dfree P_((fcookie *dir, long *buf));
1.1 root 122:
1.1.1.2 root 123: static long ARGS_ON_STACK tos_writelabel P_((fcookie *dir, const char *name));
1.1 root 124:
1.1.1.2 root 125: static long ARGS_ON_STACK tos_readlabel P_((fcookie *dir, char *name, int namelen));
1.1 root 126:
127:
128:
1.1.1.2 root 129: static long ARGS_ON_STACK tos_creat P_((fcookie *dir, const char *name, unsigned mode,
1.1 root 130:
131: int attrib, fcookie *fc));
132:
1.1.1.2 root 133: static DEVDRV * ARGS_ON_STACK tos_getdev P_((fcookie *fc, long *devsp));
1.1 root 134:
1.1.1.2 root 135: static long ARGS_ON_STACK tos_open P_((FILEPTR *f));
1.1 root 136:
1.1.1.2 root 137: static long ARGS_ON_STACK tos_write P_((FILEPTR *f, const char *buf, long bytes));
1.1 root 138:
1.1.1.2 root 139: static long ARGS_ON_STACK tos_read P_((FILEPTR *f, char *buf, long bytes));
1.1 root 140:
1.1.1.2 root 141: static long ARGS_ON_STACK tos_lseek P_((FILEPTR *f, long where, int whence));
1.1 root 142:
1.1.1.2 root 143: static long ARGS_ON_STACK tos_ioctl P_((FILEPTR *f, int mode, void *buf));
1.1 root 144:
1.1.1.2 root 145: static long ARGS_ON_STACK tos_datime P_((FILEPTR *f, short *time, int rwflag));
1.1 root 146:
1.1.1.2 root 147: static long ARGS_ON_STACK tos_close P_((FILEPTR *f, int pid));
1.1 root 148:
1.1.1.2 root 149: static long ARGS_ON_STACK tos_dskchng P_((int drv));
1.1 root 150:
151:
152:
1.1.1.3 root 153: #ifdef NEWWAY
154:
155: static long ARGS_ON_STACK tos_release P_((fcookie *fc));
156:
157: static long ARGS_ON_STACK tos_dupcookie P_((fcookie *dst, fcookie *src));
158:
159: #endif
160:
161:
162:
1.1 root 163: /* some routines from biosfs.c */
164:
1.1.1.2 root 165: extern long ARGS_ON_STACK null_select P_((FILEPTR *f, long p, int mode));
1.1 root 166:
1.1.1.2 root 167: extern void ARGS_ON_STACK null_unselect P_((FILEPTR *f, long p, int mode));
1.1 root 168:
169:
170:
171: DEVDRV tos_device = {
172:
173: tos_open, tos_write, tos_read, tos_lseek, tos_ioctl, tos_datime,
174:
175: tos_close, null_select, null_unselect
176:
177: };
178:
179:
180:
181: FILESYS tos_filesys = {
182:
183: (FILESYS *)0,
184:
1.1.1.3 root 185: FS_KNOPARSE | FS_NOXBIT | FS_LONGPATH,
1.1 root 186:
187: tos_root,
188:
189: tos_lookup, tos_creat, tos_getdev, tos_getxattr,
190:
191: tos_chattr, tos_chown, tos_chmode,
192:
193: tos_mkdir, tos_rmdir, tos_remove, tos_getname, tos_rename,
194:
195: tos_opendir, tos_readdir, tos_rewinddir, tos_closedir,
196:
197: tos_pathconf, tos_dfree, tos_writelabel, tos_readlabel,
198:
1.1.1.3 root 199: nosymlink, noreadlink, nohardlink, nofscntl, tos_dskchng,
200:
201: #ifdef NEWWAY
202:
203: tos_release, tos_dupcookie
204:
205: #else
206:
207: 0, 0
208:
209: #endif
1.1 root 210:
211: };
212:
213:
214:
215: /* some utility functions and variables: see end of file */
216:
217: static DTABUF *lastdta; /* last DTA buffer we asked TOS about */
218:
219: static DTABUF foo;
220:
221: static void do_setdta P_((DTABUF *dta));
222:
223: static int executable_extension P_((char *));
224:
225:
226:
227: /* this array keeps track of which drives have been changed */
228:
229: /* a nonzero entry means that the corresponding drive has been changed,
230:
231: * but GEMDOS doesn't know it yet
232:
233: */
234:
235: static char drvchanged[NUM_DRIVES];
236:
237:
238:
239: /* force TOS to see a media change */
240:
241: static void force_mediach P_((int drv));
242:
1.1.1.2 root 243: static long ARGS_ON_STACK Newgetbpb P_((int));
1.1 root 244:
1.1.1.2 root 245: static long ARGS_ON_STACK Newmediach P_((int));
1.1 root 246:
1.1.1.2 root 247: static long ARGS_ON_STACK Newrwabs P_((int, void *, int, int, int, long));
1.1 root 248:
249:
250:
1.1.1.3 root 251: #ifdef NEWWAY
252:
253: #define NUM_INDICES 64
254:
255: #else
256:
1.1 root 257: #define NUM_INDICES 128
258:
259: #define MIN_AGE 8
260:
1.1.1.3 root 261: #endif
262:
1.1 root 263:
264:
265: struct tindex {
266:
267: char *name; /* full path name */
268:
269: FILEPTR *open; /* fileptrs for this file; OR
270:
271: * count of number of open directories
272:
273: */
274:
275: LOCK *locks; /* locks on this file */
276:
277: /* file status */
278:
279: long size;
280:
281: short time;
282:
283: short date;
284:
285: short attr;
286:
287: short valid; /* 1 if the above status is still valid */
288:
1.1.1.3 root 289: #ifdef NEWWAY
290:
291: short links; /* how many times index is in use */
292:
293: #else
294:
1.1 root 295: short stamp; /* age of this index, for garbage collection */
296:
1.1.1.3 root 297: #endif
298:
1.1 root 299: } gl_ti[NUM_INDICES];
300:
301:
302:
303: /* temporary index for files found by readdir */
304:
305: static struct tindex tmpindex;
306:
307: static char tmpiname[PATH_MAX];
308:
309:
310:
311: static struct tindex *tstrindex P_((char *s));
312:
313: static int tfullpath P_((char *result, struct tindex *base, const char *name));
314:
315: static struct tindex *garbage_collect P_((void));
316:
317:
318:
1.1.1.3 root 319: #ifndef NEWWAY
320:
1.1 root 321: static short tclock; /* #calls to tfullpath since last garbage
322:
323: collection */
324:
1.1.1.3 root 325: #endif
326:
1.1 root 327:
328:
329: /* some extra flags for the attr field */
330:
331:
332:
333: /*
334:
335: * is a string the name of a file with executable extension?
336:
337: */
338:
339: #define FA_EXEC 0x4000
340:
341: /*
342:
343: * should the file be deleted when it is closed?
344:
345: */
346:
347: #define FA_DELETE 0x2000
348:
349:
350:
351: /*
352:
353: * NOTE: call executable_extension only on a DTA name returned from
354:
1.1.1.3 root 355: * Fsfirst(), not on an arbitrary path, for two reasons: (1) it
356:
357: * expects only upper case, and (2) it looks only for the 1st extension,
358:
359: * so a folder with a '.' in its name would confuse it.
1.1 root 360:
361: */
362:
363:
364:
365: static int
366:
367: executable_extension(s)
368:
369: char *s;
370:
371: {
372:
373: while (*s && *s != '.') s++;
374:
375: if (!*s) return 0;
376:
377: s++;
378:
379: if (s[0] == 'T') {
380:
381: return (s[1] == 'T' && s[2] == 'P') ||
382:
383: (s[1] == 'O' && s[2] == 'S');
384:
385: }
386:
387: if (s[0] == 'P')
388:
389: return s[1] == 'R' && s[2] == 'G';
390:
391: if (s[0] == 'A')
392:
393: return s[1] == 'P' && s[2] == 'P';
394:
395: if (s[0] == 'G')
396:
397: return s[1] == 'T' && s[2] == 'P';
398:
399: return 0;
400:
401: }
402:
403:
404:
405: /*
406:
407: * Look in the table of tos indices to see if an index corresponding
408:
409: * to this file name already exists. If so, mark it as being used
410:
411: * and return it. If not, find an empty slot and make an index for
412:
413: * this string. If no empty slots exist, garbage collect and
414:
415: * try again.
416:
417: *
418:
419: * This routine is pretty dumb; we really should use a hash table
420:
421: * of some sort
422:
423: */
424:
425:
426:
427: static struct tindex *tstrindex(s)
428:
429: char *s;
430:
431: {
432:
433: int i;
434:
435: char *r;
436:
437: struct tindex *t, *free = 0;
438:
439:
440:
441: assert(s != 0);
442:
443: t = gl_ti;
444:
445: for (i = 0; i < NUM_INDICES; i++, t++) {
446:
447: if (t->name && !stricmp(t->name, s)) {
448:
1.1.1.3 root 449: #ifndef NEWWAY
450:
1.1 root 451: t->stamp = tclock; /* update use time */
452:
1.1.1.3 root 453: #endif
454:
1.1 root 455: return t;
456:
457: }
458:
459: else if (!t->name && !free)
460:
461: free = t;
462:
463: }
464:
465: if (!free) {
466:
467: free = garbage_collect();
468:
469: }
470:
1.1.1.3 root 471: #ifdef NEWWAY
472:
473: if (!free) {
474:
475: FORCE("tosfs: all slots in use!!");
476:
477: FORCE("Links\tName");
478:
479: t = gl_ti;
480:
481: for (i = 0; i < NUM_INDICES; i++,t++) {
482:
483: FORCE("%d\t%s", t->links, t->name);
484:
485: }
486:
487: FATAL("tosfs: unable to get a file name index");
488:
489: }
490:
491: #else
492:
1.1 root 493: if (!free) {
494:
495: FATAL("tosfs: unable to get a file name index");
496:
497: }
498:
1.1.1.3 root 499: #endif
500:
1.1 root 501: r = kmalloc((long)strlen(s)+1);
502:
503: if (!r) {
504:
505: FATAL("tosfs: unable to allocate space for a file name");
506:
507: }
508:
509: strcpy(r, s);
510:
511: free->name = r;
512:
1.1.1.3 root 513: #ifdef NEWWAY
514:
515: free->links = 0;
516:
517: #else
518:
1.1 root 519: free->stamp = tclock;
520:
1.1.1.3 root 521: #endif
522:
1.1 root 523: free->open = 0;
524:
525: free->locks = 0;
526:
527:
528:
529: /* check to see if this file was recently returned by opendir() */
530:
1.1.1.3 root 531: #ifndef NEWWAY
532:
1.1 root 533: if (tmpindex.valid && tclock - tmpindex.stamp < MIN_AGE &&
534:
535: !stricmp(free->name, tmpindex.name)) {
536:
537: free->size = tmpindex.size;
538:
539: free->time = tmpindex.time;
540:
541: free->date = tmpindex.date;
542:
543: free->attr = tmpindex.attr;
544:
545: free->valid = 1;
546:
547: tmpindex.valid = 0;
548:
549: } else
550:
1.1.1.3 root 551: #endif
552:
1.1 root 553: free->valid = 0;
554:
555: return free;
556:
557: }
558:
559:
560:
561: /*
562:
563: * garbage collection routine: for any TOS index older than MIN_AGE,
564:
565: * check through all current processes to see if it's in use. If
566:
567: * not, free the corresponding string.
568:
569: * Returns: a pointer to a newly freed index, or NULL.
570:
571: */
572:
573:
574:
575: /* it's unlikely that the kernel would need to hold onto a file cookie
576:
577: for longer than this many calls to tstrindex() without first
578:
579: saving the cookie in a directory or file pointer
580:
581: */
582:
583:
584:
585: static struct tindex *
586:
587: garbage_collect()
588:
589: {
590:
591: struct tindex *free, *t;
592:
1.1.1.3 root 593: int i;
594:
595: #ifndef NEWWAY
596:
1.1 root 597: fcookie *fc, *gc;
598:
599: PROC *p;
600:
1.1.1.3 root 601: int j;
1.1 root 602:
603: int age;
604:
1.1.1.3 root 605: #endif
606:
1.1 root 607:
608:
609: free = 0;
610:
611: t = gl_ti;
612:
613: for (i = 0; i < NUM_INDICES; i++,t++) {
614:
615: if (!t->name) continue;
616:
1.1.1.3 root 617: #ifdef NEWWAY
618:
619: if (t->links == 0) {
620:
621: kfree(t->name);
622:
623: t->name = 0;
624:
625: if (!free) free = t;
626:
627: }
628:
629: #else
630:
1.1 root 631: age = tclock - t->stamp;
632:
633: t->stamp = 0;
634:
635: assert(age >= 0);
636:
637: if (age > MIN_AGE) {
638:
639: /* see if any process is using this index */
640:
641: if (t->open)
642:
643: goto found_index;
644:
645: for (p = proclist; p; p = p->gl_next) {
646:
647: fc = p->curdir;
648:
649: gc = p->root;
650:
651: for (j = 0; j < NUM_DRIVES; j++,fc++,gc++) {
652:
653: if (( fc->fs == &tos_filesys &&
654:
655: fc->index == (long)t ) ||
656:
657: ( gc->fs == &tos_filesys &&
658:
659: gc->index == (long)t ) )
660:
661: goto found_index;
662:
663: }
664:
665: }
666:
667: /* here, we couldn't find the index in use by any proc. */
668:
669: kfree(t->name);
670:
671: t->name = 0;
672:
673: if (!free)
674:
675: free = t;
676:
677: found_index:
678:
679: ;
680:
681: } else {
682:
683: /* make sure that future garbage collections might look at this file */
684:
685: t->stamp = -age;
686:
687: }
688:
1.1.1.3 root 689: #endif
690:
1.1 root 691: }
692:
693:
694:
1.1.1.3 root 695: #ifndef NEWWAY
696:
1.1 root 697: tclock = 0; /* reset the clock */
698:
699: tmpindex.valid = 0; /* expire the temporary Fsfirst buffer */
700:
1.1.1.3 root 701: #endif
702:
1.1 root 703: return free;
704:
705: }
706:
707:
708:
709: #define DIRSEP(c) ((c) == '\\')
710:
711:
712:
713: static int
714:
715: tfullpath(result, basei, path)
716:
717: char *result;
718:
719: struct tindex *basei;
720:
721: const char *path;
722:
723: {
724:
725: #define TNMTEMP 32
726:
727: char *n, name[TNMTEMP+1];
728:
729: int namelen, pathlen;
730:
731: char *base = basei->name;
732:
733: int r = 0;
734:
735:
736:
1.1.1.3 root 737: #ifndef NEWWAY
738:
1.1 root 739: basei->stamp = ++tclock;
740:
741: if (tclock > 10000) {
742:
743: /* garbage collect every so often whether we need it or not */
744:
745: (void)garbage_collect();
746:
747: }
748:
1.1.1.3 root 749: #endif
750:
1.1 root 751: if (!*path) {
752:
753: strncpy(result, base, PATH_MAX-1);
754:
755: return r;
756:
757: }
758:
759:
760:
761: strncpy(result, base, PATH_MAX-1);
762:
763:
764:
765: pathlen = strlen(result);
766:
767:
768:
769: /* now path is relative to what's currently in "result" */
770:
771:
772:
773: while(*path) {
774:
775: /* get next name in path */
776:
777: n = name; namelen = 0;
778:
779: while (*path && !DIRSEP(*path)) {
780:
781: /* BUG: we really should to the translation to DOS 8.3
782:
783: * format *here*, so that really long names are truncated
784:
785: * correctly.
786:
787: */
788:
789: if (namelen < TNMTEMP) {
790:
791: *n++ = toupper(*path); path++; namelen++;
792:
793: }
794:
795: else
796:
797: path++;
798:
799: }
800:
1.1.1.2 root 801: *n = 0;
1.1 root 802:
803: while (DIRSEP(*path)) path++;
804:
805: /* check for "." and ".." */
806:
807: if (!strcmp(name, ".")) continue;
808:
809: if (!strcmp(name, "..")) {
810:
811: n = strrchr(result, '\\');
812:
813: if (n) {
814:
815: *n = 0;
816:
1.1.1.2 root 817: pathlen = (int)(n - result);
1.1 root 818:
819: }
820:
821: else r = EMOUNT;
822:
823: continue;
824:
825: }
826:
827: if (pathlen + namelen < PATH_MAX - 1) {
828:
829: strcat(result, "\\");
830:
831: pathlen++;
832:
833:
834:
835: /* make sure the name is restricted to DOS 8.3 format */
836:
837: for (base = result; *base; base++)
838:
839: ;
840:
841: n = name;
842:
843: namelen = 0;
844:
845: while (*n && *n != '.' && namelen++ < 8) {
846:
847: *base++ = *n++;
848:
849: pathlen++;
850:
851: }
852:
853: while (*n && *n != '.') n++;
854:
855: if (*n == '.' && *(n+1) != 0) {
856:
857: *base++ = *n++;
858:
859: pathlen++;
860:
861: namelen = 0;
862:
863: while (*n && namelen++ < 3) {
864:
865: *base++ = *n++;
866:
867: pathlen++;
868:
869: }
870:
871: }
872:
873: *base = 0;
874:
875: }
876:
877: }
878:
879: return r;
880:
881: }
882:
883:
884:
1.1.1.2 root 885: static long ARGS_ON_STACK
1.1 root 886:
887: tos_root(drv, fc)
888:
889: int drv;
890:
891: fcookie *fc;
892:
893: {
894:
895: struct tindex *ti;
896:
897:
898:
899: ksprintf(tmpbuf, "%c:", drv+'A');
900:
901: fc->fs = &tos_filesys;
902:
903: fc->dev = drv;
904:
905: ti = tstrindex(tmpbuf);
906:
907: ti->size = ti->date = ti->time = 0;
908:
909: ti->attr = FA_DIR;
910:
911: ti->valid = 1;
912:
913: fc->index = (long)ti;
914:
915:
916:
917: /* if the drive has changed, make sure GEMDOS knows it! */
918:
919: if (drvchanged[drv]) {
920:
921: force_mediach(drv);
922:
923: }
924:
1.1.1.3 root 925: #ifdef NEWWAY
926:
927: ti->links++;
928:
929: #endif
930:
1.1 root 931: return 0;
932:
933: }
934:
935:
936:
1.1.1.2 root 937: static long ARGS_ON_STACK
1.1 root 938:
939: tos_lookup(dir, name, fc)
940:
941: fcookie *dir;
942:
943: const char *name;
944:
945: fcookie *fc;
946:
947: {
948:
949: long r;
950:
951: struct tindex *ti = (struct tindex *)dir->index;
952:
953:
954:
955: r = tfullpath(tmpbuf, ti, name);
956:
957:
958:
959: /* if the name is empty or otherwise trivial, just return the directory */
960:
961: if (!strcmp(ti->name, tmpbuf)) {
962:
963: *fc = *dir;
964:
1.1.1.3 root 965: #ifdef NEWWAY
966:
967: ti->links++;
968:
969: COOKIE_DB(("tos_lookup: %s now has %d links", ti->name, ti->links));
970:
971: #endif
972:
1.1 root 973: return r;
974:
975: }
976:
977:
978:
979: /* is there already an index for this file?? If so, is it up to date?? */
980:
981: ti = tstrindex(tmpbuf);
982:
983: if (!ti->valid) {
984:
985: if (tmpbuf[1] == ':' && tmpbuf[2] == 0) {
986:
987: /* a root directory -- lookup always succeeds */
988:
989: foo.dta_size = 0;
990:
991: foo.dta_date = foo.dta_time = 0;
992:
993: foo.dta_attrib = FA_DIR;
994:
995: foo.dta_name[0] = 0;
996:
997: } else {
998:
999: do_setdta(&foo);
1000:
1001: r = Fsfirst(tmpbuf, FILEORDIR);
1002:
1003: if (r) {
1004:
1.1.1.2 root 1005: DEBUG(("tos_lookup: Fsfirst(%s) returned %ld", tmpbuf, r));
1.1 root 1006:
1007: return r;
1008:
1009: }
1010:
1011: }
1012:
1013: ti->size = foo.dta_size;
1014:
1015: ti->date = foo.dta_date;
1016:
1017: ti->time = foo.dta_time;
1018:
1019: ti->attr = foo.dta_attrib;
1020:
1021: if (executable_extension(foo.dta_name))
1022:
1023: ti->attr |= FA_EXEC;
1024:
1025: ti->valid = 1;
1026:
1027: }
1028:
1029: fc->fs = &tos_filesys;
1030:
1031: fc->index = (long)ti;
1032:
1033: fc->dev = dir->dev;
1034:
1.1.1.3 root 1035: #ifdef NEWWAY
1036:
1037: ti->links++;
1038:
1039: COOKIE_DB(("tos_lookup: %s now has %d links", ti->name, ti->links));
1040:
1041: #endif
1042:
1.1 root 1043: return r;
1044:
1045: }
1046:
1047:
1048:
1.1.1.2 root 1049: static long ARGS_ON_STACK
1.1 root 1050:
1051: tos_getxattr(fc, xattr)
1052:
1053: fcookie *fc;
1054:
1055: XATTR *xattr;
1056:
1057: {
1058:
1059: struct tindex *ti = (struct tindex *)fc->index;
1060:
1061: long r;
1062:
1063: static long junkindex = 0;
1064:
1065:
1066:
1067: xattr->index = junkindex++;
1068:
1069: xattr->dev = fc->dev;
1070:
1.1.1.5 ! root 1071: xattr->rdev = fc->dev;
! 1072:
1.1 root 1073: xattr->nlink = 1;
1074:
1075: xattr->uid = xattr->gid = 0;
1076:
1077:
1078:
1.1.1.3 root 1079: #ifndef NEWWAY
1080:
1.1 root 1081: ti->stamp = ++tclock;
1082:
1.1.1.3 root 1083: #endif
1084:
1085:
1086:
1.1 root 1087: if (!ti->valid) {
1088:
1089: do_setdta(&foo);
1090:
1091: if (ti->name[2] == 0) { /* a root directory */
1092:
1093: /* actually, this can also happen if a program tries to open a file
1094:
1095: * with an empty name... so we should fail gracefully
1096:
1097: */
1098:
1.1.1.3 root 1099: ti->attr = FA_DIR;
1100:
1101: ti->size = 0;
1102:
1103: ti->date = ti->time = 0;
1104:
1.1 root 1105: goto around;
1106:
1107: }
1108:
1109:
1110:
1111: r = Fsfirst(ti->name, FILEORDIR);
1112:
1.1.1.3 root 1113: if (r) {
1114:
1115: DEBUG(("tosfs: search error %ld on [%s]", r, ti->name));
1.1 root 1116:
1.1.1.3 root 1117: return r;
1118:
1119: }
1.1 root 1120:
1121: ti->size = foo.dta_size;
1122:
1123: ti->date = foo.dta_date;
1124:
1125: ti->time = foo.dta_time;
1126:
1127: ti->attr = foo.dta_attrib;
1128:
1129: if (executable_extension(foo.dta_name))
1130:
1131: ti->attr |= FA_EXEC;
1132:
1133: around:
1134:
1135: ti->valid = 1;
1136:
1137: }
1138:
1139: xattr->size = ti->size;
1140:
1141:
1142:
1143: /* BUG: blksize isn't accurate if the sector size is not 512 */
1144:
1145: xattr->blksize = 1024;
1146:
1147: xattr->nblocks = (xattr->size + 1023) / 1024;
1148:
1149: xattr->mdate = xattr->cdate = xattr->adate = ti->date;
1150:
1151: xattr->mtime = xattr->ctime = xattr->atime = ti->time;
1152:
1153: xattr->mode = (ti->attr & FA_DIR) ? (S_IFDIR | DEFAULT_DIRMODE) :
1154:
1155: (S_IFREG | DEFAULT_MODE);
1156:
1157:
1158:
1.1.1.5 ! root 1159: /* TOS files have permissions rwxrwx--- */
! 1160:
! 1161: xattr->mode &= ~(S_IROTH|S_IWOTH|S_IXOTH);
! 1162:
! 1163:
! 1164:
1.1 root 1165: if (ti->attr & FA_RDONLY) {
1166:
1167: xattr->mode &= ~(S_IWUSR|S_IWGRP|S_IWOTH);
1168:
1169: }
1170:
1171:
1172:
1173: if (ti->attr & FA_EXEC) {
1174:
1175: xattr->mode |= (S_IXUSR|S_IXGRP|S_IXOTH);
1176:
1177: }
1178:
1179: xattr->attr = ti->attr & 0xff;
1180:
1181: return 0;
1182:
1183: }
1184:
1185:
1186:
1.1.1.2 root 1187: static long ARGS_ON_STACK
1.1 root 1188:
1189: tos_chattr(fc, attrib)
1190:
1191: fcookie *fc;
1192:
1193: int attrib;
1194:
1195: {
1196:
1197: struct tindex *ti = (struct tindex *)fc->index;
1198:
1199:
1200:
1201: if (ti->attr & FA_DIR) {
1202:
1.1.1.2 root 1203: DEBUG(("error: attempt to change attributes of a directory"));
1.1 root 1204:
1205: return EACCDN;
1206:
1207: }
1208:
1209: ti->valid = 0;
1210:
1211: (void)tfullpath(tmpbuf, ti, "");
1212:
1213: return Fattrib(tmpbuf, 1, attrib);
1214:
1215: }
1216:
1217:
1218:
1.1.1.2 root 1219: static long ARGS_ON_STACK
1.1 root 1220:
1221: tos_chown(dir, uid, gid)
1222:
1223: fcookie *dir;
1224:
1225: int uid, gid;
1226:
1227: {
1228:
1.1.1.2 root 1229: UNUSED(dir); UNUSED(uid); UNUSED(gid);
1230:
1.1 root 1231: return EINVFN;
1232:
1233: }
1234:
1235:
1236:
1.1.1.2 root 1237: static long ARGS_ON_STACK
1.1 root 1238:
1239: tos_chmode(fc, mode)
1240:
1241: fcookie *fc;
1242:
1243: unsigned mode;
1244:
1245: {
1246:
1247: int oldattr, newattr;
1248:
1249: long r;
1250:
1251: struct tindex *ti = (struct tindex *)fc->index;
1252:
1253:
1254:
1255: oldattr = Fattrib(ti->name, 0, 0);
1256:
1257: if (oldattr < 0)
1258:
1259: return oldattr;
1260:
1261:
1262:
1263: ti->valid = 0;
1264:
1265:
1266:
1267: if (!(mode & S_IWUSR))
1268:
1269: newattr = oldattr | FA_RDONLY;
1270:
1271: else
1272:
1273: newattr = oldattr & ~FA_RDONLY;
1274:
1275: if (newattr != oldattr)
1276:
1277: r = Fattrib(ti->name, 1, newattr);
1278:
1279: else
1280:
1281: r = 0;
1282:
1283: return (r < 0) ? r : 0;
1284:
1285: }
1286:
1287:
1288:
1.1.1.2 root 1289: static long ARGS_ON_STACK
1.1 root 1290:
1291: tos_mkdir(dir, name, mode)
1292:
1293: fcookie *dir;
1294:
1295: const char *name;
1296:
1297: unsigned mode; /* ignored under TOS */
1298:
1299: {
1300:
1.1.1.2 root 1301: UNUSED(mode);
1302:
1303:
1304:
1.1 root 1305: (void)tfullpath(tmpbuf, (struct tindex *)dir->index, name);
1306:
1307: tmpindex.valid = 0;
1308:
1309:
1310:
1311: return Dcreate(tmpbuf);
1312:
1313: }
1314:
1315:
1316:
1.1.1.2 root 1317: static long ARGS_ON_STACK
1.1 root 1318:
1319: tos_rmdir(dir, name)
1320:
1321: fcookie *dir;
1322:
1323: const char *name;
1324:
1325: {
1326:
1327: struct tindex *ti;
1328:
1329:
1330:
1331: (void)tfullpath(tmpbuf, (struct tindex *)dir->index, name);
1332:
1333: ti = tstrindex(tmpbuf);
1334:
1335: ti->valid = 0;
1336:
1337:
1338:
1339: return Ddelete(tmpbuf);
1340:
1341: }
1342:
1343:
1344:
1.1.1.2 root 1345: static long ARGS_ON_STACK
1.1 root 1346:
1347: tos_remove(dir, name)
1348:
1349: fcookie *dir;
1350:
1351: const char *name;
1352:
1353: {
1354:
1355: struct tindex *ti;
1356:
1357:
1358:
1359: (void)tfullpath(tmpbuf, (struct tindex *)dir->index, name);
1360:
1361:
1362:
1363: ti = tstrindex(tmpbuf);
1364:
1365: if (ti->open) {
1366:
1.1.1.2 root 1367: DEBUG(("tos_remove: file is open, will be deleted later"));
1.1 root 1368:
1369: if (ti->attr & FA_RDONLY)
1370:
1371: return EACCDN;
1372:
1373: ti->attr |= FA_DELETE;
1374:
1375: return 0;
1376:
1377: }
1378:
1379: ti->valid = 0;
1380:
1381: return Fdelete(tmpbuf);
1382:
1383: }
1384:
1385:
1386:
1.1.1.2 root 1387: static long ARGS_ON_STACK
1.1 root 1388:
1.1.1.3 root 1389: tos_getname(root, dir, pathname, size)
1.1 root 1390:
1391: fcookie *root, *dir;
1392:
1393: char *pathname;
1394:
1.1.1.3 root 1395: int size;
1396:
1.1 root 1397: {
1398:
1.1.1.2 root 1399: char *rootnam = ((struct tindex *)root->index)->name;
1400:
1401: char *dirnam = ((struct tindex *)dir->index)->name;
1402:
1403: int i;
1404:
1.1 root 1405:
1406:
1.1.1.2 root 1407: i = strlen(rootnam);
1.1 root 1408:
1.1.1.3 root 1409: if (strlen(dirnam) < i) {
1410:
1411: DEBUG(("tos_getname: root is longer than path"));
1.1 root 1412:
1.1.1.3 root 1413: return EINTRN;
1.1 root 1414:
1.1.1.3 root 1415: }
1.1.1.2 root 1416:
1.1.1.3 root 1417: if (strlen(dirnam+i) < size) {
1418:
1419: strcpy(pathname, dirnam + i);
1.1.1.2 root 1420:
1421: /*
1422:
1423: * BUG: must be a better way to decide upper/lower case
1424:
1425: */
1426:
1427: if (curproc->domain == DOM_MINT)
1428:
1429: strlwr(pathname);
1430:
1.1.1.3 root 1431: return 0;
1.1 root 1432:
1.1.1.3 root 1433: } else {
1434:
1435: DEBUG(("tosfs: name too long"));
1436:
1437: return ERANGE;
1438:
1439: }
1.1 root 1440:
1441: }
1442:
1443:
1444:
1.1.1.2 root 1445: static long ARGS_ON_STACK
1.1 root 1446:
1447: tos_rename(olddir, oldname, newdir, newname)
1448:
1449: fcookie *olddir;
1450:
1451: char *oldname;
1452:
1453: fcookie *newdir;
1454:
1455: const char *newname;
1456:
1457: {
1458:
1459: char newbuf[128];
1460:
1461: struct tindex *ti;
1462:
1463: long r;
1464:
1465:
1466:
1467: (void)tfullpath(tmpbuf, (struct tindex *)olddir->index, oldname);
1468:
1469: (void)tfullpath(newbuf, (struct tindex *)newdir->index, newname);
1470:
1471: r = Frename(0, tmpbuf, newbuf);
1472:
1473: if (r == 0) {
1474:
1475: ti = tstrindex(tmpbuf);
1476:
1477: kfree(ti->name);
1478:
1479: ti->name = kmalloc((long)strlen(newbuf)+1);
1480:
1481: if (!ti->name) {
1482:
1483: FATAL("tosfs: unable to allocate space for a name");
1484:
1485: }
1486:
1487: strcpy(ti->name, newbuf);
1488:
1489: ti->valid = 0;
1490:
1491: }
1492:
1493: return r;
1494:
1495: }
1496:
1497:
1498:
1499: #define DIR_FLAG(x) (x->fsstuff[0])
1500:
1501: #define STARTSEARCH 0 /* opendir() was just called */
1502:
1503: #define INSEARCH 1 /* readdir() has been called at least once */
1504:
1505: #define NMFILE 2 /* no more files to read */
1506:
1507:
1508:
1509: #define DIR_DTA(x) ((DTABUF *)(x->fsstuff + 2))
1510:
1511: #define DIR_NAME(x) (x->fsstuff + 32)
1512:
1513:
1514:
1515: /*
1516:
1517: * The directory functions are a bit tricky. What we do is have
1518:
1519: * opendir() do Fsfirst; the first readdir() picks up this name,
1520:
1521: * subsequent readdir()'s have to do Fsnext
1522:
1523: */
1524:
1525:
1526:
1.1.1.2 root 1527: static long ARGS_ON_STACK
1.1 root 1528:
1529: tos_opendir(dirh, flags)
1530:
1531: DIR *dirh;
1532:
1533: int flags;
1534:
1535: {
1536:
1537: long r;
1538:
1539: struct tindex *t = (struct tindex *)dirh->fc.index;
1540:
1541:
1542:
1.1.1.2 root 1543: UNUSED(flags);
1544:
1545:
1546:
1.1 root 1547: (void)tfullpath(tmpbuf, t, "*.*");
1548:
1549:
1550:
1551: do_setdta(DIR_DTA(dirh));
1552:
1553:
1554:
1555: r = Fsfirst(tmpbuf, FILEORDIR);
1556:
1557:
1558:
1559: if (r == 0) {
1560:
1561: t->open++;
1562:
1563: DIR_FLAG(dirh) = STARTSEARCH;
1564:
1565: return 0;
1566:
1567: } else if (r == EFILNF) {
1568:
1569: t->open++;
1570:
1571: DIR_FLAG(dirh) = NMFILE;
1572:
1573: return 0;
1574:
1575: }
1576:
1577: return r;
1578:
1579: }
1580:
1581:
1582:
1.1.1.2 root 1583: static long ARGS_ON_STACK
1.1 root 1584:
1585: tos_readdir(dirh, name, namelen, fc)
1586:
1587: DIR *dirh;
1588:
1589: char *name;
1590:
1591: int namelen;
1592:
1593: fcookie *fc;
1594:
1595: {
1596:
1597: static long index = 0;
1598:
1599: long ret;
1600:
1601: int giveindex = dirh->flags == 0;
1602:
1603: struct tindex *ti;
1604:
1605: DTABUF *dta = DIR_DTA(dirh);
1606:
1607:
1608:
1609: again:
1610:
1611: if (DIR_FLAG(dirh) == NMFILE)
1612:
1613: return ENMFIL;
1614:
1615:
1616:
1617: if (DIR_FLAG(dirh) == STARTSEARCH) {
1618:
1619: DIR_FLAG(dirh) = INSEARCH;
1620:
1621: } else {
1622:
1623: assert(DIR_FLAG(dirh) == INSEARCH);
1624:
1625: do_setdta(dta);
1626:
1627: ret = Fsnext();
1628:
1629: if (ret) {
1630:
1631: DIR_FLAG(dirh) = NMFILE;
1632:
1633: return ret;
1634:
1635: }
1636:
1637: }
1638:
1639:
1640:
1641: /* don't return volume labels from readdir */
1642:
1643: if (dta->dta_attrib == FA_LABEL) goto again;
1644:
1645:
1646:
1647: fc->fs = &tos_filesys;
1648:
1649: fc->dev = dirh->fc.dev;
1650:
1651:
1652:
1653: (void)tfullpath(tmpiname, (struct tindex *)dirh->fc.index, DIR_NAME(dirh));
1654:
1655:
1656:
1657: ti = &tmpindex;
1658:
1659: ti->name = tmpiname;
1660:
1661: ti->valid = 1;
1662:
1663: ti->size = dta->dta_size;
1664:
1665: ti->date = dta->dta_date;
1666:
1667: ti->time = dta->dta_time;
1668:
1669: ti->attr = dta->dta_attrib;
1670:
1.1.1.3 root 1671: #ifndef NEWWAY
1672:
1.1 root 1673: ti->stamp = tclock;
1674:
1.1.1.3 root 1675: #endif
1676:
1.1 root 1677: if (executable_extension(dta->dta_name))
1678:
1679: ti->attr |= FA_EXEC;
1680:
1681: fc->index = (long)ti;
1682:
1683:
1684:
1685: if (giveindex) {
1686:
1.1.1.2 root 1687: namelen -= (int) sizeof(long);
1.1 root 1688:
1689: if (namelen <= 0) return ERANGE;
1690:
1691: *((long *)name) = index++;
1692:
1693: name += sizeof(long);
1694:
1695: }
1696:
1697: strncpy(name, DIR_NAME(dirh), namelen-1);
1698:
1699: name[namelen-1] = 0;
1700:
1.1.1.3 root 1701:
1702:
1703: /* BUG: we really should do the "strlwr" operation only
1704:
1705: * for Dreaddir (i.e. if giveindex == 0) but
1706:
1707: * unfortunately some old programs rely on the behaviour
1708:
1709: * below
1710:
1711: */
1712:
1713: if (curproc->domain == DOM_MINT) {
1.1 root 1714:
1715: strlwr(name);
1716:
1717: }
1718:
1719: if (strlen(DIR_NAME(dirh)) >= namelen)
1720:
1721: return ENAMETOOLONG;
1722:
1.1.1.3 root 1723: #ifdef NEWWAY
1.1 root 1724:
1.1.1.3 root 1725: ti->links++;
1726:
1727: COOKIE_DB(("tos_readdir: %s now has %d links", ti->name, ti->links));
1728:
1729: #endif
1730:
1731: return 0;
1.1 root 1732:
1733: }
1734:
1735:
1736:
1.1.1.2 root 1737: static long ARGS_ON_STACK
1.1 root 1738:
1739: tos_rewinddir(dirh)
1740:
1741: DIR *dirh;
1742:
1743: {
1744:
1745: struct tindex *ti = (struct tindex *)dirh->fc.index;
1746:
1747: long r;
1748:
1749:
1750:
1751: (void)tfullpath(tmpbuf, ti, "*.*");
1752:
1753: do_setdta(DIR_DTA(dirh));
1754:
1755: r = Fsfirst(tmpbuf, FILEORDIR);
1756:
1757: if (r == 0) {
1758:
1759: DIR_FLAG(dirh) = STARTSEARCH;
1760:
1761: } else {
1762:
1763: DIR_FLAG(dirh) = NMFILE;
1764:
1765: }
1766:
1767: return r;
1768:
1769: }
1770:
1771:
1772:
1.1.1.2 root 1773: static long ARGS_ON_STACK
1.1 root 1774:
1775: tos_closedir(dirh)
1776:
1777: DIR *dirh;
1778:
1779: {
1780:
1781: struct tindex *t = (struct tindex *)dirh->fc.index;
1782:
1783:
1784:
1.1.1.2 root 1785: if (t->open == 0) {
1786:
1787: FATAL("t->open == 0: directory == %s", t->name);
1788:
1789: }
1.1 root 1790:
1791: --t->open;
1792:
1793: DIR_FLAG(dirh) = NMFILE;
1794:
1795: return 0;
1796:
1797: }
1798:
1799:
1800:
1.1.1.2 root 1801: static long ARGS_ON_STACK
1.1 root 1802:
1803: tos_pathconf(dir, which)
1804:
1805: fcookie *dir;
1806:
1807: int which;
1808:
1809: {
1810:
1.1.1.2 root 1811: UNUSED(dir);
1812:
1813:
1814:
1.1 root 1815: switch(which) {
1816:
1817: case -1:
1818:
1819: return DP_MAXREQ;
1820:
1821: case DP_IOPEN:
1822:
1823: return 60; /* we can only keep about this many open */
1824:
1825: case DP_MAXLINKS:
1826:
1827: return 1; /* no hard links */
1828:
1829: case DP_PATHMAX:
1830:
1831: return PATH_MAX;
1832:
1833: case DP_NAMEMAX:
1834:
1835: return 8+3+1;
1836:
1837: case DP_ATOMIC:
1838:
1839: return 512; /* we can write at least a sector atomically */
1840:
1841: case DP_TRUNC:
1842:
1843: return DP_DOSTRUNC; /* DOS style file names */
1844:
1845: case DP_CASE:
1846:
1847: return DP_CASECONV; /* names converted to upper case */
1848:
1849: default:
1850:
1851: return EINVFN;
1852:
1853: }
1854:
1855: }
1856:
1857:
1858:
1.1.1.2 root 1859: long ARGS_ON_STACK
1.1 root 1860:
1861: tos_dfree(dir, buf)
1862:
1863: fcookie *dir;
1864:
1865: long *buf;
1866:
1867: {
1868:
1869: return Dfree(buf, (dir->dev)+1);
1870:
1871: }
1872:
1873:
1874:
1875: /*
1876:
1877: * writelabel: creates a volume label
1878:
1879: * readlabel: reads a volume label
1880:
1881: * both of these are only guaranteed to work in the root directory
1882:
1883: */
1884:
1885:
1886:
1887: /*
1888:
1889: * BUG: this should first delete any old labels, so that it will
1890:
1891: * work with TOS <1.4
1892:
1893: */
1894:
1895:
1896:
1.1.1.2 root 1897: long ARGS_ON_STACK
1.1 root 1898:
1899: tos_writelabel(dir, name)
1900:
1901: fcookie *dir;
1902:
1903: const char *name;
1904:
1905: {
1906:
1907: long r;
1908:
1909: struct tindex *ti;
1910:
1911:
1912:
1913: (void)tfullpath(tmpbuf, (struct tindex *)dir->index, name);
1914:
1915: r = Fcreate(tmpbuf, FA_LABEL);
1916:
1917: if (r < 0) return r;
1918:
1.1.1.2 root 1919: (void)Fclose((int)r);
1.1 root 1920:
1921: ti = tstrindex(tmpbuf);
1922:
1923: ti->valid = 0;
1924:
1925: return 0;
1926:
1927: }
1928:
1929:
1930:
1.1.1.2 root 1931: long ARGS_ON_STACK
1.1 root 1932:
1933: tos_readlabel(dir, name, namelen)
1934:
1935: fcookie *dir;
1936:
1937: char *name;
1938:
1939: int namelen;
1940:
1941: {
1942:
1943: long r;
1944:
1945: struct tindex *ti = (struct tindex *)dir->index;
1946:
1947:
1948:
1949: if (ti->name[2] != 0) /* not a root directory? */
1950:
1951: return EFILNF;
1952:
1953:
1954:
1955: (void)tfullpath(tmpbuf, ti, "*.*");
1956:
1957: do_setdta(&foo);
1958:
1959: r = Fsfirst(tmpbuf, FA_LABEL);
1960:
1961: if (r)
1962:
1963: return r;
1964:
1965: strncpy(name, foo.dta_name, namelen-1);
1966:
1967: return (strlen(foo.dta_name) < namelen) ? 0 : ENAMETOOLONG;
1968:
1969: }
1970:
1971:
1972:
1973: /*
1974:
1975: * TOS creat: this doesn't actually create the file, rather it
1976:
1977: * sets up a (fake) index for the file that will be used by
1978:
1979: * the later tos_open call.
1980:
1981: */
1982:
1983:
1984:
1.1.1.2 root 1985: static long ARGS_ON_STACK
1.1 root 1986:
1987: tos_creat(dir, name, mode, attrib, fc)
1988:
1989: fcookie *dir;
1990:
1991: const char *name;
1992:
1993: unsigned mode;
1994:
1995: int attrib;
1996:
1997: fcookie *fc;
1998:
1999: {
2000:
2001: struct tindex *ti;
2002:
1.1.1.2 root 2003: UNUSED(mode);
2004:
1.1 root 2005:
2006:
2007: (void)tfullpath(tmpbuf, (struct tindex *)dir->index, name);
2008:
2009:
2010:
2011: ti = tstrindex(tmpbuf);
2012:
2013: ti->size = 0;
2014:
2015: ti->date = datestamp;
2016:
2017: ti->time = timestamp;
2018:
2019: ti->attr = attrib;
2020:
2021: ti->valid = 1;
2022:
2023:
2024:
2025: fc->fs = &tos_filesys;
2026:
2027: fc->index = (long)ti;
2028:
2029: fc->dev = dir->dev;
2030:
1.1.1.3 root 2031: #ifdef NEWWAY
2032:
2033: ti->links++;
2034:
2035: COOKIE_DB(("tos_creat: %s now has %d links", ti->name, ti->links));
2036:
2037: #endif
2038:
1.1 root 2039: return 0;
2040:
2041: }
2042:
2043:
2044:
2045: /*
2046:
2047: * TOS device driver
2048:
2049: */
2050:
2051:
2052:
1.1.1.2 root 2053: static DEVDRV * ARGS_ON_STACK
1.1 root 2054:
2055: tos_getdev(fc, devsp)
2056:
2057: fcookie *fc;
2058:
2059: long *devsp;
2060:
2061: {
2062:
1.1.1.2 root 2063: UNUSED(fc); UNUSED(devsp);
2064:
1.1 root 2065: return &tos_device;
2066:
2067: }
2068:
2069:
2070:
1.1.1.2 root 2071: static long ARGS_ON_STACK
1.1 root 2072:
2073: tos_open(f)
2074:
2075: FILEPTR *f;
2076:
2077: {
2078:
2079: struct tindex *ti;
2080:
2081: int mode = f->flags;
2082:
2083: int tosmode;
2084:
2085: long r;
2086:
2087: extern int flk; /* in main.c, set if _FLK cookie already present */
2088:
2089:
2090:
2091: ti = (struct tindex *)(f->fc.index);
2092:
2093: assert(ti != 0);
2094:
2095:
2096:
1.1.1.3 root 2097: #ifndef NEWWAY
2098:
1.1 root 2099: ti->stamp = ++tclock;
2100:
1.1.1.3 root 2101: #endif
2102:
1.1 root 2103: ti->valid = 0;
2104:
2105:
2106:
1.1.1.3 root 2107: #ifndef RO_FASCISM
2108:
1.1 root 2109: /* TEMPORARY HACK: change all modes to O_RDWR for files opened in
2110:
2111: * compatibility sharing mode. This is silly, but
2112:
2113: * allows broken TOS programs that write to read-only handles to continue
2114:
2115: * to work (it also helps file sharing, by making the realistic assumption
2116:
2117: * that any open TOS file can be written to). Eventually,
2118:
2119: * this should be tuneable by the user somehow.
2120:
2121: * ALSO: change O_COMPAT opens into O_DENYNONE; again, this may be temporary.
2122:
2123: */
2124:
2125: if ( (mode & O_SHMODE) == O_COMPAT ) {
2126:
2127: f->flags = (mode & ~(O_RWMODE|O_SHMODE)) | O_RDWR | O_DENYNONE;
2128:
2129: }
2130:
1.1.1.3 root 2131: #endif
2132:
1.1 root 2133:
2134:
2135: /* check to see that nobody has opened this file already in an
2136:
2137: * incompatible mode
2138:
2139: */
2140:
2141: if (denyshare(ti->open, f)) {
2142:
1.1.1.2 root 2143: TRACE(("tos_open: file sharing denied"));
1.1 root 2144:
2145: return EACCDN;
2146:
2147: }
2148:
2149:
2150:
2151: /*
2152:
2153: * now open the file; if O_TRUNC was specified, actually
2154:
2155: * create the file anew.
2156:
2157: * BUG: O_TRUNC without O_CREAT doesn't work right. The kernel doesn't
2158:
2159: * use this mode, anyways
2160:
2161: */
2162:
2163:
2164:
2165: if (mode & O_TRUNC) {
2166:
2167: if (ti->open) {
2168:
1.1.1.2 root 2169: DEBUG(("tos_open: attempt to truncate an open file"));
1.1 root 2170:
2171: return EACCDN;
2172:
2173: }
2174:
2175: r = Fcreate(ti->name, ti->attr);
2176:
2177: } else {
2178:
2179: if (flk)
2180:
2181: tosmode = mode & (O_RWMODE|O_SHMODE);
2182:
2183: else
2184:
2185: tosmode = (mode & O_RWMODE);
2186:
2187: if (tosmode == O_EXEC) tosmode = O_RDONLY;
2188:
2189:
2190:
2191: r = Fopen(ti->name, tosmode );
2192:
2193: if (r == EFILNF && (mode & O_CREAT))
2194:
2195: r = Fcreate(ti->name, ti->attr);
2196:
2197: }
2198:
2199:
2200:
2201: if (r < 0) {
2202:
2203: /* get rid of the index for the file, since it doesn't exist */
2204:
2205: kfree(ti->name);
2206:
2207: ti->name = 0;
2208:
2209: ti->valid = 0;
2210:
2211: return r;
2212:
2213: }
2214:
2215:
2216:
2217: f->devinfo = r;
2218:
2219:
2220:
2221: f->next = ti->open;
2222:
2223: ti->open = f;
2224:
2225: return 0;
2226:
2227: }
2228:
2229:
2230:
1.1.1.2 root 2231: static long ARGS_ON_STACK
1.1 root 2232:
2233: tos_write(f, buf, bytes)
2234:
2235: FILEPTR *f; const char *buf; long bytes;
2236:
2237: {
2238:
2239: struct tindex *ti = (struct tindex *)f->fc.index;
2240:
2241:
2242:
2243: ti->valid = 0;
2244:
2245: return Fwrite((int)f->devinfo, bytes, buf);
2246:
2247: }
2248:
2249:
2250:
1.1.1.2 root 2251: static long ARGS_ON_STACK
1.1 root 2252:
2253: tos_read(f, buf, bytes)
2254:
2255: FILEPTR *f; char *buf; long bytes;
2256:
2257: {
2258:
2259: return Fread((int)f->devinfo, bytes, buf);
2260:
2261: }
2262:
2263:
2264:
1.1.1.2 root 2265: static long ARGS_ON_STACK
1.1 root 2266:
2267: tos_lseek(f, where, whence)
2268:
2269: FILEPTR *f; long where; int whence;
2270:
2271: {
2272:
2273: long r;
2274:
2275:
2276:
2277: r = Fseek(where, (int)f->devinfo, whence);
2278:
2279: return r;
2280:
2281: }
2282:
2283:
2284:
1.1.1.2 root 2285: static long ARGS_ON_STACK
1.1 root 2286:
2287: tos_ioctl(f, mode, buf)
2288:
2289: FILEPTR *f; int mode; void *buf;
2290:
2291: {
2292:
2293: LOCK t, *lck, **old;
2294:
2295: struct flock *fl;
2296:
2297: long r;
2298:
2299: struct tindex *ti;
2300:
2301: extern int flk; /* set in main.c if _FLK already installed */
2302:
2303:
2304:
2305: if (mode == FIONREAD || mode == FIONWRITE) {
2306:
2307: *((long *)buf) = 1;
2308:
2309: return 0;
2310:
2311: }
2312:
1.1.1.2 root 2313: else if (mode == F_SETLK || mode == F_SETLKW || mode == F_GETLK) {
1.1 root 2314:
2315: fl = ((struct flock *)buf);
2316:
2317: t.l = *fl;
2318:
2319: switch(t.l.l_whence) {
2320:
2321: case 0:
2322:
2323: break;
2324:
2325: case 1: /* SEEK_CUR */
2326:
2327: r = Fseek(0L, (int)f->devinfo, 1);
2328:
2329: t.l.l_start += r;
2330:
2331: break;
2332:
2333: case 2:
2334:
2335: r = Fseek(0L, (int)f->devinfo, 1);
2336:
2337: t.l.l_start = Fseek(t.l.l_start, (int)f->devinfo, 2);
2338:
2339: (void)Fseek(r, (int)f->devinfo, 0);
2340:
2341: break;
2342:
2343: default:
2344:
1.1.1.2 root 2345: DEBUG(("Invalid value for l_whence"));
1.1 root 2346:
2347: return EINVFN;
2348:
2349: }
2350:
2351: /* BUG: can't lock a file starting at >2gigabytes from the beginning */
2352:
2353: if (t.l.l_start < 0) t.l.l_start = 0;
2354:
2355: t.l.l_whence = 0;
2356:
2357: ti = (struct tindex *)f->fc.index;
2358:
2359:
2360:
2361: if (mode == F_GETLK) {
2362:
2363: lck = denylock(ti->locks, &t);
2364:
2365: if (lck)
2366:
2367: *fl = lck->l;
2368:
2369: else
2370:
2371: fl->l_type = F_UNLCK;
2372:
2373: return 0;
2374:
2375: }
2376:
2377:
2378:
2379: if (t.l.l_type == F_UNLCK) {
2380:
2381: /* try to find the lock */
2382:
2383: old = &ti->locks;
2384:
2385: lck = *old;
2386:
2387: while (lck) {
2388:
2389: if (lck->l.l_pid == curproc->pid &&
2390:
2391: lck->l.l_start == t.l.l_start &&
2392:
2393: lck->l.l_len == t.l.l_len) {
2394:
2395: /* found it -- remove the lock */
2396:
2397: *old = lck->next;
2398:
1.1.1.2 root 2399: TRACE(("tosfs: unlocked %s: %ld + %ld",
1.1 root 2400:
1.1.1.2 root 2401: ti->name, t.l.l_start, t.l.l_len));
1.1 root 2402:
2403: if (flk)
2404:
2405: (void)Flock((int)f->devinfo, 1,
2406:
2407: t.l.l_start, t.l.l_len);
2408:
1.1.1.2 root 2409: /* wake up anyone waiting on the lock */
2410:
2411: wake(IO_Q, (long)lck);
2412:
2413: kfree(lck);
2414:
1.1 root 2415: break;
2416:
2417: }
2418:
2419: old = &lck->next;
2420:
2421: lck = lck->next;
2422:
2423: }
2424:
2425: return lck ? 0 : ENSLOCK;
2426:
2427: }
2428:
1.1.1.2 root 2429: TRACE(("tosfs: lock %s: %ld + %ld", ti->name,
1.1 root 2430:
1.1.1.2 root 2431: t.l.l_start, t.l.l_len));
1.1 root 2432:
1.1.1.2 root 2433: do {
1.1 root 2434:
1.1.1.2 root 2435: /* see if there's a conflicting lock */
1.1 root 2436:
1.1.1.2 root 2437: while ((lck = denylock(ti->locks, &t)) != 0) {
1.1 root 2438:
1.1.1.2 root 2439: DEBUG(("tosfs: lock conflicts with one held by %d",
1.1 root 2440:
1.1.1.2 root 2441: lck->l.l_pid));
1.1 root 2442:
1.1.1.2 root 2443: if (mode == F_SETLKW) {
1.1 root 2444:
1.1.1.2 root 2445: sleep(IO_Q, (long)lck); /* sleep a while */
2446:
2447: }
1.1 root 2448:
1.1.1.2 root 2449: else
1.1 root 2450:
1.1.1.2 root 2451: return ELOCKED;
1.1 root 2452:
1.1.1.2 root 2453: }
1.1 root 2454:
1.1.1.2 root 2455: /* if not, add this lock to the list */
1.1 root 2456:
1.1.1.2 root 2457: lck = kmalloc(SIZEOF(LOCK));
1.1 root 2458:
1.1.1.2 root 2459: if (!lck) return ENSMEM;
1.1 root 2460:
1.1.1.2 root 2461: /* see if other _FLK code might object */
1.1 root 2462:
1.1.1.2 root 2463: if (flk) {
1.1 root 2464:
1.1.1.2 root 2465: r = Flock((int)f->devinfo, 0, t.l.l_start, t.l.l_len);
2466:
2467: if (r) {
2468:
2469: kfree(lck);
2470:
2471: if (mode == F_SETLKW && r == ELOCKED) {
2472:
2473: yield();
2474:
2475: lck = NULL;
2476:
2477: }
2478:
2479: else
2480:
2481: return r;
2482:
2483: }
1.1 root 2484:
2485: }
2486:
1.1.1.2 root 2487: } while (!lck);
1.1 root 2488:
2489: lck->l = t.l;
2490:
2491: lck->l.l_pid = curproc->pid;
2492:
2493: lck->next = ti->locks;
2494:
2495: ti->locks = lck;
2496:
2497: /* mark the file as being locked */
2498:
2499: f->flags |= O_LOCK;
2500:
2501: return 0;
2502:
2503: }
2504:
2505: return EINVFN;
2506:
2507: }
2508:
2509:
2510:
1.1.1.2 root 2511: static long ARGS_ON_STACK
1.1 root 2512:
2513: tos_datime(f, timeptr, rwflag)
2514:
2515: FILEPTR *f;
2516:
2517: short *timeptr;
2518:
2519: int rwflag;
2520:
2521: {
2522:
2523: if (rwflag) {
2524:
2525: struct tindex *ti = (struct tindex *)f->fc.index;
2526:
2527: ti->valid = 0;
2528:
2529: }
2530:
2531: return Fdatime(timeptr, (int)f->devinfo, rwflag);
2532:
2533: }
2534:
2535:
2536:
1.1.1.2 root 2537: static long ARGS_ON_STACK
1.1 root 2538:
2539: tos_close(f, pid)
2540:
2541: FILEPTR *f;
2542:
2543: int pid;
2544:
2545: {
2546:
2547: LOCK *lck, **oldl;
2548:
2549: struct tindex *t;
2550:
2551: FILEPTR **old, *p;
2552:
2553: long r = 0;
2554:
2555: extern int flk; /* set in main.c */
2556:
2557:
2558:
2559: t = (struct tindex *)(f->fc.index);
2560:
2561: /* if this handle was locked, remove any locks held by the process
2562:
2563: */
2564:
2565: if (f->flags & O_LOCK) {
2566:
1.1.1.2 root 2567: TRACE(("tos_close: releasing locks (file mode: %x)", f->flags));
1.1 root 2568:
2569: oldl = &t->locks;
2570:
2571: lck = *oldl;
2572:
2573: while (lck) {
2574:
2575: if (lck->l.l_pid == pid) {
2576:
2577: *oldl = lck->next;
2578:
2579: if (flk)
2580:
2581: (void)Flock((int)f->devinfo, 1,
2582:
2583: lck->l.l_start, lck->l.l_len);
2584:
1.1.1.2 root 2585: wake(IO_Q, (long)lck);
2586:
1.1 root 2587: kfree(lck);
2588:
2589: } else {
2590:
2591: oldl = &lck->next;
2592:
2593: }
2594:
2595: lck = *oldl;
2596:
2597: }
2598:
2599: }
2600:
2601:
2602:
2603: if (f->links <= 0) {
2604:
2605: /* remove f from the list of open file pointers on this index */
2606:
2607: t->valid = 0;
2608:
2609: old = &t->open;
2610:
2611: p = t->open;
2612:
2613: while (p && p != f) {
2614:
2615: old = &p->next;
2616:
2617: p = p->next;
2618:
2619: }
2620:
2621: assert(p);
2622:
2623: *old = f->next;
2624:
2625: f->next = 0;
2626:
2627: r = Fclose((int)f->devinfo);
2628:
2629:
2630:
2631: /* if the file was marked for deletion, delete it */
2632:
2633: if (!t->open) {
2634:
2635: if (t->attr & FA_DELETE) {
2636:
2637: (void)Fdelete(t->name);
2638:
2639: t->name = 0;
2640:
2641: }
2642:
2643: }
2644:
2645: }
2646:
2647: return r;
2648:
2649: }
2650:
2651:
2652:
2653: /*
2654:
2655: * check for disk change: called by the kernel if Mediach returns a
2656:
2657: * non-zero value
2658:
2659: */
2660:
2661:
2662:
1.1.1.2 root 2663: long ARGS_ON_STACK
1.1 root 2664:
2665: tos_dskchng(drv)
2666:
2667: int drv;
2668:
2669: {
2670:
2671: char dlet;
2672:
2673: int i;
2674:
2675: struct tindex *ti;
2676:
1.1.1.3 root 2677: FILEPTR *f, *nextf;
2678:
1.1 root 2679:
2680:
2681: dlet = 'A' + drv;
2682:
1.1.1.3 root 2683: MEDIA_DB(("tos_dskchng(%c)", dlet));
2684:
1.1 root 2685: ti = gl_ti;
2686:
2687: for (i = 0; i < NUM_INDICES; i++, ti++) {
2688:
1.1.1.2 root 2689: if (ti->name && ti->name[0] == dlet) {
1.1 root 2690:
1.1.1.4 root 2691: #ifdef NEWWAY
2692:
2693: /* only free the name if this index not used by any cookie */
2694:
2695: if (ti->links != 0)
2696:
2697: ti->valid = 0;
2698:
2699: else
2700:
2701: #endif /* NEWWAY */
1.1 root 2702:
1.1.1.4 root 2703: {
1.1 root 2704:
1.1.1.4 root 2705: kfree(ti->name);
1.1.1.3 root 2706:
1.1.1.4 root 2707: ti->name = 0;
1.1.1.3 root 2708:
1.1.1.4 root 2709: }
2710:
2711: if (!(ti->attr & FA_DIR)) {
2712:
2713: nextf = ti->open;
2714:
2715: while ( (f = nextf) != 0 ) {
1.1.1.3 root 2716:
1.1.1.4 root 2717: nextf = f->next;
2718:
2719: f->next = 0;
2720:
2721: }
1.1.1.3 root 2722:
2723: }
2724:
2725: ti->open = 0;
2726:
2727: /* if there are any cookies pointing at this name, they're not
2728:
2729: * valid any more, so we will *want* to get an error if they're
2730:
2731: * used.
2732:
2733: */
2734:
1.1 root 2735: }
2736:
2737: }
2738:
1.1.1.2 root 2739: tmpindex.valid = 0;
2740:
1.1 root 2741: /*
2742:
2743: * OK, make sure that GEMDOS knows to look for a change if we
2744:
2745: * ever use this drive again.
2746:
2747: */
2748:
2749: drvchanged[drv] = 1;
2750:
2751: return 1;
2752:
2753: }
2754:
2755:
2756:
1.1.1.3 root 2757: #ifdef NEWWAY
2758:
2759: /* release/copy file cookies; these functions exist to keep
2760:
2761: * track of whether or not the kernel is still using a file
2762:
2763: */
2764:
2765: long
2766:
2767: tos_release(fc)
2768:
2769: fcookie *fc;
2770:
2771: {
2772:
2773: struct tindex *ti = (struct tindex *)fc->index;
2774:
2775:
2776:
2777: if (ti->links <= 0) {
2778:
2779: FATAL("tos_release: link count of `%s' is %d", ti->name, ti->links);
2780:
2781: }
2782:
2783: ti->links--;
2784:
2785: COOKIE_DB(("tos_release: %s now has %d links", ti->name, ti->links));
2786:
2787: return 0;
2788:
2789: }
2790:
2791:
2792:
2793: long
2794:
2795: tos_dupcookie(dest, src)
2796:
2797: fcookie *dest, *src;
2798:
2799: {
2800:
2801: struct tindex *ti = (struct tindex *)src->index;
2802:
2803:
2804:
2805: if (ti->links <= 0) {
2806:
2807: FATAL("tos_dupcookie: link count of %s is %d", ti->name, ti->links);
2808:
2809: }
2810:
2811: ti->links++;
2812:
2813: COOKIE_DB(("tos_dupcookie: %s now has %d links", ti->name, ti->links));
2814:
2815: *dest = *src;
2816:
2817: return 0;
2818:
2819: }
2820:
2821: #endif
2822:
2823:
2824:
1.1 root 2825: /*
2826:
2827: * utility function: sets the TOS DTA, and also records what directory
2828:
2829: * this was in. This is just to save us a call into the kernel if the
2830:
2831: * correct DTA has already been set.
2832:
2833: */
2834:
2835:
2836:
2837: static void
2838:
2839: do_setdta(dta)
2840:
2841: DTABUF *dta;
2842:
2843: {
2844:
2845: if (dta != lastdta) {
2846:
2847: Fsetdta(dta);
2848:
2849: lastdta = dta;
2850:
2851: }
2852:
2853: }
2854:
2855:
2856:
2857: /*
2858:
2859: * routines for forcing a media change on drive "drv"
2860:
2861: */
2862:
2863:
2864:
2865: static int chdrv;
2866:
2867:
2868:
2869: /* new Getbpb function: when this is called, all the other
2870:
2871: * vectors can be un-installed
2872:
2873: */
2874:
2875:
2876:
1.1.1.2 root 2877: static long ARGS_ON_STACK (*Oldgetbpb) P_((int));
1.1 root 2878:
1.1.1.2 root 2879: static long ARGS_ON_STACK (*Oldmediach) P_((int));
1.1 root 2880:
1.1.1.2 root 2881: static long ARGS_ON_STACK (*Oldrwabs) P_((int, void *, int, int, int, long));
1.1 root 2882:
2883:
2884:
1.1.1.2 root 2885: static long ARGS_ON_STACK
1.1 root 2886:
2887: Newgetbpb(d)
2888:
2889: int d;
2890:
2891: {
2892:
2893: if (d == chdrv) {
2894:
1.1.1.3 root 2895: MEDIA_DB(("Newgetbpb(%c)", d+'A'));
1.1 root 2896:
1.1.1.3 root 2897: if (Oldgetbpb == Newgetbpb) {
1.1 root 2898:
1.1.1.3 root 2899: MEDIA_DB(("AARGH!!! BAD BPBs"));
1.1 root 2900:
1.1.1.3 root 2901: }
1.1.1.2 root 2902:
1.1.1.3 root 2903: *((Func *)0x472L) = Oldgetbpb;
1.1.1.2 root 2904:
1.1.1.3 root 2905: *((Func *)0x476L) = Oldrwabs;
2906:
2907: *((Func *)0x47eL) = Oldmediach;
1.1.1.2 root 2908:
1.1 root 2909: }
2910:
2911: return (*Oldgetbpb)(d);
2912:
2913: }
2914:
2915:
2916:
1.1.1.2 root 2917: static long ARGS_ON_STACK
1.1 root 2918:
2919: Newmediach(d)
2920:
2921: int d;
2922:
2923: {
2924:
1.1.1.3 root 2925: if (d == chdrv) {
2926:
2927: MEDIA_DB(("Newmediach(%c)", d+'A'));
1.1 root 2928:
2929: return 2;
2930:
1.1.1.3 root 2931: }
2932:
1.1 root 2933: return (*Oldmediach)(d);
2934:
2935: }
2936:
2937:
2938:
1.1.1.2 root 2939: static long ARGS_ON_STACK
1.1 root 2940:
2941: Newrwabs(d, buf, a, b, c, l)
2942:
2943: int d;
2944:
2945: void *buf;
2946:
2947: int a, b, c;
2948:
2949: long l;
2950:
2951: {
2952:
1.1.1.3 root 2953: if (d == chdrv) {
2954:
2955: MEDIA_DB(("Newrwabs"));
1.1 root 2956:
2957: return E_CHNG;
2958:
1.1.1.3 root 2959: }
2960:
1.1 root 2961: return (*Oldrwabs)(d, buf, a, b, c, l);
2962:
2963: }
2964:
2965:
2966:
2967: static void
2968:
2969: force_mediach(d)
2970:
2971: int d;
2972:
2973: {
2974:
1.1.1.3 root 2975: #ifdef FSFIRST_MEDIACH
2976:
1.1.1.2 root 2977: static char fname[] = "X:\\*.*";
1.1 root 2978:
1.1.1.3 root 2979: #else
1.1 root 2980:
1.1.1.3 root 2981: long r;
2982:
2983: static char fname[] = "X:\\X";
2984:
2985: #endif
1.1 root 2986:
1.1.1.2 root 2987: TRACE(("tosfs: disk change drive %c", d+'A'));
1.1 root 2988:
1.1.1.3 root 2989: MEDIA_DB(("forcing media change on %c", d+'A'));
2990:
1.1 root 2991:
2992:
2993: chdrv = d;
2994:
2995: Oldrwabs = *((Func *)0x476L);
2996:
1.1.1.3 root 2997: Oldgetbpb = *((Func *)0x472L);
1.1 root 2998:
1.1.1.3 root 2999: Oldmediach = *((Func *)0x47eL);
1.1 root 3000:
3001:
3002:
1.1.1.3 root 3003: if (Oldrwabs == Newrwabs || Oldgetbpb == Newgetbpb ||
1.1 root 3004:
1.1.1.3 root 3005: Oldmediach == Newmediach) {
1.1 root 3006:
1.1.1.3 root 3007: FORCE("tosfs: error in media change code");
3008:
3009: } else {
3010:
3011: *((Func *)0x476L) = Newrwabs;
1.1 root 3012:
3013: *((Func *)0x472L) = Newgetbpb;
3014:
1.1.1.3 root 3015: *((Func *)0x47eL) = Newmediach;
3016:
1.1 root 3017: }
3018:
3019:
3020:
3021: fname[0] = d + 'A';
3022:
1.1.1.3 root 3023: MEDIA_DB(("calling GEMDOS"));
3024:
3025: #ifdef FSFIRST_MEDIACH
3026:
3027: (void)Fsfirst(fname, 8);
3028:
3029: #else
3030:
3031: r = Fopen(fname, 0);
3032:
3033: if (r >= 0) Fclose((int)r);
3034:
3035: #endif
3036:
3037: MEDIA_DB(("done calling GEMDOS"));
1.1 root 3038:
3039: drvchanged[d] = 0;
3040:
1.1.1.3 root 3041: if ( *((Func *)0x476L) == Newrwabs ) {
3042:
3043: DEBUG(("WARNING: media change not performed correctly"));
3044:
3045: *((Func *)0x472L) = Oldgetbpb;
3046:
3047: *((Func *)0x476L) = Oldrwabs;
3048:
3049: *((Func *)0x47eL) = Oldmediach;
3050:
3051: }
3052:
1.1 root 3053: }
3054:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.