Home | History | Annotate | Line # | Download | only in mtree
create.c revision 1.13
      1 /*	$NetBSD: create.c,v 1.13 1997/10/17 11:46:35 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.13 1997/10/17 11:46:35 lukem 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];
     84 
     85 	(void)time(&clock);
     86 	(void)gethostname(host, sizeof(host));
     87 	(void)printf(
     88 	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
     89 	    getlogin(), host, fullpath, ctime(&clock));
     90 
     91 	argv[0] = ".";
     92 	argv[1] = NULL;
     93 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
     94 		err("fts_open: %s", strerror(errno));
     95 	while ((p = fts_read(t)) != NULL)
     96 		switch(p->fts_info) {
     97 		case FTS_D:
     98 			(void)printf("\n# %s\n", p->fts_path);
     99 			statd(t, p, &uid, &gid, &mode);
    100 			statf(p);
    101 			break;
    102 		case FTS_DP:
    103 			if (p->fts_level > 0)
    104 				(void)printf("# %s\n..\n\n", p->fts_path);
    105 			break;
    106 		case FTS_DNR:
    107 		case FTS_ERR:
    108 		case FTS_NS:
    109 			(void)fprintf(stderr,
    110 			    "mtree: %s: %s\n", p->fts_path, strerror(errno));
    111 			break;
    112 		default:
    113 			if (!dflag)
    114 				statf(p);
    115 			break;
    116 
    117 		}
    118 	(void)fts_close(t);
    119 	if (sflag && keys & F_CKSUM)
    120 		(void)fprintf(stderr,
    121 		    "mtree: %s checksum: %u\n", fullpath, crc_total);
    122 }
    123 
    124 static void
    125 statf(p)
    126 	FTSENT *p;
    127 {
    128 	struct group *gr;
    129 	struct passwd *pw;
    130 	u_long len, val;
    131 	int fd, indent;
    132 
    133 	if (S_ISDIR(p->fts_statp->st_mode))
    134 		indent = printf("%s", p->fts_name);
    135 	else
    136 		indent = printf("    %s", p->fts_name);
    137 
    138 	if (indent > INDENTNAMELEN)
    139 		indent = MAXLINELEN;
    140 	else
    141 		indent += printf("%*s", INDENTNAMELEN - indent, "");
    142 
    143 	if (!S_ISREG(p->fts_statp->st_mode))
    144 		output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
    145 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid)
    146 		if (keys & F_UNAME && (pw = getpwuid(p->fts_statp->st_uid)))
    147 			output(&indent, "uname=%s", pw->pw_name);
    148 		else /* if (keys & F_UID) */
    149 			output(&indent, "uid=%u", p->fts_statp->st_uid);
    150 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid)
    151 		if (keys & F_GNAME && (gr = getgrgid(p->fts_statp->st_gid)))
    152 			output(&indent, "gname=%s", gr->gr_name);
    153 		else /* if (keys & F_GID) */
    154 			output(&indent, "gid=%u", p->fts_statp->st_gid);
    155 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
    156 		output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
    157 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
    158 		output(&indent, "nlink=%u", p->fts_statp->st_nlink);
    159 	if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
    160 		output(&indent, "size=%qd", p->fts_statp->st_size);
    161 	if (keys & F_TIME)
    162 		output(&indent, "time=%ld.%ld",
    163 		    p->fts_statp->st_mtimespec.tv_sec,
    164 		    p->fts_statp->st_mtimespec.tv_nsec);
    165 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
    166 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
    167 		    crc(fd, &val, &len))
    168 			err("%s: %s", p->fts_accpath, strerror(errno));
    169 		(void)close(fd);
    170 		output(&indent, "cksum=%lu", val);
    171 	}
    172 	if (keys & F_SLINK &&
    173 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
    174 		output(&indent, "link=%s", rlink(p->fts_accpath));
    175 	(void)putchar('\n');
    176 }
    177 
    178 #define	MAXGID	5000
    179 #define	MAXUID	5000
    180 #define	MAXMODE	MBITS + 1
    181 
    182 static int
    183 statd(t, parent, puid, pgid, pmode)
    184 	FTS *t;
    185 	FTSENT *parent;
    186 	uid_t *puid;
    187 	gid_t *pgid;
    188 	mode_t *pmode;
    189 {
    190 	FTSENT *p;
    191 	gid_t sgid;
    192 	uid_t suid;
    193 	mode_t smode;
    194 	struct group *gr;
    195 	struct passwd *pw;
    196 	gid_t savegid;
    197 	uid_t saveuid;
    198 	mode_t savemode;
    199 	u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE];
    200 
    201 	savegid = 0;
    202 	saveuid = 0;
    203 	savemode = 0;
    204 	if ((p = fts_children(t, 0)) == NULL) {
    205 		if (errno)
    206 			err("%s: %s", RP(parent), strerror(errno));
    207 		return (1);
    208 	}
    209 
    210 	memset(g, 0, sizeof(g));
    211 	memset(u, 0, sizeof(u));
    212 	memset(m, 0, sizeof(m));
    213 
    214 	maxuid = maxgid = maxmode = 0;
    215 	for (; p; p = p->fts_link) {
    216 		smode = p->fts_statp->st_mode & MBITS;
    217 		if (smode < MAXMODE && ++m[smode] > maxmode) {
    218 			savemode = smode;
    219 			maxmode = m[smode];
    220 		}
    221 		sgid = p->fts_statp->st_gid;
    222 		if (sgid < MAXGID && ++g[sgid] > maxgid) {
    223 			savegid = sgid;
    224 			maxgid = g[sgid];
    225 		}
    226 		suid = p->fts_statp->st_uid;
    227 		if (suid < MAXUID && ++u[suid] > maxuid) {
    228 			saveuid = suid;
    229 			maxuid = u[suid];
    230 		}
    231 	}
    232 	(void)printf("/set type=file");
    233 	if (keys & F_GID)
    234 		(void)printf(" gid=%u", savegid);
    235 	if (keys & F_GNAME)
    236 		if ((gr = getgrgid(savegid)) != NULL)
    237 			(void)printf(" gname=%s", gr->gr_name);
    238 		else
    239 			(void)printf(" gid=%u", savegid);
    240 	if (keys & F_UNAME)
    241 		if ((pw = getpwuid(saveuid)) != NULL)
    242 			(void)printf(" uname=%s", pw->pw_name);
    243 		else
    244 			(void)printf(" uid=%u", saveuid);
    245 	if (keys & F_UID)
    246 		(void)printf(" uid=%u", saveuid);
    247 	if (keys & F_MODE)
    248 		(void)printf(" mode=%#o", savemode);
    249 	if (keys & F_NLINK)
    250 		(void)printf(" nlink=1");
    251 	(void)printf("\n");
    252 	*puid = saveuid;
    253 	*pgid = savegid;
    254 	*pmode = savemode;
    255 	return (0);
    256 }
    257 
    258 static int
    259 dsort(a, b)
    260 	const FTSENT **a, **b;
    261 {
    262 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
    263 		if (!S_ISDIR((*b)->fts_statp->st_mode))
    264 			return (1);
    265 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
    266 		return (-1);
    267 	return (strcmp((*a)->fts_name, (*b)->fts_name));
    268 }
    269 
    270 #if __STDC__
    271 #include <stdarg.h>
    272 #else
    273 #include <varargs.h>
    274 #endif
    275 
    276 void
    277 #if __STDC__
    278 output(int *offset, const char *fmt, ...)
    279 #else
    280 output(offset, fmt, va_alist)
    281 	int *offset;
    282 	char *fmt;
    283         va_dcl
    284 #endif
    285 {
    286 	va_list ap;
    287 	char buf[1024];
    288 #if __STDC__
    289 	va_start(ap, fmt);
    290 #else
    291 	va_start(ap);
    292 #endif
    293 	(void)vsnprintf(buf, sizeof(buf), fmt, ap);
    294 	va_end(ap);
    295 
    296 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
    297 		(void)printf(" \\\n%*s", INDENTNAMELEN, "");
    298 		*offset = INDENTNAMELEN;
    299 	}
    300 	*offset += printf(" %s", buf) + 1;
    301 }
    302