2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)create.c 8.1 (Berkeley) 6/6/93
30 * $FreeBSD: src/usr.sbin/mtree/create.c,v 1.18.2.3 2001/01/12 19:17:18 phk Exp $
31 * $DragonFly: src/usr.sbin/mtree/create.c,v 1.5 2004/03/15 16:24:22 dillon Exp $
34 #include <sys/param.h>
60 #define INDENTNAMELEN 15
66 static u_long flags = 0xffffffff;
68 static int dsort(const FTSENT * const *, const FTSENT * const *);
69 static void output(int, int *, const char *, ...) __printflike(3, 4);
70 static int statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *,
72 static void statf(int, FTSENT *);
80 char *argv[2], host[MAXHOSTNAMELEN], dot[] = ".";
84 gethostname(host, sizeof(host));
86 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s",
87 getlogin(), host, fullpath, ctime(&clk));
91 if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
92 err(1, "line %d: fts_open", lineno);
93 while ((p = fts_read(t))) {
95 indent = p->fts_level * 4;
96 if (check_excludes(p->fts_name, p->fts_path)) {
97 fts_set(t, p, FTS_SKIP);
100 switch(p->fts_info) {
105 printf("# %s\n", p->fts_path);
106 statd(t, p, &uid, &gid, &mode, &flags);
110 if (!nflag && (p->fts_level > 0))
111 printf("%*s# %s\n", indent, "", p->fts_path);
112 printf("%*s..\n", indent, "");
119 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
129 if (sflag && keys & F_CKSUM)
130 warnx("%s checksum: %lu", fullpath, crc_total);
134 statf(int indent, FTSENT *p)
143 escaped_name = calloc(1, p->fts_namelen * 4 + 1);
144 if (escaped_name == NULL)
145 errx(1, "statf(): calloc() failed");
146 strvis(escaped_name, p->fts_name, VIS_WHITE | VIS_OCTAL);
148 if (iflag || S_ISDIR(p->fts_statp->st_mode))
149 offset = printf("%*s%s", indent, "", escaped_name);
151 offset = printf("%*s %s", indent, "", escaped_name);
155 if (offset > (INDENTNAMELEN + indent))
158 offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
160 if (!S_ISREG(p->fts_statp->st_mode) && !dflag)
161 output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode));
162 if (p->fts_statp->st_uid != uid) {
163 if (keys & F_UNAME) {
164 if ((pw = getpwuid(p->fts_statp->st_uid)) != NULL) {
165 output(indent, &offset, "uname=%s", pw->pw_name);
168 "line %d: could not get uname for uid=%u",
169 lineno, p->fts_statp->st_uid);
173 output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
175 if (p->fts_statp->st_gid != gid) {
176 if (keys & F_GNAME) {
177 if ((gr = getgrgid(p->fts_statp->st_gid)) != NULL) {
178 output(indent, &offset, "gname=%s", gr->gr_name);
181 "line %d: could not get gname for gid=%u",
182 lineno, p->fts_statp->st_gid);
186 output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
188 if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
189 output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS);
190 if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
191 output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
193 output(indent, &offset, "size=%jd",
194 (uintmax_t)p->fts_statp->st_size);
196 output(indent, &offset, "time=%ld.%ld",
197 p->fts_statp->st_mtimespec.tv_sec,
198 p->fts_statp->st_mtimespec.tv_nsec);
199 if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
200 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
202 err(1, "line %d: %s", lineno, p->fts_accpath);
204 output(indent, &offset, "cksum=%lu", val);
207 if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
208 char *digest, buf[33];
210 digest = MD5File(p->fts_accpath, buf);
212 err(1, "line %d: %s", lineno, p->fts_accpath);
214 output(indent, &offset, "md5digest=%s", digest);
219 if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
220 char *digest, buf[41];
222 digest = SHA1_File(p->fts_accpath, buf);
224 err(1, "line %d: %s", lineno, p->fts_accpath);
226 output(indent, &offset, "sha1digest=%s", digest);
231 if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
232 char *digest, buf[41];
234 digest = RIPEMD160_File(p->fts_accpath, buf);
236 err(1, "line %d: %s", lineno, p->fts_accpath);
238 output(indent, &offset, "ripemd160digest=%s", digest);
242 if (keys & F_SLINK &&
243 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
244 output(indent, &offset, "link=%s", rlink(p->fts_accpath));
245 if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
246 fflags = flags_to_string(p->fts_statp->st_flags);
247 output(indent, &offset, "flags=%s", fflags);
255 #define MAXMODE MBITS + 1
260 statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode,
270 gid_t savegid = *pgid;
271 uid_t saveuid = *puid;
272 mode_t savemode = *pmode;
273 u_long saveflags = *pflags;
274 u_short maxgid, maxuid, maxmode, maxflags;
275 u_short g[MAXGID], u[MAXUID], m[MAXMODE], f[MAXFLAGS];
277 static int first = 1;
279 if ((p = fts_children(t, 0)) == NULL) {
281 err(1, "line %d: %s", lineno, RP(parent));
290 maxuid = maxgid = maxmode = maxflags = 0;
291 for (; p; p = p->fts_link) {
292 if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) {
293 smode = p->fts_statp->st_mode & MBITS;
294 if (smode < MAXMODE && ++m[smode] > maxmode) {
298 sgid = p->fts_statp->st_gid;
299 if (sgid < MAXGID && ++g[sgid] > maxgid) {
303 suid = p->fts_statp->st_uid;
304 if (suid < MAXUID && ++u[suid] > maxuid) {
311 * note that the below will break when file flags
312 * are extended beyond the first 4 bytes of each
313 * half word of the flags
315 #define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0))
316 sflags = p->fts_statp->st_flags;
317 if (FLAGS2IDX(sflags) < MAXFLAGS &&
318 ++f[FLAGS2IDX(sflags)] > maxflags) {
320 maxflags = f[FLAGS2IDX(sflags)];
325 * If the /set record is the same as the last one we do not need to output
326 * a new one. So first we check to see if anything changed. Note that we
327 * always output a /set record for the first directory.
329 if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) ||
330 (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) ||
331 ((keys & F_MODE) && (*pmode != savemode)) ||
332 ((keys & F_FLAGS) && (*pflags != saveflags)) ||
336 printf("/set type=dir");
338 printf("/set type=file");
339 if (keys & F_UNAME) {
340 if ((pw = getpwuid(saveuid)) != NULL)
341 printf(" uname=%s", pw->pw_name);
344 "line %d: could not get uname for uid=%u",
348 printf(" uid=%lu", (u_long)saveuid);
349 if (keys & F_GNAME) {
350 if ((gr = getgrgid(savegid)) != NULL)
351 printf(" gname=%s", gr->gr_name);
354 "line %d: could not get gname for gid=%u",
358 printf(" gid=%lu", (u_long)savegid);
360 printf(" mode=%#o", savemode);
363 if (keys & F_FLAGS) {
364 fflags = flags_to_string(saveflags);
365 printf(" flags=%s", fflags);
378 dsort(const FTSENT * const *a, const FTSENT * const *b)
381 if (S_ISDIR((*a)->fts_statp->st_mode)) {
382 if (!S_ISDIR((*b)->fts_statp->st_mode))
384 } else if (S_ISDIR((*b)->fts_statp->st_mode))
386 return (strcmp((*a)->fts_name, (*b)->fts_name));
390 output(int indent, int *offset, const char *fmt, ...)
396 vsnprintf(buf, sizeof(buf), fmt, ap);
399 if (*offset + strlen(buf) > MAXLINELEN - 3) {
400 printf(" \\\n%*s", INDENTNAMELEN + indent, "");
401 *offset = INDENTNAMELEN + indent;
403 *offset += printf(" %s", buf) + 1;