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
32 static char sccsid
[] = "@(#)create.c 8.1 (Berkeley) 6/6/93";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD: src/usr.sbin/mtree/create.c,v 1.37 2005/03/29 11:44:17 tobez Exp $");
38 #include <sys/param.h>
59 #endif /* !__APPLE__ */
70 #include "commoncrypto.h"
71 #endif /* __APPLE__ */
73 #define INDENTNAMELEN 15
79 static u_long flags
= 0xffffffff;
80 static char *xattrs
= kNone
;
81 static char *acl
= kNone
;
83 static int dsort(const FTSENT
**, const FTSENT
**);
84 static void output(int, int *, const char *, ...) __printflike(3, 4);
85 static int statd(FTS
*, FTSENT
*, uid_t
*, gid_t
*, mode_t
*, u_long
*, char **, char **);
86 static void statf(int, FTSENT
*);
94 char *argv
[2], host
[MAXHOSTNAMELEN
];
101 (void)gethostname(host
, sizeof(host
));
103 "#\t user: %s\n#\tmachine: %s\n",
106 "#\t tree: %s\n#\t date: %s",
107 fullpath
, ctime(&cl
));
112 if ((t
= fts_open(argv
, ftsoptions
, dsort
)) == NULL
)
113 err(1, "fts_open()");
114 while ((p
= fts_read(t
))) {
116 indent
= p
->fts_level
* 4;
117 if (check_excludes(p
->fts_name
, p
->fts_path
)) {
118 fts_set(t
, p
, FTS_SKIP
);
121 switch(p
->fts_info
) {
126 path
= escape_path(p
->fts_path
);
127 (void)printf("# %s\n", path
);
130 statd(t
, p
, &uid
, &gid
, &mode
, &flags
, &xattrs
, &acl
);
134 if (!nflag
&& (p
->fts_level
> 0)) {
135 path
= escape_path(p
->fts_path
);
136 (void)printf("%*s# %s\n", indent
, "", path
);
139 (void)printf("%*s..\n", indent
, "");
146 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
156 if (sflag
&& keys
& F_CKSUM
)
157 warnx("%s checksum: %lu", fullpath
, (unsigned long)crc_total
);
161 statf(int indent
, FTSENT
*p
)
171 escaped_name
= calloc(1, p
->fts_namelen
* 4 + 1);
172 if (escaped_name
== NULL
)
173 errx(1, "statf(): calloc() failed");
174 strvis(escaped_name
, p
->fts_name
, VIS_WHITE
| VIS_OCTAL
| VIS_GLOB
);
176 if (iflag
|| S_ISDIR(p
->fts_statp
->st_mode
))
177 offset
= printf("%*s%s", indent
, "", escaped_name
);
179 offset
= printf("%*s %s", indent
, "", escaped_name
);
183 if (offset
> (INDENTNAMELEN
+ indent
))
186 offset
+= printf("%*s", (INDENTNAMELEN
+ indent
) - offset
, "");
188 if (!S_ISREG(p
->fts_statp
->st_mode
) && !dflag
)
189 output(indent
, &offset
, "type=%s", inotype(p
->fts_statp
->st_mode
));
190 if (p
->fts_statp
->st_uid
!= uid
) {
191 if (keys
& F_UNAME
) {
192 pw
= getpwuid(p
->fts_statp
->st_uid
);
194 output(indent
, &offset
, "uname=%s", pw
->pw_name
);
196 warnx("Could not get uname for uid=%u",
197 p
->fts_statp
->st_uid
);
200 "Could not get uname for uid=%u",
201 p
->fts_statp
->st_uid
);
204 output(indent
, &offset
, "uid=%u", p
->fts_statp
->st_uid
);
206 if (p
->fts_statp
->st_gid
!= gid
) {
207 if (keys
& F_GNAME
) {
208 gr
= getgrgid(p
->fts_statp
->st_gid
);
210 output(indent
, &offset
, "gname=%s", gr
->gr_name
);
212 warnx("Could not get gname for gid=%u",
213 p
->fts_statp
->st_gid
);
216 "Could not get gname for gid=%u",
217 p
->fts_statp
->st_gid
);
220 output(indent
, &offset
, "gid=%u", p
->fts_statp
->st_gid
);
222 if (keys
& F_MODE
&& (p
->fts_statp
->st_mode
& MBITS
) != mode
)
223 output(indent
, &offset
, "mode=%#o", p
->fts_statp
->st_mode
& MBITS
);
224 if (keys
& F_NLINK
&& p
->fts_statp
->st_nlink
!= 1)
225 output(indent
, &offset
, "nlink=%u", p
->fts_statp
->st_nlink
);
227 output(indent
, &offset
, "size=%jd",
228 (intmax_t)p
->fts_statp
->st_size
);
230 output(indent
, &offset
, "time=%ld.%09ld",
231 (long)p
->fts_statp
->st_mtimespec
.tv_sec
,
232 p
->fts_statp
->st_mtimespec
.tv_nsec
);
233 if (keys
& F_CKSUM
&& S_ISREG(p
->fts_statp
->st_mode
)) {
234 if ((fd
= open(p
->fts_accpath
, O_RDONLY
, 0)) < 0 ||
236 err(1, "%s", p
->fts_accpath
);
238 output(indent
, &offset
, "cksum=%lu", (unsigned long)val
);
241 if (keys
& F_MD5
&& S_ISREG(p
->fts_statp
->st_mode
)) {
242 char *digest
, buf
[33];
244 digest
= MD5File(p
->fts_accpath
, buf
);
246 err(1, "%s", p
->fts_accpath
);
247 output(indent
, &offset
, "md5digest=%s", digest
);
249 #endif /* ENABLE_MD5 */
251 if (keys
& F_SHA1
&& S_ISREG(p
->fts_statp
->st_mode
)) {
252 char *digest
, buf
[41];
254 digest
= SHA1_File(p
->fts_accpath
, buf
);
256 err(1, "%s", p
->fts_accpath
);
257 output(indent
, &offset
, "sha1digest=%s", digest
);
259 #endif /* ENABLE_SHA1 */
261 if (keys
& F_RMD160
&& S_ISREG(p
->fts_statp
->st_mode
)) {
262 char *digest
, buf
[41];
264 digest
= RIPEMD160_File(p
->fts_accpath
, buf
);
266 err(1, "%s", p
->fts_accpath
);
267 output(indent
, &offset
, "ripemd160digest=%s", digest
);
269 #endif /* ENABLE_RMD160 */
271 if (keys
& F_SHA256
&& S_ISREG(p
->fts_statp
->st_mode
)) {
272 char *digest
, buf
[kSHA256NullTerminatedBuffLen
];
274 digest
= SHA256_File(p
->fts_accpath
, buf
);
276 err(1, "%s", p
->fts_accpath
);
277 output(indent
, &offset
, "sha256digest=%s", digest
);
279 #endif /* ENABLE_SHA256 */
280 if (keys
& F_SLINK
&&
281 (p
->fts_info
== FTS_SL
|| p
->fts_info
== FTS_SLNONE
)) {
282 char visbuf
[MAXPATHLEN
* 4];
283 char *s
= rlink(p
->fts_accpath
);
284 strvis(visbuf
, s
, VIS_WHITE
| VIS_OCTAL
);
285 output(indent
, &offset
, "link=%s", visbuf
);
287 if (keys
& F_FLAGS
&& p
->fts_statp
->st_flags
!= flags
) {
288 fflags
= flags_to_string(p
->fts_statp
->st_flags
);
289 output(indent
, &offset
, "flags=%s", fflags
);
292 if (keys
& F_BTIME
) {
293 output(indent
, &offset
, "btime=%ld.%09ld",
294 p
->fts_statp
->st_birthtimespec
.tv_sec
,
295 p
->fts_statp
->st_birthtimespec
.tv_nsec
);
297 // only check access time on regular files, as traversing a folder will update its access time
298 if (keys
& F_ATIME
&& S_ISREG(p
->fts_statp
->st_mode
)) {
299 output(indent
, &offset
, "atime=%ld.%09ld",
300 p
->fts_statp
->st_atimespec
.tv_sec
,
301 p
->fts_statp
->st_atimespec
.tv_nsec
);
303 if (keys
& F_CTIME
) {
304 output(indent
, &offset
, "ctime=%ld.%09ld",
305 p
->fts_statp
->st_ctimespec
.tv_sec
,
306 p
->fts_statp
->st_ctimespec
.tv_nsec
);
308 // date added to parent folder is only supported for files and directories
309 if (keys
& F_PTIME
&& (S_ISREG(p
->fts_statp
->st_mode
) ||
310 S_ISDIR(p
->fts_statp
->st_mode
))) {
312 struct timespec ptimespec
= ptime(p
->fts_accpath
, &supported
);
314 output(indent
, &offset
, "ptime=%ld.%09ld",
319 if (keys
& F_XATTRS
) {
320 char *digest
, buf
[kSHA256NullTerminatedBuffLen
];
322 digest
= SHA256_Path_XATTRs(p
->fts_accpath
, buf
);
323 if (digest
&& (strcmp(digest
, xattrs
) != 0)) {
324 output(indent
, &offset
, "xattrsdigest=%s", digest
);
327 if (keys
& F_INODE
) {
328 output(indent
, &offset
, "inode=%llu", p
->fts_statp
->st_ino
);
331 char *digest
, buf
[kSHA256NullTerminatedBuffLen
];
333 digest
= SHA256_Path_ACL(p
->fts_accpath
, buf
);
334 if (digest
&& (strcmp(digest
, acl
) != 0)) {
335 output(indent
, &offset
, "acldigest=%s", digest
);
344 #define MAXMODE MBITS + 1
349 statd(FTS
*t
, FTSENT
*parent
, uid_t
*puid
, gid_t
*pgid
, mode_t
*pmode
, u_long
*pflags
, char **pxattrs
, char **pacl
)
358 gid_t savegid
= *pgid
;
359 uid_t saveuid
= *puid
;
360 mode_t savemode
= *pmode
;
361 u_long saveflags
= *pflags
;
362 char *savexattrs
= *pxattrs
;
363 char *saveacl
= *pacl
;
364 u_short maxgid
, maxuid
, maxmode
, maxflags
;
365 u_short g
[MAXGID
], u
[MAXUID
], m
[MAXMODE
], f
[MAXFLAGS
];
367 static int first
= 1;
369 if ((p
= fts_children(t
, 0)) == NULL
) {
371 err(1, "%s", RP(parent
));
380 maxuid
= maxgid
= maxmode
= maxflags
= 0;
381 for (; p
; p
= p
->fts_link
) {
382 if (!dflag
|| (dflag
&& S_ISDIR(p
->fts_statp
->st_mode
))) {
383 smode
= p
->fts_statp
->st_mode
& MBITS
;
384 if (smode
< MAXMODE
&& ++m
[smode
] > maxmode
) {
388 sgid
= p
->fts_statp
->st_gid
;
389 if (sgid
< MAXGID
&& ++g
[sgid
] > maxgid
) {
393 suid
= p
->fts_statp
->st_uid
;
394 if (suid
< MAXUID
&& ++u
[suid
] > maxuid
) {
401 * note that we don't count the most common xattr/acl digest
402 * so set will always the default value (none)
407 * note that the below will break when file flags
408 * are extended beyond the first 4 bytes of each
409 * half word of the flags
411 #define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0))
412 sflags
= p
->fts_statp
->st_flags
;
413 if (FLAGS2IDX(sflags
) < MAXFLAGS
&&
414 ++f
[FLAGS2IDX(sflags
)] > maxflags
) {
416 maxflags
= f
[FLAGS2IDX(sflags
)];
421 * If the /set record is the same as the last one we do not need to output
422 * a new one. So first we check to see if anything changed. Note that we
423 * always output a /set record for the first directory.
425 if ((((keys
& F_UNAME
) | (keys
& F_UID
)) && (*puid
!= saveuid
)) ||
426 (((keys
& F_GNAME
) | (keys
& F_GID
)) && (*pgid
!= savegid
)) ||
427 ((keys
& F_MODE
) && (*pmode
!= savemode
)) ||
428 ((keys
& F_FLAGS
) && (*pflags
!= saveflags
)) ||
432 (void)printf("/set type=dir");
434 (void)printf("/set type=file");
435 if (keys
& F_UNAME
) {
436 pw
= getpwuid(saveuid
);
438 (void)printf(" uname=%s", pw
->pw_name
);
440 warnx( "Could not get uname for uid=%u", saveuid
);
442 errx(1, "Could not get uname for uid=%u", saveuid
);
445 (void)printf(" uid=%lu", (u_long
)saveuid
);
446 if (keys
& F_GNAME
) {
447 gr
= getgrgid(savegid
);
449 (void)printf(" gname=%s", gr
->gr_name
);
451 warnx("Could not get gname for gid=%u", savegid
);
453 errx(1, "Could not get gname for gid=%u", savegid
);
456 (void)printf(" gid=%lu", (u_long
)savegid
);
458 (void)printf(" mode=%#o", savemode
);
460 (void)printf(" nlink=1");
461 if (keys
& F_FLAGS
) {
462 fflags
= flags_to_string(saveflags
);
463 (void)printf(" flags=%s", fflags
);
467 (void)printf(" xattrsdigest=%s", savexattrs
);
469 (void)printf(" acldigest=%s", saveacl
);
475 *pxattrs
= savexattrs
;
482 dsort(const FTSENT
**a
, const FTSENT
**b
)
484 if (S_ISDIR((*a
)->fts_statp
->st_mode
)) {
485 if (!S_ISDIR((*b
)->fts_statp
->st_mode
))
487 } else if (S_ISDIR((*b
)->fts_statp
->st_mode
))
489 return (strcmp((*a
)->fts_name
, (*b
)->fts_name
));
495 output(int indent
, int *offset
, const char *fmt
, ...)
500 (void)vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
503 if (*offset
+ strlen(buf
) > MAXLINELEN
- 3) {
504 (void)printf(" \\\n%*s", INDENTNAMELEN
+ indent
, "");
505 *offset
= INDENTNAMELEN
+ indent
;
507 *offset
+= printf(" %s", buf
) + 1;