Home | History | Annotate | Line # | Download | only in mtree
create.c revision 1.76
      1  1.76     sevan /*	$NetBSD: create.c,v 1.76 2018/11/18 23:03:36 sevan 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.43       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.45       jmc #if HAVE_NBTOOL_CONFIG_H
     33  1.45       jmc #include "nbtool_config.h"
     34  1.45       jmc #endif
     35  1.45       jmc 
     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.76     sevan __RCSID("$NetBSD: create.c,v 1.76 2018/11/18 23:03:36 sevan 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.44     lukem #if ! HAVE_NBTOOL_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.72  christos #include <stdint.h>
     59  1.35     lukem #include <stdlib.h>
     60  1.13     lukem #include <string.h>
     61  1.13     lukem #include <time.h>
     62   1.5       cgd #include <unistd.h>
     63  1.29    simonb 
     64  1.37     lukem #ifndef NO_MD5
     65  1.37     lukem #include <md5.h>
     66  1.37     lukem #endif
     67  1.37     lukem #ifndef NO_RMD160
     68  1.50  christos #include <rmd160.h>
     69  1.37     lukem #endif
     70  1.37     lukem #ifndef NO_SHA1
     71  1.37     lukem #include <sha1.h>
     72  1.37     lukem #endif
     73  1.47      elad #ifndef NO_SHA2
     74  1.50  christos #include <sha2.h>
     75  1.47      elad #endif
     76  1.37     lukem 
     77   1.5       cgd #include "extern.h"
     78   1.1       cgd 
     79   1.5       cgd #define	INDENTNAMELEN	15
     80   1.5       cgd #define	MAXLINELEN	80
     81   1.5       cgd 
     82   1.5       cgd static gid_t gid;
     83   1.5       cgd static uid_t uid;
     84   1.5       cgd static mode_t mode;
     85  1.18       mrg static u_long flags;
     86   1.5       cgd 
     87  1.61  christos #ifdef __FreeBSD__
     88  1.61  christos #define	FTS_CONST const
     89  1.61  christos #else
     90  1.61  christos #define	FTS_CONST
     91  1.61  christos #endif
     92  1.61  christos 
     93  1.61  christos static int	dcmp(const FTSENT *FTS_CONST *, const FTSENT *FTS_CONST *);
     94  1.73  christos static void	output(FILE *, int, int *, const char *, ...)
     95  1.73  christos     __printflike(4, 5);
     96  1.73  christos static int	statd(FILE *, FTS *, FTSENT *, uid_t *, gid_t *, mode_t *,
     97  1.73  christos     u_long *);
     98  1.73  christos static void	statf(FILE *, int, FTSENT *);
     99   1.1       cgd 
    100   1.5       cgd void
    101  1.73  christos cwalk(FILE *fp)
    102   1.1       cgd {
    103  1.13     lukem 	FTS *t;
    104  1.13     lukem 	FTSENT *p;
    105  1.33     lukem 	time_t clocktime;
    106  1.34     lukem 	char host[MAXHOSTNAMELEN + 1];
    107  1.55  christos 	const char *user;
    108  1.41     grant 	char *argv[2];
    109  1.41     grant 	char  dot[] = ".";
    110  1.63  christos 	int indent = 0;
    111  1.55  christos 
    112  1.41     grant 	argv[0] = dot;
    113  1.41     grant 	argv[1] = NULL;
    114   1.5       cgd 
    115  1.36     lukem 	time(&clocktime);
    116  1.36     lukem 	gethostname(host, sizeof(host));
    117  1.14       mrg 	host[sizeof(host) - 1] = '\0';
    118  1.55  christos 	if ((user = getlogin()) == NULL) {
    119  1.55  christos 		struct passwd *pw;
    120  1.76     sevan 		user = (pw = getpwuid(getuid())) != NULL ? pw->pw_name :
    121  1.55  christos 		    "<unknown>";
    122  1.55  christos 	}
    123  1.55  christos 
    124  1.62  christos 	if (!nflag)
    125  1.73  christos 		fprintf(fp,
    126  1.62  christos 	    	    "#\t   user: %s\n#\tmachine: %s\n#\t   tree: %s\n"
    127  1.62  christos 		    "#\t   date: %s",
    128  1.62  christos 		    user, host, fullpath, ctime(&clocktime));
    129   1.1       cgd 
    130  1.54    rillig 	if ((t = fts_open(argv, ftsoptions, dcmp)) == NULL)
    131  1.17  wsanchez 		mtree_err("fts_open: %s", strerror(errno));
    132  1.36     lukem 	while ((p = fts_read(t)) != NULL) {
    133  1.63  christos 		if (jflag)
    134  1.63  christos 			indent = p->fts_level * 4;
    135  1.36     lukem 		if (check_excludes(p->fts_name, p->fts_path)) {
    136  1.36     lukem 			fts_set(t, p, FTS_SKIP);
    137  1.36     lukem 			continue;
    138  1.36     lukem 		}
    139  1.69  christos 		if (!find_only(p->fts_path)) {
    140  1.69  christos 			fts_set(t, p, FTS_SKIP);
    141  1.69  christos 			continue;
    142  1.69  christos 		}
    143   1.1       cgd 		switch(p->fts_info) {
    144   1.1       cgd 		case FTS_D:
    145  1.68  christos 			if (!bflag)
    146  1.73  christos 				fprintf(fp, "\n");
    147  1.62  christos 			if (!nflag)
    148  1.73  christos 				fprintf(fp, "# %s\n", p->fts_path);
    149  1.73  christos 			statd(fp, t, p, &uid, &gid, &mode, &flags);
    150  1.73  christos 			statf(fp, indent, p);
    151   1.1       cgd 			break;
    152   1.1       cgd 		case FTS_DP:
    153  1.68  christos 			if (p->fts_level > 0)
    154  1.66  christos 				if (!nflag)
    155  1.73  christos 					fprintf(fp, "%*s# %s\n", indent, "",
    156  1.66  christos 					    p->fts_path);
    157  1.68  christos 			if (p->fts_level > 0 || flavor == F_FREEBSD9) {
    158  1.73  christos 				fprintf(fp, "%*s..\n", indent, "");
    159  1.68  christos 				if (!bflag)
    160  1.73  christos 					fprintf(fp, "\n");
    161  1.66  christos 			}
    162   1.5       cgd 			break;
    163   1.1       cgd 		case FTS_DNR:
    164   1.1       cgd 		case FTS_ERR:
    165   1.1       cgd 		case FTS_NS:
    166  1.36     lukem 			mtree_err("%s: %s",
    167  1.36     lukem 			    p->fts_path, strerror(p->fts_errno));
    168   1.5       cgd 			break;
    169   1.1       cgd 		default:
    170   1.5       cgd 			if (!dflag)
    171  1.73  christos 				statf(fp, indent, p);
    172   1.5       cgd 			break;
    173  1.36     lukem 
    174   1.1       cgd 		}
    175  1.36     lukem 	}
    176  1.36     lukem 	fts_close(t);
    177   1.5       cgd 	if (sflag && keys & F_CKSUM)
    178  1.40     soren 		mtree_err("%s checksum: %u", fullpath, crc_total);
    179   1.5       cgd }
    180   1.1       cgd 
    181   1.5       cgd static void
    182  1.75  christos dosum(FILE *fp, int indent, FTSENT *p, int *offset, int flag,
    183  1.75  christos     char * (*func)(const char *, char *), const char *key)
    184  1.75  christos {
    185  1.75  christos 	char *digestbuf;
    186  1.75  christos 
    187  1.75  christos 	if ((keys & flag) == 0)
    188  1.75  christos 		return;
    189  1.75  christos 
    190  1.75  christos 	digestbuf = (*func)(p->fts_accpath, NULL);
    191  1.75  christos 	if (digestbuf != NULL) {
    192  1.75  christos 		output(fp, indent, offset, "%s=%s", key, digestbuf);
    193  1.75  christos 		free(digestbuf);
    194  1.75  christos 		return;
    195  1.75  christos 	}
    196  1.75  christos 
    197  1.75  christos 	if (qflag) {
    198  1.75  christos 		warn("%s: %s failed", p->fts_path, key);
    199  1.75  christos 		return;
    200  1.75  christos 	}
    201  1.75  christos 
    202  1.75  christos 	mtree_err("%s: %s failed: %s", p->fts_path, key, strerror(errno));
    203  1.75  christos }
    204  1.75  christos 
    205  1.75  christos static char *
    206  1.75  christos crcFile(const char *fname, char *dummy __unused)
    207  1.75  christos {
    208  1.75  christos 	char *ptr;
    209  1.75  christos 	uint32_t val, len;
    210  1.75  christos 	int fd, e;
    211  1.75  christos 
    212  1.75  christos 	if ((fd = open(fname, O_RDONLY)) == -1)
    213  1.75  christos 		goto out;
    214  1.75  christos 
    215  1.75  christos 	e = crc(fd, &val, &len);
    216  1.75  christos 	close(fd);
    217  1.75  christos 	if (e)
    218  1.75  christos 		goto out;
    219  1.75  christos 
    220  1.75  christos 	if (asprintf(&ptr, "%u", val) < 0)
    221  1.75  christos 		goto out;
    222  1.75  christos 
    223  1.75  christos 	return ptr;
    224  1.75  christos out:
    225  1.75  christos 	mtree_err("%s: %s", fname, strerror(errno));
    226  1.75  christos 	return NULL;
    227  1.75  christos }
    228  1.75  christos 
    229  1.75  christos static void
    230  1.73  christos statf(FILE *fp, int indent, FTSENT *p)
    231   1.5       cgd {
    232  1.75  christos 	int offset;
    233  1.65  christos 	const char *name = NULL;
    234   1.5       cgd 
    235  1.73  christos 	offset = fprintf(fp, "%*s%s%s", indent, "",
    236  1.46     lukem 	    S_ISDIR(p->fts_statp->st_mode) ? "" : "    ", vispath(p->fts_name));
    237   1.5       cgd 
    238  1.63  christos 	if (offset > (INDENTNAMELEN + indent))
    239  1.63  christos 		offset = MAXLINELEN;
    240   1.5       cgd 	else
    241  1.73  christos 		offset += fprintf(fp, "%*s",
    242  1.73  christos 		    (INDENTNAMELEN + indent) - offset, "");
    243   1.5       cgd 
    244  1.68  christos 	if (!S_ISREG(p->fts_statp->st_mode) && (flavor == F_NETBSD6 || !dflag))
    245  1.73  christos 		output(fp, indent, &offset, "type=%s",
    246  1.63  christos 		    inotype(p->fts_statp->st_mode));
    247  1.15      ross 	if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
    248  1.30     lukem 		if (keys & F_UNAME &&
    249  1.30     lukem 		    (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
    250  1.73  christos 			output(fp, indent, &offset, "uname=%s", name);
    251  1.65  christos 		if (keys & F_UID || (keys & F_UNAME && name == NULL))
    252  1.73  christos 			output(fp, indent, &offset, "uid=%u",
    253  1.73  christos 			    p->fts_statp->st_uid);
    254  1.15      ross 	}
    255  1.15      ross 	if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
    256  1.30     lukem 		if (keys & F_GNAME &&
    257  1.30     lukem 		    (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
    258  1.73  christos 			output(fp, indent, &offset, "gname=%s", name);
    259  1.65  christos 		if (keys & F_GID || (keys & F_GNAME && name == NULL))
    260  1.73  christos 			output(fp, indent, &offset, "gid=%u",
    261  1.73  christos 			    p->fts_statp->st_gid);
    262  1.15      ross 	}
    263   1.5       cgd 	if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
    264  1.73  christos 		output(fp, indent, &offset, "mode=%#o",
    265  1.63  christos 		    p->fts_statp->st_mode & MBITS);
    266  1.32     lukem 	if (keys & F_DEV &&
    267  1.32     lukem 	    (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
    268  1.73  christos 		output(fp, indent, &offset, "device=%#jx",
    269  1.70  christos 		    (uintmax_t)p->fts_statp->st_rdev);
    270   1.5       cgd 	if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
    271  1.74  christos 		output(fp, indent, &offset, "nlink=%ju",
    272  1.74  christos 		    (uintmax_t)p->fts_statp->st_nlink);
    273  1.68  christos 	if (keys & F_SIZE &&
    274  1.71  christos 	    (flavor == F_FREEBSD9 || S_ISREG(p->fts_statp->st_mode)))
    275  1.73  christos 		output(fp, indent, &offset, "size=%ju",
    276  1.70  christos 		    (uintmax_t)p->fts_statp->st_size);
    277  1.53    rillig 	if (keys & F_TIME)
    278  1.45       jmc #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
    279  1.73  christos 		output(fp, indent, &offset, "time=%jd.%09ld",
    280  1.70  christos 		    (intmax_t)p->fts_statp->st_mtimespec.tv_sec,
    281  1.10       jtc 		    p->fts_statp->st_mtimespec.tv_nsec);
    282  1.28   hubertf #else
    283  1.73  christos 		output(fp, indent, &offset, "time=%jd.%09ld",
    284  1.70  christos 		    (intmax_t)p->fts_statp->st_mtime, (long)0);
    285  1.17  wsanchez #endif
    286  1.75  christos 	if (S_ISREG(p->fts_statp->st_mode))  {
    287  1.75  christos 		dosum(fp, indent, p, &offset, F_CKSUM, crcFile, "cksum");
    288  1.37     lukem #ifndef NO_MD5
    289  1.75  christos 		dosum(fp, indent, p, &offset, F_MD5, MD5File, MD5KEY);
    290  1.37     lukem #endif	/* ! NO_MD5 */
    291  1.37     lukem #ifndef NO_RMD160
    292  1.75  christos 		dosum(fp, indent, p, &offset, F_RMD160, RMD160File, RMD160KEY);
    293  1.37     lukem #endif	/* ! NO_RMD160 */
    294  1.37     lukem #ifndef NO_SHA1
    295  1.75  christos 		dosum(fp, indent, p, &offset, F_SHA1, SHA1File, SHA1KEY);
    296  1.37     lukem #endif	/* ! NO_SHA1 */
    297  1.47      elad #ifndef NO_SHA2
    298  1.75  christos 		dosum(fp, indent, p, &offset, F_SHA256, SHA256_File, SHA256KEY);
    299  1.60  christos #ifdef SHA384_BLOCK_LENGTH
    300  1.75  christos 		dosum(fp, indent, p, &offset, F_SHA384, SHA384_File, SHA384KEY);
    301  1.60  christos #endif
    302  1.75  christos 		dosum(fp, indent, p, &offset, F_SHA512, SHA512_File, SHA512KEY);
    303  1.75  christos #endif	/* ! NO_SHA2 */
    304  1.47      elad 	}
    305   1.5       cgd 	if (keys & F_SLINK &&
    306   1.5       cgd 	    (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
    307  1.73  christos 		output(fp, indent, &offset, "link=%s",
    308  1.63  christos 		    vispath(rlink(p->fts_accpath)));
    309  1.38        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    310  1.59       spz 	if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
    311  1.59       spz 		char *str = flags_to_string(p->fts_statp->st_flags, "none");
    312  1.73  christos 		output(fp, indent, &offset, "flags=%s", str);
    313  1.59       spz 		free(str);
    314  1.59       spz 	}
    315  1.38        tv #endif
    316  1.36     lukem 	putchar('\n');
    317   1.1       cgd }
    318   1.1       cgd 
    319  1.21       mrg /* XXX
    320  1.21       mrg  * FLAGS2INDEX will fail once the user and system settable bits need more
    321  1.21       mrg  * than one byte, respectively.
    322  1.21       mrg  */
    323  1.21       mrg #define FLAGS2INDEX(x)  (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
    324  1.21       mrg 
    325  1.19  christos #define	MTREE_MAXGID	5000
    326  1.19  christos #define	MTREE_MAXUID	5000
    327  1.21       mrg #define	MTREE_MAXMODE	(MBITS + 1)
    328  1.38        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    329  1.21       mrg #define	MTREE_MAXFLAGS  (FLAGS2INDEX(CH_MASK) + 1)   /* 1808 */
    330  1.38        tv #else
    331  1.38        tv #define MTREE_MAXFLAGS	1
    332  1.38        tv #endif
    333  1.19  christos #define	MTREE_MAXS 16
    334   1.1       cgd 
    335   1.5       cgd static int
    336  1.73  christos statd(FILE *fp, FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
    337  1.73  christos     u_long *pflags)
    338   1.1       cgd {
    339  1.13     lukem 	FTSENT *p;
    340  1.13     lukem 	gid_t sgid;
    341  1.13     lukem 	uid_t suid;
    342  1.13     lukem 	mode_t smode;
    343  1.38        tv 	u_long sflags = 0;
    344  1.65  christos 	const char *name = NULL;
    345   1.1       cgd 	gid_t savegid;
    346   1.1       cgd 	uid_t saveuid;
    347   1.1       cgd 	mode_t savemode;
    348  1.18       mrg 	u_long saveflags;
    349  1.18       mrg 	u_short maxgid, maxuid, maxmode, maxflags;
    350  1.19  christos 	u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
    351  1.19  christos 		m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
    352  1.36     lukem 	static int first = 1;
    353   1.1       cgd 
    354  1.36     lukem 	savegid = *pgid;
    355  1.36     lukem 	saveuid = *puid;
    356  1.36     lukem 	savemode = *pmode;
    357  1.36     lukem 	saveflags = *pflags;
    358   1.5       cgd 	if ((p = fts_children(t, 0)) == NULL) {
    359   1.5       cgd 		if (errno)
    360  1.17  wsanchez 			mtree_err("%s: %s", RP(parent), strerror(errno));
    361   1.5       cgd 		return (1);
    362   1.1       cgd 	}
    363   1.1       cgd 
    364  1.13     lukem 	memset(g, 0, sizeof(g));
    365  1.13     lukem 	memset(u, 0, sizeof(u));
    366  1.13     lukem 	memset(m, 0, sizeof(m));
    367  1.18       mrg 	memset(f, 0, sizeof(f));
    368   1.1       cgd 
    369  1.18       mrg 	maxuid = maxgid = maxmode = maxflags = 0;
    370   1.1       cgd 	for (; p; p = p->fts_link) {
    371  1.68  christos 		if (flavor == F_NETBSD6 || !dflag ||
    372  1.68  christos 		    (dflag && S_ISDIR(p->fts_statp->st_mode))) {
    373  1.68  christos 			smode = p->fts_statp->st_mode & MBITS;
    374  1.68  christos 			if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
    375  1.68  christos 				savemode = smode;
    376  1.68  christos 				maxmode = m[smode];
    377  1.68  christos 			}
    378  1.68  christos 			sgid = p->fts_statp->st_gid;
    379  1.68  christos 			if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
    380  1.68  christos 				savegid = sgid;
    381  1.68  christos 				maxgid = g[sgid];
    382  1.68  christos 			}
    383  1.68  christos 			suid = p->fts_statp->st_uid;
    384  1.68  christos 			if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
    385  1.68  christos 				saveuid = suid;
    386  1.68  christos 				maxuid = u[suid];
    387  1.68  christos 			}
    388  1.18       mrg 
    389  1.38        tv #if HAVE_STRUCT_STAT_ST_FLAGS
    390  1.68  christos 			sflags = FLAGS2INDEX(p->fts_statp->st_flags);
    391  1.68  christos 			if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
    392  1.68  christos 				saveflags = p->fts_statp->st_flags;
    393  1.68  christos 				maxflags = f[sflags];
    394  1.68  christos 			}
    395  1.68  christos #endif
    396  1.18       mrg 		}
    397   1.1       cgd 	}
    398  1.36     lukem 	/*
    399  1.36     lukem 	 * If the /set record is the same as the last one we do not need to
    400  1.36     lukem 	 * output a new one.  So first we check to see if anything changed.
    401  1.36     lukem 	 * Note that we always output a /set record for the first directory.
    402  1.36     lukem 	 */
    403  1.36     lukem 	if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) ||
    404  1.36     lukem 	    ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) ||
    405  1.36     lukem 	    ((keys & F_MODE) && (*pmode != savemode)) ||
    406  1.36     lukem 	    ((keys & F_FLAGS) && (*pflags != saveflags)) ||
    407  1.36     lukem 	    first) {
    408  1.36     lukem 		first = 0;
    409  1.68  christos 		if (flavor != F_NETBSD6 && dflag)
    410  1.73  christos 			fprintf(fp, "/set type=dir");
    411  1.68  christos 		else
    412  1.73  christos 			fprintf(fp, "/set type=file");
    413  1.36     lukem 		if (keys & (F_UID | F_UNAME)) {
    414  1.36     lukem 			if (keys & F_UNAME &&
    415  1.36     lukem 			    (name = user_from_uid(saveuid, 1)) != NULL)
    416  1.73  christos 				fprintf(fp, " uname=%s", name);
    417  1.65  christos 			if (keys & F_UID || (keys & F_UNAME && name == NULL))
    418  1.73  christos 				fprintf(fp, " uid=%lu", (u_long)saveuid);
    419  1.36     lukem 		}
    420  1.36     lukem 		if (keys & (F_GID | F_GNAME)) {
    421  1.36     lukem 			if (keys & F_GNAME &&
    422  1.36     lukem 			    (name = group_from_gid(savegid, 1)) != NULL)
    423  1.73  christos 				fprintf(fp, " gname=%s", name);
    424  1.65  christos 			if (keys & F_GID || (keys & F_GNAME && name == NULL))
    425  1.73  christos 				fprintf(fp, " gid=%lu", (u_long)savegid);
    426  1.36     lukem 		}
    427  1.36     lukem 		if (keys & F_MODE)
    428  1.73  christos 			fprintf(fp, " mode=%#lo", (u_long)savemode);
    429  1.36     lukem 		if (keys & F_NLINK)
    430  1.73  christos 			fprintf(fp, " nlink=1");
    431  1.59       spz 		if (keys & F_FLAGS) {
    432  1.59       spz 			char *str = flags_to_string(saveflags, "none");
    433  1.73  christos 			fprintf(fp, " flags=%s", str);
    434  1.59       spz 			free(str);
    435  1.59       spz 		}
    436  1.73  christos 		fprintf(fp, "\n");
    437  1.36     lukem 		*puid = saveuid;
    438  1.36     lukem 		*pgid = savegid;
    439  1.36     lukem 		*pmode = savemode;
    440  1.36     lukem 		*pflags = saveflags;
    441  1.15      ross 	}
    442   1.5       cgd 	return (0);
    443   1.1       cgd }
    444   1.1       cgd 
    445  1.58       apb /*
    446  1.58       apb  * dcmp --
    447  1.58       apb  *	used as a comparison function passed to fts_open() to control
    448  1.58       apb  *	the order in which fts_read() returns results.	We make
    449  1.58       apb  *	directories sort after non-directories, but otherwise sort in
    450  1.58       apb  *	strcmp() order.
    451  1.58       apb  *
    452  1.58       apb  * Keep this in sync with nodecmp() in spec.c.
    453  1.58       apb  */
    454   1.5       cgd static int
    455  1.61  christos dcmp(const FTSENT *FTS_CONST *a, const FTSENT *FTS_CONST *b)
    456   1.1       cgd {
    457  1.29    simonb 
    458   1.5       cgd 	if (S_ISDIR((*a)->fts_statp->st_mode)) {
    459   1.5       cgd 		if (!S_ISDIR((*b)->fts_statp->st_mode))
    460   1.5       cgd 			return (1);
    461   1.5       cgd 	} else if (S_ISDIR((*b)->fts_statp->st_mode))
    462   1.5       cgd 		return (-1);
    463   1.5       cgd 	return (strcmp((*a)->fts_name, (*b)->fts_name));
    464   1.5       cgd }
    465   1.1       cgd 
    466   1.5       cgd void
    467  1.73  christos output(FILE *fp, int indent, int *offset, const char *fmt, ...)
    468   1.5       cgd {
    469   1.5       cgd 	va_list ap;
    470   1.5       cgd 	char buf[1024];
    471  1.29    simonb 
    472   1.5       cgd 	va_start(ap, fmt);
    473  1.36     lukem 	vsnprintf(buf, sizeof(buf), fmt, ap);
    474   1.5       cgd 	va_end(ap);
    475   1.5       cgd 
    476   1.8       cgd 	if (*offset + strlen(buf) > MAXLINELEN - 3) {
    477  1.73  christos 		fprintf(fp, " \\\n%*s", INDENTNAMELEN + indent, "");
    478  1.63  christos 		*offset = INDENTNAMELEN + indent;
    479   1.5       cgd 	}
    480  1.73  christos 	*offset += fprintf(fp, " %s", buf) + 1;
    481   1.1       cgd }
    482