btrfs-progs: build btrfs.static needs libbtrfsutil to build
[platform/upstream/btrfs-progs.git] / mkfs / rootdir.c
index 022d118..ff00bb0 100644 (file)
@@ -139,7 +139,8 @@ static int fill_inode_item(struct btrfs_trans_handle *trans,
        }
        if (S_ISREG(src->st_mode)) {
                btrfs_set_stack_inode_size(dst, (u64)src->st_size);
-               if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info))
+               if (src->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info) &&
+                   src->st_size < sectorsize)
                        btrfs_set_stack_inode_nbytes(dst, src->st_size);
                else {
                        blocks = src->st_size / sectorsize;
@@ -248,7 +249,7 @@ static int add_xattr_item(struct btrfs_trans_handle *trans,
                cur_name_len = strlen(cur_name);
                next_location += cur_name_len + 1;
 
-               ret = getxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
+               ret = lgetxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
                if (ret < 0) {
                        if (errno == ENOTSUP)
                                return 0;
@@ -327,7 +328,8 @@ static int add_file_items(struct btrfs_trans_handle *trans,
        if (st->st_size % sectorsize)
                blocks += 1;
 
-       if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info)) {
+       if (st->st_size <= BTRFS_MAX_INLINE_DATA_SIZE(root->fs_info) &&
+           st->st_size < sectorsize) {
                char *buffer = malloc(st->st_size);
 
                if (!buffer) {
@@ -453,7 +455,6 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
        ino_t parent_inum, cur_inum;
        ino_t highest_inum = 0;
        const char *parent_dir_name;
-       char real_path[PATH_MAX];
        struct btrfs_path path;
        struct extent_buffer *leaf;
        struct btrfs_key root_dir_key;
@@ -464,7 +465,7 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
        if (!dir_entry)
                return -ENOMEM;
        dir_entry->dir_name = dir_name;
-       dir_entry->path = realpath(dir_name, real_path);
+       dir_entry->path = realpath(dir_name, NULL);
        if (!dir_entry->path) {
                error("realpath  failed for %s: %s", dir_name, strerror(errno));
                ret = -1;
@@ -616,6 +617,7 @@ static int traverse_directory(struct btrfs_trans_handle *trans,
                }
 
                free_namelist(files, count);
+               free(parent_dir_entry->path);
                free(parent_dir_entry);
 
                index_cnt = 2;
@@ -686,6 +688,7 @@ fail:
                dir_entry = list_entry(dir_head.list.next,
                                       struct directory_name_entry, list);
                list_del(&dir_entry->list);
+               free(dir_entry->path);
                free(dir_entry);
        }
 out:
@@ -693,7 +696,7 @@ out:
 }
 
 static int ftw_add_entry_size(const char *fpath, const struct stat *st,
-                             int type)
+                             int type, struct FTW *ftwbuf)
 {
        /*
         * Failed to read the directory, mostly due to EPERM.  Abort ASAP, so
@@ -728,7 +731,12 @@ u64 btrfs_mkfs_size_dir(const char *dir_name, u32 sectorsize, u64 min_dev_size,
        fs_block_size = sectorsize;
        ftw_data_size = 0;
        ftw_meta_nr_inode = 0;
-       ret = ftw(dir_name, ftw_add_entry_size, 10);
+
+       /*
+        * Symbolic link is not followed when creating files, so no need to
+        * follow them here.
+        */
+       ret = nftw(dir_name, ftw_add_entry_size, 10, FTW_PHYS);
        if (ret < 0) {
                error("ftw subdir walk of %s failed: %s", dir_name,
                        strerror(errno));