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