FAT file renaming
Subsystems (2)
v2 cover FAT file renaming
Rework of v1 posted by Burak Gerz [1].
[1] https://lore.kernel.org/u-boot/20241212221427.GM1505244@bill-the-cat/
Changes in v2:
- Adapt variable names for better consistency
- Add missing rename hooks
- Use EFI_ACCESS_DENIED for unsuccessful renames
- Avoid using of "== 0"
- Move the call to set_blk_dev earlier in the sequence
- Refactor delete_dentry_long for improved reusability
- Separate the rename implementation from changes in the filesystem layer
- Eliminate code duplication with delete_dentry_long(..)
- Ensure that the source file exists, but not the destination file
- Free the fatbuf if it has been reinitialized with fat_itr_root
- Delete orphaned FAT entry after copying file metadata
Daniel Venzin (4):
fs: Add unimplemented rename hook to FS layer
efi_loader: Support for file renaming
fs: fat: Refactor delete_dentry_long interface
fs: fat: Support basic file rename
fs/fat/fat_write.c | 134 +++++++++++++++++++++++++++++++++++--- fs/fs.c | 30 +++++++++ include/fat.h | 1 + include/fs.h | 9 +++ lib/efi_loader/efi_file.c | 11 +++- 5 files changed, 174 insertions(+), 11 deletions(-)
Signature · 1 line
2.47.2
PATCH v2 1/4 fs: Add unimplemented rename hook to FS layer
The EFI API supports file renaming, which is required by systemd-stub for its bootcounting implementation.
---
Changes in v2:
- Adapt variable names for better consistency
- Add missing rename hooks
fs/fs.c | 30 ++++++++++++++++++++++++++++++ include/fs.h | 9 +++++++++ 2 files changed, 39 insertions(+)
fs/fs.c +30 -0
@@ -143,6 +143,12 @@ static inline int fs_mkdir_unsupported(const char *dirname)return -1 ; } +static inline int fs_rename_unsupported (const char *old_filename, +const char *new_filename) +{ +return -1 ; +} +struct fstype_info {int fstype;char *name;
@@ -182,6 +188,7 @@ struct fstype_info {void (*closedir)(struct fs_dir_stream *dirs);int (*unlink)(const char *filename);int (*mkdir)(const char *dirname); +int (*rename)(const char *old_filename,const char *new_filename);int (*ln)(const char *filename,const char *target); };
@@ -201,10 +208,12 @@ static struct fstype_info fstypes[] = { .write = file_fat_write, .unlink = fat_unlink, .mkdir = fat_mkdir, + .rename = fs_rename_unsupported, #else .write = fs_write_unsupported, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .rename = fs_rename_unsupported, #endif .uuid = fat_uuid, .opendir = fat_opendir,
@@ -238,6 +247,7 @@ static struct fstype_info fstypes[] = { .closedir = ext4fs_closedir, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .rename = fs_rename_unsupported, },#endif #if IS_ENABLED (CONFIG_SANDBOX ) && !IS_ENABLED (CONFIG_XPL_BUILD )
@@ -256,6 +266,7 @@ static struct fstype_info fstypes[] = { .opendir = fs_opendir_unsupported, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .rename = fs_rename_unsupported, .ln = fs_ln_unsupported, },#endif
@@ -275,6 +286,7 @@ static struct fstype_info fstypes[] = { .opendir = fs_opendir_unsupported, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .rename = fs_rename_unsupported, .ln = fs_ln_unsupported, },#endif
@@ -295,6 +307,7 @@ static struct fstype_info fstypes[] = { .opendir = fs_opendir_unsupported, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .rename = fs_rename_unsupported, .ln = fs_ln_unsupported, },#endif
@@ -316,6 +329,7 @@ static struct fstype_info fstypes[] = { .opendir = fs_opendir_unsupported, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .rename = fs_rename_unsupported, .ln = fs_ln_unsupported, },#endif
@@ -339,6 +353,7 @@ static struct fstype_info fstypes[] = { .ln = fs_ln_unsupported, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .rename = fs_rename_unsupported, },#endif #if IS_ENABLED (CONFIG_FS_EROFS )
@@ -360,6 +375,7 @@ static struct fstype_info fstypes[] = { .ln = fs_ln_unsupported, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .rename = fs_rename_unsupported, },#endif {
@@ -377,6 +393,7 @@ static struct fstype_info fstypes[] = { .opendir = fs_opendir_unsupported, .unlink = fs_unlink_unsupported, .mkdir = fs_mkdir_unsupported, + .rename = fs_rename_unsupported, .ln = fs_ln_unsupported, }, };
@@ -697,6 +714,19 @@ int fs_mkdir(const char *dirname)return ret; } +int fs_rename (const char *old_filename,const char *new_filename) +{ +int ret; + +struct fstype_info *info =fs_get_info (fs_type); + + ret = info->rename (old_filename, new_filename); + +fs_close (); + +return ret; +} +int fs_ln (const char *fname,const char *target) {struct fstype_info *info =fs_get_info (fs_type);
include/fs.h +9 -0
@@ -270,6 +270,15 @@ int fs_unlink(const char *filename);*/ int fs_mkdir (const char *filename); +/* +* fs_rename - Rename a file +* +* @old_filename: Name of the file to rename +* @new_filename: New name for the file +* Return: 0 on success, -1 on error conditions +*/ +int fs_rename (const char *old_filename,const char *new_filename); +/* * Common implementation for various filesystem commands, optionally limited * to a specific filesystem type via the fstype parameter.
Signature · 1 line
2.47.2
PATCH v2 2/4 efi_loader: Support for file renaming
In reply to Daniel VenzinFile renaming is necessary for features like systemd-boot bootcounting.
---
Changes in v2:
- Use EFI_ACCESS_DENIED for unsuccessful renames
- Avoid using of "== 0"
- Move the call to set_blk_dev earlier in the sequence
lib/efi_loader/efi_file.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-)
lib/efi_loader/efi_file.c +8 -3
@@ -969,6 +969,10 @@ static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file, ret =EFI_ACCESS_DENIED ; goto out; } +if (set_blk_dev (fh)) { + ret =EFI_DEVICE_ERROR ; + goto out; + }/* Check for renaming */ new_file_name =malloc (utf16_utf8_strlen (info->file_name) +1 );if (!new_file_name) {
@@ -978,10 +982,11 @@ static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file, pos = new_file_name;utf16_utf8_strcpy (&pos, info->file_name);if (strcmp (new_file_name, filename)) { -/* TODO: we do not support renaming */ -EFI_PRINT ("Renaming not supported\n" ); +if (fs_rename (fh->path, new_file_name) !=0 ) + ret =EFI_ACCESS_DENIED ; +else + ret =EFI_SUCCESS ;free (new_file_name); - ret =EFI_ACCESS_DENIED ; goto out; }free (new_file_name);
Signature · 1 line
2.47.2
PATCH v2 3/4 fs: fat: Refactor delete_dentry_long interface
In reply to Daniel VenzinDirectory entries cannot be deleted without also removing their corresponding FAT entries. This refactor allows for individual selection of deleting the directory entry and the FAT entry, facilitating better reuse of existing functionality.
---
Changes in v2:
- Refactor delete_dentry_long for improved reusability
fs/fat/fat_write.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
fs/fat/fat_write.c +16 -8
@@ -31,6 +31,10 @@ static const char LONG_ONLY_CHARS[] = "+,;=[]";/* Combined size of the name and ext fields in the directory entry */ #define SHORT_NAME_SIZE 11 +/* Flags to control the behavior of the delete_dentry_long(..) */ +#define FATENT_CLEAR_SKIP 0 +#define FATENT_CLEAR 1 +/** * str2fat() - convert string to valid FAT name characters *
@@ -1568,19 +1572,23 @@ static int delete_long_name(fat_itr *itr)* delete_dentry_long() - remove directory entry * * @itr: directory iterator +* @fatent: flag indicating whether to delete the FAT entry * Return: 0 for success */ -static int delete_dentry_long (fat_itr *itr) +static int delete_dentry_long (fat_itr *itr,int fatent) { -fsdata *mydata = itr->fsdata;dir_entry *dent = itr->dent; -/* free cluster blocks */ -clear_fatent (mydata,START (dent)); -if (flush_dirty_fat_buffer (mydata) <0 ) { -printf ("Error: flush fat buffer\n" ); -return -EIO ; +if (fatent) { +fsdata *mydata = itr->fsdata; +/* free cluster blocks */ +clear_fatent (mydata,START (dent)); +if (flush_dirty_fat_buffer (mydata) <0 ) { +printf ("Error: flush fat buffer\n" ); +return -EIO ; + } } +/* Position to first directory entry for long name */ if (itr->clust != itr->dent_clust) {int ret;
@@ -1661,7 +1669,7 @@ int fat_unlink(const char *filename) } } - ret =delete_dentry_long (itr); + ret =delete_dentry_long (itr,FATENT_CLEAR ); exit:free (fsdata.fatbuf);
Signature · 1 line
2.47.2
PATCH v2 4/4 fs: fat: Support basic file rename
In reply to Daniel VenzinThis implementation currently does not support
moving files between directories.
---
Changes in v2:
- Separate the rename implementation from changes in the filesystem layer
- Eliminate code duplication with delete_dentry_long(..)
- Ensure that the source file exists, but not the destination file
- Free the fatbuf if it has been reinitialized with fat_itr_root
- Delete orphaned FAT entry after copying file metadata
fs/fat/fat_write.c | 110 +++++++++++++++++++++++++++++++++++++++++++++ fs/fs.c | 2 +- include/fat.h | 1 + 3 files changed, 112 insertions(+), 1 deletion(-)
fs/fat/fat_write.c +110 -0
@@ -1821,3 +1821,113 @@ exit:free (dotdent);return ret; } + +int file_fat_rename (const char *path,const char *new_filename) +{ +fsdata datablock = { .fatbuf =NULL , }; +fat_itr *itr =NULL ; + +dir_entry *dentry; + +char new_path[VFAT_MAXLEN_BYTES ]; +char *path_copy, *dirname, *basename; + +loff_t actwrite; + +__u16 starthi, start; +__u32 size, entry; + +int ret =-1 ; + + path_copy =strdup (path); +if (!path_copy) + goto exit; + +split_filename (path_copy, &dirname, &basename); +snprintf (new_path,sizeof (new_path),"%s/%s" , dirname, new_filename); + +if (!fat_exists (path)) { + ret = -ENOENT ; + goto exit; + } + +if (fat_exists (new_path)) { + ret = -EEXIST ; + goto exit; + } + + ret =file_fat_write (new_path, &actwrite,0 ,0 , &actwrite); +if (ret) + goto exit; + + itr =malloc_cache_aligned (sizeof (*itr)); +if (!itr) { + ret = -ENOMEM ; + goto exit; + } + + ret =fat_itr_root (itr, &datablock); +if (ret) + goto exit; + + ret =fat_itr_resolve (itr, path,TYPE_FILE ); +if (ret) + goto exit; + + dentry = itr->dent; + + starthi = dentry->starthi; + start = dentry->start; + size = dentry->size; + +free (datablock.fatbuf); + datablock.fatbuf =NULL ; + + ret =fat_itr_root (itr, &datablock); +if (ret) + goto exit; + + ret =fat_itr_resolve (itr, new_path,TYPE_FILE ); +if (ret) + goto exit; + + dentry = itr->dent; + +fsdata *mydata = itr->fsdata; + + entry =START (dentry); + + dentry->starthi = starthi; + dentry->start = start; + dentry->size = size; + + ret =flush_dir (itr); +if (ret) + goto exit; + +clear_fatent (mydata, entry); +if (flush_dirty_fat_buffer (mydata) <0 ) { +printf ("Error: flush fat buffer\n" ); + ret = -EIO ; + goto exit; + } + +free (datablock.fatbuf); + datablock.fatbuf =NULL ; + + ret =fat_itr_root (itr, &datablock); +if (ret) + goto exit; + + ret =fat_itr_resolve (itr, path,TYPE_FILE ); +if (ret) + goto exit; + + ret =delete_dentry_long (itr,FATENT_CLEAR_SKIP ); + +exit: +free (path_copy); +free (datablock.fatbuf); +free (itr); +return ret; +}
fs/fs.c +1 -1
@@ -208,7 +208,7 @@ static struct fstype_info fstypes[] = { .write = file_fat_write, .unlink = fat_unlink, .mkdir = fat_mkdir, - .rename = fs_rename_unsupported, + .rename = file_fat_rename, #else .write = fs_write_unsupported, .unlink = fs_unlink_unsupported,
include/fat.h +1 -0
@@ -207,6 +207,7 @@ int fat_readdir(struct fs_dir_stream *dirs, struct fs_dirent **dentp);void fat_closedir (struct fs_dir_stream *dirs);int fat_unlink (const char *filename);int fat_mkdir (const char *dirname); +int file_fat_rename (const char *path,const char *new_filename);void fat_close (void );void *fat_next_cluster (fat_itr *itr,unsigned int *nbytes);
Signature · 1 line
2.47.2
Quoted 5 lines · Daniel Venzin
Rework of v1 posted by Burak Gerz [1].
[1] https://lore.kernel.org/u-boot/20241212221427.GM1505244@bill-the-cat/
Quoted 12 lines
Changes in v2:
- Adapt variable names for better consistency
- Add missing rename hooks
- UseEFI_ACCESS_DENIEDfor unsuccessful renames
- Avoid using of "== 0"
- Move the call toset_blk_devearlier in the sequence
- Refactordelete_dentry_longfor improved reusability
- Separate the rename implementation from changes in the filesystem layer
- Eliminate code duplication withdelete_dentry_long(..)
- Ensure that the source file exists, but not the destination file
- Free the fatbuf if it has been reinitialized withfat_itr_root
- Delete orphaned FAT entry after copying file metadata
This is looking good. We just need some tests now. Ideally something like adding test/py/tests/test_fs/test_rename.py and updating test/py/tests/test_fs/conftest.py to have supported_fs_rename, in the style of how symlink is handled via supported_fs_symlink and test/py/tests/test_fs/test_symlink.py, thanks!
Signature · 1 line
Tom