]>
Commit | Line | Data |
---|---|---|
44a7a5ab A |
1 | /*- |
2 | * Copyright (c) 1989, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: | |
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. | |
864a4b6e | 13 | * 3. Neither the name of the University nor the names of its contributors |
44a7a5ab A |
14 | * may be used to endorse or promote products derived from this software |
15 | * without specific prior written permission. | |
16 | * | |
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 | |
27 | * SUCH DAMAGE. | |
28 | */ | |
29 | ||
44a7a5ab | 30 | #if 0 |
864a4b6e | 31 | #ifndef lint |
44a7a5ab | 32 | static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93"; |
44a7a5ab | 33 | #endif /* not lint */ |
864a4b6e A |
34 | #endif |
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 $"); | |
44a7a5ab A |
37 | |
38 | #include <sys/param.h> | |
39 | #include <sys/stat.h> | |
40 | #include <dirent.h> | |
440bd198 | 41 | #include <err.h> |
44a7a5ab A |
42 | #include <errno.h> |
43 | #include <fcntl.h> | |
44 | #include <fts.h> | |
45 | #include <grp.h> | |
4d0bb651 | 46 | #ifndef __APPLE__ |
864a4b6e | 47 | #ifdef ENABLE_MD5 |
440bd198 A |
48 | #include <md5.h> |
49 | #endif | |
864a4b6e | 50 | #ifdef ENABLE_SHA1 |
440bd198 A |
51 | #include <sha.h> |
52 | #endif | |
864a4b6e | 53 | #ifdef ENABLE_RMD160 |
440bd198 A |
54 | #include <ripemd.h> |
55 | #endif | |
864a4b6e A |
56 | #ifdef ENABLE_SHA256 |
57 | #include <sha256.h> | |
58 | #endif | |
4d0bb651 | 59 | #endif /* !__APPLE__ */ |
44a7a5ab | 60 | #include <pwd.h> |
864a4b6e | 61 | #include <stdint.h> |
44a7a5ab | 62 | #include <stdio.h> |
44a7a5ab A |
63 | #include <time.h> |
64 | #include <unistd.h> | |
440bd198 | 65 | #include <vis.h> |
f13ef9e9 | 66 | #include "metrics.h" |
44a7a5ab A |
67 | #include "mtree.h" |
68 | #include "extern.h" | |
69 | ||
4d0bb651 A |
70 | #ifdef __APPLE__ |
71 | #include "commoncrypto.h" | |
72 | #endif /* __APPLE__ */ | |
73 | ||
44a7a5ab A |
74 | #define INDENTNAMELEN 15 |
75 | #define MAXLINELEN 80 | |
76 | ||
44a7a5ab A |
77 | static gid_t gid; |
78 | static uid_t uid; | |
79 | static mode_t mode; | |
440bd198 | 80 | static u_long flags = 0xffffffff; |
e0055cbe A |
81 | static char *xattrs = kNone; |
82 | static char *acl = kNone; | |
f13ef9e9 | 83 | static u_quad_t xdstream_id; |
44a7a5ab | 84 | |
40bf83fe | 85 | static int dsort(const FTSENT **, const FTSENT **); |
864a4b6e | 86 | static void output(int, int *, const char *, ...) __printflike(3, 4); |
f13ef9e9 | 87 | static int statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *, char **, char **, u_quad_t *); |
864a4b6e | 88 | static void statf(int, FTSENT *); |
44a7a5ab A |
89 | |
90 | void | |
864a4b6e | 91 | cwalk(void) |
44a7a5ab | 92 | { |
f13ef9e9 | 93 | int error = 0; |
864a4b6e A |
94 | FTS *t; |
95 | FTSENT *p; | |
96 | time_t cl; | |
440bd198 | 97 | char *argv[2], host[MAXHOSTNAMELEN]; |
864a4b6e | 98 | char dot[] = "."; |
440bd198 | 99 | int indent = 0; |
e0055cbe | 100 | char *path; |
44a7a5ab | 101 | |
864a4b6e A |
102 | if (!nflag) { |
103 | (void)time(&cl); | |
104 | (void)gethostname(host, sizeof(host)); | |
105 | (void)printf( | |
106 | "#\t user: %s\n#\tmachine: %s\n", | |
107 | getlogin(), host); | |
108 | (void)printf( | |
109 | "#\t tree: %s\n#\t date: %s", | |
110 | fullpath, ctime(&cl)); | |
111 | } | |
44a7a5ab | 112 | |
864a4b6e | 113 | argv[0] = dot; |
44a7a5ab | 114 | argv[1] = NULL; |
f13ef9e9 A |
115 | if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) { |
116 | error = errno; | |
117 | RECORD_FAILURE(76, error); | |
118 | errc(1, error, "fts_open()"); | |
119 | } | |
440bd198 A |
120 | while ((p = fts_read(t))) { |
121 | if (iflag) | |
122 | indent = p->fts_level * 4; | |
123 | if (check_excludes(p->fts_name, p->fts_path)) { | |
124 | fts_set(t, p, FTS_SKIP); | |
125 | continue; | |
126 | } | |
44a7a5ab A |
127 | switch(p->fts_info) { |
128 | case FTS_D: | |
440bd198 A |
129 | if (!dflag) |
130 | (void)printf("\n"); | |
e0055cbe A |
131 | if (!nflag) { |
132 | path = escape_path(p->fts_path); | |
133 | (void)printf("# %s\n", path); | |
134 | free(path); | |
135 | } | |
f13ef9e9 | 136 | statd(t, p, &uid, &gid, &mode, &flags, &xattrs, &acl, &xdstream_id); |
440bd198 | 137 | statf(indent, p); |
44a7a5ab A |
138 | break; |
139 | case FTS_DP: | |
e0055cbe A |
140 | if (!nflag && (p->fts_level > 0)) { |
141 | path = escape_path(p->fts_path); | |
142 | (void)printf("%*s# %s\n", indent, "", path); | |
143 | free(path); | |
144 | } | |
440bd198 A |
145 | (void)printf("%*s..\n", indent, ""); |
146 | if (!dflag) | |
147 | (void)printf("\n"); | |
44a7a5ab A |
148 | break; |
149 | case FTS_DNR: | |
150 | case FTS_ERR: | |
151 | case FTS_NS: | |
440bd198 | 152 | warnx("%s: %s", p->fts_path, strerror(p->fts_errno)); |
44a7a5ab A |
153 | break; |
154 | default: | |
155 | if (!dflag) | |
440bd198 | 156 | statf(indent, p); |
44a7a5ab | 157 | break; |
440bd198 | 158 | |
44a7a5ab | 159 | } |
440bd198 | 160 | } |
44a7a5ab | 161 | (void)fts_close(t); |
f13ef9e9 A |
162 | if (sflag && keys & F_CKSUM) { |
163 | RECORD_FAILURE(77, WARN_CHECKSUM); | |
864a4b6e | 164 | warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total); |
f13ef9e9 | 165 | } |
44a7a5ab A |
166 | } |
167 | ||
168 | static void | |
864a4b6e | 169 | statf(int indent, FTSENT *p) |
44a7a5ab | 170 | { |
f13ef9e9 | 171 | int error = 0; |
44a7a5ab A |
172 | struct group *gr; |
173 | struct passwd *pw; | |
864a4b6e A |
174 | uint32_t val; |
175 | off_t len; | |
440bd198 A |
176 | int fd, offset; |
177 | char *fflags; | |
178 | char *escaped_name; | |
179 | ||
180 | escaped_name = calloc(1, p->fts_namelen * 4 + 1); | |
f13ef9e9 A |
181 | if (escaped_name == NULL) { |
182 | RECORD_FAILURE(78, ENOMEM); | |
440bd198 | 183 | errx(1, "statf(): calloc() failed"); |
f13ef9e9 | 184 | } |
864a4b6e | 185 | strvis(escaped_name, p->fts_name, VIS_WHITE | VIS_OCTAL | VIS_GLOB); |
44a7a5ab | 186 | |
440bd198 A |
187 | if (iflag || S_ISDIR(p->fts_statp->st_mode)) |
188 | offset = printf("%*s%s", indent, "", escaped_name); | |
44a7a5ab | 189 | else |
440bd198 | 190 | offset = printf("%*s %s", indent, "", escaped_name); |
864a4b6e | 191 | |
440bd198 | 192 | free(escaped_name); |
44a7a5ab | 193 | |
440bd198 A |
194 | if (offset > (INDENTNAMELEN + indent)) |
195 | offset = MAXLINELEN; | |
44a7a5ab | 196 | else |
440bd198 | 197 | offset += printf("%*s", (INDENTNAMELEN + indent) - offset, ""); |
44a7a5ab | 198 | |
440bd198 A |
199 | if (!S_ISREG(p->fts_statp->st_mode) && !dflag) |
200 | output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode)); | |
201 | if (p->fts_statp->st_uid != uid) { | |
202 | if (keys & F_UNAME) { | |
864a4b6e | 203 | pw = getpwuid(p->fts_statp->st_uid); |
f13ef9e9 | 204 | if (pw != NULL) { |
440bd198 | 205 | output(indent, &offset, "uname=%s", pw->pw_name); |
f13ef9e9 A |
206 | } else if (wflag) { |
207 | RECORD_FAILURE(27448, WARN_UNAME); | |
864a4b6e A |
208 | warnx("Could not get uname for uid=%u", |
209 | p->fts_statp->st_uid); | |
f13ef9e9 A |
210 | } else { |
211 | RECORD_FAILURE(79, EINVAL); | |
440bd198 | 212 | errx(1, |
864a4b6e A |
213 | "Could not get uname for uid=%u", |
214 | p->fts_statp->st_uid); | |
f13ef9e9 | 215 | } |
440bd198 A |
216 | } |
217 | if (keys & F_UID) | |
218 | output(indent, &offset, "uid=%u", p->fts_statp->st_uid); | |
44a7a5ab | 219 | } |
440bd198 A |
220 | if (p->fts_statp->st_gid != gid) { |
221 | if (keys & F_GNAME) { | |
864a4b6e | 222 | gr = getgrgid(p->fts_statp->st_gid); |
f13ef9e9 | 223 | if (gr != NULL) { |
440bd198 | 224 | output(indent, &offset, "gname=%s", gr->gr_name); |
f13ef9e9 A |
225 | } else if (wflag) { |
226 | RECORD_FAILURE(27449, WARN_UNAME); | |
864a4b6e A |
227 | warnx("Could not get gname for gid=%u", |
228 | p->fts_statp->st_gid); | |
f13ef9e9 A |
229 | } else { |
230 | RECORD_FAILURE(80, EINVAL); | |
440bd198 | 231 | errx(1, |
864a4b6e A |
232 | "Could not get gname for gid=%u", |
233 | p->fts_statp->st_gid); | |
f13ef9e9 | 234 | } |
440bd198 A |
235 | } |
236 | if (keys & F_GID) | |
237 | output(indent, &offset, "gid=%u", p->fts_statp->st_gid); | |
44a7a5ab A |
238 | } |
239 | if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) | |
440bd198 | 240 | output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS); |
44a7a5ab | 241 | if (keys & F_NLINK && p->fts_statp->st_nlink != 1) |
440bd198 A |
242 | output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink); |
243 | if (keys & F_SIZE) | |
864a4b6e A |
244 | output(indent, &offset, "size=%jd", |
245 | (intmax_t)p->fts_statp->st_size); | |
f13ef9e9 A |
246 | if (keys & F_TIME) { |
247 | if (tflag && !insert_mod) { | |
248 | output(indent, &offset, "time=%ld.%09ld", | |
249 | (long)ts.tv_sec, ts.tv_nsec); | |
250 | insert_mod = 1; | |
251 | } | |
252 | if (!tflag) { | |
253 | output(indent, &offset, "time=%ld.%09ld", | |
254 | (long)p->fts_statp->st_mtimespec.tv_sec, | |
255 | p->fts_statp->st_mtimespec.tv_nsec); | |
256 | } | |
257 | } | |
44a7a5ab A |
258 | if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { |
259 | if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || | |
f13ef9e9 A |
260 | crc(fd, &val, &len)) { |
261 | error = errno; | |
262 | RECORD_FAILURE(27450, error); | |
263 | errc(1, error, "%s", p->fts_accpath); | |
264 | } | |
44a7a5ab | 265 | (void)close(fd); |
864a4b6e | 266 | output(indent, &offset, "cksum=%lu", (unsigned long)val); |
440bd198 | 267 | } |
864a4b6e | 268 | #ifdef ENABLE_MD5 |
440bd198 A |
269 | if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) { |
270 | char *digest, buf[33]; | |
f13ef9e9 A |
271 | #ifdef __clang__ |
272 | /* clang doesn't like MD5 due to security concerns, but it's used for file data/metadata integrity.. */ | |
273 | #pragma clang diagnostic push | |
274 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" | |
440bd198 | 275 | digest = MD5File(p->fts_accpath, buf); |
f13ef9e9 A |
276 | #pragma clang diagnostic pop |
277 | #endif | |
278 | if (!digest) { | |
279 | error = errno; | |
280 | RECORD_FAILURE(81, error); | |
281 | errc(1, error, "%s", p->fts_accpath); | |
282 | } | |
864a4b6e | 283 | output(indent, &offset, "md5digest=%s", digest); |
44a7a5ab | 284 | } |
864a4b6e A |
285 | #endif /* ENABLE_MD5 */ |
286 | #ifdef ENABLE_SHA1 | |
440bd198 A |
287 | if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) { |
288 | char *digest, buf[41]; | |
f13ef9e9 A |
289 | #ifdef __clang__ |
290 | /* clang doesn't like SHA1 due to security concerns, but it's used for file data/metadata integrity.. */ | |
291 | #pragma clang diagnostic push | |
292 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" | |
440bd198 | 293 | digest = SHA1_File(p->fts_accpath, buf); |
f13ef9e9 A |
294 | #pragma clang diagnostic pop |
295 | #endif | |
296 | if (!digest) { | |
297 | error = errno; | |
298 | RECORD_FAILURE(82, error); | |
299 | errc(1, error, "%s", p->fts_accpath); | |
300 | } | |
864a4b6e | 301 | output(indent, &offset, "sha1digest=%s", digest); |
440bd198 | 302 | } |
864a4b6e A |
303 | #endif /* ENABLE_SHA1 */ |
304 | #ifdef ENABLE_RMD160 | |
440bd198 A |
305 | if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) { |
306 | char *digest, buf[41]; | |
f13ef9e9 A |
307 | #ifdef __clang__ |
308 | /* clang doesn't like RIPEMD160 due to security concerns, but it's used for file data/metadata integrity.. */ | |
309 | #pragma clang diagnostic push | |
310 | #pragma clang diagnostic ignored "-Wdeprecated-declarations" | |
440bd198 | 311 | digest = RIPEMD160_File(p->fts_accpath, buf); |
f13ef9e9 A |
312 | #pragma clang diagnostic pop |
313 | #endif | |
314 | if (!digest) { | |
315 | error = errno; | |
316 | RECORD_FAILURE(83, error); | |
317 | errc(1, error, "%s", p->fts_accpath); | |
318 | } | |
864a4b6e | 319 | output(indent, &offset, "ripemd160digest=%s", digest); |
440bd198 | 320 | } |
864a4b6e A |
321 | #endif /* ENABLE_RMD160 */ |
322 | #ifdef ENABLE_SHA256 | |
323 | if (keys & F_SHA256 && S_ISREG(p->fts_statp->st_mode)) { | |
e0055cbe | 324 | char *digest, buf[kSHA256NullTerminatedBuffLen]; |
864a4b6e A |
325 | |
326 | digest = SHA256_File(p->fts_accpath, buf); | |
f13ef9e9 A |
327 | if (!digest) { |
328 | error = errno; | |
329 | RECORD_FAILURE(84, error); | |
330 | errc(1, error, "%s", p->fts_accpath); | |
331 | } | |
864a4b6e A |
332 | output(indent, &offset, "sha256digest=%s", digest); |
333 | } | |
334 | #endif /* ENABLE_SHA256 */ | |
44a7a5ab | 335 | if (keys & F_SLINK && |
00337e45 A |
336 | (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) { |
337 | char visbuf[MAXPATHLEN * 4]; | |
338 | char *s = rlink(p->fts_accpath); | |
339 | strvis(visbuf, s, VIS_WHITE | VIS_OCTAL); | |
340 | output(indent, &offset, "link=%s", visbuf); | |
341 | } | |
440bd198 A |
342 | if (keys & F_FLAGS && p->fts_statp->st_flags != flags) { |
343 | fflags = flags_to_string(p->fts_statp->st_flags); | |
344 | output(indent, &offset, "flags=%s", fflags); | |
345 | free(fflags); | |
346 | } | |
e0055cbe | 347 | if (keys & F_BTIME) { |
f13ef9e9 A |
348 | if (tflag && !insert_birth) { |
349 | output(indent, &offset, "btime=%ld.%09ld", | |
350 | ts.tv_sec, ts.tv_nsec); | |
351 | insert_birth = 1; | |
352 | } | |
353 | if (!tflag) { | |
354 | output(indent, &offset, "btime=%ld.%09ld", | |
355 | p->fts_statp->st_birthtimespec.tv_sec, | |
356 | p->fts_statp->st_birthtimespec.tv_nsec); | |
357 | } | |
e0055cbe A |
358 | } |
359 | // only check access time on regular files, as traversing a folder will update its access time | |
360 | if (keys & F_ATIME && S_ISREG(p->fts_statp->st_mode)) { | |
f13ef9e9 A |
361 | if (tflag && !insert_access) { |
362 | output(indent, &offset, "atime=%ld.%09ld", | |
363 | ts.tv_sec, ts.tv_nsec); | |
364 | insert_access = 1; | |
365 | } | |
366 | if (!tflag) { | |
367 | output(indent, &offset, "atime=%ld.%09ld", | |
368 | p->fts_statp->st_atimespec.tv_sec, | |
369 | p->fts_statp->st_atimespec.tv_nsec); | |
370 | } | |
e0055cbe A |
371 | } |
372 | if (keys & F_CTIME) { | |
f13ef9e9 A |
373 | if (tflag && !insert_change) { |
374 | output(indent, &offset, "ctime=%ld.%09ld", | |
375 | ts.tv_sec, ts.tv_nsec); | |
376 | insert_change = 1; | |
377 | } | |
378 | if (!tflag) { | |
379 | output(indent, &offset, "ctime=%ld.%09ld", | |
380 | p->fts_statp->st_ctimespec.tv_sec, | |
381 | p->fts_statp->st_ctimespec.tv_nsec); | |
382 | } | |
e0055cbe A |
383 | } |
384 | // date added to parent folder is only supported for files and directories | |
385 | if (keys & F_PTIME && (S_ISREG(p->fts_statp->st_mode) || | |
386 | S_ISDIR(p->fts_statp->st_mode))) { | |
387 | int supported; | |
388 | struct timespec ptimespec = ptime(p->fts_accpath, &supported); | |
f13ef9e9 A |
389 | if (tflag && !insert_parent) { |
390 | output(indent, &offset, "ptime=%ld.%09ld", | |
391 | ts.tv_sec, ts.tv_nsec); | |
392 | insert_parent = 1; | |
393 | } | |
394 | if (!tflag && supported) { | |
e0055cbe A |
395 | output(indent, &offset, "ptime=%ld.%09ld", |
396 | ptimespec.tv_sec, | |
397 | ptimespec.tv_nsec); | |
398 | } | |
399 | } | |
400 | if (keys & F_XATTRS) { | |
f13ef9e9 A |
401 | char buf[kSHA256NullTerminatedBuffLen]; |
402 | xattr_info *ai; | |
e0055cbe | 403 | |
f13ef9e9 A |
404 | ai = SHA256_Path_XATTRs(p->fts_accpath, buf); |
405 | if (ai && ai->digest) { | |
406 | if ((strcmp(ai->digest, xattrs) != 0) || (ai->xdstream_priv_id != xdstream_id)) { | |
407 | output(indent, &offset, "xattrsdigest=%s.%llu", ai->digest, ai->xdstream_priv_id); | |
408 | } | |
409 | free(ai); | |
410 | ai = NULL; | |
e0055cbe A |
411 | } |
412 | } | |
413 | if (keys & F_INODE) { | |
414 | output(indent, &offset, "inode=%llu", p->fts_statp->st_ino); | |
415 | } | |
416 | if (keys & F_ACL) { | |
417 | char *digest, buf[kSHA256NullTerminatedBuffLen]; | |
418 | ||
419 | digest = SHA256_Path_ACL(p->fts_accpath, buf); | |
420 | if (digest && (strcmp(digest, acl) != 0)) { | |
421 | output(indent, &offset, "acldigest=%s", digest); | |
422 | } | |
423 | } | |
f13ef9e9 A |
424 | if (keys & F_SIBLINGID) { |
425 | uint64_t sibling_id = get_sibling_id(p->fts_accpath); | |
426 | sibling_id = (sibling_id != p->fts_statp->st_ino) ? sibling_id : 0; | |
427 | output(indent, &offset, "siblingid=%llu", sibling_id); | |
428 | } | |
e0055cbe | 429 | |
44a7a5ab A |
430 | (void)putchar('\n'); |
431 | } | |
432 | ||
433 | #define MAXGID 5000 | |
434 | #define MAXUID 5000 | |
435 | #define MAXMODE MBITS + 1 | |
440bd198 A |
436 | #define MAXFLAGS 256 |
437 | #define MAXS 16 | |
44a7a5ab A |
438 | |
439 | static int | |
f13ef9e9 | 440 | statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode, u_long *pflags, char **pxattrs, char **pacl, u_quad_t *xdstream_id) |
44a7a5ab | 441 | { |
f13ef9e9 | 442 | int error = 0; |
864a4b6e A |
443 | FTSENT *p; |
444 | gid_t sgid; | |
445 | uid_t suid; | |
446 | mode_t smode; | |
447 | u_long sflags; | |
44a7a5ab A |
448 | struct group *gr; |
449 | struct passwd *pw; | |
440bd198 A |
450 | gid_t savegid = *pgid; |
451 | uid_t saveuid = *puid; | |
452 | mode_t savemode = *pmode; | |
453 | u_long saveflags = *pflags; | |
e0055cbe A |
454 | char *savexattrs = *pxattrs; |
455 | char *saveacl = *pacl; | |
f13ef9e9 | 456 | u_quad_t savexdstream_id = *xdstream_id; |
440bd198 A |
457 | u_short maxgid, maxuid, maxmode, maxflags; |
458 | u_short g[MAXGID], u[MAXUID], m[MAXMODE], f[MAXFLAGS]; | |
459 | char *fflags; | |
460 | static int first = 1; | |
44a7a5ab | 461 | |
44a7a5ab | 462 | if ((p = fts_children(t, 0)) == NULL) { |
f13ef9e9 A |
463 | error = errno; |
464 | if (error) { | |
465 | RECORD_FAILURE(85, error); | |
466 | errc(1, error, "%s", RP(parent)); | |
467 | } | |
44a7a5ab A |
468 | return (1); |
469 | } | |
470 | ||
440bd198 A |
471 | bzero(g, sizeof(g)); |
472 | bzero(u, sizeof(u)); | |
473 | bzero(m, sizeof(m)); | |
474 | bzero(f, sizeof(f)); | |
44a7a5ab | 475 | |
440bd198 | 476 | maxuid = maxgid = maxmode = maxflags = 0; |
44a7a5ab | 477 | for (; p; p = p->fts_link) { |
440bd198 A |
478 | if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) { |
479 | smode = p->fts_statp->st_mode & MBITS; | |
480 | if (smode < MAXMODE && ++m[smode] > maxmode) { | |
481 | savemode = smode; | |
482 | maxmode = m[smode]; | |
483 | } | |
484 | sgid = p->fts_statp->st_gid; | |
485 | if (sgid < MAXGID && ++g[sgid] > maxgid) { | |
486 | savegid = sgid; | |
487 | maxgid = g[sgid]; | |
488 | } | |
489 | suid = p->fts_statp->st_uid; | |
490 | if (suid < MAXUID && ++u[suid] > maxuid) { | |
491 | saveuid = suid; | |
492 | maxuid = u[suid]; | |
493 | } | |
e0055cbe A |
494 | |
495 | /* | |
496 | * XXX | |
497 | * note that we don't count the most common xattr/acl digest | |
498 | * so set will always the default value (none) | |
499 | */ | |
440bd198 A |
500 | |
501 | /* | |
502 | * XXX | |
503 | * note that the below will break when file flags | |
504 | * are extended beyond the first 4 bytes of each | |
505 | * half word of the flags | |
506 | */ | |
507 | #define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0)) | |
508 | sflags = p->fts_statp->st_flags; | |
509 | if (FLAGS2IDX(sflags) < MAXFLAGS && | |
510 | ++f[FLAGS2IDX(sflags)] > maxflags) { | |
511 | saveflags = sflags; | |
512 | maxflags = f[FLAGS2IDX(sflags)]; | |
513 | } | |
44a7a5ab A |
514 | } |
515 | } | |
440bd198 A |
516 | /* |
517 | * If the /set record is the same as the last one we do not need to output | |
518 | * a new one. So first we check to see if anything changed. Note that we | |
519 | * always output a /set record for the first directory. | |
520 | */ | |
521 | if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) || | |
522 | (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) || | |
864a4b6e | 523 | ((keys & F_MODE) && (*pmode != savemode)) || |
440bd198 A |
524 | ((keys & F_FLAGS) && (*pflags != saveflags)) || |
525 | (first)) { | |
526 | first = 0; | |
527 | if (dflag) | |
528 | (void)printf("/set type=dir"); | |
44a7a5ab | 529 | else |
440bd198 A |
530 | (void)printf("/set type=file"); |
531 | if (keys & F_UNAME) { | |
864a4b6e | 532 | pw = getpwuid(saveuid); |
f13ef9e9 | 533 | if (pw != NULL) { |
440bd198 | 534 | (void)printf(" uname=%s", pw->pw_name); |
f13ef9e9 A |
535 | } else if (wflag) { |
536 | RECORD_FAILURE(27451, WARN_UNAME); | |
864a4b6e | 537 | warnx( "Could not get uname for uid=%u", saveuid); |
f13ef9e9 A |
538 | } else { |
539 | RECORD_FAILURE(86, EINVAL); | |
864a4b6e | 540 | errx(1, "Could not get uname for uid=%u", saveuid); |
f13ef9e9 | 541 | } |
440bd198 A |
542 | } |
543 | if (keys & F_UID) | |
544 | (void)printf(" uid=%lu", (u_long)saveuid); | |
545 | if (keys & F_GNAME) { | |
864a4b6e | 546 | gr = getgrgid(savegid); |
f13ef9e9 | 547 | if (gr != NULL) { |
440bd198 | 548 | (void)printf(" gname=%s", gr->gr_name); |
f13ef9e9 A |
549 | } else if (wflag) { |
550 | RECORD_FAILURE(27452, WARN_UNAME); | |
864a4b6e | 551 | warnx("Could not get gname for gid=%u", savegid); |
f13ef9e9 A |
552 | } else { |
553 | RECORD_FAILURE(87, EINVAL); | |
864a4b6e | 554 | errx(1, "Could not get gname for gid=%u", savegid); |
f13ef9e9 | 555 | } |
440bd198 A |
556 | } |
557 | if (keys & F_GID) | |
558 | (void)printf(" gid=%lu", (u_long)savegid); | |
559 | if (keys & F_MODE) | |
560 | (void)printf(" mode=%#o", savemode); | |
561 | if (keys & F_NLINK) | |
562 | (void)printf(" nlink=1"); | |
563 | if (keys & F_FLAGS) { | |
564 | fflags = flags_to_string(saveflags); | |
565 | (void)printf(" flags=%s", fflags); | |
566 | free(fflags); | |
567 | } | |
e0055cbe | 568 | if (keys & F_XATTRS) |
f13ef9e9 | 569 | (void)printf(" xattrsdigest=%s.%llu", savexattrs, savexdstream_id); |
e0055cbe A |
570 | if (keys & F_ACL) |
571 | (void)printf(" acldigest=%s", saveacl); | |
440bd198 A |
572 | (void)printf("\n"); |
573 | *puid = saveuid; | |
574 | *pgid = savegid; | |
575 | *pmode = savemode; | |
576 | *pflags = saveflags; | |
e0055cbe A |
577 | *pxattrs = savexattrs; |
578 | *pacl = saveacl; | |
f13ef9e9 | 579 | *xdstream_id = savexdstream_id; |
44a7a5ab | 580 | } |
44a7a5ab A |
581 | return (0); |
582 | } | |
583 | ||
584 | static int | |
40bf83fe | 585 | dsort(const FTSENT **a, const FTSENT **b) |
44a7a5ab A |
586 | { |
587 | if (S_ISDIR((*a)->fts_statp->st_mode)) { | |
588 | if (!S_ISDIR((*b)->fts_statp->st_mode)) | |
589 | return (1); | |
590 | } else if (S_ISDIR((*b)->fts_statp->st_mode)) | |
591 | return (-1); | |
592 | return (strcmp((*a)->fts_name, (*b)->fts_name)); | |
593 | } | |
594 | ||
44a7a5ab | 595 | #include <stdarg.h> |
44a7a5ab A |
596 | |
597 | void | |
440bd198 | 598 | output(int indent, int *offset, const char *fmt, ...) |
44a7a5ab A |
599 | { |
600 | va_list ap; | |
601 | char buf[1024]; | |
44a7a5ab | 602 | va_start(ap, fmt); |
44a7a5ab A |
603 | (void)vsnprintf(buf, sizeof(buf), fmt, ap); |
604 | va_end(ap); | |
605 | ||
606 | if (*offset + strlen(buf) > MAXLINELEN - 3) { | |
440bd198 A |
607 | (void)printf(" \\\n%*s", INDENTNAMELEN + indent, ""); |
608 | *offset = INDENTNAMELEN + indent; | |
44a7a5ab A |
609 | } |
610 | *offset += printf(" %s", buf) + 1; | |
611 | } |