1 /* $NetBSD: create.c,v 1.58 2009/04/03 21:18:59 apb Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
39 static char sccsid
[] = "@(#)create.c 8.1 (Berkeley) 6/6/93";
41 __RCSID("$NetBSD: create.c,v 1.58 2009/04/03 21:18:59 apb Exp $");
45 #include <sys/param.h>
48 #if ! HAVE_NBTOOL_CONFIG_H
78 #define INDENTNAMELEN 15
86 static int dcmp(const FTSENT
**, const FTSENT
**);
87 static void output(int *, const char *, ...)
88 __attribute__((__format__(__printf__
, 2, 3)));
89 static int statd(FTS
*, FTSENT
*, uid_t
*, gid_t
*, mode_t
*, u_long
*);
90 static void statf(FTSENT
*);
98 char host
[MAXHOSTNAMELEN
+ 1];
107 gethostname(host
, sizeof(host
));
108 host
[sizeof(host
) - 1] = '\0';
109 if ((user
= getlogin()) == NULL
) {
111 user
= (pw
= getpwuid(getuid())) == NULL
? pw
->pw_name
:
116 "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s",
117 user
, host
, fullpath
, ctime(&clocktime
));
119 if ((t
= fts_open(argv
, ftsoptions
, dcmp
)) == NULL
)
120 mtree_err("fts_open: %s", strerror(errno
));
121 while ((p
= fts_read(t
)) != NULL
) {
122 if (check_excludes(p
->fts_name
, p
->fts_path
)) {
123 fts_set(t
, p
, FTS_SKIP
);
126 switch(p
->fts_info
) {
128 printf("\n# %s\n", p
->fts_path
);
129 statd(t
, p
, &uid
, &gid
, &mode
, &flags
);
133 if (p
->fts_level
> 0)
134 printf("# %s\n..\n\n", p
->fts_path
);
140 p
->fts_path
, strerror(p
->fts_errno
));
150 if (sflag
&& keys
& F_CKSUM
)
151 mtree_err("%s checksum: %u", fullpath
, crc_total
);
160 #if !defined(NO_MD5) || !defined(NO_RMD160) || !defined(NO_SHA1) || !defined(NO_SHA2)
164 indent
= printf("%s%s",
165 S_ISDIR(p
->fts_statp
->st_mode
) ? "" : " ", vispath(p
->fts_name
));
167 if (indent
> INDENTNAMELEN
)
170 indent
+= printf("%*s", INDENTNAMELEN
- indent
, "");
172 if (!S_ISREG(p
->fts_statp
->st_mode
))
173 output(&indent
, "type=%s", inotype(p
->fts_statp
->st_mode
));
174 if (keys
& (F_UID
| F_UNAME
) && p
->fts_statp
->st_uid
!= uid
) {
175 if (keys
& F_UNAME
&&
176 (name
= user_from_uid(p
->fts_statp
->st_uid
, 1)) != NULL
)
177 output(&indent
, "uname=%s", name
);
178 else /* if (keys & F_UID) */
179 output(&indent
, "uid=%u", p
->fts_statp
->st_uid
);
181 if (keys
& (F_GID
| F_GNAME
) && p
->fts_statp
->st_gid
!= gid
) {
182 if (keys
& F_GNAME
&&
183 (name
= group_from_gid(p
->fts_statp
->st_gid
, 1)) != NULL
)
184 output(&indent
, "gname=%s", name
);
185 else /* if (keys & F_GID) */
186 output(&indent
, "gid=%u", p
->fts_statp
->st_gid
);
188 if (keys
& F_MODE
&& (p
->fts_statp
->st_mode
& MBITS
) != mode
)
189 output(&indent
, "mode=%#o", p
->fts_statp
->st_mode
& MBITS
);
191 (S_ISBLK(p
->fts_statp
->st_mode
) || S_ISCHR(p
->fts_statp
->st_mode
)))
192 output(&indent
, "device=%#llx",
193 (long long)p
->fts_statp
->st_rdev
);
194 if (keys
& F_NLINK
&& p
->fts_statp
->st_nlink
!= 1)
195 output(&indent
, "nlink=%u", p
->fts_statp
->st_nlink
);
196 if (keys
& F_SIZE
&& S_ISREG(p
->fts_statp
->st_mode
))
197 output(&indent
, "size=%lld", (long long)p
->fts_statp
->st_size
);
199 #if defined(BSD4_4) && !defined(HAVE_NBTOOL_CONFIG_H)
200 output(&indent
, "time=%ld.%ld",
201 (long)p
->fts_statp
->st_mtimespec
.tv_sec
,
202 p
->fts_statp
->st_mtimespec
.tv_nsec
);
204 output(&indent
, "time=%ld.%ld",
205 (long)p
->fts_statp
->st_mtime
, (long)0);
207 if (keys
& F_CKSUM
&& S_ISREG(p
->fts_statp
->st_mode
)) {
208 if ((fd
= open(p
->fts_accpath
, O_RDONLY
, 0)) < 0 ||
210 mtree_err("%s: %s", p
->fts_accpath
, strerror(errno
));
212 output(&indent
, "cksum=%lu", (long)val
);
215 if (keys
& F_MD5
&& S_ISREG(p
->fts_statp
->st_mode
)) {
216 if ((digestbuf
= MD5File(p
->fts_accpath
, NULL
)) == NULL
)
217 mtree_err("%s: MD5File failed: %s", p
->fts_accpath
, strerror(errno
));
218 output(&indent
, "md5=%s", digestbuf
);
221 #endif /* ! NO_MD5 */
223 if (keys
& F_RMD160
&& S_ISREG(p
->fts_statp
->st_mode
)) {
224 if ((digestbuf
= RMD160File(p
->fts_accpath
, NULL
)) == NULL
)
225 mtree_err("%s: RMD160File failed: %s", p
->fts_accpath
, strerror(errno
));
226 output(&indent
, "rmd160=%s", digestbuf
);
229 #endif /* ! NO_RMD160 */
231 if (keys
& F_SHA1
&& S_ISREG(p
->fts_statp
->st_mode
)) {
232 if ((digestbuf
= SHA1File(p
->fts_accpath
, NULL
)) == NULL
)
233 mtree_err("%s: SHA1File failed: %s", p
->fts_accpath
, strerror(errno
));
234 output(&indent
, "sha1=%s", digestbuf
);
237 #endif /* ! NO_SHA1 */
239 if (keys
& F_SHA256
&& S_ISREG(p
->fts_statp
->st_mode
)) {
240 if ((digestbuf
= SHA256_File(p
->fts_accpath
, NULL
)) == NULL
)
241 mtree_err("%s: SHA256_File failed: %s", p
->fts_accpath
, strerror(errno
));
242 output(&indent
, "sha256=%s", digestbuf
);
245 if (keys
& F_SHA384
&& S_ISREG(p
->fts_statp
->st_mode
)) {
246 if ((digestbuf
= SHA384_File(p
->fts_accpath
, NULL
)) == NULL
)
247 mtree_err("%s: SHA384_File failed: %s", p
->fts_accpath
, strerror(errno
));
248 output(&indent
, "sha384=%s", digestbuf
);
251 if (keys
& F_SHA512
&& S_ISREG(p
->fts_statp
->st_mode
)) {
252 if ((digestbuf
= SHA512_File(p
->fts_accpath
, NULL
)) == NULL
)
253 mtree_err("%s: SHA512_File failed: %s", p
->fts_accpath
, strerror(errno
));
254 output(&indent
, "sha512=%s", digestbuf
);
257 #endif /* ! NO_SHA2 */
258 if (keys
& F_SLINK
&&
259 (p
->fts_info
== FTS_SL
|| p
->fts_info
== FTS_SLNONE
))
260 output(&indent
, "link=%s", vispath(rlink(p
->fts_accpath
)));
261 #if HAVE_STRUCT_STAT_ST_FLAGS
262 if (keys
& F_FLAGS
&& p
->fts_statp
->st_flags
!= flags
)
263 output(&indent
, "flags=%s",
264 flags_to_string(p
->fts_statp
->st_flags
, "none"));
270 * FLAGS2INDEX will fail once the user and system settable bits need more
271 * than one byte, respectively.
273 #define FLAGS2INDEX(x) (((x >> 8) & 0x0000ff00) | (x & 0x000000ff))
275 #define MTREE_MAXGID 5000
276 #define MTREE_MAXUID 5000
277 #define MTREE_MAXMODE (MBITS + 1)
278 #if HAVE_STRUCT_STAT_ST_FLAGS
279 #define MTREE_MAXFLAGS (FLAGS2INDEX(CH_MASK) + 1) /* 1808 */
281 #define MTREE_MAXFLAGS 1
283 #define MTREE_MAXS 16
286 statd(FTS
*t
, FTSENT
*parent
, uid_t
*puid
, gid_t
*pgid
, mode_t
*pmode
,
299 u_short maxgid
, maxuid
, maxmode
, maxflags
;
300 u_short g
[MTREE_MAXGID
], u
[MTREE_MAXUID
],
301 m
[MTREE_MAXMODE
], f
[MTREE_MAXFLAGS
];
302 static int first
= 1;
308 if ((p
= fts_children(t
, 0)) == NULL
) {
310 mtree_err("%s: %s", RP(parent
), strerror(errno
));
314 memset(g
, 0, sizeof(g
));
315 memset(u
, 0, sizeof(u
));
316 memset(m
, 0, sizeof(m
));
317 memset(f
, 0, sizeof(f
));
319 maxuid
= maxgid
= maxmode
= maxflags
= 0;
320 for (; p
; p
= p
->fts_link
) {
321 smode
= p
->fts_statp
->st_mode
& MBITS
;
322 if (smode
< MTREE_MAXMODE
&& ++m
[smode
] > maxmode
) {
326 sgid
= p
->fts_statp
->st_gid
;
327 if (sgid
< MTREE_MAXGID
&& ++g
[sgid
] > maxgid
) {
331 suid
= p
->fts_statp
->st_uid
;
332 if (suid
< MTREE_MAXUID
&& ++u
[suid
] > maxuid
) {
337 #if HAVE_STRUCT_STAT_ST_FLAGS
338 sflags
= FLAGS2INDEX(p
->fts_statp
->st_flags
);
339 if (sflags
< MTREE_MAXFLAGS
&& ++f
[sflags
] > maxflags
) {
340 saveflags
= p
->fts_statp
->st_flags
;
341 maxflags
= f
[sflags
];
346 * If the /set record is the same as the last one we do not need to
347 * output a new one. So first we check to see if anything changed.
348 * Note that we always output a /set record for the first directory.
350 if (((keys
& (F_UNAME
| F_UID
)) && (*puid
!= saveuid
)) ||
351 ((keys
& (F_GNAME
| F_GID
)) && (*pgid
!= savegid
)) ||
352 ((keys
& F_MODE
) && (*pmode
!= savemode
)) ||
353 ((keys
& F_FLAGS
) && (*pflags
!= saveflags
)) ||
356 printf("/set type=file");
357 if (keys
& (F_UID
| F_UNAME
)) {
358 if (keys
& F_UNAME
&&
359 (name
= user_from_uid(saveuid
, 1)) != NULL
)
360 printf(" uname=%s", name
);
361 else /* if (keys & F_UID) */
362 printf(" uid=%lu", (u_long
)saveuid
);
364 if (keys
& (F_GID
| F_GNAME
)) {
365 if (keys
& F_GNAME
&&
366 (name
= group_from_gid(savegid
, 1)) != NULL
)
367 printf(" gname=%s", name
);
368 else /* if (keys & F_UID) */
369 printf(" gid=%lu", (u_long
)savegid
);
372 printf(" mode=%#lo", (u_long
)savemode
);
377 flags_to_string(saveflags
, "none"));
389 * used as a comparison function passed to fts_open() to control
390 * the order in which fts_read() returns results. We make
391 * directories sort after non-directories, but otherwise sort in
394 * Keep this in sync with nodecmp() in spec.c.
397 dcmp(const FTSENT
**a
, const FTSENT
**b
)
400 if (S_ISDIR((*a
)->fts_statp
->st_mode
)) {
401 if (!S_ISDIR((*b
)->fts_statp
->st_mode
))
403 } else if (S_ISDIR((*b
)->fts_statp
->st_mode
))
405 return (strcmp((*a
)->fts_name
, (*b
)->fts_name
));
409 output(int *offset
, const char *fmt
, ...)
415 vsnprintf(buf
, sizeof(buf
), fmt
, ap
);
418 if (*offset
+ strlen(buf
) > MAXLINELEN
- 3) {
419 printf(" \\\n%*s", INDENTNAMELEN
, "");
420 *offset
= INDENTNAMELEN
;
422 *offset
+= printf(" %s", buf
) + 1;