btrfs-progs: doc: make all commands and subcommands bold
[platform/upstream/btrfs-progs.git] / props.c
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.
5  *
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.
10  *
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.
15  */
16
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>
23
24 #include "ctree.h"
25 #include "commands.h"
26 #include "utils.h"
27 #include "props.h"
28
29 #define XATTR_BTRFS_PREFIX     "btrfs."
30 #define XATTR_BTRFS_PREFIX_LEN (sizeof(XATTR_BTRFS_PREFIX) - 1)
31
32
33 static int prop_read_only(enum prop_object_type type,
34                           const char *object,
35                           const char *name,
36                           const char *value)
37 {
38         int ret = 0;
39         int fd = -1;
40         u64 flags = 0;
41
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;
48         }
49
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;
56         }
57
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;
65         }
66
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;
75         }
76
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;
83         }
84
85 out:
86         if (fd != -1)
87                 close(fd);
88         return ret;
89 }
90
91 static int prop_label(enum prop_object_type type,
92                       const char *object,
93                       const char *name,
94                       const char *value)
95 {
96         int ret;
97
98         if (value) {
99                 ret = set_label((char *) object, (char *) value);
100         } else {
101                 char label[BTRFS_LABEL_SIZE];
102
103                 ret = get_label((char *) object, label);
104                 if (!ret)
105                         fprintf(stdout, "label=%s\n", label);
106         }
107
108         return ret;
109 }
110
111 static int prop_compression(enum prop_object_type type,
112                             const char *object,
113                             const char *name,
114                             const char *value)
115 {
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;
123
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;
130         }
131
132         xattr_name = malloc(XATTR_BTRFS_PREFIX_LEN + strlen(name) + 1);
133         if (!xattr_name) {
134                 ret = -ENOMEM;
135                 goto out;
136         }
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';
140
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;
154         }
155         if (!value) {
156                 size_t len = sret;
157
158                 buf = malloc(len);
159                 if (!buf) {
160                         ret = -ENOMEM;
161                         goto out;
162                 }
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;
170                 }
171                 fprintf(stdout, "compression=%.*s\n", (int)len, buf);
172         }
173
174         ret = 0;
175 out:
176         free(xattr_name);
177         free(buf);
178         if (fd >= 0)
179                 close_file_or_dir(fd, dirstream);
180
181         return ret;
182 }
183
184
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}
193 };