void writefile(struct Dir *dir, const char *name) { int r, fd; struct File *f; struct stat st; const char *last; char *start; if ((fd = open(name, O_RDONLY)) < 0) panic("open %s: %s", name, strerror(errno)); if ((r = fstat(fd, &st)) < 0) panic("stat %s: %s", name, strerror(errno)); if (!S_ISREG(st.st_mode)) panic("%s is not a regular file", name); if (st.st_size >= MAXFILESIZE) panic("%s too large", name); last = strrchr(name, '/'); if (last) last++; else last = name; f = diradd(dir, FTYPE_REG, last); start = alloc(st.st_size); readn(fd, start, st.st_size); finishfile(f, blockof(start), st.st_size); close(fd); }
int main(int argc, char **argv) { int i; char *s; struct Dir root; int flag=FLAG_ROOT; struct Dir bin, sbin; struct File *b, *sb; struct Dir vmm; struct File *v; assert(BLKSIZE % sizeof(struct File) == 0); if (argc < 3) usage(); nblocks = strtol(argv[2], &s, 0); if (*s || s == argv[2] || nblocks < 2 || nblocks > 10240) usage(); opendisk(argv[1]); startdir(&super->s_root, &root); b = diradd(&root, FTYPE_DIR, "bin"); startdir(b, &bin); sb = diradd(&root, FTYPE_DIR, "sbin"); startdir(sb, &sbin); v = diradd(&root, FTYPE_DIR, "vmm"); startdir(v, &vmm); for (i = 3; i < argc; i++) { if(strcmp("-b", argv[i]) == 0) { flag = FLAG_BIN; continue; } else if(strcmp("-sb", argv[i]) == 0) { flag = FLAG_SBIN; continue; } else if(strcmp("-g", argv[i]) == 0) { flag = FLAG_VMM; continue; } switch (flag){ case FLAG_ROOT: writefile(&root, argv[i]); break; case FLAG_BIN: writefile(&bin, argv[i]); break; case FLAG_SBIN: writefile(&sbin, argv[i]); break; case FLAG_VMM: writefile(&vmm, argv[i]); break; } } finishdir(&bin); finishdir(&sbin); finishdir(&vmm); finishdir(&root); finishdisk(); return 0; }