[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[cdi-devel] [PATCH 2/2] ext2: Prevent creating second dir entry with same name



A directory isn't supposed to contain multiple entries with the same
name. Error out when someone tries to create a new directory entry with
the same name as an existing entry uses.

Signed-off-by: Kevin Wolf <kevin@xxxxxxxxxx>
---
 ext2/libext2/directory.c         | 11 +++++++++--
 ext2/libext2/include/directory.h |  2 +-
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/ext2/libext2/directory.c b/ext2/libext2/directory.c
index f9ffb42..374a1dd 100644
--- a/ext2/libext2/directory.c
+++ b/ext2/libext2/directory.c
@@ -38,7 +38,7 @@
 #include <stdio.h>
 
 struct private_dir_get {
-    char* name;
+    const char* name;
     size_t name_len;
     ext2_dirent_t* result;
     int found;
@@ -88,7 +88,7 @@ static int dir_get_handler(ext2_dirent_t* dirent, void* prv)
     return 0;
 }
 
-ext2_dirent_t* ext2_dir_get(ext2_inode_t* inode, char* name)
+ext2_dirent_t* ext2_dir_get(ext2_inode_t* inode, const char* name)
 {
     struct private_dir_get prv = {
         .name = name,
@@ -173,6 +173,13 @@ int ext2_dir_link(ext2_inode_t* dir, ext2_inode_t* inode, const char* name)
         type = ext2_inode_type(inode);
     }
 
+    /* Don't create a second dir entry with the same name */
+    entry = ext2_dir_get(dir, name);
+    if (entry != NULL) {
+        free(entry);
+        return 0;
+    }
+
     if (dir->raw->size) {
     ext2_inode_readdata(dir, 0, dir->raw->size, buf);
     while (dir->raw->size && (pos < dir->raw->size)) {
diff --git a/ext2/libext2/include/directory.h b/ext2/libext2/include/directory.h
index ee19a56..5732922 100644
--- a/ext2/libext2/include/directory.h
+++ b/ext2/libext2/include/directory.h
@@ -82,7 +82,7 @@ void ext2_dir_foreach(ext2_inode_t* inode,
  * @return      Pointer auf den gefundenen Verzeichniseintrag. Muss mit free
  *              freigegeben.
  */
-ext2_dirent_t* ext2_dir_get(ext2_inode_t* inode, char* name);
+ext2_dirent_t* ext2_dir_get(ext2_inode_t* inode, const char* name);
 
 /**
  * Sucht einen Inode nach dem Pfad
-- 
2.1.4