extattrctl.c revision 65377
159247Srwatson/*- 259247Srwatson * Copyright (c) 1999, 2000 Robert N. M. Watson 359247Srwatson * All rights reserved. 459247Srwatson * 559247Srwatson * Redistribution and use in source and binary forms, with or without 659247Srwatson * modification, are permitted provided that the following conditions 759247Srwatson * are met: 859247Srwatson * 1. Redistributions of source code must retain the above copyright 959247Srwatson * notice, this list of conditions and the following disclaimer. 1059247Srwatson * 2. Redistributions in binary form must reproduce the above copyright 1159247Srwatson * notice, this list of conditions and the following disclaimer in the 1259247Srwatson * documentation and/or other materials provided with the distribution. 1359247Srwatson * 1459247Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1559247Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1659247Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1759247Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1859247Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1959247Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2059247Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2159247Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2259247Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2359247Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2459247Srwatson * SUCH DAMAGE. 2559247Srwatson * 2659247Srwatson * $FreeBSD: head/usr.sbin/extattrctl/extattrctl.c 65377 2000-09-02 20:31:26Z rwatson $ 2759247Srwatson */ 2859247Srwatson/* 2959247Srwatson * TrustedBSD Project - extended attribute support for UFS-like file systems 3059247Srwatson */ 3159247Srwatson 3259247Srwatson#include <sys/types.h> 3359247Srwatson#include <sys/uio.h> 3459247Srwatson#include <sys/extattr.h> 3559445Srwatson#include <sys/param.h> 3659445Srwatson#include <sys/mount.h> 3759445Srwatson 3859247Srwatson#include <ufs/ufs/extattr.h> 3959445Srwatson 4059247Srwatson#include <fcntl.h> 4159247Srwatson#include <stdio.h> 4259247Srwatson#include <stdlib.h> 4359247Srwatson#include <string.h> 4459247Srwatson#include <unistd.h> 4559247Srwatson 4659401Srwatsonextern char *optarg; 4759401Srwatsonextern int optind; 4859401Srwatson 4959247Srwatsonvoid 5059247Srwatsonusage(void) 5159247Srwatson{ 5259247Srwatson 5359247Srwatson fprintf(stderr, 5459247Srwatson "usage:\n" 5559247Srwatson " extattrctl start [path]\n" 5659247Srwatson " extattrctl stop [path]\n" 5765377Srwatson " extattrctl initattr [-p path] [attrsize] [attrfile]\n" 5859247Srwatson " extattrctl enable [path] [attrname] [attrfile]\n" 5959247Srwatson " extattrctl disable [path] [attrname]\n"); 6059401Srwatson exit(-1); 6159247Srwatson} 6259247Srwatson 6359445Srwatsonlong 6459445Srwatsonnum_inodes_by_path(char *path) 6559445Srwatson{ 6659445Srwatson struct statfs buf; 6759445Srwatson int error; 6859445Srwatson 6959445Srwatson error = statfs(path, &buf); 7059445Srwatson if (error) { 7159445Srwatson perror("statfs"); 7259445Srwatson return (-1); 7359445Srwatson } 7459445Srwatson 7559445Srwatson return (buf.f_files); 7659445Srwatson} 7759445Srwatson 7859401Srwatsonint 7959401Srwatsoninitattr(int argc, char *argv[]) 8059401Srwatson{ 8159247Srwatson struct ufs_extattr_fileheader uef; 8259445Srwatson char *fs_path = NULL; 8359445Srwatson char *zero_buf = NULL; 8459445Srwatson long loop, num_inodes; 8559401Srwatson int ch, i, error; 8659247Srwatson 8759445Srwatson optind = 0; 8862995Srwatson while ((ch = getopt(argc, argv, "p:r:w:")) != -1) 8959401Srwatson switch (ch) { 9059401Srwatson case 'p': 9159445Srwatson fs_path = strdup(optarg); 9259401Srwatson break; 9359401Srwatson case '?': 9459401Srwatson default: 9559401Srwatson usage(); 9659401Srwatson } 9759401Srwatson 9859401Srwatson argc -= optind; 9959401Srwatson argv += optind; 10059401Srwatson 10159401Srwatson if (argc != 2) 10259247Srwatson usage(); 10359401Srwatson 10459401Srwatson error = 0; 10559401Srwatson if ((i = open(argv[1], O_CREAT | O_EXCL | O_WRONLY, 0600)) != -1) { 10659401Srwatson uef.uef_magic = UFS_EXTATTR_MAGIC; 10759401Srwatson uef.uef_version = UFS_EXTATTR_VERSION; 10859401Srwatson uef.uef_size = atoi(argv[0]); 10959401Srwatson if (write(i, &uef, sizeof(uef)) == -1) 11059401Srwatson error = -1; 11159445Srwatson else if (fs_path) { 11259445Srwatson zero_buf = (char *) (malloc(uef.uef_size)); 11359445Srwatson if (zero_buf == NULL) { 11459445Srwatson perror("malloc"); 11559445Srwatson unlink(argv[1]); 11659445Srwatson return (-1); 11759445Srwatson } 11859445Srwatson memset(zero_buf, 0, uef.uef_size); 11959445Srwatson num_inodes = num_inodes_by_path(fs_path); 12059445Srwatson for (loop = 0; loop < num_inodes; loop++) { 12159445Srwatson error = write(i, zero_buf, uef.uef_size); 12259445Srwatson if (error != uef.uef_size) { 12359445Srwatson perror("write"); 12459445Srwatson unlink(argv[1]); 12559445Srwatson return (-1); 12659445Srwatson } 12759445Srwatson } 12859401Srwatson } 12959247Srwatson } 13065377Srwatson if (i == -1) { 13165377Srwatson /* unable to open file */ 13259445Srwatson perror(argv[1]); 13365377Srwatson return (-1); 13465377Srwatson } 13565377Srwatson if (error == -1) { 13665377Srwatson perror(argv[1]); 13759445Srwatson unlink(argv[1]); 13859401Srwatson return (-1); 13959401Srwatson } 14059247Srwatson 14159401Srwatson return (0); 14259401Srwatson} 14359401Srwatson 14459401Srwatsonint 14559401Srwatsonmain(int argc, char *argv[]) 14659401Srwatson{ 14759401Srwatson int error = 0; 14859401Srwatson 14959401Srwatson if (argc < 2) 15059401Srwatson usage(); 15159401Srwatson 15259247Srwatson if (!strcmp(argv[1], "start")) { 15359401Srwatson if (argc != 3) 15459247Srwatson usage(); 15559247Srwatson error = extattrctl(argv[2], UFS_EXTATTR_CMD_START, 0, 0); 15665377Srwatson if (error) 15765377Srwatson perror("extattrctl start"); 15859247Srwatson } else if (!strcmp(argv[1], "stop")) { 15959401Srwatson if (argc != 3) 16059247Srwatson usage(); 16159247Srwatson error = extattrctl(argv[2], UFS_EXTATTR_CMD_STOP, 0, 0); 16265377Srwatson if (error) 16365377Srwatson perror("extattrctl stop"); 16459247Srwatson } else if (!strcmp(argv[1], "enable")) { 16559401Srwatson if (argc != 5) 16659247Srwatson usage(); 16759247Srwatson error = extattrctl(argv[2], UFS_EXTATTR_CMD_ENABLE, argv[3], 16859247Srwatson argv[4]); 16965377Srwatson if (error) 17065377Srwatson perror("extattrctl enable"); 17159247Srwatson } else if (!strcmp(argv[1], "disable")) { 17259401Srwatson if (argc != 4) 17359247Srwatson usage(); 17459247Srwatson error = extattrctl(argv[2], UFS_EXTATTR_CMD_DISABLE, argv[3], 17559247Srwatson NULL); 17665377Srwatson if (error) 17765377Srwatson perror("extattrctl disable"); 17859247Srwatson } else if (!strcmp(argv[1], "initattr")) { 17959401Srwatson argc -= 2; 18059401Srwatson argv += 2; 18159401Srwatson error = initattr(argc, argv); 18259401Srwatson } else 18359247Srwatson usage(); 18459247Srwatson return(error); 18559247Srwatson} 186