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