9 #include "libbcachefs/bcachefs_ioctl.h"
12 #include "libbcachefs.h"
14 static void propagate_recurse(int dirfd)
16 DIR *dir = fdopendir(dirfd);
20 fprintf(stderr, "fdopendir() error: %m\n");
24 while ((errno = 0), (d = readdir(dir))) {
25 if (!strcmp(d->d_name, ".") ||
26 !strcmp(d->d_name, ".."))
29 int ret = ioctl(dirfd, BCHFS_IOC_REINHERIT_ATTRS,
32 fprintf(stderr, "error propagating attributes to %s: %m\n",
37 if (!ret) /* did no work */
40 struct stat st = xfstatat(dirfd, d->d_name,
42 if (!S_ISDIR(st.st_mode))
45 int fd = openat(dirfd, d->d_name, O_RDONLY);
47 fprintf(stderr, "error opening %s: %m\n", d->d_name);
50 propagate_recurse(fd);
55 die("readdir error: %m");
58 static void do_setattr(char *path, struct bch_opt_strs opts)
62 for (i = 0; i < bch2_opts_nr; i++) {
66 char *n = mprintf("bcachefs.%s", bch2_opt_table[i].attr.name);
68 if (setxattr(path, n, opts.by_id[i], strlen(opts.by_id[i]), 0))
69 die("setxattr error: %m");
74 struct stat st = xstat(path);
75 if (!S_ISDIR(st.st_mode))
78 int dirfd = open(path, O_RDONLY);
80 die("error opening %s: %m", path);
82 propagate_recurse(dirfd);
86 static void setattr_usage(void)
88 puts("bcachefs setattr - set attributes on files in a bcachefs filesystem\n"
89 "Usage: bcachefs setattr [OPTIONS]... <files>\n"
93 bch2_opts_usage(OPT_INODE);
94 puts(" -h Display this help and exit\n"
95 "Report bugs to <linux-bcachefs@vger.kernel.org>");
98 int cmd_setattr(int argc, char *argv[])
100 struct bch_opt_strs opts =
101 bch2_cmdline_opts_get(&argc, argv, OPT_INODE);
104 for (i = 1; i < argc; i++)
105 if (argv[i][0] == '-') {
106 printf("invalid option %s\n", argv[i]);
112 die("Please supply one or more files");
114 for (i = 1; i < argc; i++)
115 do_setattr(argv[i], opts);
116 bch2_opt_strs_free(&opts);