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. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)create.c 8.1 (Berkeley) 6/6/93
34 * $FreeBSD: src/usr.sbin/mtree/create.c,v 1.18.2.3 2001/01/12 19:17:18 phk Exp $
35 * $DragonFly: src/usr.sbin/mtree/create.c,v 1.5 2004/03/15 16:24:22 dillon Exp $
38 #include <sys/param.h>
64 #define INDENTNAMELEN 15
70 static u_long flags
= 0xffffffff;
72 static int dsort(const FTSENT
* const *, const FTSENT
* const *);
73 static void output(int, int *, const char *, ...);
74 static int statd(FTS
*, FTSENT
*, uid_t
*, gid_t
*, mode_t
*,
76 static void statf(int, FTSENT
*);
84 char *argv
[2], host
[MAXHOSTNAMELEN
], dot
[] = ".";
88 gethostname(host
, sizeof(host
));
90 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s",
91 getlogin(), host
, fullpath
, ctime(&clk
));
95 if ((t
= fts_open(argv
, ftsoptions
, dsort
)) == NULL
)
96 err(1, "line %d: fts_open", lineno
);
97 while ((p
= fts_read(t
))) {
99 indent
= p
->fts_level
* 4;
100 if (check_excludes(p
->fts_name
, p
->fts_path
)) {
101 fts_set(t
, p
, FTS_SKIP
);
104 switch(p
->fts_info
) {
109 printf("# %s\n", p
->fts_path
);
110 statd(t
, p
, &uid
, &gid
, &mode
, &flags
);
114 if (!nflag
&& (p
->fts_level
> 0))
115 printf("%*s# %s\n", indent
, "", p
->fts_path
);
116 printf("%*s..\n", indent
, "");
123 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
133 if (sflag
&& keys
& F_CKSUM
)
134 warnx("%s checksum: %lu", fullpath
, crc_total
);
138 statf(int indent
, FTSENT
*p
)
147 escaped_name
= calloc(1, p
->fts_namelen
* 4 + 1);
148 if (escaped_name
== NULL
)
149 errx(1, "statf(): calloc() failed");
150 strvis(escaped_name
, p
->fts_name
, VIS_WHITE
| VIS_OCTAL
);
152 if (iflag
|| S_ISDIR(p
->fts_statp
->st_mode
))
153 offset
= printf("%*s%s", indent
, "", escaped_name
);
155 offset
= printf("%*s %s", indent
, "", escaped_name
);
159 if (offset
> (INDENTNAMELEN
+ indent
))
162 offset
+= printf("%*s", (INDENTNAMELEN
+ indent
) - offset
, "");
164 if (!S_ISREG(p
->fts_statp
->st_mode
) && !dflag
)
165 output(indent
, &offset
, "type=%s", inotype(p
->fts_statp
->st_mode
));
166 if (p
->fts_statp
->st_uid
!= uid
) {
167 if (keys
& F_UNAME
) {
168 if ((pw
= getpwuid(p
->fts_statp
->st_uid
)) != NULL
) {
169 output(indent
, &offset
, "uname=%s", pw
->pw_name
);
172 "line %d: could not get uname for uid=%u",
173 lineno
, p
->fts_statp
->st_uid
);
177 output(indent
, &offset
, "uid=%u", p
->fts_statp
->st_uid
);
179 if (p
->fts_statp
->st_gid
!= gid
) {
180 if (keys
& F_GNAME
) {
181 if ((gr
= getgrgid(p
->fts_statp
->st_gid
)) != NULL
) {
182 output(indent
, &offset
, "gname=%s", gr
->gr_name
);
185 "line %d: could not get gname for gid=%u",
186 lineno
, p
->fts_statp
->st_gid
);
190 output(indent
, &offset
, "gid=%u", p
->fts_statp
->st_gid
);
192 if (keys
& F_MODE
&& (p
->fts_statp
->st_mode
& MBITS
) != mode
)
193 output(indent
, &offset
, "mode=%#o", p
->fts_statp
->st_mode
& MBITS
);
194 if (keys
& F_NLINK
&& p
->fts_statp
->st_nlink
!= 1)
195 output(indent
, &offset
, "nlink=%u", p
->fts_statp
->st_nlink
);
197 output(indent
, &offset
, "size=%qd", p
->fts_statp
->st_size
);
199 output(indent
, &offset
, "time=%ld.%ld",
200 p
->fts_statp
->st_mtimespec
.tv_sec
,
201 p
->fts_statp
->st_mtimespec
.tv_nsec
);
202 if (keys
& F_CKSUM
&& S_ISREG(p
->fts_statp
->st_mode
)) {
203 if ((fd
= open(p
->fts_accpath
, O_RDONLY
, 0)) < 0 ||
205 err(1, "line %d: %s", lineno
, p
->fts_accpath
);
207 output(indent
, &offset
, "cksum=%lu", val
);
210 if (keys
& F_MD5
&& S_ISREG(p
->fts_statp
->st_mode
)) {
211 char *digest
, buf
[33];
213 digest
= MD5File(p
->fts_accpath
, buf
);
215 err(1, "line %d: %s", lineno
, p
->fts_accpath
);
217 output(indent
, &offset
, "md5digest=%s", digest
);
222 if (keys
& F_SHA1
&& S_ISREG(p
->fts_statp
->st_mode
)) {
223 char *digest
, buf
[41];
225 digest
= SHA1_File(p
->fts_accpath
, buf
);
227 err(1, "line %d: %s", lineno
, p
->fts_accpath
);
229 output(indent
, &offset
, "sha1digest=%s", digest
);
234 if (keys
& F_RMD160
&& S_ISREG(p
->fts_statp
->st_mode
)) {
235 char *digest
, buf
[41];
237 digest
= RIPEMD160_File(p
->fts_accpath
, buf
);
239 err(1, "line %d: %s", lineno
, p
->fts_accpath
);
241 output(indent
, &offset
, "ripemd160digest=%s", digest
);
245 if (keys
& F_SLINK
&&
246 (p
->fts_info
== FTS_SL
|| p
->fts_info
== FTS_SLNONE
))
247 output(indent
, &offset
, "link=%s", rlink(p
->fts_accpath
));
248 if (keys
& F_FLAGS
&& p
->fts_statp
->st_flags
!= flags
) {
249 fflags
= flags_to_string(p
->fts_statp
->st_flags
);
250 output(indent
, &offset
, "flags=%s", fflags
);
258 #define MAXMODE MBITS + 1
263 statd(FTS
*t
, FTSENT
*parent
, uid_t
*puid
, gid_t
*pgid
, mode_t
*pmode
,
273 gid_t savegid
= *pgid
;
274 uid_t saveuid
= *puid
;
275 mode_t savemode
= *pmode
;
276 u_long saveflags
= *pflags
;
277 u_short maxgid
, maxuid
, maxmode
, maxflags
;
278 u_short g
[MAXGID
], u
[MAXUID
], m
[MAXMODE
], f
[MAXFLAGS
];
280 static int first
= 1;
282 if ((p
= fts_children(t
, 0)) == NULL
) {
284 err(1, "line %d: %s", lineno
, RP(parent
));
293 maxuid
= maxgid
= maxmode
= maxflags
= 0;
294 for (; p
; p
= p
->fts_link
) {
295 if (!dflag
|| (dflag
&& S_ISDIR(p
->fts_statp
->st_mode
))) {
296 smode
= p
->fts_statp
->st_mode
& MBITS
;
297 if (smode
< MAXMODE
&& ++m
[smode
] > maxmode
) {
301 sgid
= p
->fts_statp
->st_gid
;
302 if (sgid
< MAXGID
&& ++g
[sgid
] > maxgid
) {
306 suid
= p
->fts_statp
->st_uid
;
307 if (suid
< MAXUID
&& ++u
[suid
] > maxuid
) {
314 * note that the below will break when file flags
315 * are extended beyond the first 4 bytes of each
316 * half word of the flags
318 #define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0))
319 sflags
= p
->fts_statp
->st_flags
;
320 if (FLAGS2IDX(sflags
) < MAXFLAGS
&&
321 ++f
[FLAGS2IDX(sflags
)] > maxflags
) {
323 maxflags
= f
[FLAGS2IDX(sflags
)];
328 * If the /set record is the same as the last one we do not need to output
329 * a new one. So first we check to see if anything changed. Note that we
330 * always output a /set record for the first directory.
332 if ((((keys
& F_UNAME
) | (keys
& F_UID
)) && (*puid
!= saveuid
)) ||
333 (((keys
& F_GNAME
) | (keys
& F_GID
)) && (*pgid
!= savegid
)) ||
334 ((keys
& F_MODE
) && (*pmode
!= savemode
)) ||
335 ((keys
& F_FLAGS
) && (*pflags
!= saveflags
)) ||
339 printf("/set type=dir");
341 printf("/set type=file");
342 if (keys
& F_UNAME
) {
343 if ((pw
= getpwuid(saveuid
)) != NULL
)
344 printf(" uname=%s", pw
->pw_name
);
347 "line %d: could not get uname for uid=%u",
351 printf(" uid=%lu", (u_long
)saveuid
);
352 if (keys
& F_GNAME
) {
353 if ((gr
= getgrgid(savegid
)) != NULL
)
354 printf(" gname=%s", gr
->gr_name
);
357 "line %d: could not get gname for gid=%u",
361 printf(" gid=%lu", (u_long
)savegid
);
363 printf(" mode=%#o", savemode
);
366 if (keys
& F_FLAGS
) {
367 fflags
= flags_to_string(saveflags
);
368 printf(" flags=%s", fflags
);
381 dsort(const FTSENT
* const *a
, const FTSENT
* const *b
)
384 if (S_ISDIR((*a
)->fts_statp
->st_mode
)) {
385 if (!S_ISDIR((*b
)->fts_statp
->st_mode
))
387 } else if (S_ISDIR((*b
)->fts_statp
->st_mode
))
389 return (strcmp((*a
)->fts_name
, (*b
)->fts_name
));
393 output(int indent
, int *offset
, const char *fmt
, ...)
399 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
402 if (*offset
+ strlen(buf
) > MAXLINELEN
- 3) {
403 printf(" \\\n%*s", INDENTNAMELEN
+ indent
, "");
404 *offset
= INDENTNAMELEN
+ indent
;
406 *offset
+= printf(" %s", buf
) + 1;