]>
git.saurik.com Git - apple/file_cmds.git/blob - mtree/create.c
886095558331bf80e63bf9d1fe148f730cdf30fe
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>
67 #define INDENTNAMELEN 15
73 static u_long flags
= 0xffffffff;
75 static int dsort(const FTSENT
* const *, const FTSENT
* const *);
76 static void output(int, int *, const char *, ...) __printflike(3, 4);
77 static int statd(FTS
*, FTSENT
*, uid_t
*, gid_t
*, mode_t
*, u_long
*);
78 static void statf(int, FTSENT
*);
86 char *argv
[2], host
[MAXHOSTNAMELEN
];
92 (void)gethostname(host
, sizeof(host
));
94 "#\t user: %s\n#\tmachine: %s\n",
97 "#\t tree: %s\n#\t date: %s",
98 fullpath
, ctime(&cl
));
103 if ((t
= fts_open(argv
, ftsoptions
, dsort
)) == NULL
)
104 err(1, "fts_open()");
105 while ((p
= fts_read(t
))) {
107 indent
= p
->fts_level
* 4;
108 if (check_excludes(p
->fts_name
, p
->fts_path
)) {
109 fts_set(t
, p
, FTS_SKIP
);
112 switch(p
->fts_info
) {
117 (void)printf("# %s\n", p
->fts_path
);
118 statd(t
, p
, &uid
, &gid
, &mode
, &flags
);
122 if (!nflag
&& (p
->fts_level
> 0))
123 (void)printf("%*s# %s\n", indent
, "", p
->fts_path
);
124 (void)printf("%*s..\n", indent
, "");
131 warnx("%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
141 if (sflag
&& keys
& F_CKSUM
)
142 warnx("%s checksum: %lu", fullpath
, (unsigned long)crc_total
);
146 statf(int indent
, FTSENT
*p
)
156 escaped_name
= calloc(1, p
->fts_namelen
* 4 + 1);
157 if (escaped_name
== NULL
)
158 errx(1, "statf(): calloc() failed");
159 strvis(escaped_name
, p
->fts_name
, VIS_WHITE
| VIS_OCTAL
| VIS_GLOB
);
161 if (iflag
|| S_ISDIR(p
->fts_statp
->st_mode
))
162 offset
= printf("%*s%s", indent
, "", escaped_name
);
164 offset
= printf("%*s %s", indent
, "", escaped_name
);
168 if (offset
> (INDENTNAMELEN
+ indent
))
171 offset
+= printf("%*s", (INDENTNAMELEN
+ indent
) - offset
, "");
173 if (!S_ISREG(p
->fts_statp
->st_mode
) && !dflag
)
174 output(indent
, &offset
, "type=%s", inotype(p
->fts_statp
->st_mode
));
175 if (p
->fts_statp
->st_uid
!= uid
) {
176 if (keys
& F_UNAME
) {
177 pw
= getpwuid(p
->fts_statp
->st_uid
);
179 output(indent
, &offset
, "uname=%s", pw
->pw_name
);
181 warnx("Could not get uname for uid=%u",
182 p
->fts_statp
->st_uid
);
185 "Could not get uname for uid=%u",
186 p
->fts_statp
->st_uid
);
189 output(indent
, &offset
, "uid=%u", p
->fts_statp
->st_uid
);
191 if (p
->fts_statp
->st_gid
!= gid
) {
192 if (keys
& F_GNAME
) {
193 gr
= getgrgid(p
->fts_statp
->st_gid
);
195 output(indent
, &offset
, "gname=%s", gr
->gr_name
);
197 warnx("Could not get gname for gid=%u",
198 p
->fts_statp
->st_gid
);
201 "Could not get gname for gid=%u",
202 p
->fts_statp
->st_gid
);
205 output(indent
, &offset
, "gid=%u", p
->fts_statp
->st_gid
);
207 if (keys
& F_MODE
&& (p
->fts_statp
->st_mode
& MBITS
) != mode
)
208 output(indent
, &offset
, "mode=%#o", p
->fts_statp
->st_mode
& MBITS
);
209 if (keys
& F_NLINK
&& p
->fts_statp
->st_nlink
!= 1)
210 output(indent
, &offset
, "nlink=%u", p
->fts_statp
->st_nlink
);
212 output(indent
, &offset
, "size=%jd",
213 (intmax_t)p
->fts_statp
->st_size
);
215 output(indent
, &offset
, "time=%ld.%ld",
216 (long)p
->fts_statp
->st_mtimespec
.tv_sec
,
217 p
->fts_statp
->st_mtimespec
.tv_nsec
);
218 if (keys
& F_CKSUM
&& S_ISREG(p
->fts_statp
->st_mode
)) {
219 if ((fd
= open(p
->fts_accpath
, O_RDONLY
, 0)) < 0 ||
221 err(1, "%s", p
->fts_accpath
);
223 output(indent
, &offset
, "cksum=%lu", (unsigned long)val
);
226 if (keys
& F_MD5
&& S_ISREG(p
->fts_statp
->st_mode
)) {
227 char *digest
, buf
[33];
229 digest
= MD5File(p
->fts_accpath
, buf
);
231 err(1, "%s", p
->fts_accpath
);
232 output(indent
, &offset
, "md5digest=%s", digest
);
234 #endif /* ENABLE_MD5 */
236 if (keys
& F_SHA1
&& S_ISREG(p
->fts_statp
->st_mode
)) {
237 char *digest
, buf
[41];
239 digest
= SHA1_File(p
->fts_accpath
, buf
);
241 err(1, "%s", p
->fts_accpath
);
242 output(indent
, &offset
, "sha1digest=%s", digest
);
244 #endif /* ENABLE_SHA1 */
246 if (keys
& F_RMD160
&& S_ISREG(p
->fts_statp
->st_mode
)) {
247 char *digest
, buf
[41];
249 digest
= RIPEMD160_File(p
->fts_accpath
, buf
);
251 err(1, "%s", p
->fts_accpath
);
252 output(indent
, &offset
, "ripemd160digest=%s", digest
);
254 #endif /* ENABLE_RMD160 */
256 if (keys
& F_SHA256
&& S_ISREG(p
->fts_statp
->st_mode
)) {
257 char *digest
, buf
[65];
259 digest
= SHA256_File(p
->fts_accpath
, buf
);
261 err(1, "%s", p
->fts_accpath
);
262 output(indent
, &offset
, "sha256digest=%s", digest
);
264 #endif /* ENABLE_SHA256 */
265 if (keys
& F_SLINK
&&
266 (p
->fts_info
== FTS_SL
|| p
->fts_info
== FTS_SLNONE
))
267 output(indent
, &offset
, "link=%s", rlink(p
->fts_accpath
));
268 if (keys
& F_FLAGS
&& p
->fts_statp
->st_flags
!= flags
) {
269 fflags
= flags_to_string(p
->fts_statp
->st_flags
);
270 output(indent
, &offset
, "flags=%s", fflags
);
278 #define MAXMODE MBITS + 1
283 statd(FTS
*t
, FTSENT
*parent
, uid_t
*puid
, gid_t
*pgid
, mode_t
*pmode
, u_long
*pflags
)
292 gid_t savegid
= *pgid
;
293 uid_t saveuid
= *puid
;
294 mode_t savemode
= *pmode
;
295 u_long saveflags
= *pflags
;
296 u_short maxgid
, maxuid
, maxmode
, maxflags
;
297 u_short g
[MAXGID
], u
[MAXUID
], m
[MAXMODE
], f
[MAXFLAGS
];
299 static int first
= 1;
301 if ((p
= fts_children(t
, 0)) == NULL
) {
303 err(1, "%s", RP(parent
));
312 maxuid
= maxgid
= maxmode
= maxflags
= 0;
313 for (; p
; p
= p
->fts_link
) {
314 if (!dflag
|| (dflag
&& S_ISDIR(p
->fts_statp
->st_mode
))) {
315 smode
= p
->fts_statp
->st_mode
& MBITS
;
316 if (smode
< MAXMODE
&& ++m
[smode
] > maxmode
) {
320 sgid
= p
->fts_statp
->st_gid
;
321 if (sgid
< MAXGID
&& ++g
[sgid
] > maxgid
) {
325 suid
= p
->fts_statp
->st_uid
;
326 if (suid
< MAXUID
&& ++u
[suid
] > maxuid
) {
333 * note that the below will break when file flags
334 * are extended beyond the first 4 bytes of each
335 * half word of the flags
337 #define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0))
338 sflags
= p
->fts_statp
->st_flags
;
339 if (FLAGS2IDX(sflags
) < MAXFLAGS
&&
340 ++f
[FLAGS2IDX(sflags
)] > maxflags
) {
342 maxflags
= f
[FLAGS2IDX(sflags
)];
347 * If the /set record is the same as the last one we do not need to output
348 * a new one. So first we check to see if anything changed. Note that we
349 * always output a /set record for the first directory.
351 if ((((keys
& F_UNAME
) | (keys
& F_UID
)) && (*puid
!= saveuid
)) ||
352 (((keys
& F_GNAME
) | (keys
& F_GID
)) && (*pgid
!= savegid
)) ||
353 ((keys
& F_MODE
) && (*pmode
!= savemode
)) ||
354 ((keys
& F_FLAGS
) && (*pflags
!= saveflags
)) ||
358 (void)printf("/set type=dir");
360 (void)printf("/set type=file");
361 if (keys
& F_UNAME
) {
362 pw
= getpwuid(saveuid
);
364 (void)printf(" uname=%s", pw
->pw_name
);
366 warnx( "Could not get uname for uid=%u", saveuid
);
368 errx(1, "Could not get uname for uid=%u", saveuid
);
371 (void)printf(" uid=%lu", (u_long
)saveuid
);
372 if (keys
& F_GNAME
) {
373 gr
= getgrgid(savegid
);
375 (void)printf(" gname=%s", gr
->gr_name
);
377 warnx("Could not get gname for gid=%u", savegid
);
379 errx(1, "Could not get gname for gid=%u", savegid
);
382 (void)printf(" gid=%lu", (u_long
)savegid
);
384 (void)printf(" mode=%#o", savemode
);
386 (void)printf(" nlink=1");
387 if (keys
& F_FLAGS
) {
388 fflags
= flags_to_string(saveflags
);
389 (void)printf(" flags=%s", fflags
);
402 dsort(const FTSENT
* const *a
, const FTSENT
* const *b
)
404 if (S_ISDIR((*a
)->fts_statp
->st_mode
)) {
405 if (!S_ISDIR((*b
)->fts_statp
->st_mode
))
407 } else if (S_ISDIR((*b
)->fts_statp
->st_mode
))
409 return (strcmp((*a
)->fts_name
, (*b
)->fts_name
));
415 output(int indent
, int *offset
, const char *fmt
, ...)
420 (void)vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
423 if (*offset
+ strlen(buf
) > MAXLINELEN
- 3) {
424 (void)printf(" \\\n%*s", INDENTNAMELEN
+ indent
, "");
425 *offset
= INDENTNAMELEN
+ indent
;
427 *offset
+= printf(" %s", buf
) + 1;