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