]> git.sesse.net Git - bcachefs-tools-debian/blob - cmd_attr.c
Makefile, fsck: Use libexec instead of lib
[bcachefs-tools-debian] / cmd_attr.c
1 #include <dirent.h>
2 #include <stdio.h>
3 #include <sys/ioctl.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6 #include <sys/xattr.h>
7 #include <unistd.h>
8
9 #include "libbcachefs/bcachefs_ioctl.h"
10
11 #include "cmds.h"
12 #include "libbcachefs.h"
13
14 static void propagate_recurse(int dirfd)
15 {
16         DIR *dir = fdopendir(dirfd);
17         struct dirent *d;
18
19         while ((errno = 0), (d = readdir(dir))) {
20                 if (!strcmp(d->d_name, ".") ||
21                     !strcmp(d->d_name, ".."))
22                         continue;
23
24                 int ret = ioctl(dirfd, BCHFS_IOC_REINHERIT_ATTRS,
25                             d->d_name);
26                 if (ret < 0) {
27                         fprintf(stderr, "error propagating attributes to %s: %m\n",
28                                 d->d_name);
29                         continue;
30                 }
31
32                 if (!ret) /* did no work */
33                         continue;
34
35                 struct stat st = xfstatat(dirfd, d->d_name,
36                                           AT_SYMLINK_NOFOLLOW);
37                 if (!S_ISDIR(st.st_mode))
38                         continue;
39
40                 int fd = openat(dirfd, d->d_name, O_RDONLY);
41                 if (fd < 0) {
42                         fprintf(stderr, "error opening %s: %m\n", d->d_name);
43                         continue;
44                 }
45                 propagate_recurse(fd);
46                 close(fd);
47         }
48
49         if (errno)
50                 die("readdir error: %m");
51 }
52
53 static void do_setattr(char *path, struct bch_opt_strs opts)
54 {
55         unsigned i;
56
57         for (i = 0; i < bch2_opts_nr; i++) {
58                 if (!opts.by_id[i])
59                         continue;
60
61                 char *n = mprintf("bcachefs.%s", bch2_opt_table[i].attr.name);
62
63                 if (setxattr(path, n, opts.by_id[i], strlen(opts.by_id[i]), 0))
64                         die("setxattr error: %m");
65
66                 free(n);
67         }
68
69         struct stat st = xstat(path);
70         if (!S_ISDIR(st.st_mode))
71                 return;
72
73         int dirfd = open(path, O_RDONLY);
74         if (dirfd < 0)
75                 die("error opening %s: %m", path);
76
77         propagate_recurse(dirfd);
78         close(dirfd);
79 }
80
81 static void setattr_usage(void)
82 {
83         puts("bcachefs setattr - set attributes on files in a bcachefs filesystem\n"
84              "Usage: bcachefs setattr [OPTIONS]... <files>\n"
85              "\n"
86              "Options:");
87
88         bch2_opts_usage(OPT_INODE);
89         puts("  -h            Display this help and exit\n"
90              "Report bugs to <linux-bcachefs@vger.kernel.org>");
91 }
92
93 int cmd_setattr(int argc, char *argv[])
94 {
95         struct bch_opt_strs opts =
96                 bch2_cmdline_opts_get(&argc, argv, OPT_INODE);
97         unsigned i;
98
99         for (i = 1; i < argc; i++)
100                 if (argv[i][0] == '-') {
101                         printf("invalid option %s\n", argv[i]);
102                         setattr_usage();
103                         exit(EXIT_FAILURE);
104                 }
105
106         if (argc <= 1)
107                 die("Please supply one or more files");
108
109         for (i = 1; i < argc; i++)
110                 do_setattr(argv[i], opts);
111         bch2_opt_strs_free(&opts);
112
113         return 0;
114 }