OSDN Git Service

ext4_utils: clean up some warnings.
[android-x86/system-extras.git] / ext4_utils / make_ext4fs.c
index c11bebb..17b7ae6 100644 (file)
  */
 
 #include "make_ext4fs.h"
-#include "output_file.h"
 #include "ext4_utils.h"
 #include "allocate.h"
 #include "contents.h"
 #include "uuid.h"
-#include "backed_block.h"
+#include "wipe.h"
+
+#include <sparse/sparse.h>
 
 #include <assert.h>
 #include <dirent.h>
+#include <fcntl.h>
 #include <libgen.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#ifdef ANDROID
-#include <private/android_filesystem_config.h>
-#endif
-
 #ifdef USE_MINGW
 
 #include <winsock2.h>
 #define S_ISGID 0002000
 #define S_ISVTX 0001000
 
+#else
+
+#include <selinux/selinux.h>
+#include <selinux/label.h>
+#include <selinux/android.h>
+
+#define O_BINARY 0
+
 #endif
 
 /* TODO: Not implemented:
@@ -95,24 +101,42 @@ static u32 build_default_directory_structure()
 
 #ifndef USE_MINGW
 /* Read a local directory and create the same tree in the generated filesystem.
-   Calls itself recursively with each directory in the given directory */
+   Calls itself recursively with each directory in the given directory.
+   full_path is an absolute or relative path, with a trailing slash, to the
+   directory on disk that should be copied, or NULL if this is a directory
+   that does not exist on disk (e.g. lost+found).
+   dir_path is an absolute path, with trailing slash, to the same directory
+   if the image were mounted at the specified mount point */
 static u32 build_directory_structure(const char *full_path, const char *dir_path,
-               u32 dir_inode, int android)
+               u32 dir_inode, fs_config_func_t fs_config_func,
+               struct selabel_handle *sehnd, int verbose)
 {
        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;
+
+       if (full_path) {
+               entries = scandir(full_path, &namelist, filter_dot, (void*)alphasort);
+               if (entries < 0) {
+                       error_errno("scandir");
+                       return EXT4_ALLOCATE_FAILED;
+               }
+       }
 
-       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));
@@ -124,8 +148,8 @@ static u32 build_directory_structure(const char *full_path, const char *dir_path
                if (dentries[i].filename == NULL)
                        critical_error_errno("strdup");
 
-               asprintf(&dentries[i].path, "%s/%s", dir_path, namelist[i]->d_name);
-               asprintf(&dentries[i].full_path, "%s/%s", full_path, namelist[i]->d_name);
+               asprintf(&dentries[i].path, "%s%s", dir_path, namelist[i]->d_name);
+               asprintf(&dentries[i].full_path, "%s%s", full_path, namelist[i]->d_name);
 
                free(namelist[i]);
 
@@ -140,13 +164,13 @@ static u32 build_directory_structure(const char *full_path, const char *dir_path
                dentries[i].size = stat.st_size;
                dentries[i].mode = stat.st_mode & (S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO);
                dentries[i].mtime = stat.st_mtime;
-               if (android) {
+               if (fs_config_func != NULL) {
 #ifdef ANDROID
                        unsigned int mode = 0;
                        unsigned int uid = 0;
                        unsigned int gid = 0;
                        int dir = S_ISDIR(stat.st_mode);
-                       fs_config(dentries[i].path, dir, &uid, &gid, &mode);
+                       fs_config_func(dentries[i].path, dir, &uid, &gid, &mode);
                        dentries[i].mode = mode;
                        dentries[i].uid = uid;
                        dentries[i].gid = gid;
@@ -154,6 +178,16 @@ static u32 build_directory_structure(const char *full_path, const char *dir_path
                        error("can't set android permissions - built without android support");
 #endif
                }
+#ifndef USE_MINGW
+               if (sehnd) {
+                       if (selabel_lookup(sehnd, &dentries[i].secon, dentries[i].path, stat.st_mode) < 0) {
+                               error("cannot lookup security context for %s", dentries[i].path);
+                       }
+
+                       if (dentries[i].secon && verbose)
+                               printf("Labeling %s as %s\n", dentries[i].path, dentries[i].secon);
+               }
+#endif
 
                if (S_ISREG(stat.st_mode)) {
                        dentries[i].file_type = EXT4_FT_REG_FILE;
@@ -180,16 +214,51 @@ 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, "%slost+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) {
+                       if (selabel_lookup(sehnd, &dentries[0].secon, dentries[0].path, dentries[0].mode) < 0)
+                               error("cannot lookup security context for %s", dentries[0].path);
+               }
+               entries++;
+               dirs++;
+       }
+
        inode = make_directory(dir_inode, entries, dentries, dirs);
 
        for (i = 0; i < entries; i++) {
                if (dentries[i].file_type == EXT4_FT_REG_FILE) {
                        entry_inode = make_file(dentries[i].full_path, dentries[i].size);
                } else if (dentries[i].file_type == EXT4_FT_DIR) {
-                       entry_inode = build_directory_structure(dentries[i].full_path,
-                                       dentries[i].path, inode, android);
+                       char *subdir_full_path = NULL;
+                       char *subdir_dir_path;
+                       if (dentries[i].full_path) {
+                               ret = asprintf(&subdir_full_path, "%s/", dentries[i].full_path);
+                               if (ret < 0)
+                                       critical_error_errno("asprintf");
+                       }
+                       ret = asprintf(&subdir_dir_path, "%s/", dentries[i].path);
+                       if (ret < 0)
+                               critical_error_errno("asprintf");
+                       entry_inode = build_directory_structure(subdir_full_path,
+                                       subdir_dir_path, inode, fs_config_func, sehnd, verbose);
+                       free(subdir_full_path);
+                       free(subdir_dir_path);
                } else if (dentries[i].file_type == EXT4_FT_SYMLINK) {
-                       entry_inode = make_link(dentries[i].full_path, dentries[i].link);
+                       entry_inode = make_link(dentries[i].link);
                } else {
                        error("unknown file type on %s", dentries[i].path);
                        entry_inode = 0;
@@ -201,11 +270,15 @@ static u32 build_directory_structure(const char *full_path, const char *dir_path
                        dentries[i].mtime);
                if (ret)
                        error("failed to set permissions on %s\n", dentries[i].path);
+               ret = inode_set_selinux(entry_inode, dentries[i].secon);
+               if (ret)
+                       error("failed to set SELinux context on %s\n", dentries[i].path);
 
                free(dentries[i].path);
                free(dentries[i].full_path);
                free(dentries[i].link);
                free((void *)dentries[i].filename);
+               free(dentries[i].secon);
        }
 
        free(dentries);
@@ -275,32 +348,129 @@ void reset_ext4fs_info() {
     // can be called again.
     memset(&info, 0, sizeof(info));
     memset(&aux_info, 0, sizeof(aux_info));
-    free_data_blocks();
+
+    if (info.sparse_file) {
+        sparse_file_destroy(info.sparse_file);
+        info.sparse_file = NULL;
+    }
+}
+
+int make_ext4fs_sparse_fd(int fd, long long len,
+                const char *mountpoint, struct selabel_handle *sehnd)
+{
+       reset_ext4fs_info();
+       info.len = len;
+
+       return make_ext4fs_internal(fd, NULL, mountpoint, NULL, 0, 1, 0, 0, sehnd, 0);
+}
+
+int make_ext4fs(const char *filename, long long len,
+                const char *mountpoint, struct selabel_handle *sehnd)
+{
+       int fd;
+       int status;
+
+       reset_ext4fs_info();
+       info.len = len;
+
+       fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
+       if (fd < 0) {
+               error_errno("open");
+               return EXIT_FAILURE;
+       }
+
+       status = make_ext4fs_internal(fd, NULL, mountpoint, NULL, 0, 0, 0, 1, sehnd, 0);
+       close(fd);
+
+       return status;
+}
+
+/* return a newly-malloc'd string that is a copy of str.  The new string
+   is guaranteed to have a trailing slash.  If absolute is true, the new string
+   is also guaranteed to have a leading slash.
+*/
+static char *canonicalize_slashes(const char *str, bool absolute)
+{
+       char *ret;
+       int len = strlen(str);
+       int newlen = len;
+       char *ptr;
+
+       if (len == 0 && absolute) {
+               return strdup("/");
+       }
+
+       if (str[0] != '/' && absolute) {
+               newlen++;
+       }
+       if (str[len - 1] != '/') {
+               newlen++;
+       }
+       ret = malloc(newlen + 1);
+       if (!ret) {
+               critical_error("malloc");
+       }
+
+       ptr = ret;
+       if (str[0] != '/' && absolute) {
+               *ptr++ = '/';
+       }
+
+       strcpy(ptr, str);
+       ptr += len;
+
+       if (str[len - 1] != '/') {
+               *ptr++ = '/';
+       }
+
+       if (ptr != ret + newlen) {
+               critical_error("assertion failed\n");
+       }
+
+       *ptr = '\0';
+
+       return ret;
 }
 
-int make_ext4fs(const char *filename, s64 len)
+static char *canonicalize_abs_slashes(const char *str)
 {
-    reset_ext4fs_info();
-    info.len = len;
-    return make_ext4fs_internal(filename, NULL, NULL, 0, 0, 0, 0, 1, 0);
+       return canonicalize_slashes(str, true);
 }
 
-int make_ext4fs_internal(const char *filename, const char *directory,
-                         char *mountpoint, int android, int gzip, int sparse,
-                         int crc, int wipe, int init_itabs)
+static char *canonicalize_rel_slashes(const char *str)
 {
-        u32 root_inode_num;
-        u16 root_mode;
+       return canonicalize_slashes(str, false);
+}
+
+int make_ext4fs_internal(int fd, const char *_directory,
+                         const char *_mountpoint, fs_config_func_t fs_config_func, int gzip,
+                         int sparse, int crc, int wipe,
+                         struct selabel_handle *sehnd, int verbose)
+{
+       u32 root_inode_num;
+       u16 root_mode;
+       char *mountpoint;
+       char *directory = NULL;
 
        if (setjmp(setjmp_env))
                return EXIT_FAILURE; /* Handle a call to longjmp() */
 
+       if (_mountpoint == NULL) {
+               mountpoint = strdup("");
+       } else {
+               mountpoint = canonicalize_abs_slashes(_mountpoint);
+       }
+
+       if (_directory) {
+               directory = canonicalize_rel_slashes(_directory);
+       }
+
        if (info.len <= 0)
-               info.len = get_file_size(filename);
+               info.len = get_file_size(fd);
 
        if (info.len <= 0) {
                fprintf(stderr, "Need size of filesystem\n");
-                return EXIT_FAILURE;
+               return EXIT_FAILURE;
        }
 
        if (info.block_size <= 0)
@@ -336,7 +506,8 @@ int make_ext4fs_internal(const char *filename, const char *directory,
 
        info.feat_ro_compat |=
                        EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER |
-                       EXT4_FEATURE_RO_COMPAT_LARGE_FILE;
+                       EXT4_FEATURE_RO_COMPAT_LARGE_FILE |
+                       EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
 
        info.feat_incompat |=
                        EXT4_FEATURE_INCOMPAT_EXTENTS |
@@ -360,6 +531,8 @@ int make_ext4fs_internal(const char *filename, const char *directory,
        printf("    Block groups: %d\n", aux_info.groups);
        printf("    Reserved block group size: %d\n", info.bg_desc_reserve_blocks);
 
+       info.sparse_file = sparse_file_new(info.block_size, info.len);
+
        block_allocator_init();
 
        ext4_fill_in_sb();
@@ -379,7 +552,8 @@ int make_ext4fs_internal(const char *filename, const char *directory,
        root_inode_num = build_default_directory_structure();
 #else
        if (directory)
-               root_inode_num = build_directory_structure(directory, mountpoint, 0, android);
+               root_inode_num = build_directory_structure(directory, mountpoint, 0,
+                        fs_config_func, sehnd, verbose);
        else
                root_inode_num = build_default_directory_structure();
 #endif
@@ -387,10 +561,24 @@ int make_ext4fs_internal(const char *filename, 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);
 
-       ext4_update_free();
+#ifndef USE_MINGW
+       if (sehnd) {
+               char *secontext = NULL;
 
-       if (init_itabs)
-               init_unused_inode_tables();
+               if (selabel_lookup(sehnd, &secontext, mountpoint, S_IFDIR) < 0) {
+                       error("cannot lookup security context for %s", mountpoint);
+               }
+               if (secontext) {
+                       if (verbose) {
+                               printf("Labeling %s as %s\n", mountpoint, secontext);
+                       }
+                       inode_set_selinux(root_inode_num, secontext);
+               }
+               freecon(secontext);
+       }
+#endif
+
+       ext4_update_free();
 
        ext4_queue_sb();
 
@@ -400,7 +588,16 @@ int make_ext4fs_internal(const char *filename, const char *directory,
                        aux_info.sb->s_blocks_count_lo - aux_info.sb->s_free_blocks_count_lo,
                        aux_info.sb->s_blocks_count_lo);
 
-       write_ext4_image(filename, gzip, sparse, crc, wipe);
+       if (wipe)
+               wipe_block_device(fd, info.len);
+
+       write_ext4_image(fd, gzip, sparse, crc);
+
+       sparse_file_destroy(info.sparse_file);
+       info.sparse_file = NULL;
+
+       free(mountpoint);
+       free(directory);
 
        return 0;
 }