Home | History | Annotate | Line # | Download | only in mtree
create.c revision 1.35
      1 /*	$NetBSD: create.c,v 1.35 2001/10/22 07:07:46 lukem 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.35 2001/10/22 07:07:46 lukem Exp $");
     42 #endif
     43 #endif /* not lint */
     44 
     45 #include <sys/param.h>
     46 #include <sys/stat.h>
     47 
     48 #include <dirent.h>
     49 #include <errno.h>
     50 #include <fcntl.h>
     51 #include <fts.h>
     52 #include <grp.h>
     53 #include <md5.h>
     54 #include <pwd.h>
     55 #include <stdarg.h>
     56 #include <stdio.h>
     57 #include <stdlib.h>
     58 #include <string.h>
     59 #include <time.h>
     60 #include <unistd.h>
     61 #include <vis.h>
     62 
     63 #include "mtree.h"
     64 #include "extern.h"
     65 
     66 #define	INDENTNAMELEN	15
     67 #define	MAXLINELEN	80
     68 #define VISFLAGS        VIS_CSTYLE
     69 
     70 static gid_t gid;
     71 static uid_t uid;
     72 static mode_t mode;
     73 static u_long flags;
     74 static char codebuf[4*MAXPATHLEN + 1];
     75 static const char extra[] = { ' ', '\t', '\n', '\\', '#', '\0' };
     76 
     77 static int	dsort(const FTSENT **, const FTSENT **);
     78 static void	output(int *, const char *, ...)
     79 	__attribute__((__format__(__printf__, 2, 3)));
     80 static int	statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
     81 static void	statf(FTSENT *);
     82 
     83 void
     84 cwalk(void)
     85 {
     86 	FTS *t;
     87 	FTSENT *p;
     88 	time_t clocktime;
     89 	char host[MAXHOSTNAMELEN + 1];
     90 	char  dot[] = ".";		/* XXX: work around gcc warning */
     91 	char *argv[] = { dot, NULL };
     92 
     93 	(void)time(&clocktime);
     94 	(void)gethostname(host, sizeof(host));
     95 	host[sizeof(host) - 1] = '\0';
     96 	(void)printf(
     97 	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
     98 	    getlogin(), host, fullpath, ctime(&clocktime));
     99 
    100 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
    101 		mtree_err("fts_open: %s", strerror(errno));
    102 	while ((p = fts_read(t)) != NULL)
    103 		switch(p->fts_info) {
    104 		case FTS_D:
    105 			(void)printf("\n# %s\n", p->fts_path);
    106 			statd(t, p, &uid, &gid, &mode, &flags);
    107 			statf(p);
    108 			break;
    109 		case FTS_DP:
    110 			if (p->fts_level > 0)
    111 				(void)printf("# %s\n..\n\n", p->fts_path);
    112 			break;
    113 		case FTS_DNR:
    114 		case FTS_ERR:
    115 		case FTS_NS:
    116 			(void)fprintf(stderr,
    117 			    "mtree: %s: %s\n", p->fts_path, strerror(errno));
    118 			break;
    119 		default:
    120 			if (!dflag)
    121 				statf(p);
    122 			break;
    123 
    124 		}
    125 	(void)fts_close(t);
    126 	if (sflag && keys & F_CKSUM)
    127 		(void)fprintf(stderr,
    128 		    "mtree: %s checksum: %u\n", fullpath, crc_total);
    129 }
    130 
    131 static void
    132 statf(FTSENT *p)
    133 {
    134 	u_int32_t len, val;
    135 	int fd, indent;
    136 	char md5buf[33], *md5cp;
    137 	const char *name;
    138 
    139 	strsvis(codebuf, p->fts_name, VISFLAGS, extra);
    140 	if (S_ISDIR(p->fts_statp->st_mode))
    141 		indent = printf("%s", codebuf);
    142 	else
    143 		indent = printf("    %s", codebuf);
    144 
    145 	if (indent > INDENTNAMELEN)
    146 		indent = MAXLINELEN;
    147 	else
    148 		indent += printf("%*s", INDENTNAMELEN - indent, "");
    149 
    150 	if (!S_ISREG(p->fts_statp->st_mode))
    151 		output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
    152 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
    153 		if (keys & F_UNAME &&
    154 		    (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
    155 			output(&indent, "uname=%s", name);
    156 		else /* if (keys & F_UID) */
    157 			output(&indent, "uid=%u", p->fts_statp->st_uid);
    158 	}
    159 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
    160 		if (keys & F_GNAME &&
    161 		    (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
    162 			output(&indent, "gname=%s", name);
    163 		else /* if (keys & F_GID) */
    164 			output(&indent, "gid=%u", p->fts_statp->st_gid);
    165 	}
    166 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
    167 		output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
    168 	if (keys & F_DEV &&
    169 	    (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
    170 		output(&indent, "device=%#x", p->fts_statp->st_rdev);
    171 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
    172 		output(&indent, "nlink=%u", p->fts_statp->st_nlink);
    173 	if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
    174 		output(&indent, "size=%lld", (long long)p->fts_statp->st_size);
    175 #ifdef BSD4_4
    176 	if (keys & F_TIME)
    177 		output(&indent, "time=%ld.%ld",
    178 		    (long)p->fts_statp->st_mtimespec.tv_sec,
    179 		    p->fts_statp->st_mtimespec.tv_nsec);
    180 #else
    181 		output(&indent, "time=%ld.%ld",
    182 		    p->fts_statp->st_mtime, 0);
    183 #endif
    184 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
    185 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
    186 		    crc(fd, &val, &len))
    187 			mtree_err("%s: %s", p->fts_accpath, strerror(errno));
    188 		(void)close(fd);
    189 		output(&indent, "cksum=%lu", (long)val);
    190 	}
    191 	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
    192 		if ((md5cp = MD5File(p->fts_accpath, md5buf)) == NULL)
    193 			mtree_err("%s: %s", p->fts_accpath, "MD5File");
    194 				  output(&indent, "md5=%s", md5cp);
    195 	}
    196 	if (keys & F_SLINK &&
    197 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
    198 		output(&indent, "link=%s", rlink(p->fts_accpath));
    199 	if (keys & F_FLAGS && p->fts_statp->st_flags != flags)
    200 		output(&indent, "flags=%s",
    201 		    flags_to_string(p->fts_statp->st_flags, "none"));
    202 	(void)putchar('\n');
    203 }
    204 
    205 /* XXX
    206  * FLAGS2INDEX will fail once the user and system settable bits need more
    207  * than one byte, respectively.
    208  */
    209 #define FLAGS2INDEX(x)  (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
    210 
    211 #define	MTREE_MAXGID	5000
    212 #define	MTREE_MAXUID	5000
    213 #define	MTREE_MAXMODE	(MBITS + 1)
    214 #define	MTREE_MAXFLAGS  (FLAGS2INDEX(CH_MASK) + 1)   /* 1808 */
    215 #define	MTREE_MAXS 16
    216 
    217 static int
    218 statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
    219       u_long *pflags)
    220 {
    221 	FTSENT *p;
    222 	gid_t sgid;
    223 	uid_t suid;
    224 	mode_t smode;
    225 	u_long sflags;
    226 	const char *name;
    227 	gid_t savegid;
    228 	uid_t saveuid;
    229 	mode_t savemode;
    230 	u_long saveflags;
    231 	u_short maxgid, maxuid, maxmode, maxflags;
    232 	u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
    233 		m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
    234 
    235 	savegid = 0;
    236 	saveuid = 0;
    237 	savemode = 0;
    238 	saveflags = 0;
    239 	if ((p = fts_children(t, 0)) == NULL) {
    240 		if (errno)
    241 			mtree_err("%s: %s", RP(parent), strerror(errno));
    242 		return (1);
    243 	}
    244 
    245 	memset(g, 0, sizeof(g));
    246 	memset(u, 0, sizeof(u));
    247 	memset(m, 0, sizeof(m));
    248 	memset(f, 0, sizeof(f));
    249 
    250 	maxuid = maxgid = maxmode = maxflags = 0;
    251 	for (; p; p = p->fts_link) {
    252 		smode = p->fts_statp->st_mode & MBITS;
    253 		if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
    254 			savemode = smode;
    255 			maxmode = m[smode];
    256 		}
    257 		sgid = p->fts_statp->st_gid;
    258 		if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
    259 			savegid = sgid;
    260 			maxgid = g[sgid];
    261 		}
    262 		suid = p->fts_statp->st_uid;
    263 		if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
    264 			saveuid = suid;
    265 			maxuid = u[suid];
    266 		}
    267 
    268 		sflags = FLAGS2INDEX(p->fts_statp->st_flags);
    269 		if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
    270 			saveflags = p->fts_statp->st_flags;
    271 			maxflags = f[sflags];
    272 		}
    273 	}
    274 	(void)printf("/set type=file");
    275 	if (keys & F_GID)
    276 		(void)printf(" gid=%lu", (u_long)savegid);
    277 	if (keys & F_GNAME) {
    278 		if ((name = group_from_gid(savegid, 1)) != NULL)
    279 			(void)printf(" gname=%s", name);
    280 		else
    281 			(void)printf(" gid=%lu", (u_long)savegid);
    282 	}
    283 	if (keys & F_UNAME) {
    284 		if ((name = user_from_uid(saveuid, 1)) != NULL)
    285 			(void)printf(" uname=%s", name);
    286 		else
    287 			(void)printf(" uid=%lu", (u_long)saveuid);
    288 	}
    289 	if (keys & F_UID)
    290 		(void)printf(" uid=%lu", (u_long)saveuid);
    291 	if (keys & F_MODE)
    292 		(void)printf(" mode=%#lo", (u_long)savemode);
    293 	if (keys & F_NLINK)
    294 		(void)printf(" nlink=1");
    295 	if (keys & F_FLAGS)
    296 		(void)printf(" flags=%s",
    297 		    flags_to_string(saveflags, "none"));
    298 	(void)printf("\n");
    299 	*puid = saveuid;
    300 	*pgid = savegid;
    301 	*pmode = savemode;
    302 	*pflags = saveflags;
    303 	return (0);
    304 }
    305 
    306 static int
    307 dsort(const FTSENT **a, const FTSENT **b)
    308 {
    309 
    310 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
    311 		if (!S_ISDIR((*b)->fts_statp->st_mode))
    312 			return (1);
    313 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
    314 		return (-1);
    315 	return (strcmp((*a)->fts_name, (*b)->fts_name));
    316 }
    317 
    318 void
    319 output(int *offset, const char *fmt, ...)
    320 {
    321 	va_list ap;
    322 	char buf[1024];
    323 
    324 	va_start(ap, fmt);
    325 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
    326 	va_end(ap);
    327 
    328 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
    329 		(void)printf(" \\\n%*s", INDENTNAMELEN, "");
    330 		*offset = INDENTNAMELEN;
    331 	}
    332 	*offset += printf(" %s", buf) + 1;
    333 }
    334