1 #include "u.h"
2 #include "lib.h"
3 #include "mem.h"
4 #include "dat.h"
5 #include "fns.h"
6 #include "io.h"
7
8 #include "fs.h"
9
10 /*
11 * grab next element from a path, return the pointer to unprocessed portion of
12 * path.
13 */
14 char *
nextelem(char * path,char * elem)15 nextelem(char *path, char *elem)
16 {
17 int i;
18
19 while(*path == '/')
20 path++;
21 if(*path==0 || *path==' ')
22 return 0;
23 for(i=0; *path!='\0' && *path!='/' && *path!=' '; i++){
24 if(i==NAMELEN){
25 print("name component too long\n");
26 return 0;
27 }
28 *elem++ = *path++;
29 }
30 *elem = '\0';
31 return path;
32 }
33
34 int
fswalk(Fs * fs,char * path,File * f)35 fswalk(Fs *fs, char *path, File *f)
36 {
37 char element[NAMELEN];
38
39 *f = fs->root;
40 if(BADPTR(fs->walk))
41 panic("fswalk bad pointer fs->walk");
42
43 f->path = path;
44 while(path = nextelem(path, element)){
45 switch(fs->walk(f, element)){
46 case -1:
47 return -1;
48 case 0:
49 return 0;
50 }
51 }
52 return 1;
53 }
54
55 /*
56 * boot
57 */
58 int
fsboot(Fs * fs,char * path,Boot * b)59 fsboot(Fs *fs, char *path, Boot *b)
60 {
61 File file;
62 long n;
63 static char buf[8192];
64
65 switch(fswalk(fs, path, &file)){
66 case -1:
67 print("error walking to %s\n", path);
68 return -1;
69 case 0:
70 print("%s not found\n", path);
71 return -1;
72 case 1:
73 print("found %s\n", path);
74 break;
75 }
76
77 while((n = fsread(&file, buf, sizeof buf)) > 0) {
78 if(bootpass(b, buf, n) != MORE)
79 break;
80 }
81
82 bootpass(b, nil, 0); /* tries boot */
83 return -1;
84 }
85
86 int
fsread(File * file,void * a,long n)87 fsread(File *file, void *a, long n)
88 {
89 if(BADPTR(file->fs))
90 panic("bad pointer file->fs in fsread");
91 if(BADPTR(file->fs->read))
92 panic("bad pointer file->fs->read in fsread");
93 return file->fs->read(file, a, n);
94 }
95