2 * Copyright (c) 1999-2001 Robert N. M. Watson
5 * This software was developed by Robert Watson for the TrustedBSD Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Developed by the TrustedBSD Project.
32 * Support for file system extended attribute.
35 #include <sys/types.h>
37 #include <sys/extattr.h>
38 #include <sys/param.h>
39 #include <sys/mount.h>
41 #include <ufs/ufs/extattr.h>
51 int initattr(int argc, char *argv[]);
52 int showattr(int argc, char *argv[]);
53 long num_inodes_by_path(char *path);
62 " extattrctl start [path]\n"
63 " extattrctl stop [path]\n"
64 " extattrctl initattr [-f] [-p path] [attrsize] [attrfile]\n"
65 " extattrctl showattr [attrfile]\n"
66 " extattrctl enable [path] [attrnamespace] [attrname] [attrfile]\n"
67 " extattrctl disable [path] [attrnamespace] [attrname]\n");
72 num_inodes_by_path(char *path)
77 error = statfs(path, &buf);
87 initattr(int argc, char *argv[])
89 struct ufs_extattr_fileheader uef;
91 char *zero_buf = NULL;
92 long loop, num_inodes;
93 int ch, i, error, chunksize, overwrite = 0, flags;
96 while ((ch = getopt(argc, argv, "fp:r:w:")) != -1)
102 if ((fs_path = strdup(optarg)) == NULL) {
119 flags = O_CREAT | O_WRONLY;
121 flags = O_CREAT | O_EXCL | O_WRONLY;
124 if ((i = open(argv[1], flags, 0600)) != -1) {
125 uef.uef_magic = UFS_EXTATTR_MAGIC;
126 uef.uef_version = UFS_EXTATTR_VERSION;
127 uef.uef_size = atoi(argv[0]);
128 if (write(i, &uef, sizeof(uef)) == -1)
131 chunksize = sizeof(struct ufs_extattr_header) +
133 zero_buf = (char *) (malloc(chunksize));
134 if (zero_buf == NULL) {
139 memset(zero_buf, 0, chunksize);
140 num_inodes = num_inodes_by_path(fs_path);
141 for (loop = 0; loop < num_inodes; loop++) {
142 error = write(i, zero_buf, chunksize);
143 if (error != chunksize) {
152 /* unable to open file */
166 showattr(int argc, char *argv[])
168 struct ufs_extattr_fileheader uef;
174 fd = open(argv[0], O_RDONLY);
180 i = read(fd, &uef, sizeof(uef));
185 if (i != sizeof(uef)) {
186 fprintf(stderr, "%s: invalid file header\n", argv[0]);
190 if (uef.uef_magic != UFS_EXTATTR_MAGIC) {
191 fprintf(stderr, "%s: bad magic\n", argv[0]);
195 printf("%s: version %d, size %d\n", argv[0], uef.uef_version,
202 main(int argc, char *argv[])
204 int error = 0, attrnamespace;
209 if (!strcmp(argv[1], "start")) {
212 error = extattrctl(argv[2], UFS_EXTATTR_CMD_START, NULL, 0,
215 perror("extattrctl start");
218 } else if (!strcmp(argv[1], "stop")) {
221 error = extattrctl(argv[2], UFS_EXTATTR_CMD_STOP, NULL, 0,
224 perror("extattrctl stop");
227 } else if (!strcmp(argv[1], "enable")) {
230 error = extattr_string_to_namespace(argv[3], &attrnamespace);
232 perror("extattrctl enable");
235 error = extattrctl(argv[2], UFS_EXTATTR_CMD_ENABLE, argv[5],
236 attrnamespace, argv[4]);
238 perror("extattrctl enable");
241 } else if (!strcmp(argv[1], "disable")) {
244 error = extattr_string_to_namespace(argv[3], &attrnamespace);
246 perror("extattrctl disable");
249 error = extattrctl(argv[2], UFS_EXTATTR_CMD_DISABLE, NULL,
250 attrnamespace, argv[4]);
252 perror("extattrctl disable");
255 } else if (!strcmp(argv[1], "initattr")) {
258 error = initattr(argc, argv);
261 } else if (!strcmp(argv[1], "showattr")) {
264 error = showattr(argc, argv);