Skip to contentSkip to conversation

FAT file renaming

PATCH In review Daniel Venzin 2 participants 14 messages Feb 7, 2025 – Feb 8, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v3 cover FAT file renaming

This patch series represents a continuation of version 2 [1], incorporating tests and improvements.

[1] https://lore.kernel.org/u-boot/20250203211719.GX1233568@bill-the-cat/

Changes in v3:
- Abort the rename if the destination is a path instead of a file
- Add fatrnfile command for renaming files in a FAT file system
- Verify FAT file rename functionality with unit tests

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 (6):
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
cmd: fat: Add fatrnfile command
test/py: fs: Add fstest/rnfile test

 cmd/fat.c                            |  13 +++
 fs/fat/fat_write.c                   | 149 +++++++++++++++++++++++++--
 fs/fs.c                              |  50 +++++++++
 include/fat.h                        |   1 +
 include/fs.h                         |  11 ++
 lib/efi_loader/efi_file.c            |  11 +-
 test/py/tests/test_fs/conftest.py    |  75 ++++++++++++++
 test/py/tests/test_fs/test_rnfile.py |  93 +++++++++++++++++
 8 files changed, 392 insertions(+), 11 deletions(-)
 create mode 100644 test/py/tests/test_fs/test_rnfile.py
Signature · 1 line
2.39.5
#1 lore raw

PATCH v3 1/6 fs: Add unimplemented rename hook to FS layer

The EFI API supports file renaming, which is required by systemd-stub for its bootcounting implementation.

---

(no changes since v2)

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
index 99ddcc5e37b..b746d05ebcd 100644
@@ -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
index 2474880385d..8c1aa9c1a7b 100644
@@ -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.39.5
#2 lore raw

Re: [PATCH v3 1/6] fs: Add unimplemented rename hook to FS layer

Quoted 35 lines · Daniel Venzin

The EFI API supports file renaming, which is required by systemd-stub for its bootcounting implementation.

---

(no changes since v2)

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 +6 -0
index 99ddcc5e37b..b746d05ebcd 100644
@@ -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;

