btrfs-progs: rename __strncpy__null to __strncpy_null
[btrfs-progs-unstable/devel.git] / props.c
blob5b7493240b09f81825fee1ceb186e5e40a8da931
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 fprintf(stderr, "ERROR: open %s failed. %s\n",
52 object, strerror(-ret));
53 goto out;
56 ret = ioctl(fd, BTRFS_IOC_SUBVOL_GETFLAGS, &flags);
57 if (ret < 0) {
58 ret = -errno;
59 fprintf(stderr, "ERROR: failed to get flags for %s. %s\n",
60 object, strerror(-ret));
61 goto out;
64 if (!value) {
65 if (flags & BTRFS_SUBVOL_RDONLY)
66 fprintf(stdout, "ro=true\n");
67 else
68 fprintf(stdout, "ro=false\n");
69 ret = 0;
70 goto out;
73 if (!strcmp(value, "true")) {
74 flags |= BTRFS_SUBVOL_RDONLY;
75 } else if (!strcmp(value, "false")) {
76 flags = flags & ~BTRFS_SUBVOL_RDONLY;
77 } else {
78 ret = -EINVAL;
79 fprintf(stderr, "ERROR: invalid value for property.\n");
80 goto out;
83 ret = ioctl(fd, BTRFS_IOC_SUBVOL_SETFLAGS, &flags);
84 if (ret < 0) {
85 ret = -errno;
86 fprintf(stderr, "ERROR: failed to set flags for %s. %s\n",
87 object, strerror(-ret));
88 goto out;
91 out:
92 if (fd != -1)
93 close(fd);
94 return ret;
97 static int prop_label(enum prop_object_type type,
98 const char *object,
99 const char *name,
100 const char *value)
102 int ret;
104 if (value) {
105 ret = set_label((char *) object, (char *) value);
106 } else {
107 char label[BTRFS_LABEL_SIZE];
109 ret = get_label((char *) object, label);
110 if (!ret)
111 fprintf(stdout, "label=%s\n", label);
114 return ret;
117 static int prop_compression(enum prop_object_type type,
118 const char *object,
119 const char *name,
120 const char *value)
122 int ret;
123 ssize_t sret;
124 int fd = -1;
125 DIR *dirstream = NULL;
126 char *buf = NULL;
127 char *xattr_name = NULL;
128 int open_flags = value ? O_RDWR : O_RDONLY;
130 fd = open_file_or_dir3(object, &dirstream, open_flags);
131 if (fd == -1) {
132 ret = -errno;
133 fprintf(stderr, "ERROR: open %s failed. %s\n",
134 object, strerror(-ret));
135 goto out;
138 xattr_name = malloc(XATTR_BTRFS_PREFIX_LEN + strlen(name) + 1);
139 if (!xattr_name) {
140 ret = -ENOMEM;
141 goto out;
143 memcpy(xattr_name, XATTR_BTRFS_PREFIX, XATTR_BTRFS_PREFIX_LEN);
144 memcpy(xattr_name + XATTR_BTRFS_PREFIX_LEN, name, strlen(name));
145 xattr_name[XATTR_BTRFS_PREFIX_LEN + strlen(name)] = '\0';
147 if (value)
148 sret = fsetxattr(fd, xattr_name, value, strlen(value), 0);
149 else
150 sret = fgetxattr(fd, xattr_name, NULL, 0);
151 if (sret < 0) {
152 ret = -errno;
153 if (ret != -ENOATTR)
154 fprintf(stderr,
155 "ERROR: failed to %s compression for %s. %s\n",
156 value ? "set" : "get", object, strerror(-ret));
157 else
158 ret = 0;
159 goto out;
161 if (!value) {
162 size_t len = sret;
164 buf = malloc(len);
165 if (!buf) {
166 ret = -ENOMEM;
167 goto out;
169 sret = fgetxattr(fd, xattr_name, buf, len);
170 if (sret < 0) {
171 ret = -errno;
172 fprintf(stderr,
173 "ERROR: failed to get compression for %s. %s\n",
174 object, strerror(-ret));
175 goto out;
177 fprintf(stdout, "compression=%.*s\n", (int)len, buf);
180 ret = 0;
181 out:
182 free(xattr_name);
183 free(buf);
184 if (fd >= 0)
185 close_file_or_dir(fd, dirstream);
187 return ret;
190 const struct prop_handler prop_handlers[] = {
191 {"ro", "Set/get read-only flag of subvolume.", 0, prop_object_subvol,
192 prop_read_only},
193 {"label", "Set/get label of device.", 0,
194 prop_object_dev | prop_object_root, prop_label},
195 {"compression", "Set/get compression for a file or directory", 0,
196 prop_object_inode, prop_compression},
197 {NULL, NULL, 0, 0, NULL}