9 #include "libbcachefs/bcachefs_ioctl.h"
12 #include "libbcachefs.h"
14 static void propagate_recurse(int dirfd)
16 DIR *dir = fdopendir(dirfd);
19 while ((errno = 0), (d = readdir(dir))) {
20 if (!strcmp(d->d_name, ".") ||
21 !strcmp(d->d_name, ".."))
24 int ret = ioctl(dirfd, BCHFS_IOC_REINHERIT_ATTRS,
27 fprintf(stderr, "error propagating attributes to %s: %m\n",
32 if (!ret) /* did no work */
35 struct stat st = xfstatat(dirfd, d->d_name,
37 if (!S_ISDIR(st.st_mode))
40 int fd = openat(dirfd, d->d_name, O_RDONLY);
42 fprintf(stderr, "error opening %s: %m\n", d->d_name);
45 propagate_recurse(fd);
50 die("readdir error: %m");
53 static void do_setattr(char *path, struct bch_opt_strs opts)
57 for (i = 0; i < bch2_opts_nr; i++) {
61 char *n = mprintf("bcachefs.%s", bch2_opt_table[i].attr.name);
63 if (setxattr(path, n, opts.by_id[i], strlen(opts.by_id[i]), 0))
64 die("setxattr error: %m");
69 struct stat st = xstat(path);
70 if (!S_ISDIR(st.st_mode))
73 int dirfd = open(path, O_RDONLY);
75 die("error opening %s: %m", path);
77 propagate_recurse(dirfd);
81 static void setattr_usage(void)
83 puts("bcachefs setattr - set attributes on files in a bcachefs filesystem\n"
84 "Usage: bcachefs setattr [OPTIONS]... <files>\n"
88 bch2_opts_usage(OPT_INODE);
89 puts(" -h Display this help and exit\n"
90 "Report bugs to <linux-bcache@vger.kernel.org>");
93 int cmd_setattr(int argc, char *argv[])
95 struct bch_opt_strs opts =
96 bch2_cmdline_opts_get(&argc, argv, OPT_INODE);
99 for (i = 1; i < argc; i++)
100 if (argv[i][0] == '-') {
101 printf("invalid option %s\n", argv[i]);
107 die("Please supply one or more files");
109 for (i = 1; i < argc; i++)
110 do_setattr(argv[i], opts);
111 bch2_opt_strs_free(&opts);