int main(void) { int addseccount = 0; /* 文件系统后边添加的扇区数 */ if(-1 == mkfs()) { printf("ERROR: make file-system error!\n"); return -1; } if(-1 == (addseccount=add_data())) { printf("ERROR: add data to disk error!\n"); return -1; } printf("===OK!\n"); printf("Size of FS is %f MB, total %d sectors.\n", HEADS*SPT*CYLINDERS * 512.0/1024.0/1024.0, HEADS*SPT*CYLINDERS); printf("Size of DISK is %f MB, total %d sectors.\n", (HEADS*SPT*CYLINDERS + addseccount) * 512.0/1024.0/1024.0, (HEADS*SPT*CYLINDERS + addseccount)); printf(" FS -- cylinders: %d, heads: %d, spt: %d.\n", CYLINDERS, HEADS, SPT); printf("DISK -- cylinders: %d, heads: %d, spt: %d.\n", CYLINDERS + addseccount/HEADS/SPT, HEADS, SPT); printf("###Please check 'Bochs' or 'VMware' configuration.\n"); printf("mkfs exit.\n\n"); return 0; }
static void init_fs(){ /* u8 fsbuf[SECTOR_SIZE]; */ MESSAGE message; message.type=INFO_FS_DEVICE; message.device=ROOT_DEVICE; assert(dd_map[DRIVER(ROOT_DEVICE)].driver_pid!=PID_INVALID,""); send_receive(BOTH,dd_map[DRIVER(ROOT_DEVICE)].driver_pid,&message); //如果系统已经是要求的系统,就不需要在格式化系统了 get_super_block(ROOT_DEVICE,&super_block); /* if(super_block.magic!=MAGIC_V1) */{ mkfs(); get_super_block(ROOT_DEVICE,&super_block); } init_inode_table(); init_file_descriptor_table(); #ifdef DEBUG_FS //write test /* memset(fsbuf,0x23,SECTOR_SIZE); */ /* WRITE_SECTOR(ROOT_DEVICE,fsbuf,1); */ //read test u8 fsbuf[SECTOR_SIZE]; READ_SECTOR(ROOT_DEVICE,fsbuf,1); printl("read test:\nfsbuf[0]=%x fsbuf[1]=%x fsbuf[2]=%x fsbuf[3]=%x\n",fsbuf[0],fsbuf[1],fsbuf[2],fsbuf[3]); #endif }
int main() { Filesystem filesystem; setup_memory_checking(); mkfs(&filesystem); touch(&filesystem, "plants"); mkdir(&filesystem, "animals"); cd(&filesystem, "animals"); touch(&filesystem, "hippo"); touch(&filesystem, "rabbit"); mkdir(&filesystem, "wild"); touch(&filesystem, "bear"); touch(&filesystem, "lion"); touch(&filesystem, "tiger"); cd(&filesystem, "/"); mkdir(&filesystem, "birds"); cd(&filesystem, "birds"); touch(&filesystem, "peacock"); rmfs(&filesystem); check_memory_leak(); return 0; }
/** * <Ring 1> Do some preparation. * *****************************************************************************/ PRIVATE void init_fs() { int i; /* f_desc_table[] */ for (i = 0; i < NR_FILE_DESC; i++) memset(&f_desc_table[i], 0, sizeof(struct file_desc)); /* inode_table[] */ for (i = 0; i < NR_INODE; i++) memset(&inode_table[i], 0, sizeof(struct inode)); /* super_block[] */ struct super_block * sb = super_block; for (; sb < &super_block[NR_SUPER_BLOCK]; sb++) sb->sb_dev = NO_DEV; /* open the device: hard disk */ MESSAGE driver_msg; driver_msg.type = DEV_OPEN; driver_msg.DEVICE = MINOR(ROOT_DEV); assert(dd_map[MAJOR(ROOT_DEV)].driver_nr != INVALID_DRIVER); send_recv(BOTH, dd_map[MAJOR(ROOT_DEV)].driver_nr, &driver_msg); /* make FS */ mkfs(); /* load super block of ROOT */ read_super_block(ROOT_DEV); sb = get_super_block(ROOT_DEV); assert(sb->magic == MAGIC_V1); root_inode = get_inode(ROOT_DEV, ROOT_INODE); }
/** * <Ring 1> Do some preparation. * *****************************************************************************/ PRIVATE void init_fs() { /* open the device: hard disk */ MESSAGE driver_msg; driver_msg.type = DEV_OPEN; driver_msg.DEVICE = MINOR(ROOT_DEV); assert(dd_map[MAJOR(ROOT_DEV)].driver_nr != INVALID_DRIVER); send_recv(BOTH, dd_map[MAJOR(ROOT_DEV)].driver_nr, &driver_msg); mkfs(); }
static void do_mkfs() { g_message("[%s]\n", __func__); g_assert(mkfs_pending_list != NULL); GHashTableIter iter; gpointer key, value; g_hash_table_iter_init(&iter, mkfs_pending_list); while (g_hash_table_iter_next(&iter, &key, &value)) { mkfs((char*)key, (char*)value); } g_hash_table_destroy(mkfs_pending_list); }
void* init(fuse_conn_info* conn) { io_backend* io_ctx = mnt_opts.device ? ffsp::io_backend_init(mnt_opts.device->c_str()) : ffsp::io_backend_init(mnt_opts.memsize); if (!io_ctx) { log().error("ffsp::init(): init I/O context failed"); exit(EXIT_FAILURE); } if (mnt_opts.mkfs_opts && !mkfs(*io_ctx, *mnt_opts.mkfs_opts)) { log().error("fuse::init(): mkfs failed"); exit(EXIT_FAILURE); } fs_context* fs = ffsp::mount(io_ctx); if (!fs) { log().error("fuse::init(): mounting failed"); exit(EXIT_FAILURE); } if (conn) { #ifdef _WIN32 conn->max_write = fs->clustersize; log().info("Setting max_write to {}", conn->max_write); #else if (conn->capable & FUSE_CAP_ATOMIC_O_TRUNC) { conn->want |= FUSE_CAP_ATOMIC_O_TRUNC; } if (conn->capable & FUSE_CAP_BIG_WRITES) { conn->want |= FUSE_CAP_BIG_WRITES; conn->max_write = fs->clustersize; log().info("Setting max_write to {}", conn->max_write); } #endif } // TODO: Would it be ok to read all existing inode + dentry structs // into memory at mount time? // -> probably a good idea to be set via console arg to // measure max memory usage. return fs; }
int main() { Filesystem filesystem; mkfs(&filesystem); touch(&filesystem, "lion"); touch(&filesystem, "camel"); touch(&filesystem, "tiger"); touch(&filesystem, "elephant"); ls(&filesystem, "."); return 0; }
int main(){ Filesystem f; mkfs(&f); mkdir(&f, "Animals"); mkdir(&f, "Birds"); mkdir(&f, "Animals"); ls(f, "."); printf("\n"); rmfs(&f); return 0; }
/** * @ingroup shell * * Shell command fstest. * @param nargs number of arguments in args array * @param args array of arguments * @return OK for success, SYSERR for syntax error */ shellcmd xsh_fstest(int nargs, char *args[]) { /* Output help, if '--help' argument was supplied */ if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) { printf("Usage: %s\n\n", args[0]); printf("Description:\n"); printf("\tFilesystem Test\n"); printf("Options:\n"); printf("\t--help\tdisplay this help and exit\n"); return OK; } /* Check for correct number of arguments */ if (nargs > 1) { fprintf(stderr, "%s: too many arguments\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return SYSERR; } if (nargs < 1) { fprintf(stderr, "%s: too few arguments\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return SYSERR; } #ifdef FS mkbsdev(0, 5, 5); /* device "0" and default blocksize (=0) and count */ mkfs(0,100); /* bsdev 0*/ //testbitmask(); test_fs(); #else printf("No filesystem support\n"); #endif return OK; }
int main(int argc, char **argv) { uint16_t fsize, isize; if (argv[1] && strcmp(argv[1], "-X") == 0) { swizzling = 1; argv++; argc--; } if (argc != 4) { printf("Usage: mkfs [-X] device isize fsize\n"); return -1; } if (sizeof(inode) != 512) { printf("inode is the wrong size -- %d\n", (int) sizeof(inode)); } isize = (uint16_t) atoi(argv[2]); fsize = (uint16_t) atoi(argv[3]); if (fsize < 3 || isize < 2 || isize >= fsize) { printf("Bad parameter values\n"); return -1; } memset(zero512, 0, 512); printf("Making filesystem with %s byte order on device %s with fsize = %u and isize = %u.\n", swizzling==0 ? "normal" : "reversed", argv[1], fsize, isize); if (fd_open(argv[1])) { printf("Can't open device"); return -1; } mkfs(fsize, isize); return 0; }
static int setup(struct exfat_dev* dev, int sector_bits, int spc_bits, const char* volume_label, uint32_t volume_serial, uint64_t first_sector) { param.sector_bits = sector_bits; param.first_sector = first_sector; param.volume_size = exfat_get_size(dev); param.spc_bits = setup_spc_bits(sector_bits, spc_bits, param.volume_size); if (param.spc_bits == -1) return 1; if (setup_volume_label(param.volume_label, volume_label) != 0) return 1; param.volume_serial = setup_volume_serial(volume_serial); if (param.volume_serial == 0) return 1; return mkfs(dev, param.volume_size); }
static void init_fs() { int i; for( i=0; i<NR_FILE_DESC; i++ ) memset( &file_desc_table[i], 0, sizeof(struct file_desc) ); for( i=0; i<NR_INODE; i++ ) memset( &inode_table[i], 0, sizeof(struct m_inode ) ); struct m_super_block *sb = super_block_table; for( ; sb<&super_block_table[NR_SUPER_BLOCK]; sb++ ) sb->sb_dev = NO_DEV; MESSAGE driver_msg; driver_msg.type = DEV_OPEN; driver_msg.DEVICE = MINOR(ROOT_DEV); send_recv(BOTH, dd_map[MAJOR(ROOT_DEV)].driver_nr, &driver_msg); mkfs(); mount_root( ); }
int main(int argc, char** argv) { size_t part = 1024* 1024* 1024; //10485760; uint32_t inode_number = 128; uint32_t block_size = 128; char* filename; int option = 0; while((option = getopt(argc, argv, "i:b:s:")) != -1) { switch(option) { case 'i': inode_number = atoi(optarg); break; case 'b': block_size = atoi(optarg); break; case 's': part = atoi(optarg); break; } } if(optind == argc) return -1; filename = argv[opterr]; FILE* fh = fopen(filename, "w+"); fseek(fh, part, SEEK_SET); fputc('\0', fh); fclose(fh); void* p = fext_init(filename); mkfs(p, part, inode_number, block_size); f_mkdir(p, 0, "foo"); f_mkdir(p, 0, "foo0"); f_tree(p); }
int main(int argc, char **argv) { uint16_t fsize, isize; if (argc != 4) { printf("Usage: mkfs device isize fsize\n"); return -1; } if(sizeof(inode) != 512){ printf("inode is the wrong size -- %d\n", (int)sizeof(inode)); } isize = (uint16_t)atoi(argv[2]); fsize = (uint16_t)atoi(argv[3]); if (fsize < 3 || isize < 2 || isize >= fsize) { printf("Bad parameter values\n"); return -1; } memset(zero512, 0, 512); printf("Making filesystem on device %s with isize %u fsize %u.\n", argv[1], isize, fsize); if (fd_open(argv[1])) { printf("Can't open device"); return -1; } mkfs(fsize, isize); return 0; }
int main(int argc, char *argv[]) { int r; DIR *root_dir; if(argc < 2){ fprintf(stderr, "Usage: mkfs fs.img files...\n"); exit(1); } assert((512 % sizeof(struct dinode)) == 0); assert((512 % sizeof(struct xv6_dirent)) == 0); fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666); if(fsfd < 0){ perror(argv[1]); exit(1); } mkfs(995, 200, 1024); root_dir = opendir(argv[2]); root_inode = ialloc(T_DIR); assert(root_inode == ROOTINO); r = add_dir(root_dir, root_inode, root_inode); if (r != 0) { exit(EXIT_FAILURE); } balloc(usedblocks); exit(0); }
int main(int argc, char *argv[]) { int ch; struct partition *pp; struct disklabel *lp; struct disklabel mfsfakelabel; struct partition oldpartition; struct stat st; struct statfs *mp; struct rlimit rl; int fsi = -1, oflagset = 0, fso, len, n, maxpartitions; char *cp = NULL, *s1, *s2, *special, *opstring, *realdev; #ifdef MFS char mountfromname[BUFSIZ]; char *pop = NULL, node[PATH_MAX]; pid_t pid, res; struct statfs sf; struct stat mountpoint; int status; #endif uid_t mfsuid = 0; gid_t mfsgid = 0; mode_t mfsmode = 0; char *fstype = NULL; char **saveargv = argv; int ffsflag = 1; const char *errstr; long long fssize_input = 0; int fssize_usebytes = 0; u_int64_t nsecs; if (strstr(__progname, "mfs")) mfs = Nflag = quiet = 1; getphysmem(); maxpartitions = getmaxpartitions(); if (maxpartitions > 26) fatal("insane maxpartitions value %d", maxpartitions); opstring = mfs ? "P:T:b:c:e:f:i:m:o:s:" : "NO:S:T:b:c:e:f:g:h:i:m:o:qs:t:"; while ((ch = getopt(argc, argv, opstring)) != -1) { switch (ch) { case 'N': Nflag = 1; break; case 'O': Oflag = strtonum(optarg, 0, 2, &errstr); if (errstr) fatal("%s: invalid ffs version", optarg); oflagset = 1; break; case 'S': if (scan_scaled(optarg, §orsize) == -1 || sectorsize <= 0 || (sectorsize % DEV_BSIZE)) fatal("sector size invalid: %s", optarg); break; case 'T': disktype = optarg; break; case 'b': bsize = strtonum(optarg, MINBSIZE, MAXBSIZE, &errstr); if (errstr) fatal("block size is %s: %s", errstr, optarg); break; case 'c': maxfrgspercg = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("fragments per cylinder group is %s: %s", errstr, optarg); break; case 'e': maxbpg = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("blocks per file in a cylinder group is" " %s: %s", errstr, optarg); break; case 'f': fsize = strtonum(optarg, MINBSIZE / MAXFRAG, MAXBSIZE, &errstr); if (errstr) fatal("fragment size is %s: %s", errstr, optarg); break; case 'g': avgfilesize = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("average file size is %s: %s", errstr, optarg); break; case 'h': avgfilesperdir = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("average files per dir is %s: %s", errstr, optarg); break; case 'i': density = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("bytes per inode is %s: %s", errstr, optarg); break; case 'm': minfree = strtonum(optarg, 0, 99, &errstr); if (errstr) fatal("free space %% is %s: %s", errstr, optarg); break; case 'o': if (mfs) getmntopts(optarg, mopts, &mntflags); else { if (strcmp(optarg, "space") == 0) reqopt = opt = FS_OPTSPACE; else if (strcmp(optarg, "time") == 0) reqopt = opt = FS_OPTTIME; else fatal("%s: unknown optimization " "preference: use `space' or `time'.", optarg); } break; case 'q': quiet = 1; break; case 's': if (scan_scaled(optarg, &fssize_input) == -1 || fssize_input <= 0) fatal("file system size invalid: %s", optarg); fssize_usebytes = 0; /* in case of multiple -s */ for (s1 = optarg; *s1 != '\0'; s1++) if (isalpha((unsigned char)*s1)) { fssize_usebytes = 1; break; } break; case 't': fstype = optarg; if (strcmp(fstype, "ffs")) ffsflag = 0; break; #ifdef MFS case 'P': pop = optarg; break; #endif case '?': default: usage(); } if (!ffsflag) break; } argc -= optind; argv += optind; if (ffsflag && argc - mfs != 1) usage(); if (mfs) { /* Increase our data size to the max */ if (getrlimit(RLIMIT_DATA, &rl) == 0) { rl.rlim_cur = rl.rlim_max; (void)setrlimit(RLIMIT_DATA, &rl); } } special = argv[0]; if (!mfs) { char execname[PATH_MAX], name[PATH_MAX]; if (fstype == NULL) fstype = readlabelfs(special, 0); if (fstype != NULL && strcmp(fstype, "ffs")) { snprintf(name, sizeof name, "newfs_%s", fstype); saveargv[0] = name; snprintf(execname, sizeof execname, "%s/newfs_%s", _PATH_SBIN, fstype); (void)execv(execname, saveargv); snprintf(execname, sizeof execname, "%s/newfs_%s", _PATH_USRSBIN, fstype); (void)execv(execname, saveargv); err(1, "%s not found", name); } } if (mfs && !strcmp(special, "swap")) { /* * it's an MFS, mounted on "swap." fake up a label. * XXX XXX XXX */ fso = -1; /* XXX; normally done below. */ memset(&mfsfakelabel, 0, sizeof(mfsfakelabel)); mfsfakelabel.d_secsize = 512; mfsfakelabel.d_nsectors = 64; mfsfakelabel.d_ntracks = 16; mfsfakelabel.d_ncylinders = 16; mfsfakelabel.d_secpercyl = 1024; DL_SETDSIZE(&mfsfakelabel, 16384); mfsfakelabel.d_npartitions = 1; mfsfakelabel.d_version = 1; DL_SETPSIZE(&mfsfakelabel.d_partitions[0], 16384); mfsfakelabel.d_partitions[0].p_fragblock = DISKLABELV1_FFS_FRAGBLOCK(1024, 8); mfsfakelabel.d_partitions[0].p_cpg = 16; lp = &mfsfakelabel; pp = &mfsfakelabel.d_partitions[0]; goto havelabel; } if (Nflag) { fso = -1; } else { fso = opendev(special, O_WRONLY, 0, &realdev); if (fso < 0) fatal("%s: %s", special, strerror(errno)); special = realdev; /* Bail if target special is mounted */ n = getmntinfo(&mp, MNT_NOWAIT); if (n == 0) fatal("%s: getmntinfo: %s", special, strerror(errno)); len = sizeof(_PATH_DEV) - 1; s1 = special; if (strncmp(_PATH_DEV, s1, len) == 0) s1 += len; while (--n >= 0) { s2 = mp->f_mntfromname; if (strncmp(_PATH_DEV, s2, len) == 0) { s2 += len - 1; *s2 = 'r'; } if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) fatal("%s is mounted on %s", special, mp->f_mntonname); ++mp; } } if (mfs && disktype != NULL) { lp = (struct disklabel *)getdiskbyname(disktype); if (lp == NULL) fatal("%s: unknown disk type", disktype); pp = &lp->d_partitions[1]; } else { fsi = opendev(special, O_RDONLY, 0, NULL); if (fsi < 0) fatal("%s: %s", special, strerror(errno)); if (fstat(fsi, &st) < 0) fatal("%s: %s", special, strerror(errno)); if (!mfs) { if (S_ISBLK(st.st_mode)) fatal("%s: block device", special); if (!S_ISCHR(st.st_mode)) warnx("%s: not a character-special device", special); } if (*argv[0] == '\0') fatal("empty partition name supplied"); cp = argv[0] + strlen(argv[0]) - 1; if ((*cp < 'a' || *cp > ('a' + maxpartitions - 1)) && !isdigit((unsigned char)*cp)) fatal("%s: can't figure out file system partition", argv[0]); lp = getdisklabel(special, fsi); if (!mfs) { if (pledge("stdio disklabel tty", NULL) == -1) err(1, "pledge"); } if (isdigit((unsigned char)*cp)) pp = &lp->d_partitions[0]; else pp = &lp->d_partitions[*cp - 'a']; if (DL_GETPSIZE(pp) == 0) fatal("%s: `%c' partition is unavailable", argv[0], *cp); if (pp->p_fstype == FS_BOOT) fatal("%s: `%c' partition overlaps boot program", argv[0], *cp); } havelabel: if (sectorsize == 0) { sectorsize = lp->d_secsize; if (sectorsize <= 0) fatal("%s: no default sector size", argv[0]); } if (fssize_usebytes) { nsecs = fssize_input / sectorsize; if (fssize_input % sectorsize != 0) nsecs++; } else if (fssize_input == 0) nsecs = DL_GETPSIZE(pp); else nsecs = fssize_input; if (nsecs > DL_GETPSIZE(pp) && !mfs) fatal("%s: maximum file system size on the `%c' partition is " "%llu sectors", argv[0], *cp, DL_GETPSIZE(pp)); /* Can't use DL_SECTOBLK() because sectorsize may not be from label! */ fssize = nsecs * (sectorsize / DEV_BSIZE); if (oflagset == 0 && fssize >= INT_MAX) Oflag = 2; /* FFS2 */ if (fsize == 0) { fsize = DISKLABELV1_FFS_FSIZE(pp->p_fragblock); if (fsize <= 0) fsize = MAXIMUM(DFL_FRAGSIZE, lp->d_secsize); } if (bsize == 0) { bsize = DISKLABELV1_FFS_BSIZE(pp->p_fragblock); if (bsize <= 0) bsize = MINIMUM(DFL_BLKSIZE, 8 * fsize); } if (density == 0) density = NFPI * fsize; if (minfree < MINFREE && opt != FS_OPTSPACE && reqopt == -1) { warnx("warning: changing optimization to space " "because minfree is less than %d%%\n", MINFREE); opt = FS_OPTSPACE; } if (maxbpg == 0) { if (Oflag <= 1) maxbpg = MAXBLKPG_FFS1(bsize); else maxbpg = MAXBLKPG_FFS2(bsize); } oldpartition = *pp; #ifdef MFS if (mfs) { if (realpath(argv[1], node) == NULL) err(1, "realpath %s", argv[1]); if (stat(node, &mountpoint) < 0) err(ECANCELED, "stat %s", node); mfsuid = mountpoint.st_uid; mfsgid = mountpoint.st_gid; mfsmode = mountpoint.st_mode & ALLPERMS; } #endif mkfs(pp, special, fsi, fso, mfsmode, mfsuid, mfsgid); if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition))) rewritelabel(special, fso, lp); if (!Nflag) close(fso); close(fsi); #ifdef MFS if (mfs) { struct mfs_args args; memset(&args, 0, sizeof(args)); args.base = membase; args.size = fssize * DEV_BSIZE; args.export_info.ex_root = -2; if (mntflags & MNT_RDONLY) args.export_info.ex_flags = MNT_EXRDONLY; switch (pid = fork()) { case -1: err(10, "mfs"); case 0: snprintf(mountfromname, sizeof(mountfromname), "mfs:%d", getpid()); break; default: snprintf(mountfromname, sizeof(mountfromname), "mfs:%d", pid); for (;;) { /* * spin until the mount succeeds * or the child exits */ usleep(1); /* * XXX Here is a race condition: another process * can mount a filesystem which hides our * ramdisk before we see the success. */ if (statfs(node, &sf) < 0) err(ECANCELED, "statfs %s", node); if (!strcmp(sf.f_mntfromname, mountfromname) && !strncmp(sf.f_mntonname, node, MNAMELEN) && !strcmp(sf.f_fstypename, "mfs")) { if (pop != NULL) copy(pop, node, &args); exit(0); } res = waitpid(pid, &status, WNOHANG); if (res == -1) err(EDEADLK, "waitpid"); if (res != pid) continue; if (WIFEXITED(status)) { if (WEXITSTATUS(status) == 0) exit(0); errx(1, "%s: mount: %s", node, strerror(WEXITSTATUS(status))); } else errx(EDEADLK, "abnormal termination"); } /* NOTREACHED */ } (void) setsid(); (void) close(0); (void) close(1); (void) close(2); (void) chdir("/"); args.fspec = mountfromname; if (mntflags & MNT_RDONLY && pop != NULL) mntflags &= ~MNT_RDONLY; if (mount(MOUNT_MFS, node, mntflags, &args) < 0) exit(errno); /* parent prints message */ } #endif exit(0); }
int main(int argc, char **argv) { FILE *fs; int root_ino; int len; int ch; extern int optind; extern char *optarg; fs = stdout; /* Send constructed file system to stdout by default */ while ((ch = getopt(argc, argv, "qa:d:e:v::o:h?")) != -1) { switch((char)ch) { case 'd': len = strlen(optarg); root_directory_name = (char *)malloc(len + 2); memcpy(root_directory_name, optarg, len); if (root_directory_name[len - 1] != '/') { root_directory_name[len++] = '/'; } root_directory_name[len] = '\0'; break; case 'v': if (!optarg || strlen(optarg) == 0) { verbose = 1; } else if (strlen(optarg) > 1 || !isdigit(optarg[0])) { fprintf(stderr, "verbose level must be between 0 and 9!\n"); usage(); exit(1); } else { verbose = strtol(optarg, NULL, 0); } break; case 'o': fs = fopen(optarg, "w"); if (!fs) { fprintf(stderr, "unable to open file %s for output.\n", optarg); exit(1); } break; case 'a': if (strcmp(optarg, "little") == 0) { endian = ENDIAN_LITTLE; } else if (strcmp(optarg, "big") == 0) { endian = ENDIAN_BIG; } else { usage(); exit(1); } break; case 'e': MAX_CHUNK_SIZE = strtol(optarg, NULL, 0) / 2; break; case 'q': squash = 1; break; case 'h': case '?': default: usage(); exit(0); } } if ((argc -= optind)) { usage(); exit(1); } if (root_directory_name == NULL) { fprintf(stderr, "Error: must specify a root directory\n"); usage(); exit(1); } if (verbose >= 1) { fprintf(stderr, "Constructing JFFS filesystem...\n"); } root_ino = make_root_dir(fs, JFFS_MIN_INO, root_directory_name, 0); mkfs(fs, root_directory_name, root_ino + 1, root_ino, 1); fclose(fs); free(root_directory_name); exit(0); }
/* This is the routine that constructs the filesystem image. */ int mkfs(FILE *fs, const char *path, int ino, int parent, int depth) { struct dirent *dir_entry; DIR *dir; struct stat st; struct jffs_file f; int name_len; int pos = 0; int new_ino = ino; char *filename; int path_len = strlen(path); if (verbose >= 2) { fprintf(stderr, "***mkfs(): path: \"%s\"\r\n", path); } if (!(dir = opendir(path))) { perror("opendir"); fprintf(stderr, "mkfs(): opendir() failed! (%s)\n", path); exit(1); } while ((dir_entry = readdir(dir))) { if (verbose >= 2) { fprintf(stderr, "mkfs(): name: %s\n", dir_entry->d_name); } name_len = strlen(dir_entry->d_name); if (((name_len == 1) && (dir_entry->d_name[0] == '.')) || ((name_len == 2) && (dir_entry->d_name[0] == '.') && (dir_entry->d_name[1] == '.'))) { continue; } if (!(filename = (char *)alloca(path_len + name_len + 1))) { fprintf(stderr, "mkfs(): Allocation failed!\n"); exit(0); } strcpy(filename, path); strcat(filename, dir_entry->d_name); if (verbose >= 2) { fprintf(stderr, "mkfs(): filename: %s\n", filename); } if (lstat(filename, &st) < 0) { perror("lstat"); exit(1); } if (verbose >= 2) { fprintf(stderr, "mkfs(): filename: \"%s\", ino: %d, parent: %d\n", filename, new_ino, parent); } if (S_ISREG(st.st_mode) && st.st_size == 0) { char devname[32]; char type; int major; int minor; if (sscanf(dir_entry->d_name, "@%31[-a-zA-Z0-9_+],%c,%d,%d", devname, &type, &major, &minor) == 4) { strcpy(dir_entry->d_name, devname); st.st_rdev = makedev(major, minor); st.st_mode &= ~S_IFMT; switch (type) { case 'c': case 'u': st.st_mode |= S_IFCHR; break; case 'b': st.st_mode |= S_IFBLK; break; case 'p': st.st_mode |= S_IFIFO; break; default: fprintf(stderr, "mkfs(): invalid special device type '%c' for file %s\n", type, filename); exit(1); } } } if (squash) { st.st_uid = 0; st.st_gid = 0; } write_val32(&f.inode.magic, JFFS_MAGIC); write_val32(&f.inode.ino, new_ino); write_val32(&f.inode.pino, parent); write_val32(&f.inode.version, 1); write_val32(&f.inode.mode, st.st_mode); write_val16(&f.inode.uid, st.st_uid); write_val16(&f.inode.gid, st.st_gid); write_val32(&f.inode.atime, st.st_atime); write_val32(&f.inode.mtime, st.st_mtime); write_val32(&f.inode.ctime, st.st_ctime); write_val32(&f.inode.dsize, 0); write_val32(&f.inode.rsize, 0); f.inode.nsize = name_len; /*f.inode.nlink = st.st_nlink;*/ f.inode.nlink = 1; f.inode.spare = 0; f.inode.rename = 0; f.inode.deleted = 0; f.inode.accurate = 0; write_val32(&f.inode.dchksum, 0); write_val16(&f.inode.nchksum, 0); write_val16(&f.inode.chksum, 0); if (dir_entry->d_name) { f.name = strdup(dir_entry->d_name); } else { f.name = 0; } repeat: write_val32(&f.inode.offset, pos); f.data = 0; f.inode.accurate = 0; if (S_ISREG(st.st_mode) && st.st_size) { if (st.st_size - pos < MAX_CHUNK_SIZE) { write_val32(&f.inode.dsize, st.st_size - pos); } else { write_val32(&f.inode.dsize, MAX_CHUNK_SIZE); } read_data(&f, path, pos); pos += read_val32(&f.inode.dsize); } else if (S_ISLNK(st.st_mode)) { int linklen; unsigned char *linkdata = (unsigned char *)malloc(1000); if (!linkdata) { fprintf(stderr, "mkfs(): malloc() failed! (linkdata)\n"); exit(1); } if ((linklen = readlink(filename, linkdata, 1000)) < 0) { free(linkdata); fprintf(stderr, "mkfs(): readlink() failed! f.name = \"%s\"\n", f.name); exit(1); } write_val32(&f.inode.dsize, linklen); f.data = linkdata; f.data[linklen] = '\0'; } else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) { write_val32(&f.inode.dsize, sizeof(st.st_rdev) / 4); } write_val16(&f.inode.chksum, 0); if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) { write_val32(&f.inode.dchksum, jffs_checksum((void *)f.data, read_val32(&f.inode.dsize))); } else { write_val32(&f.inode.dchksum, jffs_checksum((void *)&st.st_rdev, sizeof(st.st_rdev) / 4)); } write_val16(&f.inode.nchksum, jffs_checksum((void *)f.name, f.inode.nsize)); write_val16(&f.inode.chksum, jffs_checksum((void *)&f.inode, sizeof(struct jffs_raw_inode))); f.inode.accurate = 0xff; write_file(&f, fs, st); if (S_ISREG(st.st_mode) && st.st_size) { if (pos < st.st_size) { write_val32(&f.inode.version, read_val32(&f.inode.version) + 1); goto repeat; } } new_ino++; pos = 0; if (verbose >= 1) { jffs_print_trace(f.name, depth); } if (verbose >= 2) { jffs_print_raw_inode(&f.inode); } if (S_ISDIR(st.st_mode)) { char *new_path; if (!(new_path = (char *)alloca(strlen(path) + name_len + 1 + 1))) { fprintf(stderr, "mkfs(): alloca() failed! (new_path)\n"); exit(1); } strcpy(new_path, path); strncat(new_path, f.name, f.inode.nsize); strcat(new_path, "/"); if (verbose >= 2) { fprintf(stderr, "mkfs(): new_path: \"%s\"\n", new_path); } new_ino = mkfs(fs, new_path, new_ino, new_ino - 1, depth + 1); } if (f.name) { free(f.name); } if (f.data) { free(f.data); } } closedir(dir); return new_ino; }
int main(int argc, char *argv[]) { struct partition *pp; struct disklabel *lp; struct partition oldpartition; struct stat st; char *cp, *special; intmax_t reserved; int ch, i, rval; off_t mediasize; char part_name; /* partition name, default to full disk */ part_name = 'c'; reserved = 0; while ((ch = getopt(argc, argv, "EJL:NO:RS:T:UXa:b:c:d:e:f:g:h:i:jlm:no:p:r:s:t")) != -1) switch (ch) { case 'E': Eflag = 1; break; case 'J': Jflag = 1; break; case 'L': volumelabel = optarg; i = -1; while (isalnum(volumelabel[++i])); if (volumelabel[i] != '\0') { errx(1, "bad volume label. Valid characters are alphanumerics."); } if (strlen(volumelabel) >= MAXVOLLEN) { errx(1, "bad volume label. Length is longer than %d.", MAXVOLLEN); } Lflag = 1; break; case 'N': Nflag = 1; break; case 'O': if ((Oflag = atoi(optarg)) < 1 || Oflag > 2) errx(1, "%s: bad file system format value", optarg); break; case 'R': Rflag = 1; break; case 'S': rval = expand_number_int(optarg, §orsize); if (rval < 0 || sectorsize <= 0) errx(1, "%s: bad sector size", optarg); break; case 'T': disktype = optarg; break; case 'j': jflag = 1; /* fall through to enable soft updates */ case 'U': Uflag = 1; break; case 'X': Xflag++; break; case 'a': rval = expand_number_int(optarg, &maxcontig); if (rval < 0 || maxcontig <= 0) errx(1, "%s: bad maximum contiguous blocks", optarg); break; case 'b': rval = expand_number_int(optarg, &bsize); if (rval < 0) errx(1, "%s: bad block size", optarg); if (bsize < MINBSIZE) errx(1, "%s: block size too small, min is %d", optarg, MINBSIZE); if (bsize > MAXBSIZE) errx(1, "%s: block size too large, max is %d", optarg, MAXBSIZE); break; case 'c': rval = expand_number_int(optarg, &maxblkspercg); if (rval < 0 || maxblkspercg <= 0) errx(1, "%s: bad blocks per cylinder group", optarg); break; case 'd': rval = expand_number_int(optarg, &maxbsize); if (rval < 0 || maxbsize < MINBSIZE) errx(1, "%s: bad extent block size", optarg); break; case 'e': rval = expand_number_int(optarg, &maxbpg); if (rval < 0 || maxbpg <= 0) errx(1, "%s: bad blocks per file in a cylinder group", optarg); break; case 'f': rval = expand_number_int(optarg, &fsize); if (rval < 0 || fsize <= 0) errx(1, "%s: bad fragment size", optarg); break; case 'g': rval = expand_number_int(optarg, &avgfilesize); if (rval < 0 || avgfilesize <= 0) errx(1, "%s: bad average file size", optarg); break; case 'h': rval = expand_number_int(optarg, &avgfilesperdir); if (rval < 0 || avgfilesperdir <= 0) errx(1, "%s: bad average files per dir", optarg); break; case 'i': rval = expand_number_int(optarg, &density); if (rval < 0 || density <= 0) errx(1, "%s: bad bytes per inode", optarg); break; case 'l': lflag = 1; break; case 'm': if ((minfree = atoi(optarg)) < 0 || minfree > 99) errx(1, "%s: bad free space %%", optarg); break; case 'n': nflag = 1; break; case 'o': if (strcmp(optarg, "space") == 0) opt = FS_OPTSPACE; else if (strcmp(optarg, "time") == 0) opt = FS_OPTTIME; else errx(1, "%s: unknown optimization preference: use `space' or `time'", optarg); break; case 'r': errno = 0; reserved = strtoimax(optarg, &cp, 0); if (errno != 0 || cp == optarg || *cp != '\0' || reserved < 0) errx(1, "%s: bad reserved size", optarg); break; case 'p': is_file = 1; part_name = optarg[0]; break; case 's': errno = 0; fssize = strtoimax(optarg, &cp, 0); if (errno != 0 || cp == optarg || *cp != '\0' || fssize < 0) errx(1, "%s: bad file system size", optarg); break; case 't': tflag = 1; break; case '?': default: usage(); } argc -= optind; argv += optind; if (argc != 1) usage(); special = argv[0]; if (!special[0]) err(1, "empty file/special name"); cp = strrchr(special, '/'); if (cp == 0) { /* * No path prefix; try prefixing _PATH_DEV. */ snprintf(device, sizeof(device), "%s%s", _PATH_DEV, special); special = device; } if (is_file) { /* bypass ufs_disk_fillout_blank */ bzero( &disk, sizeof(disk)); disk.d_bsize = 1; disk.d_name = special; disk.d_fd = open(special, O_RDONLY); if (disk.d_fd < 0 || (!Nflag && ufs_disk_write(&disk) == -1)) errx(1, "%s: ", special); } else if (ufs_disk_fillout_blank(&disk, special) == -1 || (!Nflag && ufs_disk_write(&disk) == -1)) { if (disk.d_error != NULL) errx(1, "%s: %s", special, disk.d_error); else err(1, "%s", special); } if (fstat(disk.d_fd, &st) < 0) err(1, "%s", special); if ((st.st_mode & S_IFMT) != S_IFCHR) { warn("%s: not a character-special device", special); is_file = 1; /* assume it is a file */ dkname = special; if (sectorsize == 0) sectorsize = 512; mediasize = st.st_size; /* set fssize from the partition */ } else { if (sectorsize == 0) if (ioctl(disk.d_fd, DIOCGSECTORSIZE, §orsize) == -1) sectorsize = 0; /* back out on error for safety */ if (sectorsize && ioctl(disk.d_fd, DIOCGMEDIASIZE, &mediasize) != -1) getfssize(&fssize, special, mediasize / sectorsize, reserved); } pp = NULL; lp = getdisklabel(special); if (lp != NULL) { if (!is_file) /* already set for files */ part_name = special[strlen(special) - 1]; if ((part_name < 'a' || part_name - 'a' >= MAXPARTITIONS) && !isdigit(part_name)) errx(1, "%s: can't figure out file system partition", special); cp = &part_name; if (isdigit(*cp)) pp = &lp->d_partitions[RAW_PART]; else pp = &lp->d_partitions[*cp - 'a']; oldpartition = *pp; if (pp->p_size == 0) errx(1, "%s: `%c' partition is unavailable", special, *cp); if (pp->p_fstype == FS_BOOT) errx(1, "%s: `%c' partition overlaps boot program", special, *cp); getfssize(&fssize, special, pp->p_size, reserved); if (sectorsize == 0) sectorsize = lp->d_secsize; if (fsize == 0) fsize = pp->p_fsize; if (bsize == 0) bsize = pp->p_frag * pp->p_fsize; if (is_file) part_ofs = pp->p_offset; } if (sectorsize <= 0) errx(1, "%s: no default sector size", special); if (fsize <= 0) fsize = MAX(DFL_FRAGSIZE, sectorsize); if (bsize <= 0) bsize = MIN(DFL_BLKSIZE, 8 * fsize); if (minfree < MINFREE && opt != FS_OPTSPACE) { fprintf(stderr, "Warning: changing optimization to space "); fprintf(stderr, "because minfree is less than %d%%\n", MINFREE); opt = FS_OPTSPACE; } realsectorsize = sectorsize; if (sectorsize != DEV_BSIZE) { /* XXX */ int secperblk = sectorsize / DEV_BSIZE; sectorsize = DEV_BSIZE; fssize *= secperblk; if (pp != NULL) pp->p_size *= secperblk; } mkfs(pp, special); if (!unlabeled) { if (realsectorsize != DEV_BSIZE) pp->p_size /= realsectorsize / DEV_BSIZE; if (!Nflag && bcmp(pp, &oldpartition, sizeof(oldpartition))) rewritelabel(special, lp); } ufs_disk_close(&disk); if (!jflag) exit(0); if (execlp("tunefs", "newfs", "-j", "enable", special, NULL) < 0) err(1, "Cannot enable soft updates journaling, tunefs"); /* NOT REACHED */ }
/** * @ingroup shell * * Shell command fstest. * @param nargs number of arguments in args array * @param args array of arguments * @return OK for success, SYSERR for syntax error */ shellcmd xsh_fstest(int nargs, char *args[]) { int rval; int fd, i, j; char *buf1, *buf2; /* Output help, if '--help' argument was supplied */ if (nargs == 2 && strncmp(args[1], "--help", 7) == 0) { printf("Usage: %s\n\n", args[0]); printf("Description:\n"); printf("\tFilesystem Test\n"); printf("Options:\n"); printf("\t--help\tdisplay this help and exit\n"); return OK; } /* Check for correct number of arguments */ if (nargs > 1) { fprintf(stderr, "%s: too many arguments\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return SYSERR; } if (nargs < 1) { fprintf(stderr, "%s: too few arguments\n", args[0]); fprintf(stderr, "Try '%s --help' for more information\n", args[0]); return SYSERR; } #ifdef FS mkbsdev(0, 0, 0); /* device "0" and default blocksize (=0) and count */ mkfs(0,DEFAULT_NUM_INODES); /* bsdev 0*/ //testbitmask(); buf1 = memget(SIZE*sizeof(char)); buf2 = memget(SIZE*sizeof(char)); fd = fcreat("a", O_CREAT); for(i=0; i<SIZE; i++) { j = i%(127-33); j = j+33; buf1[i] = (char) j; } rval = fwrite(fd,buf1,SIZE); if(rval == 0) { printf("\n\r File write failed"); } fseek(fd,-rval); rval = fread(fd, buf2, rval); buf2[rval] = '\0'; if(rval == 0) { printf("\n\r File read failed"); } printf("\n\rContent of file %s",buf2); rval = fclose(fd); if(rval != OK) { printf("\n\rReturn val for fclose : %d",rval); } memfree(buf1,SIZE); memfree(buf2,SIZE); #else printf("No filesystem support\n"); #endif return OK; }
/* This is the routine that constructs the filesystem image. */ int mkfs(FILE *fs, const char *path, int ino, int parent, int depth) { struct dirent *dir_entry; DIR *dir; struct stat st; struct jffs_file f; int name_len; int pos = 0; int new_ino = ino; char *filename; int path_len = strlen(path); __u16 chksum; if (verbose >= 2) { fprintf(stderr, "***mkfs(): path: \"%s\"\r\n", path); } if (!(dir = opendir(path))) { perror("opendir"); fprintf(stderr, "mkfs(): opendir() failed! (%s)\n", path); exit(1); } while ((dir_entry = readdir(dir))) { if (verbose >= 2) { fprintf(stderr, "mkfs(): name: %s\n", dir_entry->d_name); } name_len = strlen(dir_entry->d_name); if (((name_len == 1) && (dir_entry->d_name[0] == '.')) || ((name_len == 2) && (dir_entry->d_name[0] == '.') && (dir_entry->d_name[1] == '.'))) { continue; } if (!(filename = (char *)alloca(path_len + name_len + 1))) { fprintf(stderr, "mkfs(): Allocation failed!\n"); exit(0); } strcpy(filename, path); strcat(filename, dir_entry->d_name); if (verbose >= 2) { fprintf(stderr, "mkfs(): filename: %s\n", filename); } if (lstat(filename, &st) < 0) { perror("lstat"); exit(1); } if (verbose >= 2) { fprintf(stderr, "mkfs(): filename: \"%s\", ino: %d, parent: %d\n", filename, new_ino, parent); } f.inode.magic = JFFS_MAGIC; f.inode.ino = new_ino; f.inode.pino = parent; f.inode.version = 1; f.inode.mode = st.st_mode; f.inode.uid = st.st_uid; f.inode.gid = st.st_gid; f.inode.atime = st.st_atime; f.inode.mtime = st.st_mtime; f.inode.ctime = st.st_ctime; f.inode.dsize = 0; f.inode.rsize = 0; f.inode.nsize = name_len; /*f.inode.nlink = st.st_nlink;*/ f.inode.nlink = 1; f.inode.spare = 0; f.inode.rename = 0; f.inode.deleted = 0; f.inode.accurate = 0; f.inode.dchksum = 0; f.inode.nchksum = 0; f.inode.chksum = 0; if (dir_entry->d_name) { f.name = strdup(dir_entry->d_name); } else { f.name = 0; } repeat: f.inode.offset = pos; f.data = 0; f.inode.accurate = 0; if (S_ISREG(st.st_mode) && st.st_size) { if (st.st_size - pos < MAX_CHUNK_SIZE) { f.inode.dsize = st.st_size - pos; } else { f.inode.dsize = MAX_CHUNK_SIZE; } read_data(&f, path, pos); pos += f.inode.dsize; } else if (S_ISLNK(st.st_mode)) { int linklen; unsigned char *linkdata = (unsigned char *)malloc(1000); if (!linkdata) { fprintf(stderr, "mkfs(): malloc() failed! (linkdata)\n"); exit(1); } if ((linklen = readlink(filename, linkdata, 1000)) < 0) { free(linkdata); fprintf(stderr, "mkfs(): readlink() failed! f.name = \"%s\"\n", f.name); exit(1); } f.inode.dsize = linklen; f.data = linkdata; f.data[linklen] = '\0'; } else if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) { f.inode.dsize = sizeof(st.st_rdev) / 4; } f.inode.chksum = 0; if (!S_ISBLK(st.st_mode) && !S_ISCHR(st.st_mode)) { f.inode.dchksum = jffs_checksum((void *)f.data, f.inode.dsize); } else { f.inode.dchksum = jffs_checksum((void *)&st.st_rdev, sizeof(st.st_rdev) / 4); } f.inode.nchksum = jffs_checksum((void *)f.name, f.inode.nsize); jffs_swap_inode(&f.inode); chksum = jffs_checksum((void *)&f.inode, sizeof(struct jffs_raw_inode)); jffs_swap_inode(&f.inode); f.inode.chksum = chksum; f.inode.accurate = 0xff; write_file(&f, fs, st); if (S_ISREG(st.st_mode) && st.st_size) { if (pos < st.st_size) { f.inode.version++; goto repeat; } } new_ino++; pos = 0; if (verbose >= 1) { jffs_print_trace(f.name, depth); } if (verbose >= 2) { jffs_print_raw_inode(&f.inode); } if (S_ISDIR(st.st_mode)) { char *new_path; if (!(new_path = (char *)alloca(strlen(path) + name_len + 1 + 1))) { fprintf(stderr, "mkfs(): alloca() failed! (new_path)\n"); exit(1); } strcpy(new_path, path); strncat(new_path, f.name, f.inode.nsize); strcat(new_path, "/"); if (verbose >= 2) { fprintf(stderr, "mkfs(): new_path: \"%s\"\n", new_path); } new_ino = mkfs(fs, new_path, new_ino, new_ino - 1, depth + 1); } if (f.name) { free(f.name); } if (f.data) { free(f.data); } } closedir(dir); return new_ino; }
int main() { Filesystem filesystem; char line[LINE_MAX]= "", command[WORD_MAX]= "", temp[WORD_MAX], arg1[WORD_MAX]= "", arg2[WORD_MAX]= "", prompt[WORD_MAX]= "%"; int verbose= 0, length= 0, num_matched= 0, argument_error, done= 0; setup_memory_checking(); printf("%s ", prompt); /* continue reading lines until the end of the input */ while (!done && fgets(line, sizeof(line) - sizeof(*line), stdin) != NULL) { temp[0]= '\0'; sscanf(line, "%[ \t\n]%n", temp, &length); if (strcmp(temp, "") != 0 && length == strlen(temp)) printf("%s ", prompt); /* ignore empty input lines */ else { /* num_matched is the number of format specifiers that were matched */ num_matched= sscanf(line, "%s %s %s %s", command, arg1, arg2, temp); if (verbose == 1) printf("%s", line); /* argument_error will be 1 if the wrong number of arguments are given for any command */ argument_error= 0; switch(command_idx(command)) { case LOGOUT: case EXIT: if (num_matched == 1) done= 1; /* also quit if the command entered is logout or exit */ else argument_error= 1; break; /* call mkfs() if the line began with "mkfs" with no following arguments */ case MKFS: if (num_matched == 1) mkfs(&filesystem); else argument_error= 1; break; /* call touch() if the line began with "touch" and had one following argument; if touch() returns -1 print an appropriate error message */ case TOUCH: if (num_matched != 2) argument_error= 1; else if (touch(&filesystem, arg1) == -1) printf("Missing or invalid operand.\n"); break; /* call mkdir() if the line began with "mkdir" and had one following argument; if mkdir() returns -1 or -2 print an appropriate error message */ case MKDIR: if (num_matched != 2) argument_error= 1; else switch (mkdir(&filesystem, arg1)) { case -1: printf("Missing or invalid operand.\n"); break; case -2: printf("Cannot create directory %s: File exists.\n", arg1); break; default: break; /* no-op; 0 return is expected */ } break; /* call cd() if the line began with "cd" and had one following argument; if cd() returns -1 or -2 print an appropriate error message */ case CD: if (num_matched != 1 && num_matched != 2) argument_error= 1; else switch (cd(&filesystem, arg1)) { case -1: printf("%s: No such file or directory.\n", arg1); break; case -2: printf("%s: Not a directory.\n", arg1); break; default: break; /* no-op; 0 return is expected */ } break; /* call ls() if the line began with "ls" and had one following argument; if ls() returns -1 print an appropriate error message */ case LS: if (num_matched != 1 && num_matched != 2) argument_error= 1; else if (ls(filesystem, arg1) == -1) printf("%s: No such file or directory.\n", arg1); break; /* call pwd() if the line began with "pwd" with no following arguments */ case PWD: if (num_matched == 1) pwd(filesystem); else argument_error= 1; break; /* call rm() if the line began with "rm" and had one following argument; if rm() returns -1, -2, or -3 print an appropriate error message */ case RM: if (num_matched != 2) argument_error= 1; else switch (rm(&filesystem, arg1)) { case -1: printf("%s: No such file or directory.\n", arg1); break; case -2: printf("Cannot remove directory '%s'.\n", arg1); break; case -3: printf("Missing or invalid operand.\n"); break; default: break; /* no-op; 0 return is expected */ } break; /* call re_name() if the line began with "rename" with two following arguments; if it returns an error code (-1 through -4) print an appopriate error message */ case RENAME: if (num_matched != 3) argument_error= 1; else switch (re_name(&filesystem, arg1, arg2)) { case -1: printf("%s: No such file or directory.\n", arg1); break; case -2: printf("Missing or invalid operand.\n"); break; case -3: printf("File or directory %s already exists.\n", arg1); break; case -4: printf("%s and %s are the same file.\n", arg1, arg2); break; default: break; /* no-op; 0 return is expected */ } break; /* call rmfs() if the line began with "rmfs" with no following arguments */ case RMFS: if (num_matched == 1) rmfs(&filesystem); else argument_error= 1; break; /* the variable verbose is set to 1 if the "set verbose" command is entered. */ case SET: if (num_matched == 2 && strcmp(arg1, "verbose") == 0) verbose= 1; else argument_error= 1; break; /* the variable verbose is set to 0 if "unset verbose" is entered. */ case UNSET: if (num_matched == 2 && strcmp(arg1, "verbose") == 0) verbose= 0; else argument_error= 1; break; /* error message for a command not matching one of the function names */ default: printf("%s: Command not found.\n", command); break; } /* error message for a command with the wrong number of arguments */ if (argument_error) printf("Invalid arguments.\n"); if (verbose == 1) printf("\n"); printf("%s ", prompt); /* sscanf() will fail if the line read is empty, in which case the prior command will be executed again unless it's first cleared out */ arg1[0]= arg2[0]= command[0]= '\0'; } } check_memory_leak(); return 0; }
int main() { Filesystem filesystem; printf("Creating filesystem\n"); mkfs(&filesystem); printf("pwd: "); pwd(&filesystem); printf("Creating directory animals ad gods\n"); mkdir(&filesystem, "animals"); mkdir(&filesystem, "god"); printf("Printing current\n"); ls(&filesystem,"."); printf("Printing root\n"); ls(&filesystem,"/"); printf("changing directory to animals: "); printf("%d\n",cd(&filesystem,"animals")); pwd(&filesystem); mkdir(&filesystem, "rabbit"); printf("printing cuurrent\n"); ls(&filesystem,"."); printf("printing root\n"); ls(&filesystem,"/"); printf("Changing directory to root: "); printf("%d\n",cd(&filesystem,"/")); cd(&filesystem, "god"); mkdir(&filesystem, "lord"); ls(&filesystem, "."); ls(&filesystem, "/"); printf("---/nBACK TO ROOT/n"); cd(&filesystem,"/"); touch(&filesystem, "hippo"); touch(&filesystem, "rabbit"); printf("\n FIRST ls \n"); ls(&filesystem,""); printf("\n END 1 ls \n\n"); printf("Making directory wild in root\n"); mkdir(&filesystem, "wild"); cd(&filesystem, "wild"); printf("PATH OF WILD NOW IS "); pwd(&filesystem); mkdir(&filesystem, "bear"); mkdir(&filesystem, "lion"); mkdir(&filesystem, "tiger"); cd(&filesystem, "tiger"); printf("PATH OF TIGER INSIDE WILD IS "); pwd(&filesystem); cd(&filesystem, ".."); printf("\nSECOND ls \n"); ls(&filesystem, "."); printf("\nEND SECOND ls \n"); ls(&filesystem,"/"); printf("going up to the root\n"); cd(&filesystem,".."); ls(&filesystem,"."); cd(&filesystem,".."); mkdir(&filesystem, "plants"); cd(&filesystem,"plants"); touch(&filesystem, "flowers"); touch(&filesystem, "roses"); printf("\nTHIRD ls \n"); ls(&filesystem,"."); printf("\nEND THIRD ls \n"); cd(&filesystem,"/"); printf("\nPRINTING ANIMALS \n"); ls(&filesystem,"animals"); printf("\nEND ANIMALS \n"); printf("\nPRINTING PLANTS \n"); ls(&filesystem,"plants"); printf("\nEND PLANTS \n"); return 0; }
int main(int argc, char *argv[]) { int ch; struct partition *pp; struct disklabel *lp; struct partition oldpartition; struct stat st; struct statfs *mp; struct rlimit rl; int fsi = -1, fso, len, n, maxpartitions; char *cp = NULL, *s1, *s2, *special, *opstring, *realdev; char *fstype = NULL; char **saveargv = argv; int ffsflag = 1; const char *errstr; long long fssize_input = 0; int fssize_usebytes = 0; u_int64_t nsecs; getphysmem(); maxpartitions = getmaxpartitions(); if (maxpartitions > 26) fatal("insane maxpartitions value %d", maxpartitions); opstring = "NO:S:T:b:c:e:f:g:h:i:m:o:qs:t:"; while ((ch = getopt(argc, argv, opstring)) != -1) { switch (ch) { case 'N': Nflag = 1; break; case 'O': Oflag = strtonum(optarg, 0, 2, &errstr); if (errstr) fatal("%s: invalid ffs version", optarg); break; case 'S': if (scan_scaled(optarg, §orsize) == -1 || sectorsize <= 0 || (sectorsize % DEV_BSIZE)) fatal("sector size invalid: %s", optarg); break; case 'T': disktype = optarg; break; case 'b': bsize = strtonum(optarg, MINBSIZE, MAXBSIZE, &errstr); if (errstr) fatal("block size is %s: %s", errstr, optarg); break; case 'c': maxfrgspercg = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("fragments per cylinder group is %s: %s", errstr, optarg); break; case 'e': maxbpg = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("blocks per file in a cylinder group is" " %s: %s", errstr, optarg); break; case 'f': fsize = strtonum(optarg, MINBSIZE / MAXFRAG, MAXBSIZE, &errstr); if (errstr) fatal("fragment size is %s: %s", errstr, optarg); break; case 'g': avgfilesize = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("average file size is %s: %s", errstr, optarg); break; case 'h': avgfilesperdir = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("average files per dir is %s: %s", errstr, optarg); break; case 'i': density = strtonum(optarg, 1, INT_MAX, &errstr); if (errstr) fatal("bytes per inode is %s: %s", errstr, optarg); break; case 'm': minfree = strtonum(optarg, 0, 99, &errstr); if (errstr) fatal("free space %% is %s: %s", errstr, optarg); break; case 'o': if (strcmp(optarg, "space") == 0) reqopt = opt = FS_OPTSPACE; else if (strcmp(optarg, "time") == 0) reqopt = opt = FS_OPTTIME; else fatal("%s: unknown optimization preference: " "use `space' or `time'.", optarg); break; case 'q': quiet = 1; break; case 's': if (scan_scaled(optarg, &fssize_input) == -1 || fssize_input == 0) fatal("file system size invalid: %s", optarg); fssize_usebytes = 0; /* in case of multiple -s */ for (s1 = optarg; *s1 != '\0'; s1++) if (isalpha((unsigned char)*s1)) { fssize_usebytes = 1; break; } break; case 't': fstype = optarg; if (strcmp(fstype, "ffs")) ffsflag = 0; break; case '?': default: usage(); } if (!ffsflag) break; } argc -= optind; argv += optind; if (ffsflag && argc != 1) usage(); special = argv[0]; char execname[MAXPATHLEN], name[MAXPATHLEN]; if (fstype == NULL) fstype = readlabelfs(special, 0); if (fstype != NULL && strcmp(fstype, "ffs")) { snprintf(name, sizeof name, "newfs_%s", fstype); saveargv[0] = name; snprintf(execname, sizeof execname, "%s/newfs_%s", _PATH_SBIN, fstype); (void)execv(execname, saveargv); snprintf(execname, sizeof execname, "%s/newfs_%s", _PATH_USRSBIN, fstype); (void)execv(execname, saveargv); err(1, "%s not found", name); } if (Nflag) { fso = -1; } else { fso = opendev(special, O_WRONLY, 0, &realdev); if (fso < 0) fatal("%s: %s", special, strerror(errno)); special = realdev; /* Bail if target special is mounted */ n = getmntinfo(&mp, MNT_NOWAIT); if (n == 0) fatal("%s: getmntinfo: %s", special, strerror(errno)); len = sizeof(_PATH_DEV) - 1; s1 = special; if (strncmp(_PATH_DEV, s1, len) == 0) s1 += len; while (--n >= 0) { s2 = mp->f_mntfromname; if (strncmp(_PATH_DEV, s2, len) == 0) { s2 += len - 1; *s2 = 'r'; } if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) fatal("%s is mounted on %s", special, mp->f_mntonname); ++mp; } } fsi = opendev(special, O_RDONLY, 0, NULL); if (fsi < 0) fatal("%s: %s", special, strerror(errno)); if (fstat(fsi, &st) < 0) fatal("%s: %s", special, strerror(errno)); if (S_ISBLK(st.st_mode)) fatal("%s: block device", special); if (!S_ISCHR(st.st_mode)) warnx("%s: not a character-special device", special); if (*argv[0] == '\0') fatal("empty partition name supplied"); cp = argv[0] + strlen(argv[0]) - 1; if ((*cp < 'a' || *cp > ('a' + maxpartitions - 1)) && !isdigit((unsigned char)*cp)) fatal("%s: can't figure out file system partition", argv[0]); lp = getdisklabel(special, fsi); if (pledge("stdio disklabel tty", NULL) == -1) err(1, "pledge"); if (isdigit((unsigned char)*cp)) pp = &lp->d_partitions[0]; else pp = &lp->d_partitions[*cp - 'a']; if (DL_GETPSIZE(pp) == 0) fatal("%s: `%c' partition is unavailable", argv[0], *cp); if (pp->p_fstype == FS_BOOT) fatal("%s: `%c' partition overlaps boot program", argv[0], *cp); havelabel: if (sectorsize == 0) { sectorsize = lp->d_secsize; if (sectorsize <= 0) fatal("%s: no default sector size", argv[0]); } if (fssize_input < 0) { #if 1 fatal("-s < 0 not yet implemented"); #else long long gap; /* leave gap at the end of partition */ fssize_input = -fssize_input; if (fssize_usebytes) { gap = (daddr_t)fssize_input / (daddr_t)sectorsize; if ((daddr_t)fssize_input % (daddr_t)sectorsize != 0) gap++; } else gap = fssize_input; if (gap >= DL_GETPSIZE(pp)) fatal("%s: requested gap of %lld sectors on partition " "'%c' is too big", argv[0], gap, *cp); nsecs = DL_GETPSIZE(pp) - gap; #endif } else { if (fssize_usebytes) { nsecs = fssize_input / sectorsize; if (fssize_input % sectorsize != 0) nsecs++; } else if (fssize_input == 0) nsecs = DL_GETPSIZE(pp); else nsecs = fssize_input; } if (nsecs > DL_GETPSIZE(pp)) fatal("%s: maximum file system size on the `%c' partition is " "%llu sectors", argv[0], *cp, DL_GETPSIZE(pp)); /* Can't use DL_SECTOBLK() because sectorsize may not be from label! */ fssize = nsecs * (sectorsize / DEV_BSIZE); if (fsize == 0) { fsize = DISKLABELV1_FFS_FSIZE(pp->p_fragblock); if (fsize <= 0) fsize = MAX(DFL_FRAGSIZE, lp->d_secsize); } if (bsize == 0) { bsize = DISKLABELV1_FFS_BSIZE(pp->p_fragblock); if (bsize <= 0) bsize = MIN(DFL_BLKSIZE, 8 * fsize); } if (density == 0) density = NFPI * fsize; if (minfree < MINFREE && opt != FS_OPTSPACE && reqopt == -1) { warnx("warning: changing optimization to space " "because minfree is less than %d%%\n", MINFREE); opt = FS_OPTSPACE; } if (maxbpg == 0) { if (Oflag <= 1) maxbpg = MAXBLKPG_FFS1(bsize); else maxbpg = MAXBLKPG_FFS2(bsize); } oldpartition = *pp; mkfs(pp, special, fsi, fso); if (!Nflag && memcmp(pp, &oldpartition, sizeof(oldpartition))) rewritelabel(special, fso, lp); if (!Nflag) close(fso); close(fsi); exit(0); }
int main(int argc, char **argv) { FILE *fs; int root_ino; int len; switch (argc) { case 1: fprintf(stderr, "Too few arguments!\n"); usage(); exit(0); case 2: if ((strlen(argv[1]) <= 2) || (argv[1][0] != '-')) { usage(); exit(0); } if (argv[1][1] == 'd') { len = strlen(&argv[1][2]); root_directory_name = (char *)malloc(len + 2); memcpy(root_directory_name, &argv[1][2], len); if (root_directory_name[len - 1] != '/') { root_directory_name[len++] = '/'; } root_directory_name[len] = '\0'; } else { fprintf(stderr, "Invalid option -- %c\n", argv[1][1]); usage(); exit(0); } break; case 3: if ((strlen(argv[1]) != 2) || (argv[1][0] != '-')) { usage(); exit(0); } if (argv[1][1] != 'd') { fprintf(stderr, "Invalid option -- %c\n", argv[1][1]); usage(); exit(0); } len = strlen(argv[2]); root_directory_name = (char *)malloc(len + 2); memcpy(root_directory_name, argv[2], len); if (root_directory_name[len - 1] != '/') { root_directory_name[len++] = '/'; } root_directory_name[len] = '\0'; break; default: fprintf(stderr, "Too many arguments!\n"); usage(); exit(0); break; } fs = stdout; /* For now... */ if (verbose >= 1) { fprintf(stderr, "Constructing JFFS filesystem...\n"); } root_ino = make_root_dir(fs, JFFS_MIN_INO, root_directory_name, 0); mkfs(fs, root_directory_name, root_ino + 1, root_ino, 1); fclose(fs); free(root_directory_name); exit(0); }