Merge branch 'vendor/LIBPCAP'
[dragonfly.git] / usr.sbin / mtree / verify.c
1 /*-
2  * Copyright (c) 1990, 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. 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  * @(#)verify.c 8.1 (Berkeley) 6/6/93
30  * $FreeBSD: src/usr.sbin/mtree/verify.c,v 1.10.2.2 2001/01/12 19:17:18 phk Exp $
31  */
32
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <dirent.h>
36 #include <err.h>
37 #include <errno.h>
38 #include <fts.h>
39 #include <fnmatch.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include "mtree.h"
43 #include "extern.h"
44
45 static NODE *root;
46 static char path[MAXPATHLEN];
47
48 static void     miss(NODE *, char *);
49 static int      vwalk(void);
50
51 int
52 verify(void)
53 {
54         int rval;
55
56         root = spec();
57         rval = vwalk();
58         miss(root, path);
59         return (rval);
60 }
61
62 static int
63 vwalk(void)
64 {
65         FTS *t;
66         FTSENT *p;
67         NODE *ep, *level;
68         int specdepth, rval;
69         char *argv[2], dot[] = ".";
70
71         argv[0] = dot;
72         argv[1] = NULL;
73         if ((t = fts_open(argv, ftsoptions, NULL)) == NULL)
74                 err(1, "line %d: fts_open", lineno);
75         level = root;
76         specdepth = rval = 0;
77         while ((p = fts_read(t))) {
78                 if (check_excludes(p->fts_name, p->fts_path)) {
79                         fts_set(t, p, FTS_SKIP);
80                         continue;
81                 }
82                 switch(p->fts_info) {
83                 case FTS_D:
84                 case FTS_SL:
85                         break;
86                 case FTS_DP:
87                         if (specdepth > p->fts_level) {
88                                 for (level = level->parent; level->prev;
89                                       level = level->prev);
90                                 --specdepth;
91                         }
92                         continue;
93                 case FTS_DNR:
94                 case FTS_ERR:
95                 case FTS_NS:
96                         warnx("%s: %s", RP(p), strerror(p->fts_errno));
97                         continue;
98                 default:
99                         if (dflag)
100                                 continue;
101                 }
102
103                 if (specdepth != p->fts_level)
104                         goto extra;
105                 for (ep = level; ep; ep = ep->next)
106                         if ((ep->flags & F_MAGIC &&
107                             !fnmatch(ep->name, p->fts_name, FNM_PATHNAME)) ||
108                             !strcmp(ep->name, p->fts_name)) {
109                                 ep->flags |= F_VISIT;
110                                 if ((ep->flags & F_NOCHANGE) == 0 &&
111                                     compare(ep, p))
112                                         rval = MISMATCHEXIT;
113                                 if (ep->flags & F_IGN)
114                                         fts_set(t, p, FTS_SKIP);
115                                 else if (ep->child && ep->type == F_DIR &&
116                                     p->fts_info == FTS_D) {
117                                         level = ep->child;
118                                         ++specdepth;
119                                 }
120                                 break;
121                         }
122
123                 if (ep)
124                         continue;
125 extra:
126                 if (!eflag) {
127                         printf("%s extra", RP(p));
128                         if (rflag) {
129                                 if ((S_ISDIR(p->fts_statp->st_mode)
130                                     ? rmdir : unlink)(p->fts_accpath)) {
131                                         printf(", not removed: %s",
132                                             strerror(errno));
133                                 } else
134                                         printf(", removed");
135                         }
136                         putchar('\n');
137                 }
138                 fts_set(t, p, FTS_SKIP);
139         }
140         fts_close(t);
141         if (sflag)
142                 warnx("%s checksum: %u", fullpath, crc_total);
143         return (rval);
144 }
145
146 static void
147 miss(NODE *p, char *tail)
148 {
149         int create;
150         char *tp;
151         const char *type;
152
153         for (; p; p = p->next) {
154                 if (p->type != F_DIR && (dflag || p->flags & F_VISIT))
155                         continue;
156                 strcpy(tail, p->name);
157                 if (!(p->flags & F_VISIT)) {
158                         /* Don't print missing message if file exists as a
159                            symbolic link and the -q flag is set. */
160                         struct stat statbuf;
161  
162                         if (qflag && stat(path, &statbuf) == 0)
163                                 p->flags |= F_VISIT;
164                         else
165                                 printf("%s missing", path);
166                 }
167                 if (p->type != F_DIR && p->type != F_LINK) {
168                         putchar('\n');
169                         continue;
170                 }
171
172                 create = 0;
173                 if (p->type == F_LINK)
174                         type = "symlink";
175                 else
176                         type = "directory";
177                 if (!(p->flags & F_VISIT) && uflag) {
178                         if (!(p->flags & (F_UID | F_UNAME)))
179                                 printf(" (%s not created: user not specified)", type);
180                         else if (!(p->flags & (F_GID | F_GNAME)))
181                                 printf(" (%s not created: group not specified)", type);
182                         else if (p->type == F_LINK) {
183                                 if (symlink(p->slink, path))
184                                         printf(" (symlink not created: %s)\n",
185                                             strerror(errno));
186                                 else
187                                         printf(" (created)\n");
188                                 if (lchown(path, p->st_uid, p->st_gid))
189                                         printf("%s: user/group not modified: %s\n",
190                                             path, strerror(errno));
191                                 continue;
192                         } else if (!(p->flags & F_MODE))
193                             printf(" (directory not created: mode not specified)");
194                         else if (mkdir(path, S_IRWXU))
195                                 printf(" (directory not created: %s)",
196                                     strerror(errno));
197                         else {
198                                 create = 1;
199                                 printf(" (created)");
200                         }
201                 }
202                 if (!(p->flags & F_VISIT))
203                         putchar('\n');
204
205                 for (tp = tail; *tp; ++tp);
206                 *tp = '/';
207                 miss(p->child, tp + 1);
208                 *tp = '\0';
209
210                 if (!create)
211                         continue;
212                 if (chown(path, p->st_uid, p->st_gid)) {
213                         printf("%s: user/group/mode not modified: %s\n",
214                             path, strerror(errno));
215                         printf("%s: warning: file mode %snot set\n", path,
216                             (p->flags & F_FLAGS) ? "and file flags " : "");
217                         continue;
218                 }
219                 if (chmod(path, p->st_mode))
220                         printf("%s: permissions not set: %s\n",
221                             path, strerror(errno));
222                 if ((p->flags & F_FLAGS) && p->st_flags &&
223                     chflags(path, p->st_flags))
224                         printf("%s: file flags not set: %s\n",
225                             path, strerror(errno));
226         }
227 }