btrfs-progs: remove comments about delayed ref in backref.c
[btrfs-progs-unstable/devel.git] / props.c
blobe4edba06e573353a5a466282044d432499b59e92
1 /*
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.
17 #include <sys/stat.h>
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/xattr.h>
21 #include <fcntl.h>
22 #include <unistd.h>
24 #include <btrfsutil.h>
26 #include "ctree.h"
27 #include "commands.h"
28 #include "utils.h"
29 #include "props.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
37 #ifndef ENOATTR
38 #define ENOATTR ENODATA
39 #endif
41 static int prop_read_only(enum prop_object_type type,
42 const char *object,
43 const char *name,
44 const char *value)
46 enum btrfs_util_error err;
47 bool read_only;
49 if (value) {
50 if (!strcmp(value, "true")) {
51 read_only = true;
52 } else if (!strcmp(value, "false")) {
53 read_only = false;
54 } else {
55 error("invalid value for property: %s", value);
56 return -EINVAL;
59 err = btrfs_util_set_subvolume_read_only(object, read_only);
60 if (err) {
61 error_btrfs_util(err);
62 return -errno;
64 } else {
65 err = btrfs_util_get_subvolume_read_only(object, &read_only);
66 if (err) {
67 error_btrfs_util(err);
68 return -errno;
71 printf("ro=%s\n", read_only ? "true" : "false");
74 return 0;
77 static int prop_label(enum prop_object_type type,
78 const char *object,
79 const char *name,
80 const char *value)
82 int ret;
84 if (value) {
85 ret = set_label((char *) object, (char *) value);
86 } else {
87 char label[BTRFS_LABEL_SIZE];
89 ret = get_label((char *) object, label);
90 if (!ret)
91 fprintf(stdout, "label=%s\n", label);
94 return ret;
97 static int prop_compression(enum prop_object_type type,
98 const char *object,
99 const char *name,
100 const char *value)
102 int ret;
103 ssize_t sret;
104 int fd = -1;
105 DIR *dirstream = NULL;
106 char *buf = 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);
111 if (fd == -1) {
112 ret = -errno;
113 error("failed to open %s: %s", object, strerror(-ret));
114 goto out;
117 xattr_name = malloc(XATTR_BTRFS_PREFIX_LEN + strlen(name) + 1);
118 if (!xattr_name) {
119 ret = -ENOMEM;
120 goto out;
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';
126 if (value) {
127 if (strcmp(value, "no") == 0 || strcmp(value, "none") == 0)
128 value = "";
129 sret = fsetxattr(fd, xattr_name, value, strlen(value), 0);
130 } else {
131 sret = fgetxattr(fd, xattr_name, NULL, 0);
133 if (sret < 0) {
134 ret = -errno;
135 if (ret != -ENOATTR)
136 error("failed to %s compression for %s: %s",
137 value ? "set" : "get", object, strerror(-ret));
138 else
139 ret = 0;
140 goto out;
142 if (!value) {
143 size_t len = sret;
145 buf = malloc(len);
146 if (!buf) {
147 ret = -ENOMEM;
148 goto out;
150 sret = fgetxattr(fd, xattr_name, buf, len);
151 if (sret < 0) {
152 ret = -errno;
153 error("failed to get compression for %s: %s",
154 object, strerror(-ret));
155 goto out;
157 fprintf(stdout, "compression=%.*s\n", (int)len, buf);
160 ret = 0;
161 out:
162 free(xattr_name);
163 free(buf);
164 if (fd >= 0)
165 close_file_or_dir(fd, dirstream);
167 return ret;
170 const struct prop_handler prop_handlers[] = {
171 {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
172 prop_read_only},
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}