|
|
|
@ -168,60 +168,28 @@ 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_stat stat; |
|
|
|
|
struct mufs_dirent dirent; |
|
|
|
|
struct mufs_tree *tree; |
|
|
|
|
size_t len; |
|
|
|
|
char *full_old, *full_new; |
|
|
|
|
char *name; |
|
|
|
|
int ret = -1; |
|
|
|
|
struct mufs_tree *subtree; |
|
|
|
|
char *path; |
|
|
|
|
unsigned type; |
|
|
|
|
|
|
|
|
|
if (!(full_new = mufs_abspath(new))) |
|
|
|
|
if (!(path = mufs_abspath(old))) |
|
|
|
|
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; |
|
|
|
|
if (!(subtree = resolve_path(fs, path, &stat))) |
|
|
|
|
goto err_free_path; |
|
|
|
|
|
|
|
|
|
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; |
|
|
|
|
type = stat.type; |
|
|
|
|
free(path); |
|
|
|
|
|
|
|
|
|
if (write_dirent(tree, align_up(tree->file_size, |
|
|
|
|
fs->dev->log2_block_size), &dirent) > 0) |
|
|
|
|
goto err_free_old; |
|
|
|
|
if (mufs_mkpath(fs, new, subtree, type) < 0) |
|
|
|
|
goto err_free_path; |
|
|
|
|
|
|
|
|
|
if (mufs_rmpath(fs, full_old) < 0) |
|
|
|
|
goto err_free_old; |
|
|
|
|
if (mufs_rmpath(fs, old) < 0) |
|
|
|
|
return -1; |
|
|
|
|
|
|
|
|
|
ret = 0; |
|
|
|
|
return 0; |
|
|
|
|
|
|
|
|
|
err_free_old: |
|
|
|
|
free(full_old); |
|
|
|
|
err_free_new: |
|
|
|
|
free(full_new); |
|
|
|
|
return ret; |
|
|
|
|
err_free_path: |
|
|
|
|
free(path); |
|
|
|
|
return -1; |
|
|
|
|
} |
|
|
|
|