All of lore.kernel.org
 help / color / mirror / Atom feed
From: Akashi, Takahiro <takahiro.akashi@linaro.org>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v3 18/26] fs: fat: support unlink
Date: Tue, 11 Sep 2018 15:59:14 +0900	[thread overview]
Message-ID: <20180911065922.19141-19-takahiro.akashi@linaro.org> (raw)
In-Reply-To: <20180911065922.19141-1-takahiro.akashi@linaro.org>

From: AKASHI Takahiro <takahiro.akashi@linaro.org>

In this patch, unlink support is added to FAT file system.
A directory can be deleted only if it is empty.

In this implementation, only a directory entry for a short file name
will be removed. So entries for a long file name can and should be
reclaimed with fsck.

Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>
---
 fs/fat/fat_write.c | 132 +++++++++++++++++++++++++++++++++++++++++++++
 fs/fs.c            |   3 +-
 include/fat.h      |   1 +
 3 files changed, 135 insertions(+), 1 deletion(-)

diff --git a/fs/fat/fat_write.c b/fs/fat/fat_write.c
index 035469f31c8d..6d3d2d1abb04 100644
--- a/fs/fat/fat_write.c
+++ b/fs/fat/fat_write.c
@@ -1184,6 +1184,138 @@ int file_fat_write(const char *filename, void *buffer, loff_t offset,
 	return file_fat_write_at(filename, offset, buffer, maxsize, actwrite);
 }
 
