Fsutil: inode API simplified.

This commit is contained in:
Serge Vakulenko
2014-08-01 18:17:38 -07:00
parent 8f4aad27f9
commit 1cdf42ef94
5 changed files with 77 additions and 32 deletions

View File

@@ -185,6 +185,10 @@ int fs_inode_write (fs_inode_t *inode, unsigned long offset,
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,
fs_op_t op, int mode);
int fs_inode_lookup (fs_t *fs, fs_inode_t *inode, const char *name);
int fs_inode_create (fs_t *fs, fs_inode_t *inode, const char *name, int mode);
int fs_inode_delete (fs_t *fs, fs_inode_t *inode, const char *name);
int fs_inode_link (fs_t *fs, fs_inode_t *inode, const char *name, int mode);
int inode_build_list (fs_t *fs);
int fs_write_block (fs_t *fs, unsigned bnum, unsigned char *data);

View File

@@ -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, INODE_OP_CREATE, mode)) {
if (! fs_inode_create (fs, &file->inode, name, 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, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &file->inode, name)) {
fprintf (stderr, "%s: inode open failed\n", name);
return 0;
}

View File

@@ -309,7 +309,7 @@ void add_directory (fs_t *fs, char *name, int mode, int owner, int group)
*p = 0;
else
*buf = 0;
if (! fs_inode_by_name (fs, &parent, buf, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &parent, buf)) {
fprintf (stderr, "%s: cannot open directory\n", buf);
return;
}
@@ -317,7 +317,7 @@ void add_directory (fs_t *fs, char *name, int mode, int owner, int group)
/* Create directory. */
mode &= 07777;
mode |= INODE_MODE_FDIR;
int done = fs_inode_by_name (fs, &dir, name, INODE_OP_CREATE, mode);
int done = fs_inode_create (fs, &dir, name, mode);
if (! done) {
fprintf (stderr, "%s: directory inode create failed\n", name);
return;
@@ -333,7 +333,7 @@ void add_directory (fs_t *fs, char *name, int mode, int owner, int group)
/* Make parent link '..' */
strcpy (buf, name);
strcat (buf, "/..");
if (! fs_inode_by_name (fs, &dir, buf, INODE_OP_LINK, parent.number)) {
if (! fs_inode_link (fs, &dir, buf, parent.number)) {
fprintf (stderr, "%s: dotdot link failed\n", name);
return;
}
@@ -356,7 +356,7 @@ void add_device (fs_t *fs, char *name, int mode, int owner, int group,
mode &= 07777;
mode |= (type == 'b') ? INODE_MODE_FBLK : INODE_MODE_FCHR;
if (! fs_inode_by_name (fs, &dev, name, INODE_OP_CREATE, mode)) {
if (! fs_inode_create (fs, &dev, name, mode)) {
fprintf (stderr, "%s: device inode create failed\n", name);
return;
}
@@ -460,7 +460,7 @@ void add_hardlink (fs_t *fs, const char *path, const char *link)
fs_inode_t source, target;
/* Find source. */
if (! fs_inode_by_name (fs, &source, link, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &source, link)) {
fprintf (stderr, "%s: link source not found\n", link);
return;
}
@@ -470,7 +470,7 @@ void add_hardlink (fs_t *fs, const char *path, const char *link)
}
/* Create target link. */
if (! fs_inode_by_name (fs, &target, path, INODE_OP_LINK, source.number)) {
if (! fs_inode_link (fs, &target, path, source.number)) {
fprintf (stderr, "%s: link failed\n", path);
return;
}

View File

@@ -851,3 +851,44 @@ int fs_inode_alloc (fs_t *fs, fs_inode_t *inode)
}
}
}
/*
* Find inode by name.
* Return 0 on any error.
* Return 1 when the inode was found.
*/
int fs_inode_lookup (fs_t *fs, fs_inode_t *inode, const char *name)
{
return fs_inode_by_name (fs, inode, name, INODE_OP_LOOKUP, 0);
}
/*
* Create inode by name.
* Return 0 on any error.
* Return 1 when the inode was found.
* Return 2 when the inode was created.
*/
int fs_inode_create (fs_t *fs, fs_inode_t *inode, const char *name, int mode)
{
return fs_inode_by_name (fs, inode, name, INODE_OP_CREATE, mode);
}
/*
* Delete inode by name.
* Return 0 on any error.
* Return 2 when the inode was created.
*/
int fs_inode_delete (fs_t *fs, fs_inode_t *inode, const char *name)
{
return fs_inode_by_name (fs, inode, name, INODE_OP_DELETE, 0);
}
/*
* Create a new link for the inode.
* Return 0 on any error.
* Return 2 when the inode was linked.
*/
int fs_inode_link (fs_t *fs, fs_inode_t *inode, const char *name, int inum)
{
return fs_inode_by_name (fs, inode, name, INODE_OP_LINK, inum);
}

View File

@@ -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, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &inode, path)) {
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, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &inode, path)) {
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, INODE_OP_DELETE, 0)) {
if (! fs_inode_delete (fs, &inode, path)) {
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, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &inode, path)) {
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, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &parent, buf)) {
printlog("--- parent not found\n");
return -ENOENT;
}
/* Delete directory.
* Need to decrease a link count first. */
if (! fs_inode_by_name (fs, &inode, path, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &inode, path)) {
printlog("--- directory not found\n");
return -ENOENT;
}
--inode.nlink;
fs_inode_save (&inode, 1);
if (! fs_inode_by_name (fs, &inode, path, INODE_OP_DELETE, 0)) {
if (! fs_inode_delete (fs, &inode, path)) {
printlog("--- delete failed\n");
return -EIO;
}
@@ -404,7 +404,7 @@ int op_mkdir(const char *path, mode_t mode)
*p = 0;
else
*buf = 0;
if (! fs_inode_by_name (fs, &parent, buf, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &parent, buf)) {
printlog("--- parent not found\n");
return -ENOENT;
}
@@ -412,7 +412,7 @@ int op_mkdir(const char *path, mode_t mode)
/* Create directory. */
mode &= 07777;
mode |= INODE_MODE_FDIR;
int done = fs_inode_by_name (fs, &dir, path, INODE_OP_CREATE, mode);
int done = fs_inode_create (fs, &dir, path, mode);
if (! done) {
printlog("--- cannot create dir inode\n");
return -ENOENT;
@@ -426,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, INODE_OP_LINK, parent.number)) {
if (! fs_inode_link (fs, &dir, buf, parent.number)) {
printlog("--- dotdot link failed\n");
return -EIO;
}
@@ -450,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, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &source, path)) {
printlog("--- source not found\n");
return -ENOENT;
}
@@ -461,7 +461,7 @@ int op_link(const char *path, const char *newpath)
}
/* Create target link. */
if (! fs_inode_by_name (fs, &target, newpath, INODE_OP_LINK, source.number)) {
if (! fs_inode_link (fs, &target, newpath, source.number)) {
printlog("--- link failed\n");
return -EIO;
}
@@ -479,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, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &source, path)) {
printlog("--- source not found\n");
return -ENOENT;
}
@@ -487,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, INODE_OP_LINK, source.number)) {
if (! fs_inode_link (fs, &target, newpath, source.number)) {
printlog("--- link failed\n");
return -EIO;
}
/* Delete the source. */
if (! fs_inode_by_name (fs, &source, path, INODE_OP_DELETE, 0)) {
if (! fs_inode_delete (fs, &source, path)) {
printlog("--- delete failed\n");
return -EIO;
}
@@ -513,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, INODE_OP_LOOKUP, 0)) {
if (fs_inode_lookup (fs, &inode, path)) {
printlog("--- already exists\n");
return -EEXIST;
}
@@ -529,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, INODE_OP_CREATE, mode)) {
if (! fs_inode_create (fs, &inode, path, mode)) {
printlog("--- create failed\n");
return -EIO;
}
@@ -557,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, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &inode, path)) {
printlog("--- file not found\n");
return -ENOENT;
}
@@ -588,14 +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, INODE_OP_LOOKUP, 0)) {
if (fs_inode_lookup (fs, &inode, newpath)) {
printlog("--- already exists\n");
return -EEXIST;
}
/* Create symlink. */
mode = 0777 | INODE_MODE_FLNK;
if (! fs_inode_by_name (fs, &inode, newpath, INODE_OP_CREATE, mode)) {
if (! fs_inode_create (fs, &inode, newpath, mode)) {
printlog("--- create failed\n");
return -EIO;
}
@@ -626,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, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &inode, path)) {
printlog("--- file not found\n");
return -ENOENT;
}
@@ -650,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, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &inode, path)) {
printlog("--- file not found\n");
return -ENOENT;
}
@@ -674,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, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &inode, path)) {
printlog("--- file not found\n");
return -ENOENT;
}
@@ -759,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, INODE_OP_LOOKUP, 0)) {
if (! fs_inode_lookup (fs, &dir, path)) {
printlog("--- cannot find path %s\n", path);
return -ENOENT;
}