2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public
4 * License v2 as published by the Free Software Foundation.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 * General Public License for more details.
11 * You should have received a copy of the GNU General Public
12 * License along with this program; if not, write to the
13 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14 * Boston, MA 021110-1307, USA.
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/xattr.h>
24 #include <btrfsutil.h>
31 #define XATTR_BTRFS_PREFIX "btrfs."
32 #define XATTR_BTRFS_PREFIX_LEN (sizeof(XATTR_BTRFS_PREFIX) - 1)
35 * Defined as synonyms in attr/xattr.h
38 #define ENOATTR ENODATA
41 static int prop_read_only(enum prop_object_type type,
46 enum btrfs_util_error err;
50 if (!strcmp(value, "true")) {
52 } else if (!strcmp(value, "false")) {
55 error("invalid value for property: %s", value);
59 err = btrfs_util_set_subvolume_read_only(object, read_only);
61 error_btrfs_util(err);
65 err = btrfs_util_get_subvolume_read_only(object, &read_only);
67 error_btrfs_util(err);
71 printf("ro=%s\n", read_only ? "true" : "false");
77 static int prop_label(enum prop_object_type type,
85 ret = set_label((char *) object, (char *) value);
87 char label[BTRFS_LABEL_SIZE];
89 ret = get_label((char *) object, label);
91 fprintf(stdout, "label=%s\n", label);
97 static int prop_compression(enum prop_object_type type,
105 DIR *dirstream = NULL;
107 char *xattr_name = NULL;
108 int open_flags = value ? O_RDWR : O_RDONLY;
110 fd = open_file_or_dir3(object, &dirstream, open_flags);
113 error("failed to open %s: %s", object, strerror(-ret));
117 xattr_name = malloc(XATTR_BTRFS_PREFIX_LEN + strlen(name) + 1);
122 memcpy(xattr_name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN);
123 memcpy(xattr_name + XATTR_BTRFS_PREFIX_LEN, name, strlen(name));
124 xattr_name[XATTR_BTRFS_PREFIX_LEN + strlen(name)] = '\0';
127 if (strcmp(value, "no") == 0 || strcmp(value, "none") == 0)
129 sret = fsetxattr(fd, xattr_name, value, strlen(value), 0);
131 sret = fgetxattr(fd, xattr_name, NULL, 0);
136 error("failed to %s compression for %s: %s",
137 value ? "set" : "get", object, strerror(-ret));
150 sret = fgetxattr(fd, xattr_name, buf, len);
153 error("failed to get compression for %s: %s",
154 object, strerror(-ret));
157 fprintf(stdout, "compression=%.*s\n", (int)len, buf);
165 close_file_or_dir(fd, dirstream);
170 const struct prop_handler prop_handlers[] = {
171 {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
173 {"label", "Set/get label of device.", 0,
174 prop_object_dev | prop_object_root, prop_label},
175 {"compression", "Set/get compression for a file or directory", 0,
176 prop_object_inode, prop_compression},
177 {NULL, NULL, 0, 0, NULL}