#include <linux/kdev_t.h>
#include <linux/loop.h>
#include <linux/limits.h>
+#include <dirent.h>
#include <fcntl.h>
+#include <ftw.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
dest[ret] = 0;
return ret;
}
+
+struct path_walk_entry {
+ char *name;
+};
+
+static int path_walk_entry_cmp(const void *a, const void *b)
+{
+ const struct path_walk_entry *ea = a;
+ const struct path_walk_entry *eb = b;
+
+ return strcmp(ea->name, eb->name);
+}
+
+/*
+ * Emulate nftw()'s depth-first, preorder, FTW_PHYS walk, but visit each
+ * directory's entries in sorted name order. The struct FTW fields are filled
+ * the way nftw() reports them so existing nftw() callbacks work unchanged.
+ */
+static int path_sorted_walk_recursive(char *path, int level, path_walk_cb cb)
+{
+ struct stat st;
+ struct FTW ftwbuf;
+ struct path_walk_entry *entries = NULL;
+ size_t count = 0, cap = 0;
+ DIR *dir = NULL;
+ struct dirent *de;
+ const char *slash;
+ int ret;
+
+ slash = strrchr(path, '/');
+ ftwbuf.base = slash ? (int)(slash - path + 1) : 0;
+ ftwbuf.level = level;
+
+ if (lstat(path, &st) != 0)
+ return cb(path, NULL, FTW_NS, &ftwbuf);
+
+ if (!S_ISDIR(st.st_mode)) {
+ if (S_ISLNK(st.st_mode))
+ return cb(path, &st, FTW_SL, &ftwbuf);
+ return cb(path, &st, FTW_F, &ftwbuf);
+ }
+
+ /*
+ * Open the directory before reporting it: one we can lstat() but not
+ * open is reported once as FTW_DNR, not FTW_D then FTW_DNR.
+ */
+ dir = opendir(path);
+ if (!dir)
+ return cb(path, &st, FTW_DNR, &ftwbuf);
+
+ ret = cb(path, &st, FTW_D, &ftwbuf);
+ if (ret)
+ goto cleanup;
+
+ /*
+ * readdir() returns NULL at both end-of-stream and error; clear
+ * errno before each call to tell them apart after the loop.
+ */
+ while (1) {
+ errno = 0;
+ de = readdir(dir);
+ if (!de)
+ break;
+ if (strcmp(de->d_name, ".") == 0 ||
+ strcmp(de->d_name, "..") == 0)
+ continue;
+ if (count == cap) {
+ struct path_walk_entry *new_entries;
+
+ cap = cap ? cap * 2 : 16;
+ new_entries = reallocarray(entries, cap,
+ sizeof(*entries));
+ if (!new_entries) {
+ ret = -ENOMEM;
+ goto cleanup;
+ }
+ entries = new_entries;
+ }
+ entries[count].name = strndup(de->d_name, NAME_MAX);
+ if (!entries[count].name) {
+ ret = -ENOMEM;
+ goto cleanup;
+ }
+ count++;
+ }
+ if (errno != 0) {
+ ret = -errno;
+ goto cleanup;
+ }
+ closedir(dir);
+ dir = NULL;
+
+ if (count)
+ qsort(entries, count, sizeof(*entries), path_walk_entry_cmp);
+
+ for (size_t i = 0; i < count; i++) {
+ char *child = malloc(PATH_MAX);
+
+ if (!child) {
+ ret = -ENOMEM;
+ goto cleanup;
+ }
+ ret = path_cat_out(child, path, entries[i].name);
+ if (!ret)
+ ret = path_sorted_walk_recursive(child, level + 1, cb);
+ free(child);
+ if (ret)
+ goto cleanup;
+ }
+ ret = 0;
+
+cleanup:
+ if (dir)
+ closedir(dir);
+ for (size_t i = 0; i < count; i++)
+ free(entries[i].name);
+ free(entries);
+ return ret;
+}
+
+int path_sorted_walk(const char *root, path_walk_cb cb)
+{
+ char path[PATH_MAX];
+ size_t root_len = strlen(root);
+
+ /* glibc nftw() rejects an empty path with ENOENT and no callback. */
+ if (root_len == 0)
+ return -ENOENT;
+ if (root_len >= sizeof(path))
+ return -ENAMETOOLONG;
+ memcpy(path, root, root_len + 1);
+
+ /* Strip trailing slashes (but keep a lone "/"), matching nftw. */
+ while (root_len > 1 && path[root_len - 1] == '/')
+ path[--root_len] = '\0';
+
+ return path_sorted_walk_recursive(path, 0, cb);
+}
#define __BTRFS_PATH_UTILS_H__
#include <sys/types.h>
+#include <sys/stat.h>
#include <linux/limits.h>
+#include <ftw.h>
char *path_canonicalize_dm_name(const char *ptname);
char *path_canonicalize(const char *path);
char *path_dirname(char *path);
int path_readlink(char *dest, const char *src);
+/*
+ * Callback for path_sorted_walk(), matching nftw()'s callback signature
+ * so existing nftw() callbacks can be reused unchanged.
+ *
+ * st : lstat() result, or NULL when type == FTW_NS
+ * type : FTW_F, FTW_D (pre-order), FTW_SL (never followed),
+ * FTW_DNR (unreadable directory) or FTW_NS (lstat failed)
+ * ftwbuf : level (depth, 0 at the root) and base (basename offset)
+ *
+ * Return 0 to continue the walk, non-zero to abort it (that value is
+ * then returned by path_sorted_walk()).
+ */
+typedef int (*path_walk_cb)(const char *path, const struct stat *st,
+ int type, struct FTW *ftwbuf);
+
+/*
+ * Walk a directory tree depth-first, pre-order, visiting the entries of
+ * each directory in byte-wise (strcmp) name order. Symlinks are never
+ * followed (FTW_PHYS). Sorting makes the walk order independent of the
+ * filesystem's readdir() order.
+ *
+ * Returns 0 on success, the callback's non-zero return value if it
+ * aborted, or -errno on I/O failure.
+ */
+int path_sorted_walk(const char *root, path_walk_cb cb);
+
#endif
return v && strcmp(v, "1") == 0;
}
+bool reproducible_is_enabled(void)
+{
+ return reproducible_has_source_date() && reproducible_is_deterministic();
+}
+
void reproducible_uuid_generate(const u8 fs_uuid[BTRFS_UUID_SIZE],
enum reproducible_uuid_role role, u64 key,
u8 out[BTRFS_UUID_SIZE])
bool reproducible_is_deterministic(void);
+/* True when SOURCE_DATE_EPOCH is non-empty and DETERMINISTIC_SEED is "1". */
+bool reproducible_is_enabled(void);
+
enum reproducible_uuid_role {
REPRODUCIBLE_UUID_ROLE_CHUNK_TREE, /* singleton, key ignored */
REPRODUCIBLE_UUID_ROLE_SUBVOL, /* key = subvolume objectid */
btrfs_set_stack_timespec_nsec(&dst->otime, 0);
}
+static int xattr_name_cmp(const void *a, const void *b)
+{
+ return strcmp(*(const char * const *)a, *(const char * const *)b);
+}
+
static int add_xattr_item(struct btrfs_trans_handle *trans,
struct btrfs_root *root, u64 objectid,
const char *file_name)
char *xattr_list_end;
char *cur_name;
char cur_value[XATTR_SIZE_MAX];
+ char **names = NULL;
+ size_t nr_names = 0;
+ size_t i;
ret = llistxattr(file_name, xattr_list, XATTR_LIST_MAX);
if (ret < 0) {
return ret;
xattr_list_end = xattr_list + ret;
- cur_name = xattr_list;
- while (cur_name < xattr_list_end) {
+
+ for (cur_name = xattr_list; cur_name < xattr_list_end;
+ cur_name += strlen(cur_name) + 1)
+ nr_names++;
+
+ names = malloc(nr_names * sizeof(*names));
+ if (!names) {
+ error("not enough memory to process xattrs of %s", file_name);
+ return -ENOMEM;
+ }
+ i = 0;
+ for (cur_name = xattr_list; cur_name < xattr_list_end;
+ cur_name += strlen(cur_name) + 1)
+ names[i++] = cur_name;
+
+ /* llistxattr() order is filesystem-defined. */
+ if (reproducible_is_enabled())
+ qsort(names, nr_names, sizeof(*names), xattr_name_cmp);
+
+ for (i = 0; i < nr_names; i++) {
+ cur_name = names[i];
cur_name_len = strlen(cur_name);
ret = lgetxattr(file_name, cur_name, cur_value, XATTR_SIZE_MAX);
if (ret < 0) {
if (errno == ENOTSUP)
- return 0;
- error("getting a xattr value failed for %s attr %s: %m",
- file_name, cur_name);
- return ret;
+ ret = 0;
+ else
+ error("getting a xattr value failed for %s attr %s: %m",
+ file_name, cur_name);
+ goto out;
}
ret = btrfs_insert_xattr_item(trans, root, cur_name,
error("inserting a xattr item failed for %s: %m",
file_name);
}
-
- cur_name += cur_name_len + 1;
}
-
+out:
+ free(names);
return ret;
}
struct btrfs_inode_item inode_item = { 0 };
struct inode_entry *parent;
struct rootdir_subvol *rds;
- const bool have_hard_links = (!S_ISDIR(st->st_mode) && st->st_nlink > 1);
+ bool have_hard_links;
u64 ino;
int ret;
+ /*
+ * No stat (FTW_NS) or unreadable directory (FTW_DNR): abort rather
+ * than dereference a NULL st.
+ */
+ if (typeflag == FTW_NS || typeflag == FTW_DNR)
+ return -EPERM;
+
+ have_hard_links = (!S_ISDIR(st->st_mode) && st->st_nlink > 1);
+
/* The rootdir itself. */
if (unlikely(ftwbuf->level == 0)) {
u64 root_ino;
g_do_reflink = do_reflink;
INIT_LIST_HEAD(¤t_path.inode_list);
- ret = nftw(source_dir, ftw_add_inode, 32, FTW_PHYS);
+ /*
+ * A reproducible image needs a sorted traversal, which costs extra
+ * allocation and CPU. A regular mkfs doesn't, so use nftw() there.
+ */
+ if (reproducible_is_enabled())
+ ret = path_sorted_walk(source_dir, ftw_add_inode);
+ else
+ ret = nftw(source_dir, ftw_add_inode, 32, FTW_PHYS);
if (ret) {
error("unable to traverse directory %s: %d", source_dir, ret);
return ret;
/*
* Symbolic link is not followed when creating files, so no need to
* follow them here.
+ *
+ * This pass only sums file sizes and counts inodes, a result that is
+ * independent of walk order, so it always uses nftw() -- the sorted
+ * walk (see btrfs_mkfs_fill_dir()) is only needed where the order
+ * feeds the on-disk layout.
*/
ret = nftw(dir_name, ftw_add_entry_size, 10, FTW_PHYS);
rb_free_nodes(&hardlink_root, free_one_hardlink);
if (ret < 0) {
- error("ftw subdir walk of %s failed: %m", dir_name);
+ error("unable to walk subdir of %s: %m", dir_name);
exit(1);
}