2 * Copyright (c) 1999-2002 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
);
86 static const char zero_buf
[8192];
89 initattr(int argc
, char *argv
[])
91 struct ufs_extattr_fileheader uef
;
93 int ch
, i
, error
, flags
;
97 flags
= O_CREAT
| O_WRONLY
| O_TRUNC
| O_EXCL
;
99 while ((ch
= getopt(argc
, argv
, "fp:r:w:")) != -1)
119 if ((i
= open(argv
[1], flags
, 0600)) == -1) {
120 /* unable to open file */
124 uef
.uef_magic
= UFS_EXTATTR_MAGIC
;
125 uef
.uef_version
= UFS_EXTATTR_VERSION
;
126 uef
.uef_size
= atoi(argv
[0]);
127 if (write(i
, &uef
, sizeof(uef
)) == -1)
129 else if (fs_path
!= NULL
) {
130 easize
= (sizeof uef
+ uef
.uef_size
) *
131 num_inodes_by_path(fs_path
);
133 if (easize
> sizeof zero_buf
)
134 wlen
= write(i
, zero_buf
, sizeof zero_buf
);
136 wlen
= write(i
, zero_buf
, easize
);
154 showattr(int argc
, char *argv
[])
156 struct ufs_extattr_fileheader uef
;
162 fd
= open(argv
[0], O_RDONLY
);
168 i
= read(fd
, &uef
, sizeof(uef
));
173 if (i
!= sizeof(uef
)) {
174 fprintf(stderr
, "%s: invalid file header\n", argv
[0]);
178 if (uef
.uef_magic
!= UFS_EXTATTR_MAGIC
) {
179 fprintf(stderr
, "%s: bad magic\n", argv
[0]);
183 printf("%s: version %d, size %d\n", argv
[0], uef
.uef_version
,
190 main(int argc
, char *argv
[])
192 int error
= 0, attrnamespace
;
197 if (!strcmp(argv
[1], "start")) {
200 error
= extattrctl(argv
[2], UFS_EXTATTR_CMD_START
, NULL
, 0,
203 perror("extattrctl start");
206 } else if (!strcmp(argv
[1], "stop")) {
209 error
= extattrctl(argv
[2], UFS_EXTATTR_CMD_STOP
, NULL
, 0,
212 perror("extattrctl stop");
215 } else if (!strcmp(argv
[1], "enable")) {
218 error
= extattr_string_to_namespace(argv
[3], &attrnamespace
);
220 perror("extattrctl enable");
223 error
= extattrctl(argv
[2], UFS_EXTATTR_CMD_ENABLE
, argv
[5],
224 attrnamespace
, argv
[4]);
226 perror("extattrctl enable");
229 } else if (!strcmp(argv
[1], "disable")) {
232 error
= extattr_string_to_namespace(argv
[3], &attrnamespace
);
234 perror("extattrctl disable");
237 error
= extattrctl(argv
[2], UFS_EXTATTR_CMD_DISABLE
, NULL
,
238 attrnamespace
, argv
[4]);
240 perror("extattrctl disable");
243 } else if (!strcmp(argv
[1], "initattr")) {
246 error
= initattr(argc
, argv
);
249 } else if (!strcmp(argv
[1], "showattr")) {
252 error
= showattr(argc
, argv
);