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