Home | History | Annotate | Line # | Download | only in mtree
create.c revision 1.17
      1 /*	$NetBSD: create.c,v 1.17 1998/10/08 02:04:56 wsanchez Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1989, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     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.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)create.c	8.1 (Berkeley) 6/6/93";
     40 #else
     41 __RCSID("$NetBSD: create.c,v 1.17 1998/10/08 02:04:56 wsanchez Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/stat.h>
     47 #include <dirent.h>
     48 #include <errno.h>
     49 #include <fcntl.h>
     50 #include <fts.h>
     51 #include <grp.h>
     52 #include <pwd.h>
     53 #include <stdio.h>
     54 #include <string.h>
     55 #include <time.h>
     56 #include <unistd.h>
     57 #include "mtree.h"
     58 #include "extern.h"
     59 
     60 #define	INDENTNAMELEN	15
     61 #define	MAXLINELEN	80
     62 
     63 extern int crc_total, ftsoptions;
     64 extern int dflag, sflag;
     65 extern u_short keys;
     66 extern char fullpath[MAXPATHLEN];
     67 
     68 static gid_t gid;
     69 static uid_t uid;
     70 static mode_t mode;
     71 
     72 static int	dsort __P((const FTSENT **, const FTSENT **));
     73 static void	output __P((int *, const char *, ...));
     74 static int	statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *));
     75 static void	statf __P((FTSENT *));
     76 
     77 void
     78 cwalk()
     79 {
     80 	FTS *t;
     81 	FTSENT *p;
     82 	time_t clock;
     83 	char *argv[2], host[MAXHOSTNAMELEN + 1];
     84 
     85 	(void)time(&clock);
     86 	(void)gethostname(host, sizeof(host));
     87 	host[sizeof(host) - 1] = '\0';
     88 	(void)printf(
     89 	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
     90 	    getlogin(), host, fullpath, ctime(&clock));
     91 
     92 	argv[0] = ".";
     93 	argv[1] = NULL;
     94 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
     95 		mtree_err("fts_open: %s", strerror(errno));
     96 	while ((p = fts_read(t)) != NULL)
     97 		switch(p->fts_info) {
     98 		case FTS_D:
     99 			(void)printf("\n# %s\n", p->fts_path);
    100 			statd(t, p, &uid, &gid, &mode);
    101 			statf(p);
    102 			break;
    103 		case FTS_DP:
    104 			if (p->fts_level > 0)
    105 				(void)printf("# %s\n..\n\n", p->fts_path);
    106 			break;
    107 		case FTS_DNR:
    108 		case FTS_ERR:
    109 		case FTS_NS:
    110 			(void)fprintf(stderr,
    111 			    "mtree: %s: %s\n", p->fts_path, strerror(errno));
    112 			break;
    113 		default:
    114 			if (!dflag)
    115 				statf(p);
    116 			break;
    117 
    118 		}
    119 	(void)fts_close(t);
    120 	if (sflag && keys & F_CKSUM)
    121 		(void)fprintf(stderr,
    122 		    "mtree: %s checksum: %u\n", fullpath, crc_total);
    123 }
    124 
    125 static void
    126 statf(p)
    127 	FTSENT *p;
    128 {
    129 	struct group *gr;
    130 	struct passwd *pw;
    131 	u_int32_t len, val;
    132 	int fd, indent;
    133 
    134 	if (S_ISDIR(p->fts_statp->st_mode))
    135 		indent = printf("%s", p->fts_name);
    136 	else
    137 		indent = printf("    %s", p->fts_name);
    138 
    139 	if (indent > INDENTNAMELEN)
    140 		indent = MAXLINELEN;
    141 	else
    142 		indent += printf("%*s", INDENTNAMELEN - indent, "");
    143 
    144 	if (!S_ISREG(p->fts_statp->st_mode))
    145 		output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
    146 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
    147 		if (keys & F_UNAME && (pw = getpwuid(p->fts_statp->st_uid)))
    148 			output(&indent, "uname=%s", pw->pw_name);
    149 		else /* if (keys & F_UID) */
    150 			output(&indent, "uid=%u", p->fts_statp->st_uid);
    151 	}
    152 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
    153 		if (keys & F_GNAME && (gr = getgrgid(p->fts_statp->st_gid)))
    154 			output(&indent, "gname=%s", gr->gr_name);
    155 		else /* if (keys & F_GID) */
    156 			output(&indent, "gid=%u", p->fts_statp->st_gid);
    157 	}
    158 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
    159 		output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
    160 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
    161 		output(&indent, "nlink=%u", p->fts_statp->st_nlink);
    162 	if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
    163 		output(&indent, "size=%qd", p->fts_statp->st_size);
    164 #ifndef __APPLE__
    165 	if (keys & F_TIME)
    166 		output(&indent, "time=%ld.%ld",
    167 		    p->fts_statp->st_mtimespec.tv_sec,
    168 		    p->fts_statp->st_mtimespec.tv_nsec);
    169 #else
    170 	if (keys & F_TIME)
    171 		output(&indent, "time=%ld.%ld",
    172 		    p->fts_statp->st_mtimespec.ts_sec,
    173 		    p->fts_statp->st_mtimespec.ts_nsec);
    174 #endif
    175 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
    176 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
    177 		    crc(fd, &val, &len))
    178 			mtree_err("%s: %s", p->fts_accpath, strerror(errno));
    179 		(void)close(fd);
    180 		output(&indent, "cksum=%lu", val);
    181 	}
    182 	if (keys & F_SLINK &&
    183 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
    184 		output(&indent, "link=%s", rlink(p->fts_accpath));
    185 	(void)putchar('\n');
    186 }
    187 
    188 #define	MAXGID	5000
    189 #define	MAXUID	5000
    190 #define	MAXMODE	MBITS + 1
    191 
    192 static int
    193 statd(t, parent, puid, pgid, pmode)
    194 	FTS *t;
    195 	FTSENT *parent;
    196 	uid_t *puid;
    197 	gid_t *pgid;
    198 	mode_t *pmode;
    199 {
    200 	FTSENT *p;
    201 	gid_t sgid;
    202 	uid_t suid;
    203 	mode_t smode;
    204 	struct group *gr;
    205 	struct passwd *pw;
    206 	gid_t savegid;
    207 	uid_t saveuid;
    208 	mode_t savemode;
    209 	u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE];
    210 
    211 	savegid = 0;
    212 	saveuid = 0;
    213 	savemode = 0;
    214 	if ((p = fts_children(t, 0)) == NULL) {
    215 		if (errno)
    216 			mtree_err("%s: %s", RP(parent), strerror(errno));
    217 		return (1);
    218 	}
    219 
    220 	memset(g, 0, sizeof(g));
    221 	memset(u, 0, sizeof(u));
    222 	memset(m, 0, sizeof(m));
    223 
    224 	maxuid = maxgid = maxmode = 0;
    225 	for (; p; p = p->fts_link) {
    226 		smode = p->fts_statp->st_mode & MBITS;
    227 		if (smode < MAXMODE && ++m[smode] > maxmode) {
    228 			savemode = smode;
    229 			maxmode = m[smode];
    230 		}
    231 		sgid = p->fts_statp->st_gid;
    232 		if (sgid < MAXGID && ++g[sgid] > maxgid) {
    233 			savegid = sgid;
    234 			maxgid = g[sgid];
    235 		}
    236 		suid = p->fts_statp->st_uid;
    237 		if (suid < MAXUID && ++u[suid] > maxuid) {
    238 			saveuid = suid;
    239 			maxuid = u[suid];
    240 		}
    241 	}
    242 	(void)printf("/set type=file");
    243 	if (keys & F_GID)
    244 		(void)printf(" gid=%u", savegid);
    245 	if (keys & F_GNAME) {
    246 		if ((gr = getgrgid(savegid)) != NULL)
    247 			(void)printf(" gname=%s", gr->gr_name);
    248 		else
    249 			(void)printf(" gid=%u", savegid);
    250 	}
    251 	if (keys & F_UNAME) {
    252 		if ((pw = getpwuid(saveuid)) != NULL)
    253 			(void)printf(" uname=%s", pw->pw_name);
    254 		else
    255 			(void)printf(" uid=%u", saveuid);
    256 	}
    257 	if (keys & F_UID)
    258 		(void)printf(" uid=%u", saveuid);
    259 	if (keys & F_MODE)
    260 		(void)printf(" mode=%#o", savemode);
    261 	if (keys & F_NLINK)
    262 		(void)printf(" nlink=1");
    263 	(void)printf("\n");
    264 	*puid = saveuid;
    265 	*pgid = savegid;
    266 	*pmode = savemode;
    267 	return (0);
    268 }
    269 
    270 static int
    271 dsort(a, b)
    272 	const FTSENT **a, **b;
    273 {
    274 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
    275 		if (!S_ISDIR((*b)->fts_statp->st_mode))
    276 			return (1);
    277 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
    278 		return (-1);
    279 	return (strcmp((*a)->fts_name, (*b)->fts_name));
    280 }
    281 
    282 #if __STDC__
    283 #include <stdarg.h>
    284 #else
    285 #include <varargs.h>
    286 #endif
    287 
    288 void
    289 #if __STDC__
    290 output(int *offset, const char *fmt, ...)
    291 #else
    292 output(offset, fmt, va_alist)
    293 	int *offset;
    294 	char *fmt;
    295         va_dcl
    296 #endif
    297 {
    298 	va_list ap;
    299 	char buf[1024];
    300 #if __STDC__
    301 	va_start(ap, fmt);
    302 #else
    303 	va_start(ap);
    304 #endif
    305 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
    306 	va_end(ap);
    307 
    308 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
    309 		(void)printf(" \\\n%*s", INDENTNAMELEN, "");
    310 		*offset = INDENTNAMELEN;
    311 	}
    312 	*offset += printf(" %s", buf) + 1;
    313 }
    314