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