Merge "make_ext4fs: insert lost+found directory into filled filesystems"
diff --git a/ext4_utils/make_ext4fs.c b/ext4_utils/make_ext4fs.c
index ca81398..f4e181c 100644
--- a/ext4_utils/make_ext4fs.c
+++ b/ext4_utils/make_ext4fs.c
@@ -104,18 +104,30 @@
 {
 	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 @@
 	}
 	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++) {