xref: /plan9-contrib/sys/src/boot/vt4/fs.c (revision da917039c7f233c1a27d212bf012c6afa758af39)
1 #include "include.h"
2 #include "fs.h"
3 
4 /*
5  *  grab next element from a path, return the pointer to unprocessed portion of
6  *  path.
7  */
8 char *
nextelem(char * path,char * elem)9 nextelem(char *path, char *elem)
10 {
11 	int i;
12 
13 	while(*path == '/')
14 		path++;
15 	if(*path==0 || *path==' ')
16 		return 0;
17 	for(i=0; *path!='\0' && *path!='/' && *path!=' '; i++){
18 		if(i==NAMELEN){
19 			print("name component too long\n");
20 			return 0;
21 		}
22 		*elem++ = *path++;
23 	}
24 	*elem = '\0';
25 	return path;
26 }
27 
28 int
fswalk(Fs * fs,char * path,File * f)29 fswalk(Fs *fs, char *path, File *f)
30 {
31 	char element[NAMELEN];
32 
33 	*f = fs->root;
34 	if(BADPTR(fs->walk))
35 		panic("fswalk bad pointer fs->walk");
36 
37 	f->path = path;
38 	while(path = nextelem(path, element)){
39 		switch(fs->walk(f, element)){
40 		case -1:
41 			return -1;
42 		case 0:
43 			return 0;
44 		}
45 	}
46 	return 1;
47 }
48 
49 enum {
50 	Bufsize = 8192,
51 };
52 
53 /*
54  *  boot
55  */
56 int
fsboot(Fs * fs,char * path,Boot * b)57 fsboot(Fs *fs, char *path, Boot *b)
58 {
59 	File file;
60 	long n;
61 	static char *buf;
62 
63 	if (buf == nil)
64 		buf = malloc(Bufsize + 1);
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, Bufsize)) > 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