static void create_root_dir(ext2_filsys fs) { errcode_t retval; struct ext2_inode inode; retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, 0); if (retval) { com_err("ext2fs_mkdir", retval, _("while creating root dir")); exit(1); } if (geteuid()) { retval = ext2fs_read_inode(fs, EXT2_ROOT_INO, &inode); if (retval) { com_err("ext2fs_read_inode", retval, _("while reading root inode")); exit(1); } inode.i_uid = getuid(); if (inode.i_uid) inode.i_gid = getgid(); retval = ext2fs_write_new_inode(fs, EXT2_ROOT_INO, &inode); if (retval) { com_err("ext2fs_write_inode", retval, _("while setting root inode ownership")); exit(1); } } }
static void create_lost_and_found(ext2_filsys fs) { errcode_t retval; ext2_ino_t ino; const char *name = "lost+found"; int i; int lpf_size = 0; fs->umask = 077; retval = ext2fs_mkdir(fs, EXT2_ROOT_INO, 0, name); if (retval) { com_err("ext2fs_mkdir", retval, _("while creating /lost+found")); exit(1); } retval = ext2fs_lookup(fs, EXT2_ROOT_INO, name, strlen(name), 0, &ino); if (retval) { com_err("ext2_lookup", retval, _("while looking up /lost+found")); exit(1); } for (i=1; i < EXT2_NDIR_BLOCKS; i++) { if ((lpf_size += fs->blocksize) >= 16*1024) break; retval = ext2fs_expand_dir(fs, ino); if (retval) { com_err("ext2fs_expand_dir", retval, _("while expanding /lost+found")); exit(1); } } }
/* Make a directory in the fs */ errcode_t do_mkdir_internal(ext2_filsys fs, ext2_ino_t cwd, const char *name, struct stat *st, ext2_ino_t root) { char *cp; ext2_ino_t parent_ino; errcode_t retval; cp = strrchr(name, '/'); if (cp) { *cp = 0; retval = ext2fs_namei(fs, root, cwd, name, &parent_ino); if (retval) { com_err(name, retval, 0); return retval; } name = cp+1; } else parent_ino = cwd; try_again: retval = ext2fs_mkdir(fs, parent_ino, 0, name); if (retval == EXT2_ET_DIR_NO_SPACE) { retval = ext2fs_expand_dir(fs, parent_ino); if (retval) { com_err(__func__, retval, "while expanding directory"); return retval; } goto try_again; } if (retval) com_err("ext2fs_mkdir", retval, 0); return retval; }
/* * this function creates a new directory inode (returned in newdir) * and the link in the upper directory (e2c->curr_e2dir) * the name is the basename of the current path (e2c->curr_path) */ int e2mkdir(e2i_ctx_t *e2c, ext2_ino_t *newdir) { int ret; struct stat s; const char *dname; ext2_ino_t nd; ret = lstat(e2c->curr_path, &s); ERRNO_ERR(ret,"Could not 'stat': ", e2c->curr_path); /* sanity check */ if (!S_ISDIR(s.st_mode)) { fprintf(stderr, "File '%s' is not a directory\n", e2c->curr_path); return -1; } /* edit the directoryname and create it */ dname = basename(e2c->curr_path); ret = ext2fs_mkdir(e2c->fs, e2c->curr_e2dir, 0, dname); if (ret == EXT2_ET_DIR_NO_SPACE) { /* resize the directory */ if (ext2fs_expand_dir(e2c->fs, e2c->curr_e2dir) == 0) ret = ext2fs_mkdir(e2c->fs, e2c->curr_e2dir, 0, dname); } E2_ERR(ret, "Could not create dir: ", dname); /* say what we do and increase the counter */ if (e2c->verbose) printf ("Creating directory %s\n", dname); e2c->cnt->dir++; /* lookup the inode of the new directory if requested */ ret = ext2fs_lookup(e2c->fs, e2c->curr_e2dir, dname, strlen(dname), 0, &nd); E2_ERR(ret, "Could not Ext2-lookup: ", dname); modinode(e2c, dname, nd); if (newdir) { *newdir = nd; } return 0; }
static errcode_t create_directory(ext2_filsys fs, char *dir, ext2_ino_t *ret_ino) { struct ext2_inode inode; ext2_ino_t ino = EXT2_ROOT_INO; ext2_ino_t newdir; errcode_t retval = 0; char *fn, *cp, *next; fn = malloc(strlen(dir) + 1); if (fn == NULL) return ENOMEM; strcpy(fn, dir); cp = fn; while(1) { next = strchr(cp, '/'); if (next) *next++ = 0; if (*cp) { retval = ext2fs_new_inode(fs, ino, LINUX_S_IFDIR, NULL, &newdir); if (retval) goto errout; retval = ext2fs_mkdir(fs, ino, newdir, cp); if (retval) goto errout; ino = newdir; retval = ext2fs_read_inode(fs, ino, &inode); if (retval) goto errout; inode.i_uid = uid & 0xFFFF; ext2fs_set_i_uid_high(inode, (uid >> 16) & 0xffff); inode.i_gid = gid & 0xFFFF; ext2fs_set_i_gid_high(inode, (gid >> 16) & 0xffff); retval = ext2fs_write_inode(fs, ino, &inode); if (retval) goto errout; } if (next == NULL || *next == '\0') break; cp = next; } errout: free(fn); if (retval == 0) *ret_ino = ino; return retval; }
static ext2_ino_t ext2CreateMkDir(ext2_vd *vd, ext2_inode_t * parent, int type, const char * name) { ext2_ino_t newentry = 0; ext2_ino_t existing; if(ext2fs_namei_follow(vd->fs, vd->root, parent->ino, name, &existing) == 0){ errno = EEXIST; return 0; } errcode_t err = ext2fs_new_inode(vd->fs, parent->ino, type, 0, &newentry); if(err != EXT2_ET_OK) { errno = EFAULT; return 0; } while((err = ext2fs_mkdir(vd->fs, parent->ino, newentry, name)) == EXT2_ET_DIR_NO_SPACE) { if(ext2fs_expand_dir(vd->fs, parent->ino) != EXT2_ET_OK) { errno = EMLINK; return 0; } } if(err != EXT2_ET_OK) { errno = EMLINK; return 0; } struct ext2_inode inode; if(ext2fs_read_inode(vd->fs, newentry, &inode) == EXT2_ET_OK) { inode.i_mode = type; inode.i_uid = parent->ni.i_uid; inode.i_gid = parent->ni.i_gid; ext2fs_write_new_inode(vd->fs, newentry, &inode); } return newentry; }
static errcode_t dir_test(ext2_filsys fs) { const char *dot_name = "."; const char *stub_name = "stub"; const char *parent_name = "test"; ext2_ino_t parent, dir, tmp; errcode_t retval; char dirname[PATH_MAX]; int i; retval = ext2fs_mkdir(fs, 11, 11, stub_name); if (retval) { com_err("dir_test", retval, "while creating %s dir", stub_name); return retval; } retval = ext2fs_mkdir(fs, 11, 0, parent_name); if (retval) { com_err("dir_test", retval, "while creating %s dir", parent_name); return retval; } retval = ext2fs_lookup(fs, 11, parent_name, strlen(parent_name), 0, &parent); if (retval) { com_err("dir_test", retval, "while looking up %s dir", parent_name); return retval; } retval = ext2fs_lookup(fs, parent, dot_name, strlen(dot_name), 0, &tmp); if (retval) { com_err("dir_test", retval, "while looking up %s dir", parent_name); return retval; } if (parent != tmp) { fprintf(stderr, "tst_inline_data: parent (%u) != tmp (%u)\n", parent, tmp); return 1; } for (i = 0, dir = 13; i < 4; i++, dir++) { tmp = 0; snprintf(dirname, sizeof(dirname), "%d", i); retval = ext2fs_mkdir(fs, parent, 0, dirname); if (retval) { com_err("dir_test", retval, "while creating %s dir", dirname); return retval; } retval = ext2fs_lookup(fs, parent, dirname, strlen(dirname), 0, &tmp); if (retval) { com_err("dir_test", retval, "while looking up %s dir", parent_name); return retval; } if (dir != tmp) { fprintf(stderr, "tst_inline_data: dir (%u) != tmp (%u)\n", dir, tmp); return 1; } } snprintf(dirname, sizeof(dirname), "%d", i); retval = ext2fs_mkdir(fs, parent, 0, dirname); if (retval && retval != EXT2_ET_DIR_NO_SPACE) { com_err("dir_test", retval, "while creating %s dir", dirname); return retval; } retval = ext2fs_expand_dir(fs, parent); if (retval) { com_err("dir_test", retval, "while expanding %s dir", parent_name); return retval; } return 0; }