Commit 1c5fab86 authored by Kevin Wolf's avatar Kevin Wolf

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's avatarKevin Wolf <kevin@tyndur.org>
parent 9e37f5e2
......@@ -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)) {
......@@ -302,8 +309,10 @@ int ext2_dir_create(ext2_inode_t* parent, const char* name, ext2_inode_t* newi)
newi->raw->mode = EXT2_INODE_MODE_DIR | 0777;
// Verzeichniseintraege anlegen
if (!ext2_dir_link(parent, newi, name) ||
!ext2_dir_link(newi, newi, ".") ||
if (!ext2_dir_link(parent, newi, name)) {
goto fail;
}
if (!ext2_dir_link(newi, newi, ".") ||
!ext2_dir_link(newi, parent, ".."))
{
return 0;
......@@ -321,5 +330,9 @@ int ext2_dir_create(ext2_inode_t* parent, const char* name, ext2_inode_t* newi)
ext2_bg_update(parent->fs, bgnum, &bg);
return 1;
fail:
ext2_inode_free(newi);
return 0;
}
......@@ -48,7 +48,7 @@ int ext2_file_create(ext2_inode_t* parent, const char* name,
// Verzeichniseintrag anlegen
if (!ext2_dir_link(parent, newi, name)) {
return 0;
goto fail;
}
if (!ext2_inode_update(newi) ||
......@@ -58,5 +58,9 @@ int ext2_file_create(ext2_inode_t* parent, const char* name,
}
return 1;
fail:
ext2_inode_free(newi);
return 0;
}
......@@ -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
......
......@@ -58,7 +58,7 @@ int ext2_symlink_create(ext2_inode_t* parent,
// Verzeichniseintrag anlegen
if (!ext2_dir_link(parent, newi, name)) {
return 0;
goto fail;
}
// Initialisierung des Inode
......@@ -79,8 +79,11 @@ int ext2_symlink_create(ext2_inode_t* parent,
return 0;
}
return 1;
fail:
ext2_inode_free(newi);
return 0;
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment