Home | History | Annotate | Line # | Download | only in mtree
create.c revision 1.42
      1  1.42     lukem /*	$NetBSD: create.c,v 1.42 2002/12/23 04:40:19 lukem Exp $	*/
      2   1.9       cgd 
      3   1.1       cgd /*-
      4   1.8       cgd  * Copyright (c) 1989, 1993
      5   1.8       cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1       cgd  *    must display the following acknowledgement:
     17   1.1       cgd  *	This product includes software developed by the University of
     18   1.1       cgd  *	California, Berkeley and its contributors.
     19   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1       cgd  *    may be used to endorse or promote products derived from this software
     21   1.1       cgd  *    without specific prior written permission.
     22   1.1       cgd  *
     23   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1       cgd  * SUCH DAMAGE.
     34   1.1       cgd  */
     35   1.1       cgd 
     36  1.13     lukem #include <sys/cdefs.h>
     37  1.39        tv #if defined(__RCSID) && !defined(lint)
     38   1.9       cgd #if 0
     39   1.8       cgd static char sccsid[] = "@(#)create.c	8.1 (Berkeley) 6/6/93";
     40   1.9       cgd #else
     41  1.42     lukem __RCSID("$NetBSD: create.c,v 1.42 2002/12/23 04:40:19 lukem Exp $");
     42   1.9       cgd #endif
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/param.h>
     46   1.1       cgd #include <sys/stat.h>
     47  1.29    simonb 
     48  1.38        tv #if !HAVE_CONFIG_H
     49  1.13     lukem #include <dirent.h>
     50  1.38        tv #endif
     51  1.38        tv 
     52  1.13     lukem #include <errno.h>
     53   1.5       cgd #include <fcntl.h>
     54   1.5       cgd #include <grp.h>
     55   1.5       cgd #include <pwd.h>
     56  1.37     lukem #include <stdio.h>
     57  1.29    simonb #include <stdarg.h>
     58  1.35     lukem #include <stdlib.h>
     59  1.13     lukem #include <string.h>
     60  1.13     lukem #include <time.h>
     61   1.5       cgd #include <unistd.h>
     62  1.29    simonb 
     63  1.37     lukem #ifndef NO_MD5
     64  1.37     lukem #include <md5.h>
     65  1.37     lukem #endif
     66  1.37     lukem #ifndef NO_RMD160
     67  1.37     lukem #include <rmd160.h>
     68  1.37     lukem #endif
     69  1.37     lukem #ifndef NO_SHA1
     70  1.37     lukem #include <sha1.h>
     71  1.37     lukem #endif
     72  1.37     lukem 
     73   1.5       cgd #include "extern.h"
     74   1.1       cgd 
     75   1.5       cgd #define	INDENTNAMELEN	15
     76   1.5       cgd #define	MAXLINELEN	80
     77   1.5       cgd 
     78   1.5       cgd static gid_t gid;
     79   1.5       cgd static uid_t uid;
     80   1.5       cgd static mode_t mode;
     81  1.18       mrg static u_long flags;
     82   1.5       cgd 
     83  1.29    simonb static int	dsort(const FTSENT **, const FTSENT **);
     84  1.29    simonb static void	output(int *, const char *, ...)
     85  1.26        is 	__attribute__((__format__(__printf__, 2, 3)));
     86  1.29    simonb static int	statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
     87  1.29    simonb static void	statf(FTSENT *);
     88   1.1       cgd 
     89   1.5       cgd void
     90  1.29    simonb cwalk(void)
     91   1.1       cgd {
     92  1.13     lukem 	FTS *t;
     93  1.13     lukem 	FTSENT *p;
     94  1.33     lukem 	time_t clocktime;
     95  1.34     lukem 	char host[MAXHOSTNAMELEN + 1];
     96  1.41     grant 	char *argv[2];
     97  1.41     grant 	char  dot[] = ".";
     98  1.41     grant 	argv[0] = dot;
     99  1.41     grant 	argv[1] = NULL;
    100   1.5       cgd 
    101  1.36     lukem 	time(&clocktime);
    102  1.36     lukem 	gethostname(host, sizeof(host));
    103  1.14       mrg 	host[sizeof(host) - 1] = '\0';
    104  1.36     lukem 	printf(
    105   1.5       cgd 	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n#\t   date: %s",
    106  1.33     lukem 	    getlogin(), host, fullpath, ctime(&clocktime));
    107   1.1       cgd 
    108   1.5       cgd 	if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
    109  1.17  wsanchez 		mtree_err("fts_open: %s", strerror(errno));
    110  1.36     lukem 	while ((p = fts_read(t)) != NULL) {
    111  1.36     lukem 		if (check_excludes(p->fts_name, p->fts_path)) {
    112  1.36     lukem 			fts_set(t, p, FTS_SKIP);
    113  1.36     lukem 			continue;
    114  1.36     lukem 		}
    115   1.1       cgd 		switch(p->fts_info) {
    116   1.1       cgd 		case FTS_D:
    117  1.36     lukem 			printf("\n# %s\n", p->fts_path);
    118  1.18       mrg 			statd(t, p, &uid, &gid, &mode, &flags);
    119   1.5       cgd 			statf(p);
    120   1.1       cgd 			break;
    121   1.1       cgd 		case FTS_DP:
    122   1.5       cgd 			if (p->fts_level > 0)
    123  1.36     lukem 				printf("# %s\n..\n\n", p->fts_path);
    124   1.5       cgd 			break;
    125   1.1       cgd 		case FTS_DNR:
    126   1.1       cgd 		case FTS_ERR:
    127   1.1       cgd 		case FTS_NS:
    128  1.36     lukem 			mtree_err("%s: %s",
    129  1.36     lukem 			    p->fts_path, strerror(p->fts_errno));
    130   1.5       cgd 			break;
    131   1.1       cgd 		default:
    132   1.5       cgd 			if (!dflag)
    133   1.5       cgd 				statf(p);
    134   1.5       cgd 			break;
    135  1.36     lukem 
    136   1.1       cgd 		}
    137  1.36     lukem 	}
    138  1.36     lukem 	fts_close(t);
    139   1.5       cgd 	if (sflag && keys & F_CKSUM)
    140  1.40     soren 		mtree_err("%s checksum: %u", fullpath, crc_total);
    141   1.5       cgd }
    142   1.1       cgd 
    143   1.5       cgd static void
    144  1.29    simonb statf(FTSENT *p)
    145   1.5       cgd {
    146  1.17  wsanchez 	u_int32_t len, val;
    147   1.5       cgd 	int fd, indent;
    148  1.42     lukem 	const char *name, *path;
    149  1.37     lukem #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1)
    150  1.36     lukem 	char digestbuf[41];	/* large enough for {MD5,RMD160,SHA1}File() */
    151  1.37     lukem #endif
    152   1.5       cgd 
    153  1.42     lukem 	path = vispath(p->fts_name);
    154  1.42     lukem 	indent = printf("%s%s",
    155  1.42     lukem 	    S_ISDIR(p->fts_statp->st_mode) ? "" : "    ", path);
    156   1.5       cgd 
    157   1.5       cgd 	if (indent > INDENTNAMELEN)
    158   1.5       cgd 		indent = MAXLINELEN;
    159   1.5       cgd 	else
    160   1.5       cgd 		indent += printf("%*s", INDENTNAMELEN - indent, "");
    161   1.5       cgd 
    162   1.5       cgd 	if (!S_ISREG(p->fts_statp->st_mode))
    163   1.5       cgd 		output(&indent, "type=%s", inotype(p->fts_statp->st_mode));
    164  1.15      ross 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
    165  1.30     lukem 		if (keys & F_UNAME &&
    166  1.30     lukem 		    (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
    167  1.30     lukem 			output(&indent, "uname=%s", name);
    168   1.5       cgd 		else /* if (keys & F_UID) */
    169   1.5       cgd 			output(&indent, "uid=%u", p->fts_statp->st_uid);
    170  1.15      ross 	}
    171  1.15      ross 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
    172  1.30     lukem 		if (keys & F_GNAME &&
    173  1.30     lukem 		    (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
    174  1.30     lukem 			output(&indent, "gname=%s", name);
    175   1.5       cgd 		else /* if (keys & F_GID) */
    176   1.5       cgd 			output(&indent, "gid=%u", p->fts_statp->st_gid);
    177  1.15      ross 	}
    178   1.5       cgd 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
    179   1.5       cgd 		output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS);
    180  1.32     lukem 	if (keys & F_DEV &&
    181  1.32     lukem 	    (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
    182  1.32     lukem 		output(&indent, "device=%#x", p->fts_statp->st_rdev);
    183   1.5       cgd 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
    184   1.5       cgd 		output(&indent, "nlink=%u", p->fts_statp->st_nlink);
    185  1.11   mycroft 	if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode))
    186  1.27        is 		output(&indent, "size=%lld", (long long)p->fts_statp->st_size);
    187  1.28   hubertf #ifdef BSD4_4
    188   1.5       cgd 	if (keys & F_TIME)
    189   1.8       cgd 		output(&indent, "time=%ld.%ld",
    190  1.27        is 		    (long)p->fts_statp->st_mtimespec.tv_sec,
    191  1.10       jtc 		    p->fts_statp->st_mtimespec.tv_nsec);
    192  1.28   hubertf #else
    193  1.23  christos 		output(&indent, "time=%ld.%ld",
    194  1.23  christos 		    p->fts_statp->st_mtime, 0);
    195  1.17  wsanchez #endif
    196   1.5       cgd 	if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
    197   1.5       cgd 		if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
    198   1.5       cgd 		    crc(fd, &val, &len))
    199  1.17  wsanchez 			mtree_err("%s: %s", p->fts_accpath, strerror(errno));
    200  1.36     lukem 		close(fd);
    201  1.26        is 		output(&indent, "cksum=%lu", (long)val);
    202  1.22   hubertf 	}
    203  1.37     lukem #ifndef NO_MD5
    204  1.22   hubertf 	if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
    205  1.36     lukem 		if (MD5File(p->fts_accpath, digestbuf) == NULL)
    206  1.22   hubertf 			mtree_err("%s: %s", p->fts_accpath, "MD5File");
    207  1.36     lukem 		output(&indent, "md5=%s", digestbuf);
    208  1.36     lukem 	}
    209  1.37     lukem #endif	/* ! NO_MD5 */
    210  1.37     lukem #ifndef NO_RMD160
    211  1.36     lukem 	if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
    212  1.36     lukem 		if (RMD160File(p->fts_accpath, digestbuf) == NULL)
    213  1.36     lukem 			mtree_err("%s: %s", p->fts_accpath, "RMD160File");
    214  1.36     lukem 		output(&indent, "rmd160=%s", digestbuf);
    215  1.36     lukem 	}
    216  1.37     lukem #endif	/* ! NO_RMD160 */
    217  1.37     lukem #ifndef NO_SHA1
    218  1.36     lukem 	if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
    219  1.36     lukem 		if (SHA1File(p->fts_accpath, digestbuf) == NULL)
    220  1.36     lukem 			mtree_err("%s: %s", p->fts_accpath, "SHA1File");
    221  1.36     lukem 		output(&indent, "sha1=%s", digestbuf);
    222   1.1       cgd 	}
    223  1.37     lukem #endif	/* ! NO_SHA1 */
    224   1.5       cgd 	if (keys & F_SLINK &&
    225   1.5       cgd 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
    226   1.5       cgd 		output(&indent, "link=%s", rlink(p->fts_accpath));
    227  1.38        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    228  1.18       mrg 	if (keys & F_FLAGS && p->fts_statp->st_flags != flags)
    229  1.18       mrg 		output(&indent, "flags=%s",
    230  1.18       mrg 		    flags_to_string(p->fts_statp->st_flags, "none"));
    231  1.38        tv #endif
    232  1.36     lukem 	putchar('\n');
    233   1.1       cgd }
    234   1.1       cgd 
    235  1.21       mrg /* XXX
    236  1.21       mrg  * FLAGS2INDEX will fail once the user and system settable bits need more
    237  1.21       mrg  * than one byte, respectively.
    238  1.21       mrg  */
    239  1.21       mrg #define FLAGS2INDEX(x)  (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
    240  1.21       mrg 
    241  1.19  christos #define	MTREE_MAXGID	5000
    242  1.19  christos #define	MTREE_MAXUID	5000
    243  1.21       mrg #define	MTREE_MAXMODE	(MBITS + 1)
    244  1.38        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    245  1.21       mrg #define	MTREE_MAXFLAGS  (FLAGS2INDEX(CH_MASK) + 1)   /* 1808 */
    246  1.38        tv #else
    247  1.38        tv #define MTREE_MAXFLAGS	1
    248  1.38        tv #endif
    249  1.19  christos #define	MTREE_MAXS 16
    250   1.1       cgd 
    251   1.5       cgd static int
    252  1.29    simonb statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
    253  1.29    simonb       u_long *pflags)
    254   1.1       cgd {
    255  1.13     lukem 	FTSENT *p;
    256  1.13     lukem 	gid_t sgid;
    257  1.13     lukem 	uid_t suid;
    258  1.13     lukem 	mode_t smode;
    259  1.38        tv 	u_long sflags = 0;
    260  1.30     lukem 	const char *name;
    261   1.1       cgd 	gid_t savegid;
    262   1.1       cgd 	uid_t saveuid;
    263   1.1       cgd 	mode_t savemode;
    264  1.18       mrg 	u_long saveflags;
    265  1.18       mrg 	u_short maxgid, maxuid, maxmode, maxflags;
    266  1.19  christos 	u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
    267  1.19  christos 		m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
    268  1.36     lukem 	static int first = 1;
    269   1.1       cgd 
    270  1.36     lukem 	savegid = *pgid;
    271  1.36     lukem 	saveuid = *puid;
    272  1.36     lukem 	savemode = *pmode;
    273  1.36     lukem 	saveflags = *pflags;
    274   1.5       cgd 	if ((p = fts_children(t, 0)) == NULL) {
    275   1.5       cgd 		if (errno)
    276  1.17  wsanchez 			mtree_err("%s: %s", RP(parent), strerror(errno));
    277   1.5       cgd 		return (1);
    278   1.1       cgd 	}
    279   1.1       cgd 
    280  1.13     lukem 	memset(g, 0, sizeof(g));
    281  1.13     lukem 	memset(u, 0, sizeof(u));
    282  1.13     lukem 	memset(m, 0, sizeof(m));
    283  1.18       mrg 	memset(f, 0, sizeof(f));
    284   1.1       cgd 
    285  1.18       mrg 	maxuid = maxgid = maxmode = maxflags = 0;
    286   1.1       cgd 	for (; p; p = p->fts_link) {
    287   1.5       cgd 		smode = p->fts_statp->st_mode & MBITS;
    288  1.19  christos 		if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
    289   1.5       cgd 			savemode = smode;
    290   1.5       cgd 			maxmode = m[smode];
    291   1.5       cgd 		}
    292   1.5       cgd 		sgid = p->fts_statp->st_gid;
    293  1.19  christos 		if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
    294   1.5       cgd 			savegid = sgid;
    295   1.5       cgd 			maxgid = g[sgid];
    296   1.5       cgd 		}
    297   1.5       cgd 		suid = p->fts_statp->st_uid;
    298  1.19  christos 		if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
    299   1.5       cgd 			saveuid = suid;
    300   1.5       cgd 			maxuid = u[suid];
    301   1.1       cgd 		}
    302  1.18       mrg 
    303  1.38        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    304  1.21       mrg 		sflags = FLAGS2INDEX(p->fts_statp->st_flags);
    305  1.21       mrg 		if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
    306  1.21       mrg 			saveflags = p->fts_statp->st_flags;
    307  1.21       mrg 			maxflags = f[sflags];
    308  1.18       mrg 		}
    309  1.38        tv #endif
    310   1.1       cgd 	}
    311  1.36     lukem 	/*
    312  1.36     lukem 	 * If the /set record is the same as the last one we do not need to
    313  1.36     lukem 	 * output a new one.  So first we check to see if anything changed.
    314  1.36     lukem 	 * Note that we always output a /set record for the first directory.
    315  1.36     lukem 	 */
    316  1.36     lukem 	if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) ||
    317  1.36     lukem 	    ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) ||
    318  1.36     lukem 	    ((keys & F_MODE) && (*pmode != savemode)) ||
    319  1.36     lukem 	    ((keys & F_FLAGS) && (*pflags != saveflags)) ||
    320  1.36     lukem 	    first) {
    321  1.36     lukem 		first = 0;
    322  1.36     lukem 		printf("/set type=file");
    323  1.36     lukem 		if (keys & (F_UID | F_UNAME)) {
    324  1.36     lukem 			if (keys & F_UNAME &&
    325  1.36     lukem 			    (name = user_from_uid(saveuid, 1)) != NULL)
    326  1.36     lukem 				printf(" uname=%s", name);
    327  1.36     lukem 			else /* if (keys & F_UID) */
    328  1.36     lukem 				printf(" uid=%lu", (u_long)saveuid);
    329  1.36     lukem 		}
    330  1.36     lukem 		if (keys & (F_GID | F_GNAME)) {
    331  1.36     lukem 			if (keys & F_GNAME &&
    332  1.36     lukem 			    (name = group_from_gid(savegid, 1)) != NULL)
    333  1.36     lukem 				printf(" gname=%s", name);
    334  1.36     lukem 			else /* if (keys & F_UID) */
    335  1.36     lukem 				printf(" gid=%lu", (u_long)savegid);
    336  1.36     lukem 		}
    337  1.36     lukem 		if (keys & F_MODE)
    338  1.36     lukem 			printf(" mode=%#lo", (u_long)savemode);
    339  1.36     lukem 		if (keys & F_NLINK)
    340  1.36     lukem 			printf(" nlink=1");
    341  1.36     lukem 		if (keys & F_FLAGS)
    342  1.36     lukem 			printf(" flags=%s",
    343  1.36     lukem 			    flags_to_string(saveflags, "none"));
    344  1.36     lukem 		printf("\n");
    345  1.36     lukem 		*puid = saveuid;
    346  1.36     lukem 		*pgid = savegid;
    347  1.36     lukem 		*pmode = savemode;
    348  1.36     lukem 		*pflags = saveflags;
    349  1.15      ross 	}
    350   1.5       cgd 	return (0);
    351   1.1       cgd }
    352   1.1       cgd 
    353   1.5       cgd static int
    354  1.29    simonb dsort(const FTSENT **a, const FTSENT **b)
    355   1.1       cgd {
    356  1.29    simonb 
    357   1.5       cgd 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
    358   1.5       cgd 		if (!S_ISDIR((*b)->fts_statp->st_mode))
    359   1.5       cgd 			return (1);
    360   1.5       cgd 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
    361   1.5       cgd 		return (-1);
    362   1.5       cgd 	return (strcmp((*a)->fts_name, (*b)->fts_name));
    363   1.5       cgd }
    364   1.1       cgd 
    365   1.5       cgd void
    366   1.5       cgd output(int *offset, const char *fmt, ...)
    367   1.5       cgd {
    368   1.5       cgd 	va_list ap;
    369   1.5       cgd 	char buf[1024];
    370  1.29    simonb 
    371   1.5       cgd 	va_start(ap, fmt);
    372  1.36     lukem 	vsnprintf(buf, sizeof(buf), fmt, ap);
    373   1.5       cgd 	va_end(ap);
    374   1.5       cgd 
    375   1.8       cgd 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
    376  1.36     lukem 		printf(" \\\n%*s", INDENTNAMELEN, "");
    377   1.5       cgd 		*offset = INDENTNAMELEN;
    378   1.5       cgd 	}
    379   1.5       cgd 	*offset += printf(" %s", buf) + 1;
    380   1.1       cgd }
    381