OSDN Git Service

make_ext4fs: fix sepolicy lookup for lost+found
[android-x86/system-extras.git] / ext4_utils / make_ext4fs.c
index be63e27..97cd26f 100644 (file)
@@ -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));
@@ -157,15 +169,18 @@ static u32 build_directory_structure(const char *full_path, const char *dir_path
                        error("can't set android permissions - built without android support");
 #endif
                }
-#ifdef HAVE_SELINUX
+#ifndef USE_MINGW
                if (sehnd) {
                        char *sepath = NULL;
                        asprintf(&sepath, "/%s", dentries[i].path);
                        if (selabel_lookup(sehnd, &dentries[i].secon, sepath, stat.st_mode) < 0) {
                                error("cannot lookup security context for %s", sepath);
                        }
+#if 0
+                       // TODO make this a debug flag
                        if (dentries[i].secon)
                                printf("Labeling %s as %s\n", sepath, dentries[i].secon);
+#endif
                        free(sepath);
                }
 #endif
@@ -195,6 +210,32 @@ 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");
+               asprintf(&dentries[0].path, "%s/lost+found", dir_path);
+               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) {
+                       char *sepath = NULL;
+                       asprintf(&sepath, "/%s", dentries[0].path);
+                       if (selabel_lookup(sehnd, &dentries[0].secon, sepath, dentries[0].mode) < 0)
+                               error("cannot lookup security context for %s", dentries[0].path);
+                       free(sepath);
+               }
+               entries++;
+               dirs++;
+       }
+
        inode = make_directory(dir_inode, entries, dentries, dirs);
 
        for (i = 0; i < entries; i++) {
@@ -323,8 +364,9 @@ int make_ext4fs(const char *filename, s64 len,
 }
 
 int make_ext4fs_internal(int fd, const char *directory,
-                         char *mountpoint, fs_config_func_t fs_config_func, int gzip, int sparse,
-                         int crc, int wipe, int init_itabs, struct selabel_handle *sehnd)
+                         const char *mountpoint, fs_config_func_t fs_config_func, int gzip,
+                         int sparse, int crc, int wipe, int init_itabs,
+                         struct selabel_handle *sehnd)
 {
        u32 root_inode_num;
        u16 root_mode;
@@ -427,7 +469,7 @@ int make_ext4fs_internal(int fd, const char *directory,
        root_mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
        inode_set_permissions(root_inode_num, root_mode, 0, 0, 0);
 
-#ifdef HAVE_SELINUX
+#ifndef USE_MINGW
        if (sehnd) {
                char *sepath = NULL;
                char *secontext = NULL;