mufs: implement mufs_rename() to move files and directories

tags/0.1.0
S.J.R. van Schaik 7 years ago
parent 65c1fc28d3
commit 8b780d57d9
  1. 1
      include/fs/mufs.h
  2. 61
      source/fs/mufs/path.c

@ -43,6 +43,7 @@ int mufs_format(struct flash_dev *dev);
char *mufs_abspath(const char *path);
int mufs_stat(struct mufs *fs, const char *path, struct mufs_stat *stat);
int mufs_rename(struct mufs *fs, const char *old, const char *new);
struct mufs_dir *mufs_opendir(struct mufs *fs, const char *path);
void mufs_closedir(struct mufs_dir *dir);

@ -137,3 +137,64 @@ int mufs_stat(struct mufs *fs, const char *path, struct mufs_stat *stat)
return 0;
}
int mufs_rename(struct mufs *fs, const char *old, const char *new)
{
struct mufs_stat stat;
struct mufs_dirent dirent;
struct mufs_tree *tree;
size_t len;
char *full_old, *full_new;
char *name;
int ret = -1;
if (!(full_new = mufs_abspath(new)))
return -1;
if (resolve_path(fs, full_new, NULL))
goto err_free_new;
if (!(full_old = mufs_abspath(old)))
goto err_free_new;
name = strrchr(full_new, '/');
*name++ = '\0';
if (*full_new == '\0')
full_new = "/";
if (*name == '\0')
goto err_free_old;
if (!(tree = resolve_path(fs, full_old, &stat)))
goto err_free_old;
len = min(strlen(name), sizeof(dirent.path) - 1);
memcpy(dirent.path, name, len);
dirent.path[len] = '\0';
dirent.type = stat.type;
dirent.tree.fs = fs;
dirent.tree.file_size = tree->file_size;
dirent.tree.root = tree->root;
dirent.tree.depth = tree->depth;
if (!is_aligned(tree->file_size, fs->dev->log2_block_size) &&
write_dirent(tree, tree->file_size, &dirent) > 0)
goto err_free_old;
if (write_dirent(tree, align_up(tree->file_size,
fs->dev->log2_block_size), &dirent) > 0)
goto err_free_old;
if (mufs_rmpath(fs, full_old) < 0)
goto err_free_old;
ret = 0;
err_free_old:
free(full_old);
err_free_new:
free(full_new);
return ret;
}

Loading…
Cancel
Save