]>
Commit | Line | Data |
---|---|---|
44a7a5ab A |
1 | /* $NetBSD: create.c,v 1.16 1998/08/30 03:20:09 nathanw Exp $ */ |
2 | ||
3 | /*- | |
4 | * Copyright (c) 1989, 1993 | |
5 | * The Regents of the University of California. All rights reserved. | |
6 | * | |
7 | * Redistribution and use in source and binary forms, with or without | |
8 | * modification, are permitted provided that the following conditions | |
9 | * are met: | |
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. All advertising materials mentioning features or use of this software | |
16 | * must display the following acknowledgement: | |
17 | * This product includes software developed by the University of | |
18 | * California, Berkeley and its contributors. | |
19 | * 4. Neither the name of the University nor the names of its contributors | |
20 | * may be used to endorse or promote products derived from this software | |
21 | * without specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
24 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
27 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
28 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
29 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
30 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
31 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
32 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
33 | * SUCH DAMAGE. | |
34 | */ | |
35 | ||
36 | #include <sys/cdefs.h> | |
37 | #ifndef lint | |
38 | #if 0 | |
39 | static char sccsid[] = "@(#)create.c 8.1 (Berkeley) 6/6/93"; | |
40 | #else | |
41 | __RCSID("$NetBSD: create.c,v 1.16 1998/08/30 03:20:09 nathanw Exp $"); | |
42 | #endif | |
43 | #endif /* not lint */ | |
44 | ||
45 | #include <sys/param.h> | |
46 | #include <sys/stat.h> | |
47 | #include <dirent.h> | |
48 | #include <errno.h> | |
49 | #include <fcntl.h> | |
50 | #include <fts.h> | |
51 | #include <grp.h> | |
52 | #include <pwd.h> | |
53 | #include <stdio.h> | |
54 | #include <string.h> | |
55 | #include <time.h> | |
56 | #include <unistd.h> | |
57 | #include "mtree.h" | |
58 | #include "extern.h" | |
59 | ||
60 | #define INDENTNAMELEN 15 | |
61 | #define MAXLINELEN 80 | |
62 | ||
63 | extern int crc_total, ftsoptions; | |
64 | extern int dflag, sflag; | |
65 | extern u_short keys; | |
66 | extern char fullpath[MAXPATHLEN]; | |
67 | ||
68 | static gid_t gid; | |
69 | static uid_t uid; | |
70 | static mode_t mode; | |
71 | ||
72 | static int dsort __P((const FTSENT **, const FTSENT **)); | |
73 | static void output __P((int *, const char *, ...)); | |
74 | static int statd __P((FTS *, FTSENT *, uid_t *, gid_t *, mode_t *)); | |
75 | static void statf __P((FTSENT *)); | |
76 | ||
77 | void | |
78 | cwalk() | |
79 | { | |
80 | FTS *t; | |
81 | FTSENT *p; | |
82 | time_t clock; | |
83 | char *argv[2], host[MAXHOSTNAMELEN + 1]; | |
84 | ||
85 | (void)time(&clock); | |
86 | (void)gethostname(host, sizeof(host)); | |
87 | host[sizeof(host) - 1] = '\0'; | |
88 | (void)printf( | |
89 | "#\t user: %s\n#\tmachine: %s\n#\t tree: %s\n#\t date: %s", | |
90 | getlogin(), host, fullpath, ctime(&clock)); | |
91 | ||
92 | argv[0] = "."; | |
93 | argv[1] = NULL; | |
94 | if ((t = fts_open(argv, ftsoptions, dsort)) == NULL) | |
95 | mtree_err("fts_open: %s", strerror(errno)); | |
96 | while ((p = fts_read(t)) != NULL) | |
97 | switch(p->fts_info) { | |
98 | case FTS_D: | |
99 | (void)printf("\n# %s\n", p->fts_path); | |
100 | statd(t, p, &uid, &gid, &mode); | |
101 | statf(p); | |
102 | break; | |
103 | case FTS_DP: | |
104 | if (p->fts_level > 0) | |
105 | (void)printf("# %s\n..\n\n", p->fts_path); | |
106 | break; | |
107 | case FTS_DNR: | |
108 | case FTS_ERR: | |
109 | case FTS_NS: | |
110 | (void)fprintf(stderr, | |
111 | "mtree: %s: %s\n", p->fts_path, strerror(errno)); | |
112 | break; | |
113 | default: | |
114 | if (!dflag) | |
115 | statf(p); | |
116 | break; | |
117 | ||
118 | } | |
119 | (void)fts_close(t); | |
120 | if (sflag && keys & F_CKSUM) | |
121 | (void)fprintf(stderr, | |
122 | "mtree: %s checksum: %u\n", fullpath, crc_total); | |
123 | } | |
124 | ||
125 | static void | |
126 | statf(p) | |
127 | FTSENT *p; | |
128 | { | |
129 | struct group *gr; | |
130 | struct passwd *pw; | |
131 | u_int32_t len, val; | |
132 | int fd, indent; | |
133 | ||
134 | if (S_ISDIR(p->fts_statp->st_mode)) | |
135 | indent = printf("%s", p->fts_name); | |
136 | else | |
137 | indent = printf(" %s", p->fts_name); | |
138 | ||
139 | if (indent > INDENTNAMELEN) | |
140 | indent = MAXLINELEN; | |
141 | else | |
142 | indent += printf("%*s", INDENTNAMELEN - indent, ""); | |
143 | ||
144 | if (!S_ISREG(p->fts_statp->st_mode)) | |
145 | output(&indent, "type=%s", inotype(p->fts_statp->st_mode)); | |
146 | if (keys & (F_UID | F_UNAME) && p->fts_statp->st_uid != uid) { | |
147 | if (keys & F_UNAME && (pw = getpwuid(p->fts_statp->st_uid))) | |
148 | output(&indent, "uname=%s", pw->pw_name); | |
149 | else /* if (keys & F_UID) */ | |
150 | output(&indent, "uid=%u", p->fts_statp->st_uid); | |
151 | } | |
152 | if (keys & (F_GID | F_GNAME) && p->fts_statp->st_gid != gid) { | |
153 | if (keys & F_GNAME && (gr = getgrgid(p->fts_statp->st_gid))) | |
154 | output(&indent, "gname=%s", gr->gr_name); | |
155 | else /* if (keys & F_GID) */ | |
156 | output(&indent, "gid=%u", p->fts_statp->st_gid); | |
157 | } | |
158 | if (keys & F_MODE && (p->fts_statp->st_mode & MBITS) != mode) | |
159 | output(&indent, "mode=%#o", p->fts_statp->st_mode & MBITS); | |
160 | if (keys & F_NLINK && p->fts_statp->st_nlink != 1) | |
161 | output(&indent, "nlink=%u", p->fts_statp->st_nlink); | |
162 | if (keys & F_SIZE && S_ISREG(p->fts_statp->st_mode)) | |
163 | output(&indent, "size=%qd", p->fts_statp->st_size); | |
164 | if (keys & F_TIME) | |
165 | output(&indent, "time=%ld.%ld", | |
166 | p->fts_statp->st_mtimespec.tv_sec, | |
167 | p->fts_statp->st_mtimespec.tv_nsec); | |
168 | if (keys & F_CKSUM && S_ISREG(p->fts_statp->st_mode)) { | |
169 | if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0 || | |
170 | crc(fd, &val, &len)) | |
171 | mtree_err("%s: %s", p->fts_accpath, strerror(errno)); | |
172 | (void)close(fd); | |
173 | output(&indent, "cksum=%lu", val); | |
174 | } | |
175 | if (keys & F_SLINK && | |
176 | (p->fts_info == FTS_SL || p->fts_info == FTS_SLNONE)) | |
177 | output(&indent, "link=%s", rlink(p->fts_accpath)); | |
178 | (void)putchar('\n'); | |
179 | } | |
180 | ||
181 | #define MAXGID 5000 | |
182 | #define MAXUID 5000 | |
183 | #define MAXMODE MBITS + 1 | |
184 | ||
185 | static int | |
186 | statd(t, parent, puid, pgid, pmode) | |
187 | FTS *t; | |
188 | FTSENT *parent; | |
189 | uid_t *puid; | |
190 | gid_t *pgid; | |
191 | mode_t *pmode; | |
192 | { | |
193 | FTSENT *p; | |
194 | gid_t sgid; | |
195 | uid_t suid; | |
196 | mode_t smode; | |
197 | struct group *gr; | |
198 | struct passwd *pw; | |
199 | gid_t savegid; | |
200 | uid_t saveuid; | |
201 | mode_t savemode; | |
202 | u_short maxgid, maxuid, maxmode, g[MAXGID], u[MAXUID], m[MAXMODE]; | |
203 | ||
204 | savegid = 0; | |
205 | saveuid = 0; | |
206 | savemode = 0; | |
207 | if ((p = fts_children(t, 0)) == NULL) { | |
208 | if (errno) | |
209 | mtree_err("%s: %s", RP(parent), strerror(errno)); | |
210 | return (1); | |
211 | } | |
212 | ||
213 | memset(g, 0, sizeof(g)); | |
214 | memset(u, 0, sizeof(u)); | |
215 | memset(m, 0, sizeof(m)); | |
216 | ||
217 | maxuid = maxgid = maxmode = 0; | |
218 | for (; p; p = p->fts_link) { | |
219 | smode = p->fts_statp->st_mode & MBITS; | |
220 | if (smode < MAXMODE && ++m[smode] > maxmode) { | |
221 | savemode = smode; | |
222 | maxmode = m[smode]; | |
223 | } | |
224 | sgid = p->fts_statp->st_gid; | |
225 | if (sgid < MAXGID && ++g[sgid] > maxgid) { | |
226 | savegid = sgid; | |
227 | maxgid = g[sgid]; | |
228 | } | |
229 | suid = p->fts_statp->st_uid; | |
230 | if (suid < MAXUID && ++u[suid] > maxuid) { | |
231 | saveuid = suid; | |
232 | maxuid = u[suid]; | |
233 | } | |
234 | } | |
235 | (void)printf("/set type=file"); | |
236 | if (keys & F_GID) | |
237 | (void)printf(" gid=%u", savegid); | |
238 | if (keys & F_GNAME) { | |
239 | if ((gr = getgrgid(savegid)) != NULL) | |
240 | (void)printf(" gname=%s", gr->gr_name); | |
241 | else | |
242 | (void)printf(" gid=%u", savegid); | |
243 | } | |
244 | if (keys & F_UNAME) { | |
245 | if ((pw = getpwuid(saveuid)) != NULL) | |
246 | (void)printf(" uname=%s", pw->pw_name); | |
247 | else | |
248 | (void)printf(" uid=%u", saveuid); | |
249 | } | |
250 | if (keys & F_UID) | |
251 | (void)printf(" uid=%u", saveuid); | |
252 | if (keys & F_MODE) | |
253 | (void)printf(" mode=%#o", savemode); | |
254 | if (keys & F_NLINK) | |
255 | (void)printf(" nlink=1"); | |
256 | (void)printf("\n"); | |
257 | *puid = saveuid; | |
258 | *pgid = savegid; | |
259 | *pmode = savemode; | |
260 | return (0); | |
261 | } | |
262 | ||
263 | static int | |
264 | dsort(a, b) | |
265 | const FTSENT **a, **b; | |
266 | { | |
267 | if (S_ISDIR((*a)->fts_statp->st_mode)) { | |
268 | if (!S_ISDIR((*b)->fts_statp->st_mode)) | |
269 | return (1); | |
270 | } else if (S_ISDIR((*b)->fts_statp->st_mode)) | |
271 | return (-1); | |
272 | return (strcmp((*a)->fts_name, (*b)->fts_name)); | |
273 | } | |
274 | ||
275 | #if __STDC__ | |
276 | #include <stdarg.h> | |
277 | #else | |
278 | #include <varargs.h> | |
279 | #endif | |
280 | ||
281 | void | |
282 | #if __STDC__ | |
283 | output(int *offset, const char *fmt, ...) | |
284 | #else | |
285 | output(offset, fmt, va_alist) | |
286 | int *offset; | |
287 | char *fmt; | |
288 | va_dcl | |
289 | #endif | |
290 | { | |
291 | va_list ap; | |
292 | char buf[1024]; | |
293 | #if __STDC__ | |
294 | va_start(ap, fmt); | |
295 | #else | |
296 | va_start(ap); | |
297 | #endif | |
298 | (void)vsnprintf(buf, sizeof(buf), fmt, ap); | |
299 | va_end(ap); | |
300 | ||
301 | if (*offset + strlen(buf) > MAXLINELEN - 3) { | |
302 | (void)printf(" \\\n%*s", INDENTNAMELEN, ""); | |
303 | *offset = INDENTNAMELEN; | |
304 | } | |
305 | *offset += printf(" %s", buf) + 1; | |
306 | } |