btrfs-progs: fix show super unknown flag output
[btrfs-progs-unstable/devel.git] / props.c
blob9fd612f970263c71d3a2b1f88e10bc77b7284559
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 <attr/xattr.h>
21 #include <fcntl.h>
22 #include <unistd.h>
24 #include "ctree.h"
25 #include "commands.h"
26 #include "utils.h"
27 #include "props.h"
29 #define XATTR_BTRFS_PREFIX "btrfs."
30 #define XATTR_BTRFS_PREFIX_LEN (sizeof(XATTR_BTRFS_PREFIX) - 1)
33 static int prop_read_only(enum prop_object_type type,
34 const char *object,
35 const char *name,
36 const char *value)
38 int ret = 0;
39 int fd = -1;
40 u64 flags = 0;
42 fd = open(object, O_RDONLY);
43 if (fd < 0) {
44 ret = -errno;
45 fprintf(stderr, "ERROR: open %s failed. %s\n",
46 object, strerror(-ret));
47 goto out;
50 ret = ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags);
51 if (ret < 0) {
52 ret = -errno;
53 fprintf(stderr, "ERROR: failed to get flags for %s. %s\n",
54 object, strerror(-ret));
55 goto out;
58 if (!value) {
59 if (flags & BTRFS_SUBVOL_RDONLY)
60 fprintf(stdout, "ro=true\n");
61 else
62 fprintf(stdout, "ro=false\n");
63 ret = 0;
64 goto out;
67 if (!strcmp(value, "true")) {
68 flags |= BTRFS_SUBVOL_RDONLY;
69 } else if (!strcmp(value, "false")) {
70 flags = flags & ~BTRFS_SUBVOL_RDONLY;
71 } else {
72 ret = -EINVAL;
73 fprintf(stderr, "ERROR: invalid value for property.\n");
74 goto out;
77 ret = ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &flags);
78 if (ret < 0) {
79 ret = -errno;
80 fprintf(stderr, "ERROR: failed to set flags for %s. %s\n",
81 object, strerror(-ret));
82 goto out;
85 out:
86 if (fd != -1)
87 close(fd);
88 return ret;
91 static int prop_label(enum prop_object_type type,
92 const char *object,
93 const char *name,
94 const char *value)
96 int ret;
98 if (value) {
99 ret = set_label((char *) object, (char *) value);
100 } else {
101 char label[BTRFS_LABEL_SIZE];
103 ret = get_label((char *) object, label);
104 if (!ret)
105 fprintf(stdout, "label=%s\n", label);
108 return ret;
111 static int prop_compression(enum prop_object_type type,
112 const char *object,
113 const char *name,
114 const char *value)
116 int ret;
117 ssize_t sret;
118 int fd = -1;
119 DIR *dirstream = NULL;
120 char *buf = NULL;
121 char *xattr_name = NULL;
122 int open_flags = value ? O_RDWR : O_RDONLY;
124 fd = open_file_or_dir3(object, &dirstream, open_flags);
125 if (fd == -1) {
126 ret = -errno;
127 fprintf(stderr, "ERROR: open %s failed. %s\n",
128 object, strerror(-ret));
129 goto out;
132 xattr_name = malloc(XATTR_BTRFS_PREFIX_LEN + strlen(name) + 1);
133 if (!xattr_name) {
134 ret = -ENOMEM;
135 goto out;
137 memcpy(xattr_name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN);
138 memcpy(xattr_name + XATTR_BTRFS_PREFIX_LEN, name, strlen(name));
139 xattr_name[XATTR_BTRFS_PREFIX_LEN + strlen(name)] = '\0';
141 if (value)
142 sret = fsetxattr(fd, xattr_name, value, strlen(value), 0);
143 else
144 sret = fgetxattr(fd, xattr_name, NULL, 0);
145 if (sret < 0) {
146 ret = -errno;
147 if (ret != -ENOATTR)
148 fprintf(stderr,
149 "ERROR: failed to %s compression for %s. %s\n",
150 value ? "set" : "get", object, strerror(-ret));
151 else
152 ret = 0;
153 goto out;
155 if (!value) {
156 size_t len = sret;
158 buf = malloc(len);
159 if (!buf) {
160 ret = -ENOMEM;
161 goto out;
163 sret = fgetxattr(fd, xattr_name, buf, len);
164 if (sret < 0) {
165 ret = -errno;
166 fprintf(stderr,
167 "ERROR: failed to get compression for %s. %s\n",
168 object, strerror(-ret));
169 goto out;
171 fprintf(stdout, "compression=%.*s\n", (int)len, buf);
174 ret = 0;
175 out:
176 free(xattr_name);
177 free(buf);
178 if (fd >= 0)
179 close_file_or_dir(fd, dirstream);
181 return ret;
185 const struct prop_handler prop_handlers[] = {
186 {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
187 prop_read_only},
188 {"label", "Set/get label of device.", 0,
189 prop_object_dev | prop_object_root, prop_label},
190 {"compression", "Set/get compression for a file or directory", 0,
191 prop_object_inode, prop_compression},
192 {0, 0, 0, 0, 0}