Home | History | Annotate | Line # | Download | only in mtree
create.c revision 1.20
      1 /*	$NetBSD: create.c,v 1.20 1998/12/19 15:38:45 christos 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.20 1998/12/19 15:38:45 christos 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 int keys;
     66 extern char fullpath[MAXPATHLEN];
     67 
     68 static gid_t gid;
     69 static uid_t uid;
     70 static mode_t mode;
     71 static u_long flags;
     72 
     73 static int	dsort __P((const FTSENT **, const FTSENT **));
     74 static void	output __P((int *, const char *, ...));
     75 static int	statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *,
     76 			   u_long *));
     77 static void	statf __P((FTSENT *));
     78 
     79 void
     80 cwalk()
     81 {
     82 	FTS *t;
     83 	FTSENT *p;
     84 	time_t clock;
     85 	char *argv[2], host[MAXHOSTNAMELEN + 1];
     86 
     87 	(void)time(&clock);
     88 	(void)gethostname(host, sizeof(host));
     89 	host[sizeof(host) - 1] = '\0';
     90 	(void)printf(
     91 	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
     92 	    getlogin(), host, fullpath, ctime(&clock));
     93 
     94 	argv[0] = ".";
     95 	argv[1] = NULL;
     96 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
     97 		mtree_err("fts_open: %s", strerror(errno));
     98 	while ((p = fts_read(t)) != NULL)
     99 		switch(p->fts_info) {
    100 		case FTS_D:
    101 			(void)printf("\n# %s\n", p->fts_path);
    102 			statd(t, p, &uid, &gid, &mode, &flags);
    103 			statf(p);
    104 			break;
    105 		case FTS_DP:
    106 			if (p->fts_level > 0)
    107 				(void)printf("# %s\n..\n\n", p->fts_path);
    108 			break;
    109 		case FTS_DNR:
    110 		case FTS_ERR:
    111 		case FTS_NS:
    112 			(void)fprintf(stderr,
    113 			    "mtree: %s: %s\n", p->fts_path, strerror(errno));
    114 			break;
    115 		default:
    116 			if (!dflag)
    117 				statf(p);
    118 			break;
    119 
    120 		}
    121 	(void)fts_close(t);
    122 	if (sflag && keys & F_CKSUM)
    123 		(void)fprintf(stderr,
    124 		    "mtree: %s checksum: %u\n", fullpath, crc_total);
    125 }
    126 
    127 static void
    128 statf(p)
    129 	FTSENT *p;
    130 {
    131 	struct group *gr;
    132 	struct passwd *pw;
    133 	u_int32_t len, val;
    134 	int fd, indent;
    135 
    136 	if (S_ISDIR(p->fts_statp->st_mode))
    137 		indent = printf("%s", p->fts_name);
    138 	else
    139 		indent = printf("    %s", p->fts_name);
    140 
    141 	if (indent > INDENTNAMELEN)
    142 		indent = MAXLINELEN;
    143 	else
    144 		indent += printf("%*s", INDENTNAMELEN - indent, "");
    145 
    146 	if (!S_ISREG(p->fts_statp->st_mode))
    147 		output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
    148 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
    149 		if (keys & F_UNAME && (pw = getpwuid(p->fts_statp->st_uid)))
    150 			output(&indent, "uname=%s", pw->pw_name);
    151 		else /* if (keys & F_UID) */
    152 			output(&indent, "uid=%u", p->fts_statp->st_uid);
    153 	}
    154 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
    155 		if (keys & F_GNAME && (gr = getgrgid(p->fts_statp->st_gid)))
    156 			output(&indent, "gname=%s", gr->gr_name);
    157 		else /* if (keys & F_GID) */
    158 			output(&indent, "gid=%u", p->fts_statp->st_gid);
    159 	}
    160 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
    161 		output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
    162 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
    163 		output(&indent, "nlink=%u", p->fts_statp->st_nlink);
    164 	if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
    165 		output(&indent, "size=%qd", p->fts_statp->st_size);
    166 #ifndef __APPLE__
    167 	if (keys & F_TIME)
    168 		output(&indent, "time=%ld.%ld",
    169 		    p->fts_statp->st_mtimespec.tv_sec,
    170 		    p->fts_statp->st_mtimespec.tv_nsec);
    171 #else
    172 	if (keys & F_TIME)
    173 		output(&indent, "time=%ld.%ld",
    174 		    p->fts_statp->st_mtimespec.ts_sec,
    175 		    p->fts_statp->st_mtimespec.ts_nsec);
    176 #endif
    177 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
    178 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
    179 		    crc(fd, &val, &len))
    180 			mtree_err("%s: %s", p->fts_accpath, strerror(errno));
    181 		(void)close(fd);
    182 		output(&indent, "cksum=%lu", val);
    183 	}
    184 	if (keys & F_SLINK &&
    185 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
    186 		output(&indent, "link=%s", rlink(p->fts_accpath));
    187 	if (keys & F_FLAGS && p->fts_statp->st_flags != flags)
    188 		output(&indent, "flags=%s",
    189 		    flags_to_string(p->fts_statp->st_flags, "none"));
    190 	(void)putchar('\n');
    191 }
    192 
    193 #define	MTREE_MAXGID	5000
    194 #define	MTREE_MAXUID	5000
    195 #define	MTREE_MAXMODE	MBITS + 1
    196 #define	MTREE_MAXFLAGS 256
    197 #define	MTREE_MAXS 16
    198 
    199 static int
    200 statd(t, parent, puid, pgid, pmode, pflags)
    201 	FTS *t;
    202 	FTSENT *parent;
    203 	uid_t *puid;
    204 	gid_t *pgid;
    205 	mode_t *pmode;
    206 	u_long *pflags;
    207 {
    208 	FTSENT *p;
    209 	gid_t sgid;
    210 	uid_t suid;
    211 	mode_t smode;
    212 	u_long sflags;
    213 	struct group *gr;
    214 	struct passwd *pw;
    215 	gid_t savegid;
    216 	uid_t saveuid;
    217 	mode_t savemode;
    218 	u_long saveflags;
    219 	u_short maxgid, maxuid, maxmode, maxflags;
    220 	u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
    221 		m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
    222 
    223 	savegid = 0;
    224 	saveuid = 0;
    225 	savemode = 0;
    226 	saveflags = 0;
    227 	if ((p = fts_children(t, 0)) == NULL) {
    228 		if (errno)
    229 			mtree_err("%s: %s", RP(parent), strerror(errno));
    230 		return (1);
    231 	}
    232 
    233 	memset(g, 0, sizeof(g));
    234 	memset(u, 0, sizeof(u));
    235 	memset(m, 0, sizeof(m));
    236 	memset(f, 0, sizeof(f));
    237 
    238 	maxuid = maxgid = maxmode = maxflags = 0;
    239 	for (; p; p = p->fts_link) {
    240 		smode = p->fts_statp->st_mode & MBITS;
    241 		if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
    242 			savemode = smode;
    243 			maxmode = m[smode];
    244 		}
    245 		sgid = p->fts_statp->st_gid;
    246 		if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
    247 			savegid = sgid;
    248 			maxgid = g[sgid];
    249 		}
    250 		suid = p->fts_statp->st_uid;
    251 		if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
    252 			saveuid = suid;
    253 			maxuid = u[suid];
    254 		}
    255 /*
    256  * XXX
    257  * note that the below will break when file flags are extended
    258  * beyond the first 4 bytes of each half word of the flags
    259  */
    260 #define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0))
    261 
    262 		sflags = p->fts_statp->st_flags;
    263 		if (FLAGS2IDX(sflags) < MTREE_MAXFLAGS &&
    264 		    ++f[FLAGS2IDX(sflags)] > maxflags) {
    265 			saveflags = sflags;
    266 			maxflags = u[FLAGS2IDX(sflags)];
    267 		}
    268 	}
    269 	(void)printf("/set type=file");
    270 	if (keys & F_GID)
    271 		(void)printf(" gid=%lu", (u_long)savegid);
    272 	if (keys & F_GNAME) {
    273 		if ((gr = getgrgid(savegid)) != NULL)
    274 			(void)printf(" gname=%s", gr->gr_name);
    275 		else
    276 			(void)printf(" gid=%lu", (u_long)savegid);
    277 	}
    278 	if (keys & F_UNAME) {
    279 		if ((pw = getpwuid(saveuid)) != NULL)
    280 			(void)printf(" uname=%s", pw->pw_name);
    281 		else
    282 			(void)printf(" uid=%lu", (u_long)saveuid);
    283 	}
    284 	if (keys & F_UID)
    285 		(void)printf(" uid=%lu", (u_long)saveuid);
    286 	if (keys & F_MODE)
    287 		(void)printf(" mode=%#lo", (u_long)savemode);
    288 	if (keys & F_NLINK)
    289 		(void)printf(" nlink=1");
    290 	if (keys & F_FLAGS && saveflags)
    291 		(void)printf(" flags=%s",
    292 		    flags_to_string(saveflags, "none"));
    293 	(void)printf("\n");
    294 	*puid = saveuid;
    295 	*pgid = savegid;
    296 	*pmode = savemode;
    297 	*pflags = saveflags;
    298 	return (0);
    299 }
    300 
    301 static int
    302 dsort(a, b)
    303 	const FTSENT **a, **b;
    304 {
    305 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
    306 		if (!S_ISDIR((*b)->fts_statp->st_mode))
    307 			return (1);
    308 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
    309 		return (-1);
    310 	return (strcmp((*a)->fts_name, (*b)->fts_name));
    311 }
    312 
    313 #if __STDC__
    314 #include <stdarg.h>
    315 #else
    316 #include <varargs.h>
    317 #endif
    318 
    319 void
    320 #if __STDC__
    321 output(int *offset, const char *fmt, ...)
    322 #else
    323 output(offset, fmt, va_alist)
    324 	int *offset;
    325 	char *fmt;
    326         va_dcl
    327 #endif
    328 {
    329 	va_list ap;
    330 	char buf[1024];
    331 #if __STDC__
    332 	va_start(ap, fmt);
    333 #else
    334 	va_start(ap);
    335 #endif
    336 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
    337 	va_end(ap);
    338 
    339 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
    340 		(void)printf(" \\\n%*s", INDENTNAMELEN, "");
    341 		*offset = INDENTNAMELEN;
    342 	}
    343 	*offset += printf(" %s", buf) + 1;
    344 }
    345