Deleted Added
full compact
create.c (124389) create.c (144295)
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
32static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93";
33#endif /* not lint */
34#endif
35#include <sys/cdefs.h>
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
32static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93";
33#endif /* not lint */
34#endif
35#include <sys/cdefs.h>
36__FBSDID("$FreeBSD: head/usr.sbin/mtree/create.c 124389 2004-01-11 19:38:48Z phk $");
36__FBSDID("$FreeBSD: head/usr.sbin/mtree/create.c 144295 2005-03-29 11:44:17Z tobez $");
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#ifdef MD5
47#include <md5.h>
48#endif
49#ifdef SHA1
50#include <sha.h>
51#endif
52#ifdef RMD160
53#include <ripemd.h>
54#endif
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#ifdef MD5
47#include <md5.h>
48#endif
49#ifdef SHA1
50#include <sha.h>
51#endif
52#ifdef RMD160
53#include <ripemd.h>
54#endif
55#ifdef SHA256
56#include <sha256.h>
57#endif
55#include <pwd.h>
56#include <stdint.h>
57#include <stdio.h>
58#include <time.h>
59#include <unistd.h>
60#include <vis.h>
61#include "mtree.h"
62#include "extern.h"
63
64#define INDENTNAMELEN 15
65#define MAXLINELEN 80
66
67static gid_t gid;
68static uid_t uid;
69static mode_t mode;
70static u_long flags = 0xffffffff;
71
72static int dsort(const FTSENT * const *, const FTSENT * const *);
73static void output(int, int *, const char *, ...) __printflike(3, 4);
74static int statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
75static void statf(int, FTSENT *);
76
77void
78cwalk(void)
79{
80 FTS *t;
81 FTSENT *p;
82 time_t cl;
83 char *argv[2], host[MAXHOSTNAMELEN];
84 char dot[] = ".";
85 int indent = 0;
86
87 if (!nflag) {
88 (void)time(&cl);
89 (void)gethostname(host, sizeof(host));
90 (void)printf(
91 "#\t user: %s\n#\tmachine: %s\n",
92 getlogin(), host);
93 (void)printf(
94 "#\t tree: %s\n#\t date: %s",
95 fullpath, ctime(&cl));
96 }
97
98 argv[0] = dot;
99 argv[1] = NULL;
100 if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
101 err(1, "fts_open()");
102 while ((p = fts_read(t))) {
103 if (iflag)
104 indent = p->fts_level * 4;
105 if (check_excludes(p->fts_name, p->fts_path)) {
106 fts_set(t, p, FTS_SKIP);
107 continue;
108 }
109 switch(p->fts_info) {
110 case FTS_D:
111 if (!dflag)
112 (void)printf("\n");
113 if (!nflag)
114 (void)printf("# %s\n", p->fts_path);
115 statd(t, p, &uid, &gid, &mode, &flags);
116 statf(indent, p);
117 break;
118 case FTS_DP:
119 if (!nflag && (p->fts_level > 0))
120 (void)printf("%*s# %s\n", indent, "", p->fts_path);
121 (void)printf("%*s..\n", indent, "");
122 if (!dflag)
123 (void)printf("\n");
124 break;
125 case FTS_DNR:
126 case FTS_ERR:
127 case FTS_NS:
128 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
129 break;
130 default:
131 if (!dflag)
132 statf(indent, p);
133 break;
134
135 }
136 }
137 (void)fts_close(t);
138 if (sflag && keys & F_CKSUM)
139 warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total);
140}
141
142static void
143statf(int indent, FTSENT *p)
144{
145 struct group *gr;
146 struct passwd *pw;
147 uint32_t val;
148 off_t len;
149 int fd, offset;
150 char *fflags;
151 char *escaped_name;
152
153 escaped_name = calloc(1, p->fts_namelen * 4 + 1);
154 if (escaped_name == NULL)
155 errx(1, "statf(): calloc() failed");
156 strvis(escaped_name, p->fts_name, VIS_WHITE | VIS_OCTAL | VIS_GLOB);
157
158 if (iflag || S_ISDIR(p->fts_statp->st_mode))
159 offset = printf("%*s%s", indent, "", escaped_name);
160 else
161 offset = printf("%*s %s", indent, "", escaped_name);
162
163 free(escaped_name);
164
165 if (offset > (INDENTNAMELEN + indent))
166 offset = MAXLINELEN;
167 else
168 offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
169
170 if (!S_ISREG(p->fts_statp->st_mode) && !dflag)
171 output(indent, &offset, "type=%s", inotype(p->fts_statp->st_mode));
172 if (p->fts_statp->st_uid != uid) {
173 if (keys & F_UNAME) {
174 pw = getpwuid(p->fts_statp->st_uid);
175 if (pw != NULL)
176 output(indent, &offset, "uname=%s", pw->pw_name);
177 else if (wflag)
178 warnx("Could not get uname for uid=%u",
179 p->fts_statp->st_uid);
180 else
181 errx(1,
182 "Could not get uname for uid=%u",
183 p->fts_statp->st_uid);
184 }
185 if (keys & F_UID)
186 output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
187 }
188 if (p->fts_statp->st_gid != gid) {
189 if (keys & F_GNAME) {
190 gr = getgrgid(p->fts_statp->st_gid);
191 if (gr != NULL)
192 output(indent, &offset, "gname=%s", gr->gr_name);
193 else if (wflag)
194 warnx("Could not get gname for gid=%u",
195 p->fts_statp->st_gid);
196 else
197 errx(1,
198 "Could not get gname for gid=%u",
199 p->fts_statp->st_gid);
200 }
201 if (keys & F_GID)
202 output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
203 }
204 if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode)
205 output(indent, &offset, "mode=%#o", p->fts_statp->st_mode & MBITS);
206 if (keys & F_NLINK && p->fts_statp->st_nlink != 1)
207 output(indent, &offset, "nlink=%u", p->fts_statp->st_nlink);
208 if (keys & F_SIZE)
209 output(indent, &offset, "size=%jd",
210 (intmax_t)p->fts_statp->st_size);
211 if (keys & F_TIME)
212 output(indent, &offset, "time=%ld.%ld",
213 (long)p->fts_statp->st_mtimespec.tv_sec,
214 p->fts_statp->st_mtimespec.tv_nsec);
215 if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) {
216 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 ||
217 crc(fd, &val, &len))
218 err(1, "%s", p->fts_accpath);
219 (void)close(fd);
220 output(indent, &offset, "cksum=%lu", (unsigned long)val);
221 }
222#ifdef MD5
223 if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
224 char *digest, buf[33];
225
226 digest = MD5File(p->fts_accpath, buf);
227 if (!digest)
228 err(1, "%s", p->fts_accpath);
229 output(indent, &offset, "md5digest=%s", digest);
230 }
231#endif /* MD5 */
232#ifdef SHA1
233 if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
234 char *digest, buf[41];
235
236 digest = SHA1_File(p->fts_accpath, buf);
237 if (!digest)
238 err(1, "%s", p->fts_accpath);
239 output(indent, &offset, "sha1digest=%s", digest);
240 }
241#endif /* SHA1 */
242#ifdef RMD160
243 if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
244 char *digest, buf[41];
245
246 digest = RIPEMD160_File(p->fts_accpath, buf);
247 if (!digest)
248 err(1, "%s", p->fts_accpath);
249 output(indent, &offset, "ripemd160digest=%s", digest);
250 }
251#endif /* RMD160 */
58#include <pwd.h>
59#include <stdint.h>
60#include <stdio.h>
61#include <time.h>
62#include <unistd.h>
63#include <vis.h>
64#include "mtree.h"
65#include "extern.h"
66
67#define INDENTNAMELEN 15
68#define MAXLINELEN 80
69
70static gid_t gid;
71static uid_t uid;
72static mode_t mode;
73static u_long flags = 0xffffffff;
74
75static int dsort(const FTSENT * const *, const FTSENT * const *);
76static void output(int, int *, const char *, ...) __printflike(3, 4);
77static int statd(FTS *, FTSENT *, uid_t *, gid_t *, mode_t *, u_long *);
78static void statf(int, FTSENT *);
79
80void
81cwalk(void)
82{
83 FTS *t;
84 FTSENT *p;
85 time_t cl;
86 char *argv[2], host[MAXHOSTNAMELEN];
87 char dot[] = ".";
88 int indent = 0;
89
90 if (!nflag) {
91 (void)time(&cl);
92 (void)gethostname(host, sizeof(host));
93 (void)printf(
94 "#\t user: %s\n#\tmachine: %s\n",
95 getlogin(), host);
96 (void)printf(
97 "#\t tree: %s\n#\t date: %s",
98 fullpath, ctime(&cl));
99 }
100
101 argv[0] = dot;
102 argv[1] = NULL;
103 if ((t = fts_open(argv, ftsoptions, dsort)) == NULL)
104 err(1, "fts_open()");
105 while ((p = fts_read(t))) {
106 if (iflag)
107 indent = p->fts_level * 4;
108 if (check_excludes(p->fts_name, p->fts_path)) {
109 fts_set(t, p, FTS_SKIP);
110 continue;
111 }
112 switch(p->fts_info) {
113 case FTS_D:
114 if (!dflag)
115 (void)printf("\n");
116 if (!nflag)
117 (void)printf("# %s\n", p->fts_path);
118 statd(t, p, &uid, &gid, &mode, &flags);
119 statf(indent, p);
120 break;
121 case FTS_DP:
122 if (!nflag && (p->fts_level > 0))
123 (void)printf("%*s# %s\n", indent, "", p->fts_path);
124 (void)printf("%*s..\n", indent, "");
125 if (!dflag)
126 (void)printf("\n");
127 break;
128 case FTS_DNR:
129 case FTS_ERR:
130 case FTS_NS:
131 warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
132 break;
133 default:
134 if (!dflag)
135 statf(indent, p);
136 break;
137
138 }
139 }
140 (void)fts_close(t);
141 if (sflag && keys & F_CKSUM)
142 warnx("%s checksum: %lu", fullpath, (unsigned long)crc_total);
143}
144
145static void
146statf(int indent, FTSENT *p)
147{
148 struct group *gr;
149 struct passwd *pw;
150 uint32_t val;
151 off_t len;
152 int fd, offset;
153 char *fflags;
154 char *escaped_name;
155
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);
160
161 if (iflag || S_ISDIR(p->fts_statp->st_mode))
162 offset = printf("%*s%s", indent, "", escaped_name);
163 else
164 offset = printf("%*s %s", indent, "", escaped_name);
165
166 free(escaped_name);
167
168 if (offset > (INDENTNAMELEN + indent))
169 offset = MAXLINELEN;
170 else
171 offset += printf("%*s", (INDENTNAMELEN + indent) - offset, "");
172
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);
178 if (pw != NULL)
179 output(indent, &offset, "uname=%s", pw->pw_name);
180 else if (wflag)
181 warnx("Could not get uname for uid=%u",
182 p->fts_statp->st_uid);
183 else
184 errx(1,
185 "Could not get uname for uid=%u",
186 p->fts_statp->st_uid);
187 }
188 if (keys & F_UID)
189 output(indent, &offset, "uid=%u", p->fts_statp->st_uid);
190 }
191 if (p->fts_statp->st_gid != gid) {
192 if (keys & F_GNAME) {
193 gr = getgrgid(p->fts_statp->st_gid);
194 if (gr != NULL)
195 output(indent, &offset, "gname=%s", gr->gr_name);
196 else if (wflag)
197 warnx("Could not get gname for gid=%u",
198 p->fts_statp->st_gid);
199 else
200 errx(1,
201 "Could not get gname for gid=%u",
202 p->fts_statp->st_gid);
203 }
204 if (keys & F_GID)
205 output(indent, &offset, "gid=%u", p->fts_statp->st_gid);
206 }
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);
211 if (keys & F_SIZE)
212 output(indent, &offset, "size=%jd",
213 (intmax_t)p->fts_statp->st_size);
214 if (keys & F_TIME)
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 ||
220 crc(fd, &val, &len))
221 err(1, "%s", p->fts_accpath);
222 (void)close(fd);
223 output(indent, &offset, "cksum=%lu", (unsigned long)val);
224 }
225#ifdef MD5
226 if (keys & F_MD5 && S_ISREG(p->fts_statp->st_mode)) {
227 char *digest, buf[33];
228
229 digest = MD5File(p->fts_accpath, buf);
230 if (!digest)
231 err(1, "%s", p->fts_accpath);
232 output(indent, &offset, "md5digest=%s", digest);
233 }
234#endif /* MD5 */
235#ifdef SHA1
236 if (keys & F_SHA1 && S_ISREG(p->fts_statp->st_mode)) {
237 char *digest, buf[41];
238
239 digest = SHA1_File(p->fts_accpath, buf);
240 if (!digest)
241 err(1, "%s", p->fts_accpath);
242 output(indent, &offset, "sha1digest=%s", digest);
243 }
244#endif /* SHA1 */
245#ifdef RMD160
246 if (keys & F_RMD160 && S_ISREG(p->fts_statp->st_mode)) {
247 char *digest, buf[41];
248
249 digest = RIPEMD160_File(p->fts_accpath, buf);
250 if (!digest)
251 err(1, "%s", p->fts_accpath);
252 output(indent, &offset, "ripemd160digest=%s", digest);
253 }
254#endif /* RMD160 */
255#ifdef SHA256
256 if (keys & F_SHA256 && S_ISREG(p->fts_statp->st_mode)) {
257 char *digest, buf[65];
258
259 digest = SHA256_File(p->fts_accpath, buf);
260 if (!digest)
261 err(1, "%s", p->fts_accpath);
262 output(indent, &offset, "sha256digest=%s", digest);
263 }
264#endif /* SHA256 */
252 if (keys & F_SLINK &&
253 (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE))
254 output(indent, &offset, "link=%s", rlink(p->fts_accpath));
255 if (keys & F_FLAGS && p->fts_statp->st_flags != flags) {
256 fflags = flags_to_string(p->fts_statp->st_flags);
257 output(indent, &offset, "flags=%s", fflags);
258 free(fflags);
259 }
260 (void)putchar('\n');
261}
262
263#define MAXGID 5000
264#define MAXUID 5000
265#define MAXMODE MBITS + 1
266#define MAXFLAGS 256
267#define MAXS 16
268
269static int
270statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode, u_long *pflags)
271{
272 FTSENT *p;
273 gid_t sgid;
274 uid_t suid;
275 mode_t smode;
276 u_long sflags;
277 struct group *gr;
278 struct passwd *pw;
279 gid_t savegid = *pgid;
280 uid_t saveuid = *puid;
281 mode_t savemode = *pmode;
282 u_long saveflags = *pflags;
283 u_short maxgid, maxuid, maxmode, maxflags;
284 u_short g[MAXGID], u[MAXUID], m[MAXMODE], f[MAXFLAGS];
285 char *fflags;
286 static int first = 1;
287
288 if ((p = fts_children(t, 0)) == NULL) {
289 if (errno)
290 err(1, "%s", RP(parent));
291 return (1);
292 }
293
294 bzero(g, sizeof(g));
295 bzero(u, sizeof(u));
296 bzero(m, sizeof(m));
297 bzero(f, sizeof(f));
298
299 maxuid = maxgid = maxmode = maxflags = 0;
300 for (; p; p = p->fts_link) {
301 if (!dflag || (dflag && S_ISDIR(p->fts_statp->st_mode))) {
302 smode = p->fts_statp->st_mode & MBITS;
303 if (smode < MAXMODE && ++m[smode] > maxmode) {
304 savemode = smode;
305 maxmode = m[smode];
306 }
307 sgid = p->fts_statp->st_gid;
308 if (sgid < MAXGID && ++g[sgid] > maxgid) {
309 savegid = sgid;
310 maxgid = g[sgid];
311 }
312 suid = p->fts_statp->st_uid;
313 if (suid < MAXUID && ++u[suid] > maxuid) {
314 saveuid = suid;
315 maxuid = u[suid];
316 }
317
318 /*
319 * XXX
320 * note that the below will break when file flags
321 * are extended beyond the first 4 bytes of each
322 * half word of the flags
323 */
324#define FLAGS2IDX(f) ((f & 0xf) | ((f >> 12) & 0xf0))
325 sflags = p->fts_statp->st_flags;
326 if (FLAGS2IDX(sflags) < MAXFLAGS &&
327 ++f[FLAGS2IDX(sflags)] > maxflags) {
328 saveflags = sflags;
329 maxflags = f[FLAGS2IDX(sflags)];
330 }
331 }
332 }
333 /*
334 * If the /set record is the same as the last one we do not need to output
335 * a new one. So first we check to see if anything changed. Note that we
336 * always output a /set record for the first directory.
337 */
338 if ((((keys & F_UNAME) | (keys & F_UID)) && (*puid != saveuid)) ||
339 (((keys & F_GNAME) | (keys & F_GID)) && (*pgid != savegid)) ||
340 ((keys & F_MODE) && (*pmode != savemode)) ||
341 ((keys & F_FLAGS) && (*pflags != saveflags)) ||
342 (first)) {
343 first = 0;
344 if (dflag)
345 (void)printf("/set type=dir");
346 else
347 (void)printf("/set type=file");
348 if (keys & F_UNAME) {
349 pw = getpwuid(saveuid);
350 if (pw != NULL)
351 (void)printf(" uname=%s", pw->pw_name);
352 else if (wflag)
353 warnx( "Could not get uname for uid=%u", saveuid);
354 else
355 errx(1, "Could not get uname for uid=%u", saveuid);
356 }
357 if (keys & F_UID)
358 (void)printf(" uid=%lu", (u_long)saveuid);
359 if (keys & F_GNAME) {
360 gr = getgrgid(savegid);
361 if (gr != NULL)
362 (void)printf(" gname=%s", gr->gr_name);
363 else if (wflag)
364 warnx("Could not get gname for gid=%u", savegid);
365 else
366 errx(1, "Could not get gname for gid=%u", savegid);
367 }
368 if (keys & F_GID)
369 (void)printf(" gid=%lu", (u_long)savegid);
370 if (keys & F_MODE)
371 (void)printf(" mode=%#o", savemode);
372 if (keys & F_NLINK)
373 (void)printf(" nlink=1");
374 if (keys & F_FLAGS) {
375 fflags = flags_to_string(saveflags);
376 (void)printf(" flags=%s", fflags);
377 free(fflags);
378 }
379 (void)printf("\n");
380 *puid = saveuid;
381 *pgid = savegid;
382 *pmode = savemode;
383 *pflags = saveflags;
384 }
385 return (0);
386}
387
388static int
389dsort(const FTSENT * const *a, const FTSENT * const *b)
390{
391 if (S_ISDIR((*a)->fts_statp->st_mode)) {
392 if (!S_ISDIR((*b)->fts_statp->st_mode))
393 return (1);
394 } else if (S_ISDIR((*b)->fts_statp->st_mode))
395 return (-1);
396 return (strcmp((*a)->fts_name, (*b)->fts_name));
397}
398
399#include <stdarg.h>
400
401void
402output(int indent, int *offset, const char *fmt, ...)
403{
404 va_list ap;
405 char buf[1024];
406 va_start(ap, fmt);
407 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
408 va_end(ap);
409
410 if (*offset + strlen(buf) > MAXLINELEN - 3) {
411 (void)printf(" \\\n%*s", INDENTNAMELEN + indent, "");
412 *offset = INDENTNAMELEN + indent;
413 }
414 *offset += printf(" %s", buf) + 1;
415}
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);
271 free(fflags);
272 }
273 (void)putchar('\n');
274}
275
276#define MAXGID 5000
277#define MAXUID 5000
278#define MAXMODE MBITS + 1
279#define MAXFLAGS 256
280#define MAXS 16
281
282static int
283statd(FTS *t, FTSENT *parent, uid_t *puid, gid_t *pgid, mode_t *pmode, u_long *pflags)
284{
285 FTSENT *p;
286 gid_t sgid;
287 uid_t suid;
288 mode_t smode;
289 u_long sflags;
290 struct group *gr;
291 struct passwd *pw;
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];
298 char *fflags;
299 static int first = 1;
300
301 if ((p = fts_children(t, 0)) == NULL) {
302 if (errno)
303 err(1, "%s", RP(parent));
304 return (1);
305 }
306
307 bzero(g, sizeof(g));
308 bzero(u, sizeof(u));
309 bzero(m, sizeof(m));
310 bzero(f, sizeof(f));
311
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) {
317 savemode = smode;
318 maxmode = m[smode];
319 }
320 sgid = p->fts_statp->st_gid;
321 if (sgid < MAXGID && ++g[sgid] > maxgid) {
322 savegid = sgid;
323 maxgid = g[sgid];
324 }
325 suid = p->fts_statp->st_uid;
326 if (suid < MAXUID && ++u[suid] > maxuid) {
327 saveuid = suid;
328 maxuid = u[suid];
329 }
330
331 /*
332 * XXX
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
336 */
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) {
341 saveflags = sflags;
342 maxflags = f[FLAGS2IDX(sflags)];
343 }
344 }
345 }
346 /*
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.
350 */
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)) ||
355 (first)) {
356 first = 0;
357 if (dflag)
358 (void)printf("/set type=dir");
359 else
360 (void)printf("/set type=file");
361 if (keys & F_UNAME) {
362 pw = getpwuid(saveuid);
363 if (pw != NULL)
364 (void)printf(" uname=%s", pw->pw_name);
365 else if (wflag)
366 warnx( "Could not get uname for uid=%u", saveuid);
367 else
368 errx(1, "Could not get uname for uid=%u", saveuid);
369 }
370 if (keys & F_UID)
371 (void)printf(" uid=%lu", (u_long)saveuid);
372 if (keys & F_GNAME) {
373 gr = getgrgid(savegid);
374 if (gr != NULL)
375 (void)printf(" gname=%s", gr->gr_name);
376 else if (wflag)
377 warnx("Could not get gname for gid=%u", savegid);
378 else
379 errx(1, "Could not get gname for gid=%u", savegid);
380 }
381 if (keys & F_GID)
382 (void)printf(" gid=%lu", (u_long)savegid);
383 if (keys & F_MODE)
384 (void)printf(" mode=%#o", savemode);
385 if (keys & F_NLINK)
386 (void)printf(" nlink=1");
387 if (keys & F_FLAGS) {
388 fflags = flags_to_string(saveflags);
389 (void)printf(" flags=%s", fflags);
390 free(fflags);
391 }
392 (void)printf("\n");
393 *puid = saveuid;
394 *pgid = savegid;
395 *pmode = savemode;
396 *pflags = saveflags;
397 }
398 return (0);
399}
400
401static int
402dsort(const FTSENT * const *a, const FTSENT * const *b)
403{
404 if (S_ISDIR((*a)->fts_statp->st_mode)) {
405 if (!S_ISDIR((*b)->fts_statp->st_mode))
406 return (1);
407 } else if (S_ISDIR((*b)->fts_statp->st_mode))
408 return (-1);
409 return (strcmp((*a)->fts_name, (*b)->fts_name));
410}
411
412#include <stdarg.h>
413
414void
415output(int indent, int *offset, const char *fmt, ...)
416{
417 va_list ap;
418 char buf[1024];
419 va_start(ap, fmt);
420 (void)vsnprintf(buf, sizeof(buf), fmt, ap);
421 va_end(ap);
422
423 if (*offset + strlen(buf) > MAXLINELEN - 3) {
424 (void)printf(" \\\n%*s", INDENTNAMELEN + indent, "");
425 *offset = INDENTNAMELEN + indent;
426 }
427 *offset += printf(" %s", buf) + 1;
428}