2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999-2002 Robert N. M. Watson
7 * This software was developed by Robert Watson for the TrustedBSD Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Developed by the TrustedBSD Project.
34 * Support for file system extended attribute.
37 #include <sys/types.h>
39 #include <sys/extattr.h>
40 #include <sys/param.h>
41 #include <sys/mount.h>
43 #include <ufs/ufs/extattr.h>
53 int initattr(int argc, char *argv[]);
54 int showattr(int argc, char *argv[]);
55 long num_inodes_by_path(char *path);
64 " extattrctl start path\n"
65 " extattrctl stop path\n"
66 " extattrctl initattr [-f] [-p path] attrsize attrfile\n"
67 " extattrctl showattr attrfile\n"
68 " extattrctl enable path attrnamespace attrname attrfile\n"
69 " extattrctl disable path attrnamespace attrname\n");
74 num_inodes_by_path(char *path)
79 error = statfs(path, &buf);
88 static const char zero_buf[8192];
91 initattr(int argc, char *argv[])
93 struct ufs_extattr_fileheader uef;
95 int ch, i, error, flags;
99 flags = O_CREAT | O_WRONLY | O_TRUNC | O_EXCL;
101 while ((ch = getopt(argc, argv, "fp:r:w:")) != -1)
121 if ((i = open(argv[1], flags, 0600)) == -1) {
122 /* unable to open file */
126 uef.uef_magic = UFS_EXTATTR_MAGIC;
127 uef.uef_version = UFS_EXTATTR_VERSION;
128 uef.uef_size = atoi(argv[0]);
129 if (write(i, &uef, sizeof(uef)) == -1)
131 else if (fs_path != NULL) {
132 easize = (sizeof uef + uef.uef_size) *
133 num_inodes_by_path(fs_path);
135 if (easize > sizeof zero_buf)
136 wlen = write(i, zero_buf, sizeof zero_buf);
138 wlen = write(i, zero_buf, easize);
158 showattr(int argc, char *argv[])
160 struct ufs_extattr_fileheader uef;
166 fd = open(argv[0], O_RDONLY);
172 i = read(fd, &uef, sizeof(uef));
178 if (i != sizeof(uef)) {
179 fprintf(stderr, "%s: invalid file header\n", argv[0]);
184 if (uef.uef_magic != UFS_EXTATTR_MAGIC) {
185 fprintf(stderr, "%s: bad magic\n", argv[0]);
190 printf("%s: version %d, size %d\n", argv[0], uef.uef_version,
198 main(int argc, char *argv[])
200 int error = 0, attrnamespace;
205 if (!strcmp(argv[1], "start")) {
208 error = extattrctl(argv[2], UFS_EXTATTR_CMD_START, NULL, 0,
211 perror("extattrctl start");
214 } else if (!strcmp(argv[1], "stop")) {
217 error = extattrctl(argv[2], UFS_EXTATTR_CMD_STOP, NULL, 0,
220 perror("extattrctl stop");
223 } else if (!strcmp(argv[1], "enable")) {
226 error = extattr_string_to_namespace(argv[3], &attrnamespace);
228 perror("extattrctl enable");
231 error = extattrctl(argv[2], UFS_EXTATTR_CMD_ENABLE, argv[5],
232 attrnamespace, argv[4]);
234 perror("extattrctl enable");
237 } else if (!strcmp(argv[1], "disable")) {
240 error = extattr_string_to_namespace(argv[3], &attrnamespace);
242 perror("extattrctl disable");
245 error = extattrctl(argv[2], UFS_EXTATTR_CMD_DISABLE, NULL,
246 attrnamespace, argv[4]);
248 perror("extattrctl disable");
251 } else if (!strcmp(argv[1], "initattr")) {
254 error = initattr(argc, argv);
257 } else if (!strcmp(argv[1], "showattr")) {
260 error = showattr(argc, argv);