shithub: lwext4

Download patch

ref: 723951999f8d6037ef2576064982176338350915
parent: 73a57bbf40e9acd4d7200d3c9693d02b727aca9b
author: ngkaho1234 <ngkaho1234@gmail.com>
date: Sat Jan 16 08:18:03 EST 2016

ext4: introduce ext4_trunc_dir specific to removing directory.

--- a/lwext4/ext4.c
+++ b/lwext4/ext4.c
@@ -834,6 +834,39 @@
 	return r;
 }
 
+static int ext4_trunc_dir(struct ext4_mountpoint *mp,
+			  struct ext4_inode_ref *parent,
+			  struct ext4_inode_ref *dir)
+{
+	int r = EOK;
+	bool is_dir = ext4_inode_is_type(&mp->fs.sb, dir->inode,
+			EXT4_INODE_MODE_DIRECTORY);
+	uint32_t block_size = ext4_sb_get_block_size(&mp->fs.sb);
+	if (!is_dir)
+		return EINVAL;
+
+#if CONFIG_DIR_INDEX_ENABLE
+	/* Initialize directory index if supported */
+	if (ext4_sb_feature_com(&mp->fs.sb, EXT4_FCOM_DIR_INDEX)) {
+		r = ext4_dir_dx_init(dir, parent);
+		if (r != EOK)
+			return r;
+
+		r = ext4_trunc_inode(mp, dir->index,
+				     EXT4_DIR_DX_INIT_BCNT * block_size);
+		if (r != EOK)
+			return r;
+	} else
+#endif
+	{
+		r = ext4_trunc_inode(mp, dir->index, block_size);
+		if (r != EOK)
+			return r;
+	}
+
+	return ext4_fs_truncate_inode(dir, 0);
+}
+
 /*
  * NOTICE: if filetype is equal to EXT4_DIRENTRY_UNKNOWN,
  * any filetype of the target dir entry will be accepted.
@@ -2632,6 +2665,7 @@
 				/*Get child inode reference do unlink
 				 * directory/file.*/
 				uint32_t cinode;
+				uint32_t inode_type;
 				cinode = ext4_dir_en_get_inode(it.curr);
 				r = ext4_fs_get_inode_ref(fs, cinode, &child);
 				if (r != EOK)
@@ -2653,9 +2687,15 @@
 					ext4_fs_put_inode_ref(&child);
 					goto End;
 				}
+				inode_type = ext4_inode_type(&mp->fs.sb,
+						child.inode);
 
 				/* Truncate */
-				r = ext4_fs_truncate_inode(&child, 0);
+				if (inode_type != EXT4_INODE_MODE_DIRECTORY)
+					r = ext4_trunc_inode(mp, child.index, 0);
+				else
+					r = ext4_trunc_dir(mp, &act, &child);
+
 				if (r != EOK) {
 					ext4_fs_put_inode_ref(&child);
 					goto End;
@@ -2750,8 +2790,8 @@
 			ext4_inode_set_del_time(act.inode, -1L);
 			ext4_inode_set_links_cnt(act.inode, 0);
 			act.dirty = true;
-			/*Turncate*/
-			r = ext4_fs_truncate_inode(&act, 0);
+			/*Truncate*/
+			r = ext4_trunc_dir(mp, &parent, &act);
 			if (r != EOK) {
 				ext4_fs_put_inode_ref(&parent);
 				ext4_fs_put_inode_ref(&act);
--- a/lwext4/ext4_dir_idx.h
+++ b/lwext4/ext4_dir_idx.h
@@ -52,6 +52,8 @@
 #include <stdint.h>
 #include <stdbool.h>
 
+#define EXT4_DIR_DX_INIT_BCNT 2
+
 /**@brief Initialize index structure of new directory.
  * @param dir Pointer to directory i-node
  * @param dir Pointer to parent directory i-node