fs/fs.c struct fstype_info { · line 190

Quoted 3 lines
fs/fs.c +0 -0
@@ -182,6 +188,7 @@ struct fstype_info {
  	void (*closedir)(struct fs_dir_stream *dirs);
  	int (*unlink)(const char *filename);
  	int (*mkdir)(const char *dirname);

The structure and its elements should be documented.

The expected return values should be described.

Quoted 106 lines
fs/fs.c +24 -0
+	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 +0 -0
index 2474880385d..8c1aa9c1a7b 100644

include/fs.h int fs_unlink(const char *filename); · line 273

Quoted 4 lines
include/fs.h +1 -0
@@ -270,6 +270,15 @@ int fs_unlink(const char *filename);
   */
  int fs_mkdir(const char *filename);
 
+/*

/**

include/fs.h int fs_unlink(const char *filename); · line 274

Quoted 1 line
include/fs.h +1 -0
+ * fs_rename - Rename a file

fs_rename()

Please, have a look at
https://docs.kernel.org/doc-guide/kernel-doc.html#function-documentation

Please, describe that set_blk_dev() must be called before invoking fs_rename() and that fs_close() is called implicitly.

include/fs.h int fs_unlink(const char *filename); · line 278

Quoted 4 lines
include/fs.h +4 -0
+ *
+ * @old_filename: Name of the file to rename
+ * @new_filename: New name for the file
+ * Return: 0 on success, -1 on error conditions

Other file system functions return a negative error code like -ENOENT, -EIO, or -ENOMEM. Why do you require -1 here?

Best regards

Heinrich

Quoted 6 lines
include/fs.h +3 -0
+ */
+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.
#3 lore raw

Re: [PATCH v3 1/6] fs: Add unimplemented rename hook to FS layer

In reply to Daniel Venzin
Quoted 4 lines · Daniel Venzin

The EFI API supports file renaming, which is required by systemd-stub for its bootcounting implementation.

Having a separate rename and move functionality makes no sense to me.

The implementation for FAT would be nearly the same.

Quoted 140 lines

---

(no changes since v2)

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
index 99ddcc5e37b..b746d05ebcd 100644
@@ -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 +0 -0
index 2474880385d..8c1aa9c1a7b 100644

include/fs.h int fs_unlink(const char *filename); · line 274

Quoted 5 lines
include/fs.h +2 -0
@@ -270,6 +270,15 @@ int fs_unlink(const char *filename);
   */
  int fs_mkdir(const char *filename);
 
+/*
+ * fs_rename - Rename a file

fs_move() - move file

include/fs.h int fs_unlink(const char *filename); · line 277

Quoted 3 lines
include/fs.h +3 -0
+ *
+ * @old_filename: Name of the file to rename
+ * @new_filename: New name for the file

@old_path: path to file or directory to move
@new_path: new path to file or directory

Best regards

Heinrich

Quoted 7 lines
include/fs.h +4 -0
+ * 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.
#4 lore raw

PATCH v3 2/6 efi_loader: Support for file renaming

In reply to Daniel Venzin

File renaming is necessary for features like systemd-boot bootcounting.

---

(no changes since v2)

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
index 201fa5f8f3c..9a5a345bfc0 100644
@@ -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.39.5
#5 lore raw

Re: [PATCH v3 2/6] efi_loader: Support for file renaming

Quoted 35 lines · Daniel Venzin

File renaming is necessary for features like systemd-boot bootcounting.

---

(no changes since v2)

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 +4 -0
index 201fa5f8f3c..9a5a345bfc0 100644
@@ -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);

lib/efi_loader/efi_file.c static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file, · line 990

Quoted 10 lines
lib/efi_loader/efi_file.c +4 -3
  		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;

Why would you ignore truncation or any attribute change other than EFI_FILE_DIRECTORY here?

Best regards

Heinrich

Quoted 2 lines
lib/efi_loader/efi_file.c +0 -0
  		}
  		free(new_file_name);
#6 lore raw

PATCH v3 3/6 fs: fat: Refactor delete_dentry_long interface

In reply to Daniel Venzin

Directory 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.

---

(no changes since v2)

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
index ea877ee9171..c78b80e0b7a 100644
@@ -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.39.5
#7 lore raw

Re: [PATCH v3 3/6] fs: fat: Refactor delete_dentry_long interface

Quoted 41 lines · Daniel Venzin

Directory 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.

---

(no changes since v2)

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 +6 -1
index ea877ee9171..c78b80e0b7a 100644
@@ -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)

fs/fat/fat_write.c static int delete_long_name(fat_itr *itr) · line 1582

Quoted 10 lines
fs/fat/fat_write.c +1 -6
  {
-	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) {

Why do we define a constant, if we don't compare with FATENT_CLEAR here?

fs/fat/fat_write.c static int delete_long_name(fat_itr *itr) · line 1587

Quoted 5 lines
fs/fat/fat_write.c +5 -0
+		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");

Please, do not print in file system functions. This may mess up EFI application screen output.

log_debug() is fine.

Best regards

Heinrich

Quoted 16 lines
fs/fat/fat_write.c +4 -1
+			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);
#8 lore raw

PATCH v3 4/6 fs: fat: Support basic file rename

In reply to Daniel Venzin

This implementation currently does not support
moving files between directories.

---

Changes in v3:
- Abort the rename if the destination is a path instead of a file

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 | 125 +++++++++++++++++++++++++++++++++++++++++++++
 fs/fs.c            |   2 +-
 include/fat.h      |   1 +
 3 files changed, 127 insertions(+), 1 deletion(-)
fs/fat/fat_write.c +125 -0
index c78b80e0b7a..6e4ff7c57d4 100644
@@ -1821,3 +1821,128 @@ exit:
 	free(dotdent);
 	return ret;
 }
+
+int file_fat_rename(const char *path, const char *new_fname)
+{
+	fsdata datablock = { .fatbuf = NULL, };
+	fat_itr *itr = NULL;
+
+	dir_entry *dentry;
+
+	char new_path[VFAT_MAXLEN_BYTES];
+	char *new_fname_copy = NULL, *new_fname_dirname, *new_fname_basename;
+	char *path_copy = NULL, *path_dirname, *path_basename;
+
+	loff_t actwrite;
+
+	__u16 starthi, start;
+	__u32 size, entry;
+
+	int ret = -1;
+
+	new_fname_copy = strdup(new_fname);
+	if (!new_fname_copy)
+		goto exit;
+
+	split_filename(new_fname_copy, &new_fname_dirname, &new_fname_basename);
+
+	if (strlen(new_fname_dirname) > 1 ||
+		(strcmp(new_fname_dirname, "/") != 0 &&
+		 strcmp(new_fname_dirname, ".") != 0)) {
+		ret = -EINVAL;
+		goto exit;
+	}
+
+	path_copy = strdup(path);
+	if (!path_copy)
+		goto exit;
+
+	split_filename(path_copy, &path_dirname, &path_basename);
+	snprintf(new_path, sizeof(new_path), "%s/%s", path_dirname, new_fname_basename);
+
+	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(new_fname_copy);
+	free(path_copy);
+	free(datablock.fatbuf);
+	free(itr);
+	return ret;
+}
fs/fs.c +1 -1
index b746d05ebcd..3576d5c6644 100644
@@ -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
index 3dce99a23cf..415ebd7b7c8 100644
@@ -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.39.5
#9 lore raw

Re: [PATCH v3 4/6] fs: fat: Support basic file rename

Quoted 2 lines · Daniel Venzin

This implementation currently does not support
moving files between directories.

Why would we make such a restriction?

Quoted 25 lines

---

Changes in v3:
- Abort the rename if the destination is a path instead of a file

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 | 125 +++++++++++++++++++++++++++++++++++++++++++++
  fs/fs.c            |   2 +-
  include/fat.h      |   1 +
  3 files changed, 127 insertions(+), 1 deletion(-)
fs/fat/fat_write.c +0 -0
index c78b80e0b7a..6e4ff7c57d4 100644

fs/fat/fat_write.c exit: · line 1825

Quoted 5 lines
fs/fat/fat_write.c +2 -0
@@ -1821,3 +1821,128 @@ exit:
  	free(dotdent);
  	return ret;
  }
+
+int file_fat_rename(const char *path, const char *new_fname)

Below you are expecting that new_fname is a path not only a file name.

%s/new_fname/new_path/

Quoted 16 lines
fs/fat/fat_write.c +16 -0
+{
+	fsdata datablock = { .fatbuf = NULL, };
+	fat_itr *itr = NULL;
+
+	dir_entry *dentry;
+
+	char new_path[VFAT_MAXLEN_BYTES];
+	char *new_fname_copy = NULL, *new_fname_dirname, *new_fname_basename;
+	char *path_copy = NULL, *path_dirname, *path_basename;
+
+	loff_t actwrite;
+
+	__u16 starthi, start;
+	__u32 size, entry;
+
+	int ret = -1;

fs/fat/fat_write.c exit: · line 1851

Quoted 10 lines
fs/fat/fat_write.c +10 -0
+
+	new_fname_copy = strdup(new_fname);
+	if (!new_fname_copy)
+		goto exit;
+
+	split_filename(new_fname_copy, &new_fname_dirname, &new_fname_basename);
+
+	if (strlen(new_fname_dirname) > 1 ||
+		(strcmp(new_fname_dirname, "/") != 0 &&
+		 strcmp(new_fname_dirname, ".") != 0)) {

No clue, why you expect "/" or "." at the beginning of a path. In U-Boot file paths are always relative to the root directory.

Shouldn't fat_itr_resolve() take care of checking the validity of the path?

fs/fat/fat_write.c exit: · line 1861

Quoted 10 lines
fs/fat/fat_write.c +10 -0
+		ret = -EINVAL;
+		goto exit;
+	}
+
+	path_copy = strdup(path);
+	if (!path_copy)
+		goto exit;
+
+	split_filename(path_copy, &path_dirname, &path_basename);
+	snprintf(new_path, sizeof(new_path), "%s/%s", path_dirname, new_fname_basename);

You already determined new_fname_dirname. You must not ignore it. You should delete above statement.

Quoted 16 lines
fs/fat/fat_write.c +16 -0
+
+	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));

fs/fat/fat_write.c exit: · line 1887

Quoted 10 lines
fs/fat/fat_write.c +10 -0
+	if (!itr) {
+		ret = -ENOMEM;
+		goto exit;
+	}
+
+	ret = fat_itr_root(itr, &datablock);
+	if (ret)
+		goto exit;
+
+	ret = fat_itr_resolve(itr, path, TYPE_FILE);

Why don't we allow renaming of directories?

TYPE_FILE | TYPE_DIR

Quoted 7 lines
fs/fat/fat_write.c +7 -0
+	if (ret)
+		goto exit;
+
+	dentry = itr->dent;
+
+	starthi = dentry->starthi;
+	start = dentry->start;

fs/fat/fat_write.c exit: · line 1904

Quoted 10 lines
fs/fat/fat_write.c +10 -0
+	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);

This would not detect if new_path points to an existing or non-existing directory.

Check that new_fname_dirname exists and then use the same itr to check that new_fname_dirname/new_basename does not exist.

This avoids iterating once here and once in fat_rename().

fs/fat/fat_write.c exit: · line 1906

Quoted 2 lines
fs/fat/fat_write.c +2 -0
+	if (ret)
+		goto exit;

We don't want to overwrite existing files or directories. Don't we expect -ENOENT or -ENOTDIR here?

Quoted 22 lines
fs/fat/fat_write.c +22 -0
+
+	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;
+	}
+

fs/fat/fat_write.c exit: · line 1938

Quoted 10 lines
fs/fat/fat_write.c +10 -0
+	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;

Why don't you use separate variables for the old and the new itr value? Then you would not have to resolve the old path twice.

Best regards

Heinrich

Quoted 34 lines
fs/fat/fat_write.c +9 -0
+
+	ret = delete_dentry_long(itr, FATENT_CLEAR_SKIP);> +
+exit:
+	free(new_fname_copy);
+	free(path_copy);
+	free(datablock.fatbuf);
+	free(itr);
+	return ret;
+}
fs/fs.c +1 -1
index b746d05ebcd..3576d5c6644 100644
@@ -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
index 3dce99a23cf..415ebd7b7c8 100644
@@ -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);
 
#10 lore raw

Re: [PATCH v3 4/6] fs: fat: Support basic file rename

Quoted 90 lines · Heinrich Schuchardt
Quoted 2 lines · Daniel Venzin

This implementation currently does not support
moving files between directories.

Why would we make such a restriction?

Quoted 25 lines

---

Changes in v3:
- Abort the rename if the destination is a path instead of a file

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 | 125 +++++++++++++++++++++++++++++++++++++++++++++ fs/fs.c            |   2 +-
  include/fat.h      |   1 +

  3 files changed, 127 insertions(+), 1 deletion(-)
fs/fat/fat_write.c +0 -0
index c78b80e0b7a..6e4ff7c57d4 100644

fs/fat/fat_write.c exit: · line 1825

Quoted 5 lines
fs/fat/fat_write.c +2 -0
@@ -1821,3 +1821,128 @@ exit:
       free(dotdent);
       return ret;
   }
+
+int file_fat_rename(const char *path, const char *new_fname)

Below you are expecting that new_fname is a path not only a file name.

%s/new_fname/new_path/

Quoted 23 lines
fs/fat/fat_write.c +8 -0
+{
+    fsdata datablock = { .fatbuf = NULL, };
+    fat_itr *itr = NULL;
+
+    dir_entry *dentry;
+
+    char new_path[VFAT_MAXLEN_BYTES];
+    char *new_fname_copy = NULL, *new_fname_dirname,

*new_fname_basename;
+    char *path_copy = NULL, *path_dirname, *path_basename;

fs/fat/fat_write.c +13 -0
+
+    loff_t actwrite;
+
+    __u16 starthi, start;
+    __u32 size, entry;
+
+    int ret = -1;
+
+    new_fname_copy = strdup(new_fname);
+    if (!new_fname_copy)
+        goto exit;
+
+    split_filename(new_fname_copy, &new_fname_dirname,
Quoted 5 lines

&new_fname_basename);
+
+    if (strlen(new_fname_dirname) > 1 ||
+        (strcmp(new_fname_dirname, "/") != 0 &&
+         strcmp(new_fname_dirname, ".") != 0)) {

No clue, why you expect "/" or "." at the beginning of a path. In U-Boot file paths are always relative to the root directory.

Shouldn't fat_itr_resolve() take care of checking the validity of the path?

Quoted 11 lines

+        ret = -EINVAL;
+        goto exit;
+    }

fs/fat/fat_write.c +7 -0
+
+    path_copy = strdup(path);
+    if (!path_copy)
+        goto exit;
+
+    split_filename(path_copy, &path_dirname, &path_basename);
+    snprintf(new_path, sizeof(new_path), "%s/%s", path_dirname,

new_fname_basename);

You already determined new_fname_dirname. You must not ignore it. You should delete above statement.

Quoted 10 lines

fs/fat/fat_write.c exit: · line 1871

Quoted 10 lines
fs/fat/fat_write.c +10 -0
+
+    if (!fat_exists(path)) {
+        ret = -ENOENT;
+        goto exit;
+    }
+
+    if (fat_exists(new_path)) {
+        ret = -EEXIST;
+        goto exit;
+    }

Please, do not iterate multiple times over the same paths. You already call far_itr_resolve() below.

Best regards

Heinrich

Quoted 129 lines
Quoted 6 lines
fs/fat/fat_write.c +6 -0
+
+    ret = file_fat_write(new_path, &actwrite, 0, 0, &actwrite);
+    if (ret)
+        goto exit;
+
+    itr = malloc_cache_aligned(sizeof(*itr));

fs/fat/fat_write.c exit: · line 1887

Quoted 10 lines
fs/fat/fat_write.c +10 -0
+    if (!itr) {
+        ret = -ENOMEM;
+        goto exit;
+    }
+
+    ret = fat_itr_root(itr, &datablock);
+    if (ret)
+        goto exit;
+
+    ret = fat_itr_resolve(itr, path, TYPE_FILE);

Why don't we allow renaming of directories?

TYPE_FILE | TYPE_DIR

Quoted 7 lines

+    if (ret)
+        goto exit;

fs/fat/fat_write.c +5 -0
+
+    dentry = itr->dent;
+
+    starthi = dentry->starthi;
+    start = dentry->start;

fs/fat/fat_write.c exit: · line 1904

Quoted 10 lines
fs/fat/fat_write.c +10 -0
+    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);

This would not detect if new_path points to an existing or non-existing directory.

Check that new_fname_dirname exists and then use the same itr to check that new_fname_dirname/new_basename does not exist.

This avoids iterating once here and once in fat_rename().

Quoted 2 lines

+    if (ret)
+        goto exit;

We don't want to overwrite existing files or directories. Don't we expect -ENOENT or -ENOTDIR here?

Quoted 22 lines
fs/fat/fat_write.c +22 -0
+
+    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;
+    }
+

fs/fat/fat_write.c exit: · line 1938

Quoted 10 lines
fs/fat/fat_write.c +10 -0
+    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;

Why don't you use separate variables for the old and the new itr value? Then you would not have to resolve the old path twice.

Best regards

Heinrich

Quoted 35 lines
fs/fat/fat_write.c +9 -0
+
+    ret = delete_dentry_long(itr, FATENT_CLEAR_SKIP);> +
+exit:
+    free(new_fname_copy);
+    free(path_copy);
+    free(datablock.fatbuf);
+    free(itr);
+    return ret;
+}
fs/fs.c +1 -1
index b746d05ebcd..3576d5c6644 100644
@@ -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
index 3dce99a23cf..415ebd7b7c8 100644
@@ -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);
#11 lore raw

PATCH v3 5/6 cmd: fat: Add fatrnfile command

In reply to Daniel Venzin

The FAT rename file command supports renaming of a file, used by fs_test test suite.

---

Changes in v3:
- Add fatrnfile command for renaming files in a FAT file system

 cmd/fat.c    | 13 +++++++++++++
 fs/fs.c      | 20 ++++++++++++++++++++
 include/fs.h |  2 ++
 3 files changed, 35 insertions(+)
cmd/fat.c +13 -0
index 5b7484dc1af..c66583cb427 100644
@@ -132,4 +132,17 @@ U_BOOT_CMD(
 	"<interface> [<dev[:part]>] <directory>\n"
 	"    - create a directory in 'dev' on 'interface'"
 );
+
+static int do_fat_rnfile(struct cmd_tbl *cmdtp, int flag, int argc,
+			char *const argv[])
+{
+	return do_rnfile(cmdtp, flag, argc, argv, FS_TYPE_FAT);
+}
+
+U_BOOT_CMD(
+	fatrnfile,	5,	1,	do_fat_rnfile,
+	"rename a file",
+	"<interface> [<dev[:part]>] <source> <destination>\n"
+	"    - rename <source> to <destination> in 'dev' on 'interface'"
+);
 #endif
fs/fs.c +20 -0
index 3576d5c6644..fafe00cdb9c 100644
@@ -990,6 +990,26 @@ int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
 	return 0;
 }
 
+int do_rnfile(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
+	     int fstype)
+{
+	int ret;
+
+	if (argc != 5)
+		return CMD_RET_USAGE;
+
+	if (fs_set_blk_dev(argv[1], argv[2], fstype))
+		return 1;
+
+	ret = fs_rename(argv[3], argv[4]);
+	if (ret) {
+		log_err("** Unable to rename file from \"%s\" to \"%s\" **\n", argv[3], argv[4]);
+		return 1;
+	}
+
+	return 0;
+}
+
 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
 	  int fstype)
 {
include/fs.h +2 -0
index 8c1aa9c1a7b..79dc442ea75 100644
@@ -297,6 +297,8 @@ int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
 	  int fstype);
 int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
 	     int fstype);
+int do_rnfile(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
+	     int fstype);
 int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
 	  int fstype);
 
Signature · 1 line
2.39.5
#12 lore raw

Re: [PATCH v3 5/6] cmd: fat: Add fatrnfile command

Quoted 50 lines · Daniel Venzin

The FAT rename file command supports renaming of a file, used by fs_test test suite.

---

Changes in v3:
- Add fatrnfile command for renaming files in a FAT file system

  cmd/fat.c    | 13 +++++++++++++
  fs/fs.c      | 20 ++++++++++++++++++++
  include/fs.h |  2 ++
  3 files changed, 35 insertions(+)
cmd/fat.c +13 -0
index 5b7484dc1af..c66583cb427 100644
@@ -132,4 +132,17 @@ U_BOOT_CMD(
  	"<interface> [<dev[:part]>] <directory>\n"
  	"    - create a directory in 'dev' on 'interface'"
  );
+
+static int do_fat_rnfile(struct cmd_tbl *cmdtp, int flag, int argc,
+			char *const argv[])
+{
+	return do_rnfile(cmdtp, flag, argc, argv, FS_TYPE_FAT);
+}
+
+U_BOOT_CMD(
+	fatrnfile,	5,	1,	do_fat_rnfile,
+	"rename a file",
+	"<interface> [<dev[:part]>] <source> <destination>\n"
+	"    - rename <source> to <destination> in 'dev' on 'interface'"
+);
  #endif
fs/fs.c +5 -0
index 3576d5c6644..fafe00cdb9c 100644
@@ -990,6 +990,26 @@ int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  	return 0;
  }
 
+int do_rnfile(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
+	     int fstype)
+{
+	int ret;
+

fs/fs.c int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[], · line 1007

Quoted 10 lines
fs/fs.c +10 -0
+	if (argc != 5)
+		return CMD_RET_USAGE;
+
+	if (fs_set_blk_dev(argv[1], argv[2], fstype))
+		return 1;
+
+	ret = fs_rename(argv[3], argv[4]);
+	if (ret) {
+		log_err("** Unable to rename file from \"%s\" to \"%s\" **\n", argv[3], argv[4]);
+		return 1;

The test should test not only test the positive case but also the cases where we expect moving or renaming a file to fail.

Best regards

Heinrich

Quoted 21 lines
fs/fs.c +5 -0
+	}
+
+	return 0;
+}
+
  int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  	  int fstype)
  {
include/fs.h +2 -0
index 8c1aa9c1a7b..79dc442ea75 100644
@@ -297,6 +297,8 @@ int do_rm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  	  int fstype);
  int do_mkdir(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  	     int fstype);
+int do_rnfile(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
+	     int fstype);
  int do_ln(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[],
  	  int fstype);
 
#13 lore raw

PATCH v3 6/6 test/py: fs: Add fstest/rnfile test

In reply to Daniel Venzin

Extend the test_fs test suite with tests to verify operations related to FAT file renaming.

---

Changes in v3:
- Verify FAT file rename functionality with unit tests

 test/py/tests/test_fs/conftest.py    | 75 ++++++++++++++++++++++
 test/py/tests/test_fs/test_rnfile.py | 93 ++++++++++++++++++++++++++++
 2 files changed, 168 insertions(+)
 create mode 100644 test/py/tests/test_fs/test_rnfile.py
test/py/tests/test_fs/conftest.py +75 -0
index af2adaf1645..e9b08c693b2 100644
@@ -16,6 +16,7 @@ supported_fs_basic = ['fat16', 'fat32', 'ext4']
 supported_fs_ext = ['fat12', 'fat16', 'fat32']
 supported_fs_fat = ['fat12', 'fat16']
 supported_fs_mkdir = ['fat12', 'fat16', 'fat32']
+supported_fs_rnfile = ['fat12', 'fat16', 'fat32']
 supported_fs_unlink = ['fat12', 'fat16', 'fat32']
 supported_fs_symlink = ['ext4']
 
@@ -53,6 +54,7 @@ def pytest_configure(config):
     global supported_fs_ext
     global supported_fs_fat
     global supported_fs_mkdir
+    global supported_fs_rnfile
     global supported_fs_unlink
     global supported_fs_symlink
 
@@ -66,6 +68,7 @@ def pytest_configure(config):
         supported_fs_ext =  intersect(supported_fs, supported_fs_ext)
         supported_fs_fat =  intersect(supported_fs, supported_fs_fat)
         supported_fs_mkdir =  intersect(supported_fs, supported_fs_mkdir)
+        supported_fs_rnfile = intersect(supported_fs, supported_fs_rnfile)
         supported_fs_unlink =  intersect(supported_fs, supported_fs_unlink)
         supported_fs_symlink =  intersect(supported_fs, supported_fs_symlink)
 
@@ -93,6 +96,9 @@ def pytest_generate_tests(metafunc):
     if 'fs_obj_mkdir' in metafunc.fixturenames:
         metafunc.parametrize('fs_obj_mkdir', supported_fs_mkdir,
             indirect=True, scope='module')
+    if 'fs_obj_rnfile' in metafunc.fixturenames:
+        metafunc.parametrize('fs_obj_rnfile', supported_fs_rnfile,
+            indirect=True, scope='module')
     if 'fs_obj_unlink' in metafunc.fixturenames:
         metafunc.parametrize('fs_obj_unlink', supported_fs_unlink,
             indirect=True, scope='module')
@@ -388,6 +394,75 @@ def fs_obj_mkdir(request, u_boot_config):
         yield [fs_ubtype, fs_img]
     call('rm -f %s' % fs_img, shell=True)
 
+#
+# Fixture for rnfile test
+#
[email protected]()
+def fs_obj_rnfile(request, u_boot_config):
+    """Set up a file system to be used in rnfile test.
+
+    Args:
+        request: Pytest request object.
+	u_boot_config: U-Boot configuration.
+
+    Return:
+        A fixture for rnfile test, i.e. a duplet of file system type and
+        volume file name.
+    """
+    fs_type = request.param
+    fs_img = ''
+
+    fs_ubtype = fstype_to_ubname(fs_type)
+    check_ubconfig(u_boot_config, fs_ubtype)
+
+    scratch_dir = u_boot_config.persistent_data_dir + '/scratch'
+
+    try:
+        check_call('mkdir -p %s' % scratch_dir, shell=True)
+    except CalledProcessError as err:
+        pytest.skip('Preparing mount folder failed for filesystem: ' + fs_type + '. {}'.format(err))
+        call('rm -f %s' % fs_img, shell=True)
+        return
+
+    try:
+        # Test Case 1
+        check_call('mkdir %s/dir1' % scratch_dir, shell=True)
+        check_call('dd if=/dev/urandom of=%s/dir1/destination bs=1K count=1'
+                                    % scratch_dir, shell=True)
+
+        # Test Case 2
+        check_call('mkdir %s/dir2' % scratch_dir, shell=True)
+        check_call('dd if=/dev/urandom of=%s/dir2/source bs=1K count=1'
+                                    % scratch_dir, shell=True)
+        check_call('dd if=/dev/urandom of=%s/dir2/destination bs=1K count=1'
+                                    % scratch_dir, shell=True)
+
+        # Test Case 3
+        check_call('mkdir %s/dir3' % scratch_dir, shell=True)
+        check_call('dd if=/dev/urandom of=%s/dir3/source bs=1K count=1'
+                                    % scratch_dir, shell=True)
+
+        # Test Case 4
+        check_call('mkdir %s/dir4' % scratch_dir, shell=True)
+        check_call('dd if=/dev/urandom of=%s/dir4/source bs=1K count=1'
+                                    % scratch_dir, shell=True)
+
+        try:
+            # 128MiB volume
+            fs_img = fs_helper.mk_fs(u_boot_config, fs_type, 0x8000000, '128MB', scratch_dir)
+        except CalledProcessError as err:
+            pytest.skip('Creating failed for filesystem: ' + fs_type + '. {}'.format(err))
+            return
+
+    except CalledProcessError:
+        pytest.skip('Setup failed for filesystem: ' + fs_type)
+        return
+    else:
+        yield [fs_ubtype, fs_img]
+    finally:
+        call('rm -rf %s' % scratch_dir, shell=True)
+        call('rm -f %s' % fs_img, shell=True)
+
 #
 # Fixture for unlink test
 #
test/py/tests/test_fs/test_rnfile.py new file +93 -0

93 lines not on this page · raw message

Signature · 1 line
2.39.5