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