btrfs-progs: convert: add missing newlines for printfs
[btrfs-progs-unstable/devel.git] / props.c
bloba7e3e96bc92e24041a65aae3e3a1d01a3d02686e
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 "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 * Defined as synonyms in attr/xattr.h
35 #ifndef ENOATTR
36 #define ENOATTR ENODATA
37 #endif
39 static int prop_read_only(enum prop_object_type type,
40 const char *object,
41 const char *name,
42 const char *value)
44 int ret = 0;
45 int fd = -1;
46 u64 flags = 0;
48 fd = open(object, O_RDONLY);
49 if (fd < 0) {
50 ret = -errno;
51 error("failed to open %s: %s", object, strerror(-ret));
52 goto out;
55 ret = ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags);
56 if (ret < 0) {
57 ret = -errno;
58 error("failed to get flags for %s: %s", object,
59 strerror(-ret));
60 goto out;
63 if (!value) {
64 if (flags & BTRFS_SUBVOL_RDONLY)
65 fprintf(stdout, "ro=true\n");
66 else
67 fprintf(stdout, "ro=false\n");
68 ret = 0;
69 goto out;
72 if (!strcmp(value, "true")) {
73 flags |= BTRFS_SUBVOL_RDONLY;
74 } else if (!strcmp(value, "false")) {
75 flags = flags & ~BTRFS_SUBVOL_RDONLY;
76 } else {
77 ret = -EINVAL;
78 error("invalid value for property: %s", value);
79 goto out;
82 ret = ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &flags);
83 if (ret < 0) {
84 ret = -errno;
85 error("failed to set flags for %s: %s", object,
86 strerror(-ret));
87 goto out;
90 out:
91 if (fd != -1)
92 close(fd);
93 return ret;
96 static int prop_label(enum prop_object_type type,
97 const char *object,
98 const char *name,
99 const char *value)
101 int ret;
103 if (value) {
104 ret = set_label((char *) object, (char *) value);
105 } else {
106 char label[BTRFS_LABEL_SIZE];
108 ret = get_label((char *) object, label);
109 if (!ret)
110 fprintf(stdout, "label=%s\n", label);
113 return ret;
116 static int prop_compression(enum prop_object_type type,
117 const char *object,
118 const char *name,
119 const char *value)
121 int ret;
122 ssize_t sret;
123 int fd = -1;
124 DIR *dirstream = NULL;
125 char *buf = NULL;
126 char *xattr_name = NULL;
127 int open_flags = value ? O_RDWR : O_RDONLY;
129 fd = open_file_or_dir3(object, &dirstream, open_flags);
130 if (fd == -1) {
131 ret = -errno;
132 error("failed to open %s: %s", object, strerror(-ret));
133 goto out;
136 xattr_name = malloc(XATTR_BTRFS_PREFIX_LEN + strlen(name) + 1);
137 if (!xattr_name) {
138 ret = -ENOMEM;
139 goto out;
141 memcpy(xattr_name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN);
142 memcpy(xattr_name + XATTR_BTRFS_PREFIX_LEN, name, strlen(name));
143 xattr_name[XATTR_BTRFS_PREFIX_LEN + strlen(name)] = '\0';
145 if (value)
146 sret = fsetxattr(fd, xattr_name, value, strlen(value), 0);
147 else
148 sret = fgetxattr(fd, xattr_name, NULL, 0);
149 if (sret < 0) {
150 ret = -errno;
151 if (ret != -ENOATTR)
152 error("failed to %s compression for %s: %s",
153 value ? "set" : "get", object, strerror(-ret));
154 else
155 ret = 0;
156 goto out;
158 if (!value) {
159 size_t len = sret;
161 buf = malloc(len);
162 if (!buf) {
163 ret = -ENOMEM;
164 goto out;
166 sret = fgetxattr(fd, xattr_name, buf, len);
167 if (sret < 0) {
168 ret = -errno;
169 error("failed to get compression for %s: %s",
170 object, strerror(-ret));
171 goto out;
173 fprintf(stdout, "compression=%.*s\n", (int)len, buf);
176 ret = 0;
177 out:
178 free(xattr_name);
179 free(buf);
180 if (fd >= 0)
181 close_file_or_dir(fd, dirstream);
183 return ret;
186 const struct prop_handler prop_handlers[] = {
187 {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
188 prop_read_only},
189 {"label", "Set/get label of device.", 0,
190 prop_object_dev | prop_object_root, prop_label},
191 {"compression", "Set/get compression for a file or directory", 0,
192 prop_object_inode, prop_compression},
193 {NULL, NULL, 0, 0, NULL}