]> git.saurik.com Git - apple/file_cmds.git/blob - mtree/create.c
66992a73f2147063f6739834fdf30bdb91b0e40d
[apple/file_cmds.git] / mtree / create.c
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.
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.
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
30 #if 0
31 #ifndef lint
32 static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93";
33 #endif /* not lint */
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 $");
37
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <dirent.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <fts.h>
45 #include <grp.h>
46 #ifndef __APPLE__
47 #ifdef ENABLE_MD5
48 #include <md5.h>
49 #endif
50 #ifdef ENABLE_SHA1
51 #include <sha.h>
52 #endif
53 #ifdef ENABLE_RMD160
54 #include <ripemd.h>
55 #endif
56 #ifdef ENABLE_SHA256
57 #include <sha256.h>
58 #endif
59 #endif /* !__APPLE__ */
60 #include <pwd.h>
61 #include <stdint.h>
62 #include <stdio.h>
63 #include <time.h>
64 #include <unistd.h>
65 #include <vis.h>
66 #include "mtree.h"
67 #include "extern.h"
68
69 #ifdef __APPLE__
70 #include "commoncrypto.h"
71 #endif /* __APPLE__ */
72
73 #define INDENTNAMELEN 15
74 #define MAXLINELEN 80
75
76 static gid_t gid;
77 static uid_t uid;
78 static mode_t mode;
79 static u_long flags = 0xffffffff;
80 static char *xattrs = kNone;
81 static char *acl = kNone;
82
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 *);
87
88 void
89 cwalk(void)
90 {
91 FTS *t;
92 FTSENT *p;
93 time_t cl;
94 char *argv[2], host[MAXHOSTNAMELEN];
95 char dot[] = ".";
96 int indent = 0;
97 char *path;
98
99 if (!nflag) {
100 (void)time(&cl);
101 (void)gethostname(host, sizeof(host));
102 (void)printf(
103 "#\t user: %s\n#\tmachine: %s\n",
104 getlogin(), host);
105 (void)printf(
106 "#\t tree: %s\n#\t date: %s",
107 fullpath, ctime(&cl));
108 }
109
110 argv[0] = dot;
111 argv[1] = NULL;
112 if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
113 err(1, "fts_open()");
114 while ((p = fts_read(t))) {
115 if (iflag)
116 indent = p->fts_level * 4;
117 if (check_excludes(p->fts_name, p->fts_path)) {
118 fts_set(t, p, FTS_SKIP);
119 continue;
120 }
121 switch(p->fts_info) {
122 case FTS_D:
123 if (!dflag)
124 (void)printf("\n");
125 if (!nflag) {
126 path = escape_path(p->fts_path);
127 (void)printf("# %s\n", path);
128 free(path);
129 }
130 statd(t, p, &uid, &gid, &mode, &flags, &xattrs, &acl);
131 statf(indent, p);
132 break;
133 case FTS_DP:
134 if (!nflag && (p->fts_level > 0)) {
135 path = escape_path(p->fts_path);
136 (void)printf("%*s# %s\n", indent, "", path);
137 free(path);
138 }
139 (void)printf("%*s..\n", indent, "");
140 if (!dflag)
141 (void)printf("\n");
142 break;
143 case FTS_DNR:
144 case FTS_ERR:
145 case FTS_NS:
146 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
147 break;
148 default:
149 if (!dflag)
150 statf(indent, p);
151 break;
152
153 }
154 }
155 (void)fts_close(t);
156 if (sflag && keys & F_CKSUM)
157 warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total);
158 }
159
160 static void
161 statf(int indent, FTSENT *p)
162 {
163 struct group *gr;
164 struct passwd *pw;
165 uint32_t val;
166 off_t len;
167 int fd, offset;
168 char *fflags;
169 char *escaped_name;
170
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);
175
176 if (iflag || S_ISDIR(p->fts_statp->st_mode))
177 offset = printf("%*s%s", indent, "", escaped_name);
178 else
179 offset = printf("%*s %s", indent, "", escaped_name);
180
181 free(escaped_name);
182
183 if (offset > (INDENTNAMELEN + indent))
184 offset = MAXLINELEN;
185 else
186 offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
187
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);
193 if (pw != NULL)
194 output(indent, &offset, "uname=%s", pw->pw_name);
195 else if (wflag)
196 warnx("Could not get uname for uid=%u",
197 p->fts_statp->st_uid);
198 else
199 errx(1,
200 "Could not get uname for uid=%u",
201 p->fts_statp->st_uid);
202 }
203 if (keys & F_UID)
204 output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
205 }
206 if (p->fts_statp->st_gid != gid) {
207 if (keys & F_GNAME) {
208 gr = getgrgid(p->fts_statp->st_gid);
209 if (gr != NULL)
210 output(indent, &offset, "gname=%s", gr->gr_name);
211 else if (wflag)
212 warnx("Could not get gname for gid=%u",
213 p->fts_statp->st_gid);
214 else
215 errx(1,
216 "Could not get gname for gid=%u",
217 p->fts_statp->st_gid);
218 }
219 if (keys & F_GID)
220 output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
221 }
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);
226 if (keys & F_SIZE)
227 output(indent, &offset, "size=%jd",
228 (intmax_t)p->fts_statp->st_size);
229 if (keys & F_TIME)
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 ||
235 crc(fd, &val, &len))
236 err(1, "%s", p->fts_accpath);
237 (void)close(fd);
238 output(indent, &offset, "cksum=%lu", (unsigned long)val);
239 }
240 #ifdef ENABLE_MD5
241 if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
242 char *digest, buf[33];
243
244 digest = MD5File(p->fts_accpath, buf);
245 if (!digest)
246 err(1, "%s", p->fts_accpath);
247 output(indent, &offset, "md5digest=%s", digest);
248 }
249 #endif /* ENABLE_MD5 */
250 #ifdef ENABLE_SHA1
251 if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
252 char *digest, buf[41];
253
254 digest = SHA1_File(p->fts_accpath, buf);
255 if (!digest)
256 err(1, "%s", p->fts_accpath);
257 output(indent, &offset, "sha1digest=%s", digest);
258 }
259 #endif /* ENABLE_SHA1 */
260 #ifdef ENABLE_RMD160
261 if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
262 char *digest, buf[41];
263
264 digest = RIPEMD160_File(p->fts_accpath, buf);
265 if (!digest)
266 err(1, "%s", p->fts_accpath);
267 output(indent, &offset, "ripemd160digest=%s", digest);
268 }
269 #endif /* ENABLE_RMD160 */
270 #ifdef ENABLE_SHA256
271 if (keys & F_SHA256 && S_ISREG(p->fts_statp->st_mode)) {
272 char *digest, buf[kSHA256NullTerminatedBuffLen];
273
274 digest = SHA256_File(p->fts_accpath, buf);
275 if (!digest)
276 err(1, "%s", p->fts_accpath);
277 output(indent, &offset, "sha256digest=%s", digest);
278 }
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);
286 }
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);
290 free(fflags);
291 }
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);
296 }
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);
302 }
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);
307 }
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))) {
311 int supported;
312 struct timespec ptimespec = ptime(p->fts_accpath, &supported);
313 if (supported) {
314 output(indent, &offset, "ptime=%ld.%09ld",
315 ptimespec.tv_sec,
316 ptimespec.tv_nsec);
317 }
318 }
319 if (keys & F_XATTRS) {
320 char *digest, buf[kSHA256NullTerminatedBuffLen];
321
322 digest = SHA256_Path_XATTRs(p->fts_accpath, buf);
323 if (digest && (strcmp(digest, xattrs) != 0)) {
324 output(indent, &offset, "xattrsdigest=%s", digest);
325 }
326 }
327 if (keys & F_INODE) {
328 output(indent, &offset, "inode=%llu", p->fts_statp->st_ino);
329 }
330 if (keys & F_ACL) {
331 char *digest, buf[kSHA256NullTerminatedBuffLen];
332
333 digest = SHA256_Path_ACL(p->fts_accpath, buf);
334 if (digest && (strcmp(digest, acl) != 0)) {
335 output(indent, &offset, "acldigest=%s", digest);
336 }
337 }
338
339 (void)putchar('\n');
340 }
341
342 #define MAXGID 5000
343 #define MAXUID 5000
344 #define MAXMODE MBITS + 1
345 #define MAXFLAGS 256
346 #define MAXS 16
347
348 static int
349 statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode, u_long *pflags, char **pxattrs, char **pacl)
350 {
351 FTSENT *p;
352 gid_t sgid;
353 uid_t suid;
354 mode_t smode;
355 u_long sflags;
356 struct group *gr;
357 struct passwd *pw;
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];
366 char *fflags;
367 static int first = 1;
368
369 if ((p = fts_children(t, 0)) == NULL) {
370 if (errno)
371 err(1, "%s", RP(parent));
372 return (1);
373 }
374
375 bzero(g, sizeof(g));
376 bzero(u, sizeof(u));
377 bzero(m, sizeof(m));
378 bzero(f, sizeof(f));
379
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) {
385 savemode = smode;
386 maxmode = m[smode];
387 }
388 sgid = p->fts_statp->st_gid;
389 if (sgid < MAXGID && ++g[sgid] > maxgid) {
390 savegid = sgid;
391 maxgid = g[sgid];
392 }
393 suid = p->fts_statp->st_uid;
394 if (suid < MAXUID && ++u[suid] > maxuid) {
395 saveuid = suid;
396 maxuid = u[suid];
397 }
398
399 /*
400 * XXX
401 * note that we don't count the most common xattr/acl digest
402 * so set will always the default value (none)
403 */
404
405 /*
406 * XXX
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
410 */
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) {
415 saveflags = sflags;
416 maxflags = f[FLAGS2IDX(sflags)];
417 }
418 }
419 }
420 /*
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.
424 */
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)) ||
429 (first)) {
430 first = 0;
431 if (dflag)
432 (void)printf("/set type=dir");
433 else
434 (void)printf("/set type=file");
435 if (keys & F_UNAME) {
436 pw = getpwuid(saveuid);
437 if (pw != NULL)
438 (void)printf(" uname=%s", pw->pw_name);
439 else if (wflag)
440 warnx( "Could not get uname for uid=%u", saveuid);
441 else
442 errx(1, "Could not get uname for uid=%u", saveuid);
443 }
444 if (keys & F_UID)
445 (void)printf(" uid=%lu", (u_long)saveuid);
446 if (keys & F_GNAME) {
447 gr = getgrgid(savegid);
448 if (gr != NULL)
449 (void)printf(" gname=%s", gr->gr_name);
450 else if (wflag)
451 warnx("Could not get gname for gid=%u", savegid);
452 else
453 errx(1, "Could not get gname for gid=%u", savegid);
454 }
455 if (keys & F_GID)
456 (void)printf(" gid=%lu", (u_long)savegid);
457 if (keys & F_MODE)
458 (void)printf(" mode=%#o", savemode);
459 if (keys & F_NLINK)
460 (void)printf(" nlink=1");
461 if (keys & F_FLAGS) {
462 fflags = flags_to_string(saveflags);
463 (void)printf(" flags=%s", fflags);
464 free(fflags);
465 }
466 if (keys & F_XATTRS)
467 (void)printf(" xattrsdigest=%s", savexattrs);
468 if (keys & F_ACL)
469 (void)printf(" acldigest=%s", saveacl);
470 (void)printf("\n");
471 *puid = saveuid;
472 *pgid = savegid;
473 *pmode = savemode;
474 *pflags = saveflags;
475 *pxattrs = savexattrs;
476 *pacl = saveacl;
477 }
478 return (0);
479 }
480
481 static int
482 dsort(const FTSENT **a, const FTSENT **b)
483 {
484 if (S_ISDIR((*a)->fts_statp->st_mode)) {
485 if (!S_ISDIR((*b)->fts_statp->st_mode))
486 return (1);
487 } else if (S_ISDIR((*b)->fts_statp->st_mode))
488 return (-1);
489 return (strcmp((*a)->fts_name, (*b)->fts_name));
490 }
491
492 #include <stdarg.h>
493
494 void
495 output(int indent, int *offset, const char *fmt, ...)
496 {
497 va_list ap;
498 char buf[1024];
499 va_start(ap, fmt);
500 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
501 va_end(ap);
502
503 if (*offset + strlen(buf) > MAXLINELEN - 3) {
504 (void)printf(" \\\n%*s", INDENTNAMELEN + indent, "");
505 *offset = INDENTNAMELEN + indent;
506 }
507 *offset += printf(" %s", buf) + 1;
508 }