1 /* $NetBSD: create.c,v 1.69 2013/02/03 19:15:17 christos Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
39 static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93";
41 __RCSID("$NetBSD: create.c,v 1.69 2013/02/03 19:15:17 christos Exp $");
45 #include <sys/param.h>
48 #if ! HAVE_NBTOOL_CONFIG_H
78 #define INDENTNAMELEN 15
87 #define FTS_CONST const
92 static int dcmp(const FTSENT *FTS_CONST *, const FTSENT *FTS_CONST *);
93 static void output(int, int *, const char *, ...)
94 __attribute__((__format__(__printf__, 3, 4)));
95 static int statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
96 static void statf(int, FTSENT *);
104 char host[MAXHOSTNAMELEN + 1];
114 gethostname(host, sizeof(host));
115 host[sizeof(host) - 1] = '\0';
116 if ((user = getlogin()) == NULL) {
118 user = (pw = getpwuid(getuid())) == NULL ? pw->pw_name :
124 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n"
126 user, host, fullpath, ctime(&clocktime));
128 if ((t = fts_open(argv, ftsoptions, dcmp)) == NULL)
129 mtree_err("fts_open: %s", strerror(errno));
130 while ((p = fts_read(t)) != NULL) {
132 indent = p->fts_level * 4;
133 if (check_excludes(p->fts_name, p->fts_path)) {
134 fts_set(t, p, FTS_SKIP);
137 if (!find_only(p->fts_path)) {
138 fts_set(t, p, FTS_SKIP);
141 switch(p->fts_info) {
146 printf("# %s\n", p->fts_path);
147 statd(t, p, &uid, &gid, &mode, &flags);
151 if (p->fts_level > 0)
153 printf("%*s# %s\n", indent, "",
155 if (p->fts_level > 0 || flavor == F_FREEBSD9) {
156 printf("%*s..\n", indent, "");
165 p->fts_path, strerror(p->fts_errno));
175 if (sflag && keys & F_CKSUM)
176 mtree_err("%s checksum: %u", fullpath, crc_total);
180 statf(int indent, FTSENT *p)
184 const char *name = NULL;
185 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
189 offset = printf("%*s%s%s", indent, "",
190 S_ISDIR(p->fts_statp->st_mode) ? "" : " ", vispath(p->fts_name));
192 if (offset > (INDENTNAMELEN + indent))
195 offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
197 if (!S_ISREG(p->fts_statp->st_mode) && (flavor == F_NETBSD6 || !dflag))
198 output(indent, &offset, "type=%s",
199 inotype(p->fts_statp->st_mode));
200 if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) {
201 if (keys & F_UNAME &&
202 (name = user_from_uid(p->fts_statp->st_uid, 1)) != NULL)
203 output(indent, &offset, "uname=%s", name);
204 if (keys & F_UID || (keys & F_UNAME && name == NULL))
205 output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
207 if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) {
208 if (keys & F_GNAME &&
209 (name = group_from_gid(p->fts_statp->st_gid, 1)) != NULL)
210 output(indent, &offset, "gname=%s", name);
211 if (keys & F_GID || (keys & F_GNAME && name == NULL))
212 output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
214 if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
215 output(indent, &offset, "mode=%#o",
216 p->fts_statp->st_mode & MBITS);
218 (S_ISBLK(p->fts_statp->st_mode) || S_ISCHR(p->fts_statp->st_mode)))
219 output(indent, &offset, "device=%#llx",
220 (long long)p->fts_statp->st_rdev);
221 if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
222 output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
224 (flavor != F_NETBSD6 || S_ISREG(p->fts_statp->st_mode)))
225 output(indent, &offset, "size=%lld",
226 (long long)p->fts_statp->st_size);
228 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
229 output(indent, &offset, "time=%ld.%09ld",
230 (long)p->fts_statp->st_mtimespec.tv_sec,
231 p->fts_statp->st_mtimespec.tv_nsec);
233 output(indent, &offset, "time=%ld.%09ld",
234 (long)p->fts_statp->st_mtime, (long)0);
236 if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
237 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
239 mtree_err("%s: %s", p->fts_accpath, strerror(errno));
241 output(indent, &offset, "cksum=%lu", (long)val);
244 if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
245 if ((digestbuf = MD5File(p->fts_accpath, NULL)) == NULL)
246 mtree_err("%s: MD5File failed: %s", p->fts_accpath,
248 output(indent, &offset, "%s=%s", MD5KEY, digestbuf);
251 #endif /* ! NO_MD5 */
253 if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
254 if ((digestbuf = RMD160File(p->fts_accpath, NULL)) == NULL)
255 mtree_err("%s: RMD160File failed: %s", p->fts_accpath,
257 output(indent, &offset, "%s=%s", RMD160KEY, digestbuf);
260 #endif /* ! NO_RMD160 */
262 if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
263 if ((digestbuf = SHA1File(p->fts_accpath, NULL)) == NULL)
264 mtree_err("%s: SHA1File failed: %s", p->fts_accpath,
266 output(indent, &offset, "%s=%s", SHA1KEY, digestbuf);
269 #endif /* ! NO_SHA1 */
271 if (keys & F_SHA256 && S_ISREG(p->fts_statp->st_mode)) {
272 if ((digestbuf = SHA256_File(p->fts_accpath, NULL)) == NULL)
273 mtree_err("%s: SHA256_File failed: %s", p->fts_accpath,
275 output(indent, &offset, "%s=%s", SHA256KEY, digestbuf);
278 #ifdef SHA384_BLOCK_LENGTH
279 if (keys & F_SHA384 && S_ISREG(p->fts_statp->st_mode)) {
280 if ((digestbuf = SHA384_File(p->fts_accpath, NULL)) == NULL)
281 mtree_err("%s: SHA384_File failed: %s", p->fts_accpath,
283 output(indent, &offset, "%s=%s", SHA384KEY, digestbuf);
287 if (keys & F_SHA512 && S_ISREG(p->fts_statp->st_mode)) {
288 if ((digestbuf = SHA512_File(p->fts_accpath, NULL)) == NULL)
289 mtree_err("%s: SHA512_File failed: %s", p->fts_accpath,
291 output(indent, &offset, "%s=%s", SHA512KEY, digestbuf);
294 #endif /* ! NO_SHA2 */
295 if (keys & F_SLINK &&
296 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
297 output(indent, &offset, "link=%s",
298 vispath(rlink(p->fts_accpath)));
299 #if HAVE_STRUCT_STAT_ST_FLAGS
300 if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
301 char *str = flags_to_string(p->fts_statp->st_flags, "none");
302 output(indent, &offset, "flags=%s", str);
310 * FLAGS2INDEX will fail once the user and system settable bits need more
311 * than one byte, respectively.
313 #define FLAGS2INDEX(x) (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
315 #define MTREE_MAXGID 5000
316 #define MTREE_MAXUID 5000
317 #define MTREE_MAXMODE (MBITS + 1)
318 #if HAVE_STRUCT_STAT_ST_FLAGS
319 #define MTREE_MAXFLAGS (FLAGS2INDEX(CH_MASK) + 1) /* 1808 */
321 #define MTREE_MAXFLAGS 1
323 #define MTREE_MAXS 16
326 statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
334 const char *name = NULL;
339 u_short maxgid, maxuid, maxmode, maxflags;
340 u_short g[MTREE_MAXGID], u[MTREE_MAXUID],
341 m[MTREE_MAXMODE], f[MTREE_MAXFLAGS];
342 static int first = 1;
348 if ((p = fts_children(t, 0)) == NULL) {
350 mtree_err("%s: %s", RP(parent), strerror(errno));
354 memset(g, 0, sizeof(g));
355 memset(u, 0, sizeof(u));
356 memset(m, 0, sizeof(m));
357 memset(f, 0, sizeof(f));
359 maxuid = maxgid = maxmode = maxflags = 0;
360 for (; p; p = p->fts_link) {
361 if (flavor == F_NETBSD6 || !dflag ||
362 (dflag && S_ISDIR(p->fts_statp->st_mode))) {
363 smode = p->fts_statp->st_mode & MBITS;
364 if (smode < MTREE_MAXMODE && ++m[smode] > maxmode) {
368 sgid = p->fts_statp->st_gid;
369 if (sgid < MTREE_MAXGID && ++g[sgid] > maxgid) {
373 suid = p->fts_statp->st_uid;
374 if (suid < MTREE_MAXUID && ++u[suid] > maxuid) {
379 #if HAVE_STRUCT_STAT_ST_FLAGS
380 sflags = FLAGS2INDEX(p->fts_statp->st_flags);
381 if (sflags < MTREE_MAXFLAGS && ++f[sflags] > maxflags) {
382 saveflags = p->fts_statp->st_flags;
383 maxflags = f[sflags];
389 * If the /set record is the same as the last one we do not need to
390 * output a new one. So first we check to see if anything changed.
391 * Note that we always output a /set record for the first directory.
393 if (((keys & (F_UNAME | F_UID)) && (*puid != saveuid)) ||
394 ((keys & (F_GNAME | F_GID)) && (*pgid != savegid)) ||
395 ((keys & F_MODE) && (*pmode != savemode)) ||
396 ((keys & F_FLAGS) && (*pflags != saveflags)) ||
399 if (flavor != F_NETBSD6 && dflag)
400 printf("/set type=dir");
402 printf("/set type=file");
403 if (keys & (F_UID | F_UNAME)) {
404 if (keys & F_UNAME &&
405 (name = user_from_uid(saveuid, 1)) != NULL)
406 printf(" uname=%s", name);
407 if (keys & F_UID || (keys & F_UNAME && name == NULL))
408 printf(" uid=%lu", (u_long)saveuid);
410 if (keys & (F_GID | F_GNAME)) {
411 if (keys & F_GNAME &&
412 (name = group_from_gid(savegid, 1)) != NULL)
413 printf(" gname=%s", name);
414 if (keys & F_GID || (keys & F_GNAME && name == NULL))
415 printf(" gid=%lu", (u_long)savegid);
418 printf(" mode=%#lo", (u_long)savemode);
421 if (keys & F_FLAGS) {
422 char *str = flags_to_string(saveflags, "none");
423 printf(" flags=%s", str);
437 * used as a comparison function passed to fts_open() to control
438 * the order in which fts_read() returns results. We make
439 * directories sort after non-directories, but otherwise sort in
442 * Keep this in sync with nodecmp() in spec.c.
445 dcmp(const FTSENT *FTS_CONST *a, const FTSENT *FTS_CONST *b)
448 if (S_ISDIR((*a)->fts_statp->st_mode)) {
449 if (!S_ISDIR((*b)->fts_statp->st_mode))
451 } else if (S_ISDIR((*b)->fts_statp->st_mode))
453 return (strcmp((*a)->fts_name, (*b)->fts_name));
457 output(int indent, int *offset, const char *fmt, ...)
463 vsnprintf(buf, sizeof(buf), fmt, ap);
466 if (*offset + strlen(buf) > MAXLINELEN - 3) {
467 printf(" \\\n%*s", INDENTNAMELEN + indent, "");
468 *offset = INDENTNAMELEN + indent;
470 *offset += printf(" %s", buf) + 1;