diff options
author | Colin Cross <ccross@android.com> | 2012-11-20 19:41:42 -0800 |
---|---|---|
committer | Colin Cross <ccross@android.com> | 2012-11-26 13:42:59 -0800 |
commit | c18120d04f164c00bad9d7006d29b4a795cf5d05 (patch) | |
tree | bd69bc4a9f3e7b05252c533fe2c7fc9a29c9229e /ext4_utils | |
parent | 8748938228acad9e53c4f3cdfa132d2aff7917bf (diff) | |
download | extras-c18120d04f164c00bad9d7006d29b4a795cf5d05.tar.gz |
make_ext4fs: insert lost+found directory into filled filesystems
Insert a lost+found directory if necessary when creating a filesystem
based on a directory structure. This will make the resulting image
clean when tested with e2fsck, allowing e2fsck to be run on all images
as a validity test.
Change-Id: I81d0b6b4a802713270d3469e5a3bc555b680ec7e
Diffstat (limited to 'ext4_utils')
-rw-r--r-- | ext4_utils/make_ext4fs.c | 44 |
1 files changed, 39 insertions, 5 deletions
diff --git a/ext4_utils/make_ext4fs.c b/ext4_utils/make_ext4fs.c index ca81398b..f4e181ce 100644 --- a/ext4_utils/make_ext4fs.c +++ b/ext4_utils/make_ext4fs.c @@ -104,18 +104,30 @@ static u32 build_directory_structure(const char *full_path, const char *dir_path { int entries = 0; struct dentry *dentries; - struct dirent **namelist; + struct dirent **namelist = NULL; struct stat stat; int ret; int i; u32 inode; u32 entry_inode; u32 dirs = 0; + bool needs_lost_and_found = false; - entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort); - if (entries < 0) { - error_errno("scandir"); - return EXT4_ALLOCATE_FAILED; + if (full_path) { + entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort); + if (entries < 0) { + error_errno("scandir"); + return EXT4_ALLOCATE_FAILED; + } + } + + if (dir_inode == 0) { + /* root directory, check if lost+found already exists */ + for (i = 0; i < entries; i++) + if (strcmp(namelist[i]->d_name, "lost+found") == 0) + break; + if (i == entries) + needs_lost_and_found = true; } dentries = calloc(entries, sizeof(struct dentry)); @@ -198,6 +210,28 @@ static u32 build_directory_structure(const char *full_path, const char *dir_path } free(namelist); + if (needs_lost_and_found) { + /* insert a lost+found directory at the beginning of the dentries */ + struct dentry *tmp = calloc(entries + 1, sizeof(struct dentry)); + memset(tmp, 0, sizeof(struct dentry)); + memcpy(tmp + 1, dentries, entries * sizeof(struct dentry)); + dentries = tmp; + + dentries[0].filename = strdup("lost+found"); + dentries[0].path = strdup("/lost+found"); + dentries[0].full_path = NULL; + dentries[0].size = 0; + dentries[0].mode = S_IRWXU; + dentries[0].file_type = EXT4_FT_DIR; + dentries[0].uid = 0; + dentries[0].gid = 0; + if (sehnd) + if (selabel_lookup(sehnd, &dentries[0].secon, "lost+found", dentries[0].mode) < 0) + error("cannot lookup security context for /lost+found"); + entries++; + dirs++; + } + inode = make_directory(dir_inode, entries, dentries, dirs); for (i = 0; i < entries; i++) { |