]> git.sesse.net Git - bcachefs-tools-debian/commitdiff
New setattr command
authorKent Overstreet <kent.overstreet@gmail.com>
Wed, 19 Dec 2018 20:41:47 +0000 (15:41 -0500)
committerKent Overstreet <kent.overstreet@gmail.com>
Wed, 19 Dec 2018 23:27:57 +0000 (18:27 -0500)
bcachefs.c
cmd_attr.c [new file with mode: 0644]
cmds.h

index 2ade9d81671d76f671f2502e1f5b059dc21ce51f..9c503e00208542cfc69a49881c93dacad3b2fa2e 100644 (file)
@@ -69,6 +69,8 @@ static void usage(void)
             "  migrate              Migrate an existing filesystem to bcachefs, in place\n"
             "  migrate-superblock   Add default superblock, after bcachefs migrate\n"
             "\n"
+            "Commands for operating on files in a bcachefs filesystem:\n"
+            "  setattr              Set various per file attributes\n"
             "Debug:\n"
             "These commands work on offline, unmounted filesystems\n"
             "  dump                 Dump filesystem metadata to a qcow2 image\n"
@@ -198,6 +200,9 @@ int main(int argc, char *argv[])
        if (!strcmp(cmd, "list"))
                return cmd_list(argc, argv);
 
+       if (!strcmp(cmd, "setattr"))
+               return cmd_setattr(argc, argv);
+
        if (!strcmp(cmd, "--help")) {
                usage();
                return 0;
diff --git a/cmd_attr.c b/cmd_attr.c
new file mode 100644 (file)
index 0000000..a3c8212
--- /dev/null
@@ -0,0 +1,113 @@
+#include <dirent.h>
+#include <stdio.h>
+#include <sys/ioctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/xattr.h>
+#include <unistd.h>
+
+#include "libbcachefs/bcachefs_ioctl.h"
+
+#include "cmds.h"
+#include "libbcachefs.h"
+
+static void propagate_recurse(int dirfd)
+{
+       DIR *dir = fdopendir(dirfd);
+       struct dirent *d;
+
+       while ((errno = 0), (d = readdir(dir))) {
+               if (!strcmp(d->d_name, ".") ||
+                   !strcmp(d->d_name, ".."))
+                       continue;
+
+               int ret = ioctl(dirfd, BCHFS_IOC_REINHERIT_ATTRS,
+                           d->d_name);
+               if (ret < 0) {
+                       fprintf(stderr, "error propagating attributes to %s: %m\n",
+                               d->d_name);
+                       continue;
+               }
+
+               if (!ret) /* did no work */
+                       continue;
+
+               struct stat st = xfstatat(dirfd, d->d_name,
+                                         AT_SYMLINK_NOFOLLOW);
+               if (!S_ISDIR(st.st_mode))
+                       continue;
+
+               int fd = openat(dirfd, d->d_name, O_RDONLY);
+               if (fd < 0) {
+                       fprintf(stderr, "error opening %s: %m\n", d->d_name);
+                       continue;
+               }
+               propagate_recurse(fd);
+               close(fd);
+       }
+
+       if (errno)
+               die("readdir error: %m");
+}
+
+static void do_setattr(char *path, struct bch_opt_strs opts)
+{
+       unsigned i;
+
+       for (i = 0; i < bch2_opts_nr; i++) {
+               if (!opts.by_id[i])
+                       continue;
+
+               char *n = mprintf("bcachefs.%s", bch2_opt_table[i].attr.name);
+
+               if (setxattr(path, n, opts.by_id[i], strlen(opts.by_id[i]), 0))
+                       die("setxattr error: %m");
+
+               free(n);
+       }
+
+       struct stat st = xstat(path);
+       if (!S_ISDIR(st.st_mode))
+               return;
+
+       int dirfd = open(path, O_RDONLY);
+       if (dirfd < 0)
+               die("error opening %s: %m", path);
+
+       propagate_recurse(dirfd);
+       close(dirfd);
+}
+
+static void setattr_usage(void)
+{
+       puts("bcachefs setattr - set attributes on files in a bcachefs filessytem\n"
+            "Usage: bcachefs setattr [OPTIONS]... <files>\n"
+            "\n"
+            "Options:");
+
+       bch2_opts_usage(OPT_INODE);
+       puts("  -h            Display this help and exit\n"
+            "Report bugs to <linux-bcache@vger.kernel.org>");
+}
+
+int cmd_setattr(int argc, char *argv[])
+{
+       struct bch_opt_strs opts =
+               bch2_cmdline_opts_get(&argc, argv, OPT_INODE);
+       unsigned i;
+
+       for (i = 1; i < argc; i++)
+               if (argv[i][0] == '-') {
+                       printf("invalid option %s\n", argv[i]);
+                       setattr_usage();
+                       exit(EXIT_FAILURE);
+               }
+
+       if (argc <= 1)
+               die("Please supply one or more files");
+
+       for (i = 1; i < argc; i++)
+               do_setattr(argv[i], opts);
+
+       return 0;
+}
diff --git a/cmds.h b/cmds.h
index 3ebd12f805e01e6cb87e0cf2aa06f5d4759336eb..d64ffeeb81bc18273a160b6a40c852d6f81758fd 100644 (file)
--- a/cmds.h
+++ b/cmds.h
@@ -45,4 +45,6 @@ int cmd_migrate_superblock(int argc, char *argv[]);
 
 int cmd_version(int argc, char *argv[]);
 
+int cmd_setattr(int argc, char *argv[]);
+
 #endif /* _CMDS_H */