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