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