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