1 /* $NetBSD: extattrctl.c,v 1.1 2005/08/28 19:37:59 thorpej Exp $ */
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
30 * $FreeBSD: src/usr.sbin/extattrctl/extattrctl.c,v 1.19 2002/04/19 01:42:55 rwatson Exp $
34 * Developed by the TrustedBSD Project.
35 * Support for file system extended attribute.
38 #include <sys/types.h>
40 #include <sys/extattr.h>
41 #include <sys/param.h>
42 #include <sys/mount.h>
44 #include <ufs/ufs/extattr.h>
54 #include <machine/bswap.h>
55 #include <machine/endian.h>
75 " %s initattr [-f] [-p path] attrsize attrfile\n"
76 " %s showattr attrfile\n"
77 " %s enable path attrnamespace attrname attrfile\n"
78 " %s disable path attrnamespace attrname\n",
79 getprogname(), getprogname(), getprogname(),
80 getprogname(), getprogname(), getprogname());
85 num_inodes_by_path(const char *path
)
89 if (statvfs(path
, &buf
) == -1) {
90 warn("statvfs(%s)", path
);
97 static const char zero_buf
[8192];
100 initattr(int argc
, char *argv
[])
102 struct ufs_extattr_fileheader uef
;
103 char *fs_path
= NULL
;
104 int ch
, i
, error
, flags
;
108 flags
= O_CREAT
| O_WRONLY
| O_TRUNC
| O_EXCL
;
110 while ((ch
= getopt(argc
, argv
, "fp:r:w:")) != -1) {
119 #if BYTE_ORDER == LITTLE_ENDIAN
120 if (strcmp(optarg
, "le") == 0)
122 else if (strcmp(optarg
, "be") == 0)
127 if (strcmp(optarg
, "be") == 0)
129 else if (strcmp(optarg
, "le") == 0)
148 if ((i
= open(argv
[1], flags
, 0600)) == -1) {
149 warn("open(%s)", argv
[1]);
152 uef
.uef_magic
= rw32(UFS_EXTATTR_MAGIC
);
153 uef
.uef_version
= rw32(UFS_EXTATTR_VERSION
);
154 uef
.uef_size
= rw32(atoi(argv
[0]));
155 if (write(i
, &uef
, sizeof(uef
)) != sizeof(uef
)) {
156 warn("unable to write arribute file header");
158 } else if (fs_path
!= NULL
) {
159 easize
= (sizeof(uef
) + uef
.uef_size
) *
160 num_inodes_by_path(fs_path
);
162 size_t x
= (easize
> sizeof(zero_buf
)) ?
163 sizeof(zero_buf
) : easize
;
164 wlen
= write(i
, zero_buf
, x
);
165 if ((size_t)wlen
!= x
) {
166 warn("unable to write attribute file");
182 showattr(int argc
, char *argv
[])
184 struct ufs_extattr_fileheader uef
;
191 fd
= open(argv
[0], O_RDONLY
);
193 warn("open(%s)", argv
[0]);
197 i
= read(fd
, &uef
, sizeof(uef
));
198 if (i
!= sizeof(uef
)) {
199 warn("unable to read attribute file header");
203 if (rw32(uef
.uef_magic
) != UFS_EXTATTR_MAGIC
) {
205 if (rw32(uef
.uef_magic
) != UFS_EXTATTR_MAGIC
) {
206 fprintf(stderr
, "%s: bad magic\n", argv
[0]);
211 #if BYTE_ORDER == LITTLE_ENDIAN
212 bo
= needswap
? "big-endian" : "little-endian";
214 bo
= needswap
? "little-endian" : "big-endian";
217 printf("%s: version %u, size %u, byte-order: %s\n",
218 argv
[0], rw32(uef
.uef_version
), rw32(uef
.uef_size
), bo
);
224 main(int argc
, char *argv
[])
226 int error
= 0, attrnamespace
;
231 if (!strcmp(argv
[1], "start")) {
234 error
= extattrctl(argv
[2], UFS_EXTATTR_CMD_START
, NULL
, 0,
238 } else if (!strcmp(argv
[1], "stop")) {
241 error
= extattrctl(argv
[2], UFS_EXTATTR_CMD_STOP
, NULL
, 0,
245 } else if (!strcmp(argv
[1], "enable")) {
248 error
= extattr_string_to_namespace(argv
[3], &attrnamespace
);
250 errx(1, "bad namespace: %s", argv
[3]);
251 error
= extattrctl(argv
[2], UFS_EXTATTR_CMD_ENABLE
, argv
[5],
252 attrnamespace
, argv
[4]);
255 } else if (!strcmp(argv
[1], "disable")) {
258 error
= extattr_string_to_namespace(argv
[3], &attrnamespace
);
260 errx(1, "bad namespace: %s", argv
[3]);
261 error
= extattrctl(argv
[2], UFS_EXTATTR_CMD_DISABLE
, NULL
,
262 attrnamespace
, argv
[4]);
265 } else if (!strcmp(argv
[1], "initattr")) {
268 error
= initattr(argc
, argv
);
271 } else if (!strcmp(argv
[1], "showattr")) {
274 error
= showattr(argc
, argv
);