From eb1fb36b6b56582babfb9ad621b68ddd2fdb129c Mon Sep 17 00:00:00 2001 From: Serge Vakulenko Date: Thu, 31 Jul 2014 17:46:24 -0700 Subject: [PATCH] Fsutil library: use names for inode ops. --- tools/fsutil/bsdfs.h | 9 +++++++- tools/fsutil/file.c | 4 ++-- tools/fsutil/fsutil.c | 8 +++---- tools/fsutil/inode.c | 15 +++++-------- tools/fsutil/mount.c | 51 +++++++++++++++++++++++-------------------- 5 files changed, 46 insertions(+), 41 deletions(-) diff --git a/tools/fsutil/bsdfs.h b/tools/fsutil/bsdfs.h index 7b5954d..60be772 100644 --- a/tools/fsutil/bsdfs.h +++ b/tools/fsutil/bsdfs.h @@ -145,6 +145,13 @@ typedef struct { unsigned long offset; /* current i/o offset */ } fs_file_t; +typedef enum { + INODE_OP_LOOKUP, /* lookup inode by name */ + INODE_OP_CREATE, /* create new file */ + INODE_OP_DELETE, /* delete file */ + INODE_OP_LINK, /* make a link to a file */ +} fs_op_t; + int fs_seek (fs_t *fs, unsigned long offset); int fs_read8 (fs_t *fs, unsigned char *val); int fs_read16 (fs_t *fs, unsigned short *val); @@ -177,7 +184,7 @@ int fs_inode_write (fs_inode_t *inode, unsigned long offset, unsigned char *data, unsigned long bytes); int fs_inode_alloc (fs_t *fs, fs_inode_t *inode); int fs_inode_by_name (fs_t *fs, fs_inode_t *inode, const char *name, - int op, int mode); + fs_op_t op, int mode); int inode_build_list (fs_t *fs); int fs_write_block (fs_t *fs, unsigned bnum, unsigned char *data); diff --git a/tools/fsutil/file.c b/tools/fsutil/file.c index 3d0275e..8218edb 100644 --- a/tools/fsutil/file.c +++ b/tools/fsutil/file.c @@ -29,7 +29,7 @@ extern int verbose; int fs_file_create (fs_t *fs, fs_file_t *file, const char *name, int mode) { - if (! fs_inode_by_name (fs, &file->inode, name, 1, mode)) { + if (! fs_inode_by_name (fs, &file->inode, name, INODE_OP_CREATE, mode)) { fprintf (stderr, "%s: inode open failed\n", name); return 0; } @@ -46,7 +46,7 @@ int fs_file_create (fs_t *fs, fs_file_t *file, const char *name, int mode) int fs_file_open (fs_t *fs, fs_file_t *file, const char *name, int wflag) { - if (! fs_inode_by_name (fs, &file->inode, name, 0, 0)) { + if (! fs_inode_by_name (fs, &file->inode, name, INODE_OP_LOOKUP, 0)) { fprintf (stderr, "%s: inode open failed\n", name); return 0; } diff --git a/tools/fsutil/fsutil.c b/tools/fsutil/fsutil.c index 20327b6..39c386b 100644 --- a/tools/fsutil/fsutil.c +++ b/tools/fsutil/fsutil.c @@ -300,13 +300,13 @@ void add_directory (fs_t *fs, char *name) *p = 0; else *buf = 0; - if (! fs_inode_by_name (fs, &parent, buf, 0, 0)) { + if (! fs_inode_by_name (fs, &parent, buf, INODE_OP_LOOKUP, 0)) { fprintf (stderr, "%s: cannot open directory\n", buf); return; } /* Create directory. */ - int done = fs_inode_by_name (fs, &dir, name, 1, INODE_MODE_FDIR | 0777); + int done = fs_inode_by_name (fs, &dir, name, INODE_OP_CREATE, INODE_MODE_FDIR | 0777); if (! done) { fprintf (stderr, "%s: directory inode create failed\n", name); return; @@ -320,7 +320,7 @@ void add_directory (fs_t *fs, char *name) /* Make parent link '..' */ strcpy (buf, name); strcat (buf, "/.."); - if (! fs_inode_by_name (fs, &dir, buf, 3, parent.number)) { + if (! fs_inode_by_name (fs, &dir, buf, INODE_OP_LINK, parent.number)) { fprintf (stderr, "%s: dotdot link failed\n", name); return; } @@ -349,7 +349,7 @@ void add_device (fs_t *fs, char *name, char *spec) fprintf (stderr, "expected c: or b:\n"); return; } - if (! fs_inode_by_name (fs, &dev, name, 1, 0666 | + if (! fs_inode_by_name (fs, &dev, name, INODE_OP_CREATE, 0666 | ((type == 'b') ? INODE_MODE_FBLK : INODE_MODE_FCHR))) { fprintf (stderr, "%s: device inode create failed\n", name); return; diff --git a/tools/fsutil/inode.c b/tools/fsutil/inode.c index d021cce..ec9d88b 100644 --- a/tools/fsutil/inode.c +++ b/tools/fsutil/inode.c @@ -551,13 +551,8 @@ void fs_dirent_unpack (fs_dirent_t *dirent, unsigned char *data) * Return 1 when the inode was found. * Return 2 when the inode was created/deleted/linked. */ -#define LOOKUP 0 /* perform name lookup only */ -#define CREATE 1 /* setup for file creation */ -#define DELETE 2 /* setup for file deletion */ -#define LINK 3 /* setup for link */ - int fs_inode_by_name (fs_t *fs, fs_inode_t *inode, const char *name, - int op, int mode) + fs_op_t op, int mode) { fs_inode_t dir; int c, namlen, reclen; @@ -577,7 +572,7 @@ int fs_inode_by_name (fs_t *fs, fs_inode_t *inode, const char *name, c = *name++; while (c == '/') c = *name++; - if (! c && op != LOOKUP) { + if (! c && op != INODE_OP_LOOKUP) { /* Cannot write or delete root directory. */ return 0; } @@ -631,7 +626,7 @@ cloop: /* Here a component matched in a directory. * If there is more pathname, go back to * cloop, otherwise return. */ - if (op == DELETE && ! c) { + if (op == INODE_OP_DELETE && ! c) { goto delete_file; } if (! fs_inode_get (fs, &dir, dirent.inum)) { @@ -643,9 +638,9 @@ cloop: } /* If at the end of the directory, the search failed. * Report what is appropriate as per flag. */ - if (op == CREATE && ! c) + if (op == INODE_OP_CREATE && ! c) goto create_file; - if (op == LINK && ! c) + if (op == INODE_OP_LINK && ! c) goto create_link; return 0; diff --git a/tools/fsutil/mount.c b/tools/fsutil/mount.c index bb8f0be..9e451ad 100644 --- a/tools/fsutil/mount.c +++ b/tools/fsutil/mount.c @@ -116,7 +116,7 @@ int op_getattr(const char *path, struct stat *statbuf) printlog("--- op_getattr(path=\"%s\", statbuf=%p)\n", path, statbuf); - if (! fs_inode_by_name (fs, &inode, path, 0, 0)) { + if (! fs_inode_by_name (fs, &inode, path, INODE_OP_LOOKUP, 0)) { printlog("--- search failed\n"); return -ENOENT; } @@ -307,7 +307,7 @@ int op_unlink(const char *path) printlog("--- op_unlink(path=\"%s\")\n", path); /* Get the file type. */ - if (! fs_inode_by_name (fs, &inode, path, 0, 0)) { + if (! fs_inode_by_name (fs, &inode, path, INODE_OP_LOOKUP, 0)) { printlog("--- search failed\n"); return -ENOENT; } @@ -317,7 +317,7 @@ int op_unlink(const char *path) } /* Delete file. */ - if (! fs_inode_by_name (fs, &inode, path, 2, 0)) { + if (! fs_inode_by_name (fs, &inode, path, INODE_OP_DELETE, 0)) { printlog("--- delete failed\n"); return -EIO; } @@ -337,7 +337,7 @@ int op_rmdir(const char *path) printlog("--- op_rmdir(path=\"%s\")\n", path); /* Get the file type. */ - if (! fs_inode_by_name (fs, &inode, path, 0, 0)) { + if (! fs_inode_by_name (fs, &inode, path, INODE_OP_LOOKUP, 0)) { printlog("--- search failed\n"); return -ENOENT; } @@ -357,20 +357,20 @@ int op_rmdir(const char *path) *p = 0; else *buf = 0; - if (! fs_inode_by_name (fs, &parent, buf, 0, 0)) { + if (! fs_inode_by_name (fs, &parent, buf, INODE_OP_LOOKUP, 0)) { printlog("--- parent not found\n"); return -ENOENT; } /* Delete directory. * Need to decrease a link count first. */ - if (! fs_inode_by_name (fs, &inode, path, 0, 0)) { + if (! fs_inode_by_name (fs, &inode, path, INODE_OP_LOOKUP, 0)) { printlog("--- directory not found\n"); return -ENOENT; } --inode.nlink; fs_inode_save (&inode, 1); - if (! fs_inode_by_name (fs, &inode, path, 2, 0)) { + if (! fs_inode_by_name (fs, &inode, path, INODE_OP_DELETE, 0)) { printlog("--- delete failed\n"); return -EIO; } @@ -404,13 +404,15 @@ int op_mkdir(const char *path, mode_t mode) *p = 0; else *buf = 0; - if (! fs_inode_by_name (fs, &parent, buf, 0, 0)) { + if (! fs_inode_by_name (fs, &parent, buf, INODE_OP_LOOKUP, 0)) { printlog("--- parent not found\n"); return -ENOENT; } /* Create directory. */ - int done = fs_inode_by_name (fs, &dir, path, 1, INODE_MODE_FDIR | (mode & 07777)); + mode &= 07777; + mode |= INODE_MODE_FDIR; + int done = fs_inode_by_name (fs, &dir, path, INODE_OP_CREATE, mode); if (! done) { printlog("--- cannot create dir inode\n"); return -ENOENT; @@ -424,7 +426,7 @@ int op_mkdir(const char *path, mode_t mode) /* Make parent link '..' */ strcpy (buf, path); strcat (buf, "/.."); - if (! fs_inode_by_name (fs, &dir, buf, 3, parent.number)) { + if (! fs_inode_by_name (fs, &dir, buf, INODE_OP_LINK, parent.number)) { printlog("--- dotdot link failed\n"); return -EIO; } @@ -448,7 +450,7 @@ int op_link(const char *path, const char *newpath) printlog("--- op_link(path=\"%s\", newpath=\"%s\")\n", path, newpath); /* Find source. */ - if (! fs_inode_by_name (fs, &source, path, 0, 0)) { + if (! fs_inode_by_name (fs, &source, path, INODE_OP_LOOKUP, 0)) { printlog("--- source not found\n"); return -ENOENT; } @@ -459,7 +461,7 @@ int op_link(const char *path, const char *newpath) } /* Create target link. */ - if (! fs_inode_by_name (fs, &target, newpath, 3, source.number)) { + if (! fs_inode_by_name (fs, &target, newpath, INODE_OP_LINK, source.number)) { printlog("--- link failed\n"); return -EIO; } @@ -477,7 +479,7 @@ int op_rename(const char *path, const char *newpath) printlog("--- op_rename(path=\"%s\", newpath=\"%s\")\n", path, newpath); /* Find source and increase the link count. */ - if (! fs_inode_by_name (fs, &source, path, 0, 0)) { + if (! fs_inode_by_name (fs, &source, path, INODE_OP_LOOKUP, 0)) { printlog("--- source not found\n"); return -ENOENT; } @@ -485,13 +487,13 @@ int op_rename(const char *path, const char *newpath) fs_inode_save (&source, 1); /* Create target link. */ - if (! fs_inode_by_name (fs, &target, newpath, 3, source.number)) { + if (! fs_inode_by_name (fs, &target, newpath, INODE_OP_LINK, source.number)) { printlog("--- link failed\n"); return -EIO; } /* Delete the source. */ - if (! fs_inode_by_name (fs, &source, path, 2, 0)) { + if (! fs_inode_by_name (fs, &source, path, INODE_OP_DELETE, 0)) { printlog("--- delete failed\n"); return -EIO; } @@ -511,7 +513,7 @@ int op_mknod(const char *path, mode_t mode, dev_t dev) path, mode, dev); /* Check if the file already exists. */ - if (fs_inode_by_name (fs, &inode, path, 0, 0)) { + if (fs_inode_by_name (fs, &inode, path, INODE_OP_LOOKUP, 0)) { printlog("--- already exists\n"); return -EEXIST; } @@ -527,7 +529,7 @@ int op_mknod(const char *path, mode_t mode, dev_t dev) return -EINVAL; /* Create the file. */ - if (! fs_inode_by_name (fs, &inode, path, 1, mode)) { + if (! fs_inode_by_name (fs, &inode, path, INODE_OP_CREATE, mode)) { printlog("--- create failed\n"); return -EIO; } @@ -555,7 +557,7 @@ int op_readlink(const char *path, char *link, size_t size) path, link, size); /* Open the file. */ - if (! fs_inode_by_name (fs, &inode, path, 0, 0)) { + if (! fs_inode_by_name (fs, &inode, path, INODE_OP_LOOKUP, 0)) { printlog("--- file not found\n"); return -ENOENT; } @@ -586,13 +588,14 @@ int op_symlink(const char *path, const char *newpath) printlog("--- op_symlink(path=\"%s\", newpath=\"%s\")\n", path, newpath); /* Check if the file already exists. */ - if (fs_inode_by_name (fs, &inode, newpath, 0, 0)) { + if (fs_inode_by_name (fs, &inode, newpath, INODE_OP_LOOKUP, 0)) { printlog("--- already exists\n"); return -EEXIST; } + /* Create symlink. */ mode = 0777 | INODE_MODE_FLNK; - if (! fs_inode_by_name (fs, &inode, newpath, 1, mode)) { + if (! fs_inode_by_name (fs, &inode, newpath, INODE_OP_CREATE, mode)) { printlog("--- create failed\n"); return -EIO; } @@ -623,7 +626,7 @@ int op_chmod(const char *path, mode_t mode) printlog("--- op_chmod(path=\"%s\", mode=0%03o)\n", path, mode); /* Open the file. */ - if (! fs_inode_by_name (fs, &inode, path, 0, 0)) { + if (! fs_inode_by_name (fs, &inode, path, INODE_OP_LOOKUP, 0)) { printlog("--- file not found\n"); return -ENOENT; } @@ -647,7 +650,7 @@ int op_chown(const char *path, uid_t uid, gid_t gid) printlog("--- op_chown(path=\"%s\", uid=%d, gid=%d)\n", path, uid, gid); /* Open the file. */ - if (! fs_inode_by_name (fs, &inode, path, 0, 0)) { + if (! fs_inode_by_name (fs, &inode, path, INODE_OP_LOOKUP, 0)) { printlog("--- file not found\n"); return -ENOENT; } @@ -671,7 +674,7 @@ int op_utime(const char *path, struct utimbuf *ubuf) printlog("--- op_utime(path=\"%s\", ubuf=%p)\n", path, ubuf); /* Open the file. */ - if (! fs_inode_by_name (fs, &inode, path, 0, 0)) { + if (! fs_inode_by_name (fs, &inode, path, INODE_OP_LOOKUP, 0)) { printlog("--- file not found\n"); return -ENOENT; } @@ -756,7 +759,7 @@ int op_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset printlog("--- op_readdir(path=\"%s\", buf=%p, filler=%p, offset=%lld, fi=%p)\n", path, buf, filler, offset, fi); - if (! fs_inode_by_name (fs, &dir, path, 0, 0)) { + if (! fs_inode_by_name (fs, &dir, path, INODE_OP_LOOKUP, 0)) { printlog("--- cannot find path %s\n", path); return -ENOENT; }