extattrctl.c revision 59247
1/*-
2 * Copyright (c) 1999, 2000 Robert N. M. Watson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *	$FreeBSD: head/usr.sbin/extattrctl/extattrctl.c 59247 2000-04-15 05:14:39Z rwatson $
27 */
28/*
29 * TrustedBSD Project - extended attribute support for UFS-like file systems
30 */
31
32#include <sys/types.h>
33#include <sys/uio.h>
34#include <sys/extattr.h>
35#include <ufs/ufs/extattr.h>
36#include <fcntl.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41
42void
43usage(void)
44{
45
46	fprintf(stderr,
47	    "usage:\n"
48	    "  extattrctl start [path]\n"
49	    "  extattrctl stop [path]\n"
50	    "  extattrctl initattr [attrsize] [attrfile]\n"
51	    "  extattrctl enable [path] [attrname] [attrfile]\n"
52	    "  extattrctl disable [path] [attrname]\n");
53}
54
55int
56main(int argc, char *argv[])
57{
58	struct ufs_extattr_fileheader	uef;
59	int	error = 0, i;
60
61	if (argc < 2) {
62		usage();
63		return(-1);
64	}
65
66	if (!strcmp(argv[1], "start")) {
67		if (argc != 3) {
68			usage();
69			return(-1);
70		}
71		error = extattrctl(argv[2], UFS_EXTATTR_CMD_START, 0, 0);
72	} else if (!strcmp(argv[1], "stop")) {
73		if (argc != 3) {
74			usage();
75			return(-1);
76		}
77		error = extattrctl(argv[2], UFS_EXTATTR_CMD_STOP, 0, 0);
78	} else if (!strcmp(argv[1], "enable")) {
79		if (argc != 5) {
80			usage();
81			return(-1);
82		}
83		error = extattrctl(argv[2], UFS_EXTATTR_CMD_ENABLE, argv[3],
84		    argv[4]);
85	} else if (!strcmp(argv[1], "disable")) {
86		if (argc != 4) {
87			usage();
88			return(-1);
89		}
90		error = extattrctl(argv[2], UFS_EXTATTR_CMD_DISABLE, argv[3],
91		    NULL);
92	} else if (!strcmp(argv[1], "initattr")) {
93		if (argc != 4) {
94			usage();
95			return(-1);
96		}
97		if ((i = open(argv[3], O_CREAT | O_EXCL | O_WRONLY, 0600)) !=
98		    -1) {
99			uef.uef_write_perm = UFS_EXTATTR_PERM_OWNER;
100			uef.uef_read_perm = UFS_EXTATTR_PERM_ANYONE;
101			uef.uef_size = atoi(argv[2]);
102			if (write(i, &uef, sizeof(uef)) == -1) {
103				error = -1;
104			} else
105				error = close(i);
106		} else
107			error = -1;
108	} else {
109		usage();
110		return(-1);
111	}
112
113	if (error)
114		perror(argv[1]);
115
116	return(error);
117}
118