fs: add rename infrastructure

The selection for *rename as the name for the rename/move operation
derives from the POSIX specification where they name the function
rename/renameat. [1] This aligns with Linux where the syscalls for
renaming/moving also use the rename/renameat naming.

[1] https://pubs.opengroup.org/onlinepubs/9799919799/functions/rename.html

Signed-off-by: Gabriel Dalimonte <gabriel.dalimonte@gmail.com>
Acked-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
This commit is contained in:
Gabriel Dalimonte 2025-02-17 13:26:43 -05:00 committed by Tom Rini
parent 1742b8484e
commit d9c149664f
2 changed files with 45 additions and 1 deletions

32
fs/fs.c
View file

@ -143,6 +143,12 @@ static inline int fs_mkdir_unsupported(const char *dirname)
return -1; return -1;
} }
static inline int fs_rename_unsupported(const char *old_path,
const char *new_path)
{
return -1;
}
struct fstype_info { struct fstype_info {
int fstype; int fstype;
char *name; char *name;
@ -183,6 +189,7 @@ struct fstype_info {
int (*unlink)(const char *filename); int (*unlink)(const char *filename);
int (*mkdir)(const char *dirname); int (*mkdir)(const char *dirname);
int (*ln)(const char *filename, const char *target); int (*ln)(const char *filename, const char *target);
int (*rename)(const char *old_path, const char *new_path);
}; };
static struct fstype_info fstypes[] = { static struct fstype_info fstypes[] = {
@ -206,6 +213,7 @@ static struct fstype_info fstypes[] = {
.unlink = fs_unlink_unsupported, .unlink = fs_unlink_unsupported,
.mkdir = fs_mkdir_unsupported, .mkdir = fs_mkdir_unsupported,
#endif #endif
.rename = fs_rename_unsupported,
.uuid = fat_uuid, .uuid = fat_uuid,
.opendir = fat_opendir, .opendir = fat_opendir,
.readdir = fat_readdir, .readdir = fat_readdir,
@ -238,6 +246,7 @@ static struct fstype_info fstypes[] = {
.closedir = ext4fs_closedir, .closedir = ext4fs_closedir,
.unlink = fs_unlink_unsupported, .unlink = fs_unlink_unsupported,
.mkdir = fs_mkdir_unsupported, .mkdir = fs_mkdir_unsupported,
.rename = fs_rename_unsupported,
}, },
#endif #endif
#if IS_ENABLED(CONFIG_SANDBOX) && !IS_ENABLED(CONFIG_XPL_BUILD) #if IS_ENABLED(CONFIG_SANDBOX) && !IS_ENABLED(CONFIG_XPL_BUILD)
@ -257,6 +266,7 @@ static struct fstype_info fstypes[] = {
.unlink = fs_unlink_unsupported, .unlink = fs_unlink_unsupported,
.mkdir = fs_mkdir_unsupported, .mkdir = fs_mkdir_unsupported,
.ln = fs_ln_unsupported, .ln = fs_ln_unsupported,
.rename = fs_rename_unsupported,
}, },
#endif #endif
#if CONFIG_IS_ENABLED(SEMIHOSTING) #if CONFIG_IS_ENABLED(SEMIHOSTING)
@ -276,6 +286,7 @@ static struct fstype_info fstypes[] = {
.unlink = fs_unlink_unsupported, .unlink = fs_unlink_unsupported,
.mkdir = fs_mkdir_unsupported, .mkdir = fs_mkdir_unsupported,
.ln = fs_ln_unsupported, .ln = fs_ln_unsupported,
.rename = fs_rename_unsupported,
}, },
#endif #endif
#ifndef CONFIG_XPL_BUILD #ifndef CONFIG_XPL_BUILD
@ -296,6 +307,7 @@ static struct fstype_info fstypes[] = {
.unlink = fs_unlink_unsupported, .unlink = fs_unlink_unsupported,
.mkdir = fs_mkdir_unsupported, .mkdir = fs_mkdir_unsupported,
.ln = fs_ln_unsupported, .ln = fs_ln_unsupported,
.rename = fs_rename_unsupported,
}, },
#endif #endif
#endif #endif
@ -317,6 +329,7 @@ static struct fstype_info fstypes[] = {
.unlink = fs_unlink_unsupported, .unlink = fs_unlink_unsupported,
.mkdir = fs_mkdir_unsupported, .mkdir = fs_mkdir_unsupported,
.ln = fs_ln_unsupported, .ln = fs_ln_unsupported,
.rename = fs_rename_unsupported,
}, },
#endif #endif
#endif #endif
@ -339,6 +352,7 @@ static struct fstype_info fstypes[] = {
.ln = fs_ln_unsupported, .ln = fs_ln_unsupported,
.unlink = fs_unlink_unsupported, .unlink = fs_unlink_unsupported,
.mkdir = fs_mkdir_unsupported, .mkdir = fs_mkdir_unsupported,
.rename = fs_rename_unsupported,
}, },
#endif #endif
#if IS_ENABLED(CONFIG_FS_EROFS) #if IS_ENABLED(CONFIG_FS_EROFS)
@ -360,6 +374,7 @@ static struct fstype_info fstypes[] = {
.ln = fs_ln_unsupported, .ln = fs_ln_unsupported,
.unlink = fs_unlink_unsupported, .unlink = fs_unlink_unsupported,
.mkdir = fs_mkdir_unsupported, .mkdir = fs_mkdir_unsupported,
.rename = fs_rename_unsupported,
}, },
#endif #endif
{ {
@ -378,6 +393,7 @@ static struct fstype_info fstypes[] = {
.unlink = fs_unlink_unsupported, .unlink = fs_unlink_unsupported,
.mkdir = fs_mkdir_unsupported, .mkdir = fs_mkdir_unsupported,
.ln = fs_ln_unsupported, .ln = fs_ln_unsupported,
.rename = fs_rename_unsupported,
}, },
}; };
@ -713,6 +729,22 @@ int fs_ln(const char *fname, const char *target)
return ret; return ret;
} }
int fs_rename(const char *old_path, const char *new_path)
{
struct fstype_info *info = fs_get_info(fs_type);
int ret;
ret = info->rename(old_path, new_path);
if (ret < 0) {
log_debug("Unable to rename %s -> %s\n", old_path, new_path);
ret = -1;
}
fs_close();
return ret;
}
int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], int do_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
int fstype) int fstype)
{ {

View file

@ -86,7 +86,7 @@ int fs_set_blk_dev_with_part(struct blk_desc *desc, int part);
* *
* Many file functions implicitly call fs_close(), e.g. fs_closedir(), * Many file functions implicitly call fs_close(), e.g. fs_closedir(),
* fs_exist(), fs_ln(), fs_ls(), fs_mkdir(), fs_read(), fs_size(), fs_write(), * fs_exist(), fs_ln(), fs_ls(), fs_mkdir(), fs_read(), fs_size(), fs_write(),
* fs_unlink(). * fs_unlink(), fs_rename().
*/ */
void fs_close(void); void fs_close(void);
@ -270,6 +270,18 @@ int fs_unlink(const char *filename);
*/ */
int fs_mkdir(const char *filename); int fs_mkdir(const char *filename);
/**
* fs_rename - rename/move a file or directory
*
* @old_path: existing path of the file/directory to rename
* @new_path: new path of the file/directory. If this points to an existing
* file or empty directory, the existing file/directory will be unlinked.
* If this points to a non-empty directory, the rename will fail.
*
* Return: 0 on success, -1 on error conditions
*/
int fs_rename(const char *old_path, const char *new_path);
/* /*
* Common implementation for various filesystem commands, optionally limited * Common implementation for various filesystem commands, optionally limited
* to a specific filesystem type via the fstype parameter. * to a specific filesystem type via the fstype parameter.