+static int fat_dir_entries(fat_itr *itr)
+{
+	fat_itr *dirs;
+	fsdata fsdata = { .fatbuf = NULL, }, *mydata = &fsdata;
+						/* for FATBUFSIZE */
+	int count;
+
+	dirs = malloc_cache_aligned(sizeof(fat_itr));
+	if (!dirs) {
+		debug("Error: allocating memory\n");
+		count = -ENOMEM;
+		goto exit;
+	}
+
+	/* duplicate fsdata */
+	fat_itr_child(dirs, itr);
+	fsdata = *dirs->fsdata;
+
+	/* allocate local fat buffer */
+	fsdata.fatbuf = malloc_cache_aligned(FATBUFSIZE);
+	if (!fsdata.fatbuf) {
+		debug("Error: allocating memory\n");
+		count = -ENOMEM;
+		goto exit;
+	}
+	fsdata.fatbufnum = -1;
+	dirs->fsdata = &fsdata;
+
+	for (count = 0; fat_itr_next(dirs); count++)
+		;
+
+exit:
+	free(fsdata.fatbuf);
+	free(dirs);
+	return count;
+}
+
+static int delete_dentry(fat_itr *itr)
+{
+	fsdata *mydata = itr->fsdata;
+	dir_entry *dentptr = itr->dent;
+
+	/* free cluster blocks */
+	clear_fatent(mydata, START(dentptr));
+	if (flush_dirty_fat_buffer(mydata) < 0) {
+		printf("Error: flush fat buffer\n");
+		return -EIO;
+	}
+
+	/*
+	 * update a directory entry
+	 * TODO:
+	 *  - long file name support
+	 *  - find and mark the "new" first invalid entry as name[0]=0x00
+	 */
+	memset(dentptr, 0, sizeof(*dentptr));
+	dentptr->name[0] = 0xe5;
+
+	if (set_cluster(mydata, itr->clust, itr->block,
+			mydata->clust_size * mydata->sect_size) != 0) {
+		printf("error: writing directory entry\n");
+		return -EIO;
+	}
+
+	return 0;
+}
+
+int fat_unlink(const char *filename)
+{
+	fsdata fsdata = { .fatbuf = NULL, };
+	fat_itr *itr = NULL;
+	int n_entries, ret;
+	char *filename_copy, *dirname, *basename;
+
+	filename_copy = strdup(filename);
+	split_filename(filename_copy, &dirname, &basename);
+
+	if (!strcmp(dirname, "/") && !strcmp(basename, "")) {
+		printf("Error: cannot remove root\n");
+		ret = -EINVAL;
+		goto exit;
+	}
+
+	itr = malloc_cache_aligned(sizeof(fat_itr));
+	if (!itr) {
+		printf("Error: allocating memory\n");
+		return -ENOMEM;
+	}
+
+	ret = fat_itr_root(itr, &fsdata);
+	if (ret)
+		goto exit;
+
+	total_sector = fsdata.total_sect;
+
+	ret = fat_itr_resolve(itr, dirname, TYPE_DIR);
+	if (ret) {
+		printf("%s: doesn't exist (%d)\n", dirname, ret);
+		ret = -ENOENT;
+		goto exit;
+	}
+
+	if (!find_directory_entry(itr, basename)) {
+		printf("%s: doesn't exist\n", basename);
+		ret = -ENOENT;
+		goto exit;
+	}
+
+	if (fat_itr_isdir(itr)) {
+		n_entries = fat_dir_entries(itr);
+		if (n_entries < 0) {
+			ret = n_entries;
+			goto exit;
+		}
+		if (n_entries > 2) {
+			printf("Error: directory is not empty: %d\n",
+			       n_entries);
+			ret = -EINVAL;
+			goto exit;
+		}
+	}
+
+	ret = delete_dentry(itr);
+
+exit:
+	free(fsdata.fatbuf);
+	free(itr);
+	free(filename_copy);
+
+	return ret;
+}
+
 int fat_mkdir(const char *new_dirname)
 {
 	dir_entry *retdent;
diff --git a/fs/fs.c b/fs/fs.c
index ba9a65166c70..adae98d021ee 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -170,16 +170,17 @@ static struct fstype_info fstypes[] = {
 		.read = fat_read_file,
 #ifdef CONFIG_FAT_WRITE
 		.write = file_fat_write,
+		.unlink = fat_unlink,
 		.mkdir = fat_mkdir,
 #else
 		.write = fs_write_unsupported,
+		.unlink = fs_unlink_unsupported,
 		.mkdir = fs_mkdir_unsupported,
 #endif
 		.uuid = fs_uuid_unsupported,
 		.opendir = fat_opendir,
 		.readdir = fat_readdir,
 		.closedir = fat_closedir,
-		.unlink = fs_unlink_unsupported,
 	},
 #endif
 #ifdef CONFIG_FS_EXT4
diff --git a/include/fat.h b/include/fat.h
index 97460a3cdff1..ca92a735b4fd 100644
--- a/include/fat.h
+++ b/include/fat.h
@@ -205,6 +205,7 @@ int fat_read_file(const char *filename, void *buf, loff_t offset, loff_t len,
 int fat_opendir(const char *filename, struct fs_dir_stream **dirsp);
 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);
 void fat_close(void);
 #endif /* CONFIG_FS_FAT */
-- 
2.18.0

  parent reply	other threads:[~2018-09-11  6:59 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-11  6:58 [U-Boot] [PATCH v3 00/26] subject: fs: fat: extend FAT write operations Akashi, Takahiro
2018-09-11  6:58 ` [U-Boot] [PATCH v3 01/26] fs: fat: guard the content of include/fat.h Akashi, Takahiro
2018-09-11 10:12   ` Alexander Graf
2018-09-12  0:53     ` Akashi, Takahiro
2018-09-12  5:41       ` Alexander Graf
2018-09-12  6:55   ` [U-Boot] [PATCH v3.1 " Akashi, Takahiro
2018-09-23 14:42     ` Alexander Graf
2018-09-25  4:54       ` Akashi, Takahiro
2018-09-11  6:58 ` [U-Boot] [PATCH v3 02/26] fs: fat: extend get_fs_info() for write use Akashi, Takahiro
2018-09-11  6:58 ` [U-Boot] [PATCH v3 03/26] fs: fat: handle "." and ".." of root dir correctly with fat_itr_resolve() Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 04/26] fs: fat: assure iterator's ->dent belongs to ->clust Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 05/26] Revert "fs: fat: cannot write to subdirectories" Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 06/26] fs: fat: check and normalize file name Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 07/26] fs: fat: write returns error code instead of -1 Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 08/26] fs: fat: support write with sub-directory path Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 09/26] fs: fat: refactor write interface for a file offset Akashi, Takahiro
2019-03-12  8:41   ` [U-Boot] [BUG] cb8af8af5ba0 "fs: fat: support write with non-zero offset" fatwrite followed by fatload and then cmp fails Faiz Abbas
2019-03-13 17:11     ` Rizvi, Mohammad Faiz Abbas
2019-03-13 17:25       ` Tom Rini
2019-03-18  1:42     ` Akashi, Takahiro
2019-03-18  1:44       ` Tom Rini
2019-03-18  1:57         ` Akashi, Takahiro
2019-03-18  1:59           ` Tom Rini
2019-03-21  6:50             ` Faiz Abbas
2019-03-22  9:13               ` Faiz Abbas
2018-09-11  6:59 ` [U-Boot] [PATCH v3 10/26] fs: fat: support write with non-zero offset Akashi, Takahiro
2018-09-11 11:09   ` Alexander Graf
2018-09-12  2:14     ` Akashi, Takahiro
2018-09-12  5:42       ` Alexander Graf
2018-10-15 11:42   ` Jean-Jacques Hiblot
2018-10-31 10:00     ` Clément Péron
2018-10-31 12:22       ` Alexander Graf
2018-10-31 20:54         ` Heinrich Schuchardt
2018-11-01  5:11           ` AKASHI Takahiro
2018-11-05 16:44             ` Clément Péron
2019-02-25 18:20   ` [U-Boot] [BUG] cb8af8af5ba0 "fs: fat: support write with non-zero offset" leads to link error Heinrich Schuchardt
2018-09-11  6:59 ` [U-Boot] [PATCH v3 11/26] cmd: fat: add offset parameter to fatwrite Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 12/26] fs: add mkdir interface Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 13/26] fs: fat: remember the starting cluster number of directory Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 14/26] fs: fat: support mkdir Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 15/26] cmd: fat: add fatmkdir command Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 16/26] efi_loader: file: support creating a directory Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 17/26] fs: add unlink interface Akashi, Takahiro
2018-09-11  6:59 ` Akashi, Takahiro [this message]
2018-09-11  6:59 ` [U-Boot] [PATCH v3 19/26] cmd: fat: add fatrm command Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 20/26] efi_loader: implement a file delete Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 21/26] fs-test: fix false positive error at Test Case 12 Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 22/26] fs-test: update the test result as of v2018.09 Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 23/26] test/py: convert fs-test.sh to pytest Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 24/26] test/py: fs: add extended write operation test Akashi, Takahiro
2018-09-11  6:59 ` [U-Boot] [PATCH v3 25/26] test/py: fs: add fstest/mkdir test Akashi, Takahiro

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180911065922.19141-19-takahiro.akashi@linaro.org \
    --to=takahiro.akashi@linaro.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.