xref: /plan9/sys/src/9/boot/bootcache.c (revision c0a4f1a4887df56ae7edf153ebb41e58eef29ccc)
1 #include <u.h>
2 #include <libc.h>
3 #include <../boot/boot.h>
4 
5 uchar statbuf[Statsz];
6 
7 int
cache(int fd)8 cache(int fd)
9 {
10 	int argc, i, p[2];
11 	char *argv[5], bd[32], buf[256], partition[64], *pp;
12 
13 	if(stat("/boot/cfs", statbuf, sizeof statbuf) < 0)
14 		return fd;
15 
16 	*partition = 0;
17 
18 	bind("#S", "/dev", MAFTER);
19 	readfile("#e/cfs", buf, sizeof(buf));
20 	if(*buf){
21 		argc = tokenize(buf, argv, 4);
22 		for(i = 0; i < argc; i++){
23 			if(strcmp(argv[i], "off") == 0)
24 				return fd;
25 			else if(stat(argv[i], statbuf, sizeof statbuf) >= 0){
26 				strncpy(partition, argv[i], sizeof(partition)-1);
27 				partition[sizeof(partition)-1] = 0;
28 			}
29 		}
30 	}
31 
32 	if(*partition == 0){
33 		readfile("#e/bootdisk", bd, sizeof(bd));
34 		if(*bd){
35 			if(pp = strchr(bd, ':'))
36 				*pp = 0;
37 			/* damned artificial intelligence */
38 			i = strlen(bd);
39 			if(strcmp("disk", &bd[i-4]) == 0)
40 				bd[i-4] = 0;
41 			else if(strcmp("fs", &bd[i-2]) == 0)
42 				bd[i-2] = 0;
43 			else if(strcmp("fossil", &bd[i-6]) == 0)
44 				bd[i-6] = 0;
45 			sprint(partition, "%scache", bd);
46 			if(stat(partition, statbuf, sizeof statbuf) < 0)
47 				*bd = 0;
48 		}
49 		if(*bd == 0){
50 			sprint(partition, "%scache", bootdisk);
51 			if(stat(partition, statbuf, sizeof statbuf) < 0)
52 				return fd;
53 		}
54 	}
55 
56 	print("cfs...");
57 	if(pipe(p)<0)
58 		fatal("pipe");
59 	switch(fork()){
60 	case -1:
61 		fatal("fork");
62 	case 0:
63 		close(p[1]);
64 		dup(fd, 0);
65 		close(fd);
66 		dup(p[0], 1);
67 		close(p[0]);
68 		if(fflag)
69 			execl("/boot/cfs", "bootcfs", "-rs", "-f", partition, 0);
70 		else
71 			execl("/boot/cfs", "bootcfs", "-s", "-f", partition, 0);
72 		break;
73 	default:
74 		close(p[0]);
75 		close(fd);
76 		fd = p[1];
77 		break;
78 	}
79 	return fd;
80 }
81