125f3fb54SBrooks Davis /* $NetBSD: create.c,v 1.73 2014/04/24 17:22:41 christos Exp $ */
2c6ec7d31SBrooks Davis
3c6ec7d31SBrooks Davis /*-
4c6ec7d31SBrooks Davis * Copyright (c) 1989, 1993
5c6ec7d31SBrooks Davis * The Regents of the University of California. All rights reserved.
6c6ec7d31SBrooks Davis *
7c6ec7d31SBrooks Davis * Redistribution and use in source and binary forms, with or without
8c6ec7d31SBrooks Davis * modification, are permitted provided that the following conditions
9c6ec7d31SBrooks Davis * are met:
10c6ec7d31SBrooks Davis * 1. Redistributions of source code must retain the above copyright
11c6ec7d31SBrooks Davis * notice, this list of conditions and the following disclaimer.
12c6ec7d31SBrooks Davis * 2. Redistributions in binary form must reproduce the above copyright
13c6ec7d31SBrooks Davis * notice, this list of conditions and the following disclaimer in the
14c6ec7d31SBrooks Davis * documentation and/or other materials provided with the distribution.
15c6ec7d31SBrooks Davis * 3. Neither the name of the University nor the names of its contributors
16c6ec7d31SBrooks Davis * may be used to endorse or promote products derived from this software
17c6ec7d31SBrooks Davis * without specific prior written permission.
18c6ec7d31SBrooks Davis *
19c6ec7d31SBrooks Davis * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20c6ec7d31SBrooks Davis * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21c6ec7d31SBrooks Davis * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22c6ec7d31SBrooks Davis * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23c6ec7d31SBrooks Davis * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24c6ec7d31SBrooks Davis * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25c6ec7d31SBrooks Davis * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26c6ec7d31SBrooks Davis * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27c6ec7d31SBrooks Davis * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28c6ec7d31SBrooks Davis * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29c6ec7d31SBrooks Davis * SUCH DAMAGE.
30c6ec7d31SBrooks Davis */
31c6ec7d31SBrooks Davis
32c6ec7d31SBrooks Davis #if HAVE_NBTOOL_CONFIG_H
33c6ec7d31SBrooks Davis #include "nbtool_config.h"
34c6ec7d31SBrooks Davis #endif
35c6ec7d31SBrooks Davis
36c6ec7d31SBrooks Davis #include <sys/cdefs.h>
37c6ec7d31SBrooks Davis #if defined(__RCSID) && !defined(lint)
38c6ec7d31SBrooks Davis #if 0
39c6ec7d31SBrooks Davis static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93";
40c6ec7d31SBrooks Davis #else
4125f3fb54SBrooks Davis __RCSID("$NetBSD: create.c,v 1.73 2014/04/24 17:22:41 christos Exp $");
42c6ec7d31SBrooks Davis #endif
43c6ec7d31SBrooks Davis #endif /* not lint */
44c6ec7d31SBrooks Davis
45c6ec7d31SBrooks Davis #include <sys/param.h>
46c6ec7d31SBrooks Davis #include <sys/stat.h>
47c6ec7d31SBrooks Davis
48c6ec7d31SBrooks Davis #if ! HAVE_NBTOOL_CONFIG_H
49c6ec7d31SBrooks Davis #include <dirent.h>
50c6ec7d31SBrooks Davis #endif
51c6ec7d31SBrooks Davis
52c6ec7d31SBrooks Davis #include <errno.h>
53c6ec7d31SBrooks Davis #include <fcntl.h>
54c6ec7d31SBrooks Davis #include <grp.h>
55c6ec7d31SBrooks Davis #include <pwd.h>
56c6ec7d31SBrooks Davis #include <stdio.h>
57c6ec7d31SBrooks Davis #include <stdarg.h>
5819fa1aa6SBrooks Davis #include <stdint.h>
59c6ec7d31SBrooks Davis #include <stdlib.h>
60c6ec7d31SBrooks Davis #include <string.h>
61c6ec7d31SBrooks Davis #include <time.h>
62c6ec7d31SBrooks Davis #include <unistd.h>
63c6ec7d31SBrooks Davis
64c6ec7d31SBrooks Davis #ifndef NO_MD5
65c6ec7d31SBrooks Davis #include <md5.h>
66c6ec7d31SBrooks Davis #endif
67c6ec7d31SBrooks Davis #ifndef NO_RMD160
68c6ec7d31SBrooks Davis #include <rmd160.h>
69c6ec7d31SBrooks Davis #endif
70c6ec7d31SBrooks Davis #ifndef NO_SHA1
71c6ec7d31SBrooks Davis #include <sha1.h>
72c6ec7d31SBrooks Davis #endif
73c6ec7d31SBrooks Davis #ifndef NO_SHA2
74c6ec7d31SBrooks Davis #include <sha2.h>
75c6ec7d31SBrooks Davis #endif
76c6ec7d31SBrooks Davis
77c6ec7d31SBrooks Davis #include "extern.h"
78c6ec7d31SBrooks Davis
79c6ec7d31SBrooks Davis #define INDENTNAMELEN 15
80c6ec7d31SBrooks Davis #define MAXLINELEN 80
81c6ec7d31SBrooks Davis
82c6ec7d31SBrooks Davis static gid_t gid;
83c6ec7d31SBrooks Davis static uid_t uid;
84c6ec7d31SBrooks Davis static mode_t mode;
85c6ec7d31SBrooks Davis static u_long flags;
86c6ec7d31SBrooks Davis
87c6ec7d31SBrooks Davis #ifdef __FreeBSD__
88c6ec7d31SBrooks Davis #define FTS_CONST const
89c6ec7d31SBrooks Davis #else
90c6ec7d31SBrooks Davis #define FTS_CONST
91c6ec7d31SBrooks Davis #endif
92c6ec7d31SBrooks Davis
93c6ec7d31SBrooks Davis static int dcmp(const FTSENT *FTS_CONST *, const FTSENT *FTS_CONST *);
9425f3fb54SBrooks Davis static void output(FILE *, int, int *, const char *, ...)
9525f3fb54SBrooks Davis __printflike(4, 5);
9625f3fb54SBrooks Davis static int statd(FILE *, FTS *, FTSENT *, uid_t *, gid_t *, mode_t *,
9725f3fb54SBrooks Davis u_long *);
9825f3fb54SBrooks Davis static void statf(FILE *, int, FTSENT *);
99c6ec7d31SBrooks Davis
100c6ec7d31SBrooks Davis void
cwalk(FILE * fp)10125f3fb54SBrooks Davis cwalk(FILE *fp)
102c6ec7d31SBrooks Davis {
103c6ec7d31SBrooks Davis FTS *t;
104c6ec7d31SBrooks Davis FTSENT *p;
105c6ec7d31SBrooks Davis time_t clocktime;
106c6ec7d31SBrooks Davis char host[MAXHOSTNAMELEN + 1];
107c6ec7d31SBrooks Davis const char *user;
108c6ec7d31SBrooks Davis char *argv[2];
109c6ec7d31SBrooks Davis char dot[] = ".";
110c6ec7d31SBrooks Davis int indent = 0;
111c6ec7d31SBrooks Davis
112c6ec7d31SBrooks Davis argv[0] = dot;
113c6ec7d31SBrooks Davis argv[1] = NULL;
114c6ec7d31SBrooks Davis
115c6ec7d31SBrooks Davis time(&clocktime);
116c6ec7d31SBrooks Davis gethostname(host, sizeof(host));
117c6ec7d31SBrooks Davis host[sizeof(host) - 1] = '\0';
118c6ec7d31SBrooks Davis if ((user = getlogin()) == NULL) {
119c6ec7d31SBrooks Davis struct passwd *pw;
12009c9ed3bSBryan Drewery user = (pw = getpwuid(getuid())) != NULL ? pw->pw_name :
121c6ec7d31SBrooks Davis "<unknown>";
122c6ec7d31SBrooks Davis }
123c6ec7d31SBrooks Davis
124c6ec7d31SBrooks Davis if (!nflag)
12525f3fb54SBrooks Davis fprintf(fp,
126c6ec7d31SBrooks Davis "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n"
127c6ec7d31SBrooks Davis "#\t date: %s",
128c6ec7d31SBrooks Davis user, host, fullpath, ctime(&clocktime));
129c6ec7d31SBrooks Davis
130c6ec7d31SBrooks Davis if ((t = fts_open(argv, ftsoptions, dcmp)) == NULL)
131c6ec7d31SBrooks Davis mtree_err("fts_open: %s", strerror(errno));
132*2dfa4b66SBryan Drewery while (errno = 0, (p = fts_read(t)) != NULL) {
133c6ec7d31SBrooks Davis if (jflag)
134c6ec7d31SBrooks Davis indent = p->fts_level * 4;
135c6ec7d31SBrooks Davis if (check_excludes(p->fts_name, p->fts_path)) {
136c6ec7d31SBrooks Davis fts_set(t, p, FTS_SKIP);
137c6ec7d31SBrooks Davis continue;
138c6ec7d31SBrooks Davis }
139edd6bc76SEd Schouten if (!find_only(p->fts_path)) {
140edd6bc76SEd Schouten fts_set(t, p, FTS_SKIP);
141edd6bc76SEd Schouten continue;
142edd6bc76SEd Schouten }
143c6ec7d31SBrooks Davis switch(p->fts_info) {
144c6ec7d31SBrooks Davis case FTS_D:
145c6ec7d31SBrooks Davis if (!bflag)
14625f3fb54SBrooks Davis fprintf(fp, "\n");
147c6ec7d31SBrooks Davis if (!nflag)
14825f3fb54SBrooks Davis fprintf(fp, "# %s\n", p->fts_path);
14925f3fb54SBrooks Davis statd(fp, t, p, &uid, &gid, &mode, &flags);
15025f3fb54SBrooks Davis statf(fp, indent, p);
151c6ec7d31SBrooks Davis break;
152c6ec7d31SBrooks Davis case FTS_DP:
153c6ec7d31SBrooks Davis if (p->fts_level > 0)
154c6ec7d31SBrooks Davis if (!nflag)
15525f3fb54SBrooks Davis fprintf(fp, "%*s# %s\n", indent, "",
156c6ec7d31SBrooks Davis p->fts_path);
157c6ec7d31SBrooks Davis if (p->fts_level > 0 || flavor == F_FREEBSD9) {
15825f3fb54SBrooks Davis fprintf(fp, "%*s..\n", indent, "");
159c6ec7d31SBrooks Davis if (!bflag)
16025f3fb54SBrooks Davis fprintf(fp, "\n");
161c6ec7d31SBrooks Davis }
162c6ec7d31SBrooks Davis break;
163c6ec7d31SBrooks Davis case FTS_DNR:
164c6ec7d31SBrooks Davis case FTS_ERR:
165c6ec7d31SBrooks Davis case FTS_NS:
166c6ec7d31SBrooks Davis mtree_err("%s: %s",
167c6ec7d31SBrooks Davis p->fts_path, strerror(p->fts_errno));
168c6ec7d31SBrooks Davis break;
169c6ec7d31SBrooks Davis default:
170c6ec7d31SBrooks Davis if (!dflag)
17125f3fb54SBrooks Davis statf(fp, indent, p);
172c6ec7d31SBrooks Davis break;
173c6ec7d31SBrooks Davis
174c6ec7d31SBrooks Davis }
175c6ec7d31SBrooks Davis }
176*2dfa4b66SBryan Drewery if (errno != 0)
177*2dfa4b66SBryan Drewery mtree_err("fts_read: %s", strerror(errno));
178c6ec7d31SBrooks Davis fts_close(t);
179c6ec7d31SBrooks Davis if (sflag && keys & F_CKSUM)
180c6ec7d31SBrooks Davis mtree_err("%s checksum: %u", fullpath, crc_total);
181c6ec7d31SBrooks Davis }
182c6ec7d31SBrooks Davis
183c6ec7d31SBrooks Davis static void
statf(FILE * fp,int indent,FTSENT * p)18425f3fb54SBrooks Davis statf(FILE *fp, int indent, FTSENT *p)
185c6ec7d31SBrooks Davis {
186c6ec7d31SBrooks Davis u_int32_t len, val;
187c6ec7d31SBrooks Davis int fd, offset;
188c6ec7d31SBrooks Davis const char *name = NULL;
189c6ec7d31SBrooks Davis #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
190c6ec7d31SBrooks Davis char *digestbuf;
191c6ec7d31SBrooks Davis #endif
192c6ec7d31SBrooks Davis
19325f3fb54SBrooks Davis offset = fprintf(fp, "%*s%s%s", indent, "",
194c6ec7d31SBrooks Davis S_ISDIR(p->fts_statp->st_mode) ? "" : " ", vispath(p->fts_name));
195c6ec7d31SBrooks Davis
196c6ec7d31SBrooks Davis if (offset > (INDENTNAMELEN + indent))
197c6ec7d31SBrooks Davis offset = MAXLINELEN;
198c6ec7d31SBrooks Davis else
19925f3fb54SBrooks Davis offset += fprintf(fp, "%*s",
20025f3fb54SBrooks Davis (INDENTNAMELEN + indent) - offset, "");
201c6ec7d31SBrooks Davis
202c6ec7d31SBrooks Davis if (!S_ISREG(p->fts_statp->st_mode) && (flavor == F_NETBSD6 || !dflag))
20325f3fb54SBrooks Davis output(fp, indent, &offset, "type=%s",
204c6ec7d31SBrooks Davis inotype(p->fts_statp->st_mode));
205c6ec7d31SBrooks Davis if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
206c6ec7d31SBrooks Davis if (keys & F_UNAME &&
207c6ec7d31SBrooks Davis (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
20825f3fb54SBrooks Davis output(fp, indent, &offset, "uname=%s", name);
209c6ec7d31SBrooks Davis if (keys & F_UID || (keys & F_UNAME && name == NULL))
21025f3fb54SBrooks Davis output(fp, indent, &offset, "uid=%u",
21125f3fb54SBrooks Davis p->fts_statp->st_uid);
212c6ec7d31SBrooks Davis }
213c6ec7d31SBrooks Davis if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
214c6ec7d31SBrooks Davis if (keys & F_GNAME &&
215c6ec7d31SBrooks Davis (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
21625f3fb54SBrooks Davis output(fp, indent, &offset, "gname=%s", name);
217c6ec7d31SBrooks Davis if (keys & F_GID || (keys & F_GNAME && name == NULL))
21825f3fb54SBrooks Davis output(fp, indent, &offset, "gid=%u",
21925f3fb54SBrooks Davis p->fts_statp->st_gid);
220c6ec7d31SBrooks Davis }
221c6ec7d31SBrooks Davis if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
22225f3fb54SBrooks Davis output(fp, indent, &offset, "mode=%#o",
223c6ec7d31SBrooks Davis p->fts_statp->st_mode & MBITS);
224c6ec7d31SBrooks Davis if (keys & F_DEV &&
225c6ec7d31SBrooks Davis (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
22625f3fb54SBrooks Davis output(fp, indent, &offset, "device=%#jx",
2276ab38b8eSBrooks Davis (uintmax_t)p->fts_statp->st_rdev);
228c6ec7d31SBrooks Davis if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
2291c324569SKonstantin Belousov output(fp, indent, &offset, "nlink=%ju",
2301c324569SKonstantin Belousov (uintmax_t)p->fts_statp->st_nlink);
231c6ec7d31SBrooks Davis if (keys & F_SIZE &&
2326ab38b8eSBrooks Davis (flavor == F_FREEBSD9 || S_ISREG(p->fts_statp->st_mode)))
23325f3fb54SBrooks Davis output(fp, indent, &offset, "size=%ju",
2346ab38b8eSBrooks Davis (uintmax_t)p->fts_statp->st_size);
235c6ec7d31SBrooks Davis if (keys & F_TIME)
236c6ec7d31SBrooks Davis #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
23725f3fb54SBrooks Davis output(fp, indent, &offset, "time=%jd.%09ld",
2386ab38b8eSBrooks Davis (intmax_t)p->fts_statp->st_mtimespec.tv_sec,
239c6ec7d31SBrooks Davis p->fts_statp->st_mtimespec.tv_nsec);
240c6ec7d31SBrooks Davis #else
24125f3fb54SBrooks Davis output(fp, indent, &offset, "time=%jd.%09ld",
2426ab38b8eSBrooks Davis (intmax_t)p->fts_statp->st_mtime, (long)0);
243c6ec7d31SBrooks Davis #endif
244c6ec7d31SBrooks Davis if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
245c6ec7d31SBrooks Davis if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
246c6ec7d31SBrooks Davis crc(fd, &val, &len))
247c6ec7d31SBrooks Davis mtree_err("%s: %s", p->fts_accpath, strerror(errno));
248c6ec7d31SBrooks Davis close(fd);
24925f3fb54SBrooks Davis output(fp, indent, &offset, "cksum=%lu", (long)val);
250c6ec7d31SBrooks Davis }
251c6ec7d31SBrooks Davis #ifndef NO_MD5
252c6ec7d31SBrooks Davis if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
253c6ec7d31SBrooks Davis if ((digestbuf = MD5File(p->fts_accpath, NULL)) == NULL)
254c6ec7d31SBrooks Davis mtree_err("%s: MD5File failed: %s", p->fts_accpath,
255c6ec7d31SBrooks Davis strerror(errno));
25625f3fb54SBrooks Davis output(fp, indent, &offset, "%s=%s", MD5KEY, digestbuf);
257c6ec7d31SBrooks Davis free(digestbuf);
258c6ec7d31SBrooks Davis }
259c6ec7d31SBrooks Davis #endif /* ! NO_MD5 */
260c6ec7d31SBrooks Davis #ifndef NO_RMD160
261c6ec7d31SBrooks Davis if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
262c6ec7d31SBrooks Davis if ((digestbuf = RMD160File(p->fts_accpath, NULL)) == NULL)
263c6ec7d31SBrooks Davis mtree_err("%s: RMD160File failed: %s", p->fts_accpath,
264c6ec7d31SBrooks Davis strerror(errno));
26525f3fb54SBrooks Davis output(fp, indent, &offset, "%s=%s", RMD160KEY, digestbuf);
266c6ec7d31SBrooks Davis free(digestbuf);
267c6ec7d31SBrooks Davis }
268c6ec7d31SBrooks Davis #endif /* ! NO_RMD160 */
269c6ec7d31SBrooks Davis #ifndef NO_SHA1
270c6ec7d31SBrooks Davis if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
271c6ec7d31SBrooks Davis if ((digestbuf = SHA1File(p->fts_accpath, NULL)) == NULL)
272c6ec7d31SBrooks Davis mtree_err("%s: SHA1File failed: %s", p->fts_accpath,
273c6ec7d31SBrooks Davis strerror(errno));
27425f3fb54SBrooks Davis output(fp, indent, &offset, "%s=%s", SHA1KEY, digestbuf);
275c6ec7d31SBrooks Davis free(digestbuf);
276c6ec7d31SBrooks Davis }
277c6ec7d31SBrooks Davis #endif /* ! NO_SHA1 */
278c6ec7d31SBrooks Davis #ifndef NO_SHA2
279c6ec7d31SBrooks Davis if (keys & F_SHA256 && S_ISREG(p->fts_statp->st_mode)) {
280c6ec7d31SBrooks Davis if ((digestbuf = SHA256_File(p->fts_accpath, NULL)) == NULL)
281c6ec7d31SBrooks Davis mtree_err("%s: SHA256_File failed: %s", p->fts_accpath,
282c6ec7d31SBrooks Davis strerror(errno));
28325f3fb54SBrooks Davis output(fp, indent, &offset, "%s=%s", SHA256KEY, digestbuf);
284c6ec7d31SBrooks Davis free(digestbuf);
285c6ec7d31SBrooks Davis }
286c6ec7d31SBrooks Davis #ifdef SHA384_BLOCK_LENGTH
287c6ec7d31SBrooks Davis if (keys & F_SHA384 && S_ISREG(p->fts_statp->st_mode)) {
288c6ec7d31SBrooks Davis if ((digestbuf = SHA384_File(p->fts_accpath, NULL)) == NULL)
289c6ec7d31SBrooks Davis mtree_err("%s: SHA384_File failed: %s", p->fts_accpath,
290c6ec7d31SBrooks Davis strerror(errno));
29125f3fb54SBrooks Davis output(fp, indent, &offset, "%s=%s", SHA384KEY, digestbuf);
292c6ec7d31SBrooks Davis free(digestbuf);
293c6ec7d31SBrooks Davis }
294c6ec7d31SBrooks Davis #endif
295c6ec7d31SBrooks Davis if (keys & F_SHA512 && S_ISREG(p->fts_statp->st_mode)) {
296c6ec7d31SBrooks Davis if ((digestbuf = SHA512_File(p->fts_accpath, NULL)) == NULL)
297c6ec7d31SBrooks Davis mtree_err("%s: SHA512_File failed: %s", p->fts_accpath,
298c6ec7d31SBrooks Davis strerror(errno));
29925f3fb54SBrooks Davis output(fp, indent, &offset, "%s=%s", SHA512KEY, digestbuf);
300c6ec7d31SBrooks Davis free(digestbuf);
301c6ec7d31SBrooks Davis }
302c6ec7d31SBrooks Davis #endif /* ! NO_SHA2 */
303c6ec7d31SBrooks Davis if (keys & F_SLINK &&
304c6ec7d31SBrooks Davis (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
30525f3fb54SBrooks Davis output(fp, indent, &offset, "link=%s",
306c6ec7d31SBrooks Davis vispath(rlink(p->fts_accpath)));
307c6ec7d31SBrooks Davis #if HAVE_STRUCT_STAT_ST_FLAGS
308c6ec7d31SBrooks Davis if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
309c6ec7d31SBrooks Davis char *str = flags_to_string(p->fts_statp->st_flags, "none");
31025f3fb54SBrooks Davis output(fp, indent, &offset, "flags=%s", str);
311c6ec7d31SBrooks Davis free(str);
312c6ec7d31SBrooks Davis }
313c6ec7d31SBrooks Davis #endif
314c6ec7d31SBrooks Davis putchar('\n');
315c6ec7d31SBrooks Davis }
316c6ec7d31SBrooks Davis
317c6ec7d31SBrooks Davis /* XXX
318c6ec7d31SBrooks Davis * FLAGS2INDEX will fail once the user and system settable bits need more
319c6ec7d31SBrooks Davis * than one byte, respectively.
320c6ec7d31SBrooks Davis */
321c6ec7d31SBrooks Davis #define FLAGS2INDEX(x) (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
322c6ec7d31SBrooks Davis
323c6ec7d31SBrooks Davis #define MTREE_MAXGID 5000
324c6ec7d31SBrooks Davis #define MTREE_MAXUID 5000
325c6ec7d31SBrooks Davis #define MTREE_MAXMODE (MBITS + 1)
326c6ec7d31SBrooks Davis #if HAVE_STRUCT_STAT_ST_FLAGS
327c6ec7d31SBrooks Davis #define MTREE_MAXFLAGS (FLAGS2INDEX(CH_MASK) + 1) /* 1808 */
328c6ec7d31SBrooks Davis #else
329c6ec7d31SBrooks Davis #define MTREE_MAXFLAGS 1
330c6ec7d31SBrooks Davis #endif
331c6ec7d31SBrooks Davis #define MTREE_MAXS 16
332c6ec7d31SBrooks Davis
333c6ec7d31SBrooks Davis static int
statd(FILE * fp,FTS * t,FTSENT * parent,uid_t * puid,gid_t * pgid,mode_t * pmode,u_long * pflags)33425f3fb54SBrooks Davis statd(FILE *fp, FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
335c6ec7d31SBrooks Davis u_long *pflags)
336c6ec7d31SBrooks Davis {
337c6ec7d31SBrooks Davis FTSENT *p;
338c6ec7d31SBrooks Davis gid_t sgid;
339c6ec7d31SBrooks Davis uid_t suid;
340c6ec7d31SBrooks Davis mode_t smode;
341c6ec7d31SBrooks Davis u_long sflags = 0;
342c6ec7d31SBrooks Davis const char *name = NULL;
343c6ec7d31SBrooks Davis gid_t savegid;
344c6ec7d31SBrooks Davis uid_t saveuid;
345c6ec7d31SBrooks Davis mode_t savemode;
346c6ec7d31SBrooks Davis u_long saveflags;
347c6ec7d31SBrooks Davis u_short maxgid, maxuid, maxmode, maxflags;
348c6ec7d31SBrooks Davis u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
349c6ec7d31SBrooks Davis m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
350c6ec7d31SBrooks Davis static int first = 1;
351c6ec7d31SBrooks Davis
352c6ec7d31SBrooks Davis savegid = *pgid;
353c6ec7d31SBrooks Davis saveuid = *puid;
354c6ec7d31SBrooks Davis savemode = *pmode;
355c6ec7d31SBrooks Davis saveflags = *pflags;
356c6ec7d31SBrooks Davis if ((p = fts_children(t, 0)) == NULL) {
357c6ec7d31SBrooks Davis if (errno)
358c6ec7d31SBrooks Davis mtree_err("%s: %s", RP(parent), strerror(errno));
359c6ec7d31SBrooks Davis return (1);
360c6ec7d31SBrooks Davis }
361c6ec7d31SBrooks Davis
362c6ec7d31SBrooks Davis memset(g, 0, sizeof(g));
363c6ec7d31SBrooks Davis memset(u, 0, sizeof(u));
364c6ec7d31SBrooks Davis memset(m, 0, sizeof(m));
365c6ec7d31SBrooks Davis memset(f, 0, sizeof(f));
366c6ec7d31SBrooks Davis
367c6ec7d31SBrooks Davis maxuid = maxgid = maxmode = maxflags = 0;
368c6ec7d31SBrooks Davis for (; p; p = p->fts_link) {
369c6ec7d31SBrooks Davis if (flavor == F_NETBSD6 || !dflag ||
370c6ec7d31SBrooks Davis (dflag && S_ISDIR(p->fts_statp->st_mode))) {
371c6ec7d31SBrooks Davis smode = p->fts_statp->st_mode & MBITS;
372c6ec7d31SBrooks Davis if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
373c6ec7d31SBrooks Davis savemode = smode;
374c6ec7d31SBrooks Davis maxmode = m[smode];
375c6ec7d31SBrooks Davis }
376c6ec7d31SBrooks Davis sgid = p->fts_statp->st_gid;
377c6ec7d31SBrooks Davis if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
378c6ec7d31SBrooks Davis savegid = sgid;
379c6ec7d31SBrooks Davis maxgid = g[sgid];
380c6ec7d31SBrooks Davis }
381c6ec7d31SBrooks Davis suid = p->fts_statp->st_uid;
382c6ec7d31SBrooks Davis if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
383c6ec7d31SBrooks Davis saveuid = suid;
384c6ec7d31SBrooks Davis maxuid = u[suid];
385c6ec7d31SBrooks Davis }
386c6ec7d31SBrooks Davis
387c6ec7d31SBrooks Davis #if HAVE_STRUCT_STAT_ST_FLAGS
388c6ec7d31SBrooks Davis sflags = FLAGS2INDEX(p->fts_statp->st_flags);
389c6ec7d31SBrooks Davis if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
390c6ec7d31SBrooks Davis saveflags = p->fts_statp->st_flags;
391c6ec7d31SBrooks Davis maxflags = f[sflags];
392c6ec7d31SBrooks Davis }
393c6ec7d31SBrooks Davis #endif
394c6ec7d31SBrooks Davis }
395c6ec7d31SBrooks Davis }
396c6ec7d31SBrooks Davis /*
397c6ec7d31SBrooks Davis * If the /set record is the same as the last one we do not need to
398c6ec7d31SBrooks Davis * output a new one. So first we check to see if anything changed.
399c6ec7d31SBrooks Davis * Note that we always output a /set record for the first directory.
400c6ec7d31SBrooks Davis */
401c6ec7d31SBrooks Davis if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) ||
402c6ec7d31SBrooks Davis ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) ||
403c6ec7d31SBrooks Davis ((keys & F_MODE) && (*pmode != savemode)) ||
404c6ec7d31SBrooks Davis ((keys & F_FLAGS) && (*pflags != saveflags)) ||
405c6ec7d31SBrooks Davis first) {
406c6ec7d31SBrooks Davis first = 0;
407c6ec7d31SBrooks Davis if (flavor != F_NETBSD6 && dflag)
40825f3fb54SBrooks Davis fprintf(fp, "/set type=dir");
409c6ec7d31SBrooks Davis else
41025f3fb54SBrooks Davis fprintf(fp, "/set type=file");
411c6ec7d31SBrooks Davis if (keys & (F_UID | F_UNAME)) {
412c6ec7d31SBrooks Davis if (keys & F_UNAME &&
413c6ec7d31SBrooks Davis (name = user_from_uid(saveuid, 1)) != NULL)
41425f3fb54SBrooks Davis fprintf(fp, " uname=%s", name);
415c6ec7d31SBrooks Davis if (keys & F_UID || (keys & F_UNAME && name == NULL))
41625f3fb54SBrooks Davis fprintf(fp, " uid=%lu", (u_long)saveuid);
417c6ec7d31SBrooks Davis }
418c6ec7d31SBrooks Davis if (keys & (F_GID | F_GNAME)) {
419c6ec7d31SBrooks Davis if (keys & F_GNAME &&
420c6ec7d31SBrooks Davis (name = group_from_gid(savegid, 1)) != NULL)
42125f3fb54SBrooks Davis fprintf(fp, " gname=%s", name);
422c6ec7d31SBrooks Davis if (keys & F_GID || (keys & F_GNAME && name == NULL))
42325f3fb54SBrooks Davis fprintf(fp, " gid=%lu", (u_long)savegid);
424c6ec7d31SBrooks Davis }
425c6ec7d31SBrooks Davis if (keys & F_MODE)
42625f3fb54SBrooks Davis fprintf(fp, " mode=%#lo", (u_long)savemode);
427c6ec7d31SBrooks Davis if (keys & F_NLINK)
42825f3fb54SBrooks Davis fprintf(fp, " nlink=1");
429c6ec7d31SBrooks Davis if (keys & F_FLAGS) {
430c6ec7d31SBrooks Davis char *str = flags_to_string(saveflags, "none");
43125f3fb54SBrooks Davis fprintf(fp, " flags=%s", str);
432c6ec7d31SBrooks Davis free(str);
433c6ec7d31SBrooks Davis }
43425f3fb54SBrooks Davis fprintf(fp, "\n");
435c6ec7d31SBrooks Davis *puid = saveuid;
436c6ec7d31SBrooks Davis *pgid = savegid;
437c6ec7d31SBrooks Davis *pmode = savemode;
438c6ec7d31SBrooks Davis *pflags = saveflags;
439c6ec7d31SBrooks Davis }
440c6ec7d31SBrooks Davis return (0);
441c6ec7d31SBrooks Davis }
442c6ec7d31SBrooks Davis
443c6ec7d31SBrooks Davis /*
444c6ec7d31SBrooks Davis * dcmp --
445c6ec7d31SBrooks Davis * used as a comparison function passed to fts_open() to control
446c6ec7d31SBrooks Davis * the order in which fts_read() returns results. We make
447c6ec7d31SBrooks Davis * directories sort after non-directories, but otherwise sort in
448c6ec7d31SBrooks Davis * strcmp() order.
449c6ec7d31SBrooks Davis *
450c6ec7d31SBrooks Davis * Keep this in sync with nodecmp() in spec.c.
451c6ec7d31SBrooks Davis */
452c6ec7d31SBrooks Davis static int
dcmp(const FTSENT * FTS_CONST * a,const FTSENT * FTS_CONST * b)453c6ec7d31SBrooks Davis dcmp(const FTSENT *FTS_CONST *a, const FTSENT *FTS_CONST *b)
454c6ec7d31SBrooks Davis {
455c6ec7d31SBrooks Davis
456c6ec7d31SBrooks Davis if (S_ISDIR((*a)->fts_statp->st_mode)) {
457c6ec7d31SBrooks Davis if (!S_ISDIR((*b)->fts_statp->st_mode))
458c6ec7d31SBrooks Davis return (1);
459c6ec7d31SBrooks Davis } else if (S_ISDIR((*b)->fts_statp->st_mode))
460c6ec7d31SBrooks Davis return (-1);
461c6ec7d31SBrooks Davis return (strcmp((*a)->fts_name, (*b)->fts_name));
462c6ec7d31SBrooks Davis }
463c6ec7d31SBrooks Davis
464c6ec7d31SBrooks Davis void
output(FILE * fp,int indent,int * offset,const char * fmt,...)46525f3fb54SBrooks Davis output(FILE *fp, int indent, int *offset, const char *fmt, ...)
466c6ec7d31SBrooks Davis {
467c6ec7d31SBrooks Davis va_list ap;
468c6ec7d31SBrooks Davis char buf[1024];
469c6ec7d31SBrooks Davis
470c6ec7d31SBrooks Davis va_start(ap, fmt);
471c6ec7d31SBrooks Davis vsnprintf(buf, sizeof(buf), fmt, ap);
472c6ec7d31SBrooks Davis va_end(ap);
473c6ec7d31SBrooks Davis
474c6ec7d31SBrooks Davis if (*offset + strlen(buf) > MAXLINELEN - 3) {
47525f3fb54SBrooks Davis fprintf(fp, " \\\n%*s", INDENTNAMELEN + indent, "");
476c6ec7d31SBrooks Davis *offset = INDENTNAMELEN + indent;
477c6ec7d31SBrooks Davis }
47825f3fb54SBrooks Davis *offset += fprintf(fp, " %s", buf) + 1;
479c6ec7d31SBrooks Davis }
480