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