Merge branch 'vendor/BINUTILS221'
[dragonfly.git] / usr.sbin / mtree / compare.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  * @(#)compare.c        8.1 (Berkeley) 6/6/93
34  * $FreeBSD: src/usr.sbin/mtree/compare.c,v 1.15.2.4 2003/05/07 17:55:17 tobez Exp $
35  * $DragonFly: src/usr.sbin/mtree/compare.c,v 1.5 2004/03/15 16:24:22 dillon Exp $
36  */
37
38 #include <sys/param.h>
39 #include <sys/stat.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <fts.h>
44 #ifdef MD5
45 #include <md5.h>
46 #endif
47 #ifdef SHA1
48 #include <sha.h>
49 #endif
50 #ifdef RMD160
51 #include <ripemd.h>
52 #endif
53 #include <stdio.h>
54 #include <time.h>
55 #include <unistd.h>
56 #include "mtree.h"
57 #include "extern.h"
58
59 static const char *ftype(u_int);
60
61 #define INDENTNAMELEN   8
62 #define LABEL \
63         if (!label++) { \
64                 len = printf("%s changed\n", RP(p)); \
65                 tab = "\t"; \
66         }
67
68 int
69 compare(NODE *s, FTSENT *p)
70 {
71         u_long len, val;
72         int fd, label;
73         char *cp, *fflags;
74         const char *tab = "";
75
76         label = 0;
77         switch(s->type) {
78         case F_BLOCK:
79                 if (!S_ISBLK(p->fts_statp->st_mode))
80                         goto typeerr;
81                 break;
82         case F_CHAR:
83                 if (!S_ISCHR(p->fts_statp->st_mode))
84                         goto typeerr;
85                 break;
86         case F_DIR:
87                 if (!S_ISDIR(p->fts_statp->st_mode))
88                         goto typeerr;
89                 break;
90         case F_FIFO:
91                 if (!S_ISFIFO(p->fts_statp->st_mode))
92                         goto typeerr;
93                 break;
94         case F_FILE:
95                 if (!S_ISREG(p->fts_statp->st_mode))
96                         goto typeerr;
97                 break;
98         case F_LINK:
99                 if (!S_ISLNK(p->fts_statp->st_mode))
100                         goto typeerr;
101                 break;
102         case F_SOCK:
103                 if (!S_ISSOCK(p->fts_statp->st_mode)) {
104 typeerr:                LABEL;
105                         printf("\ttype expected %s found %s\n",
106                             ftype(s->type), inotype(p->fts_statp->st_mode));
107                         return (label);
108                 }
109                 break;
110         }
111         /* Set the uid/gid first, then set the mode. */
112         if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
113                 LABEL;
114                 printf("%suser expected %lu found %lu",
115                     tab, (u_long)s->st_uid, (u_long)p->fts_statp->st_uid);
116                 if (uflag)
117                         if (chown(p->fts_accpath, s->st_uid, -1))
118                                 printf(" not modified: %s\n",
119                                     strerror(errno));
120                         else
121                                 printf(" modified\n");
122                 else
123                         printf("\n");
124                 tab = "\t";
125         }
126         if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
127                 LABEL;
128                 printf("%sgid expected %lu found %lu",
129                     tab, (u_long)s->st_gid, (u_long)p->fts_statp->st_gid);
130                 if (uflag)
131                         if (chown(p->fts_accpath, -1, s->st_gid))
132                                 printf(" not modified: %s\n",
133                                     strerror(errno));
134                         else
135                                 printf(" modified\n");
136                 else
137                         printf("\n");
138                 tab = "\t";
139         }
140         if (s->flags & F_MODE &&
141             !S_ISLNK(p->fts_statp->st_mode) &&
142             s->st_mode != (p->fts_statp->st_mode & MBITS)) {
143                 LABEL;
144                 printf("%spermissions expected %#o found %#o",
145                     tab, s->st_mode, p->fts_statp->st_mode & MBITS);
146                 if (uflag)
147                         if (chmod(p->fts_accpath, s->st_mode))
148                                 printf(" not modified: %s\n",
149                                     strerror(errno));
150                         else
151                                 printf(" modified\n");
152                 else
153                         printf("\n");
154                 tab = "\t";
155         }
156         if (s->flags & F_NLINK && s->type != F_DIR &&
157             s->st_nlink != p->fts_statp->st_nlink) {
158                 LABEL;
159                 printf("%slink_count expected %u found %u\n",
160                     tab, s->st_nlink, p->fts_statp->st_nlink);
161                 tab = "\t";
162         }
163         if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size &&
164                 !S_ISDIR(p->fts_statp->st_mode)) {
165                 LABEL;
166                 printf("%ssize expected %jd found %jd\n", tab,
167                     (intmax_t)s->st_size, (intmax_t)p->fts_statp->st_size);
168                 tab = "\t";
169         }
170         /*
171          * XXX
172          * Catches nano-second differences, but doesn't display them.
173          */
174         if ((s->flags & F_TIME) &&
175              ((s->st_mtimespec.tv_sec != p->fts_statp->st_mtimespec.tv_sec) ||
176              (s->st_mtimespec.tv_nsec != p->fts_statp->st_mtimespec.tv_nsec))) {
177                 LABEL;
178                 printf("%smodification time expected %.24s ",
179                     tab, ctime(&s->st_mtimespec.tv_sec));
180                 printf("found %.24s\n",
181                     ctime(&p->fts_statp->st_mtimespec.tv_sec));
182                 tab = "\t";
183         }
184         if (s->flags & F_CKSUM) {
185                 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
186                         LABEL;
187                         printf("%scksum: %s: %s\n",
188                             tab, p->fts_accpath, strerror(errno));
189                         tab = "\t";
190                 } else if (crc(fd, &val, &len)) {
191                         close(fd);
192                         LABEL;
193                         printf("%scksum: %s: %s\n",
194                             tab, p->fts_accpath, strerror(errno));
195                         tab = "\t";
196                 } else {
197                         close(fd);
198                         if (s->cksum != val) {
199                                 LABEL;
200                                 printf("%scksum expected %lu found %lu\n",
201                                     tab, s->cksum, val);
202                                 tab = "\t";
203                         }
204                 }
205         }
206         /*
207          * XXX
208          * since chflags(2) will reset file times, the utimes() above
209          * may have been useless!  oh well, we'd rather have correct
210          * flags, rather than times?
211          */
212         if ((s->flags & F_FLAGS) && s->st_flags != p->fts_statp->st_flags) {
213                 LABEL;
214                 fflags = flags_to_string(s->st_flags);
215                 printf("%sflags expected \"%s\"", tab, fflags);
216                 free(fflags);
217
218                 fflags = flags_to_string(p->fts_statp->st_flags);
219                 printf(" found \"%s\"", fflags);
220                 free(fflags);
221
222                 if (uflag)
223                         if (chflags(p->fts_accpath, s->st_flags))
224                                 printf(" not modified: %s\n",
225                                     strerror(errno));
226                         else
227                                 printf(" modified\n");
228                 else
229                         printf("\n");
230                 tab = "\t";
231         }
232 #ifdef MD5
233         if (s->flags & F_MD5) {
234                 char *new_digest, buf[33];
235
236                 new_digest = MD5File(p->fts_accpath, buf);
237                 if (!new_digest) {
238                         LABEL;
239                         printf("%sMD5: %s: %s\n", tab, p->fts_accpath,
240                                strerror(errno));
241                         tab = "\t";
242                 } else if (strcmp(new_digest, s->md5digest)) {
243                         LABEL;
244                         printf("%sMD5 expected %s found %s\n", tab, s->md5digest,
245                                new_digest);
246                         tab = "\t";
247                 }
248         }
249 #endif /* MD5 */
250 #ifdef SHA1
251         if (s->flags & F_SHA1) {
252                 char *new_digest, buf[41];
253
254                 new_digest = SHA1_File(p->fts_accpath, buf);
255                 if (!new_digest) {
256                         LABEL;
257                         printf("%sSHA-1: %s: %s\n", tab, p->fts_accpath,
258                                strerror(errno));
259                         tab = "\t";
260                 } else if (strcmp(new_digest, s->sha1digest)) {
261                         LABEL;
262                         printf("%sSHA-1 expected %s found %s\n", 
263                                tab, s->sha1digest, new_digest);
264                         tab = "\t";
265                 }
266         }
267 #endif /* SHA1 */
268 #ifdef RMD160
269         if (s->flags & F_RMD160) {
270                 char *new_digest, buf[41];
271
272                 new_digest = RIPEMD160_File(p->fts_accpath, buf);
273                 if (!new_digest) {
274                         LABEL;
275                         printf("%sRIPEMD160: %s: %s\n", tab,
276                                p->fts_accpath, strerror(errno));
277                         tab = "\t";
278                 } else if (strcmp(new_digest, s->rmd160digest)) {
279                         LABEL;
280                         printf("%sRIPEMD160 expected %s found %s\n",
281                                tab, s->rmd160digest, new_digest);
282                         tab = "\t";
283                 }
284         }
285 #endif /* RMD160 */
286
287         if (s->flags & F_SLINK &&
288             strcmp(cp = rlink(p->fts_accpath), s->slink)) {
289                 LABEL;
290                 printf("%slink_ref expected %s found %s\n",
291                       tab, s->slink, cp);
292         }
293         return (label);
294 }
295
296 const char *
297 inotype(u_int type)
298 {
299         switch(type & S_IFMT) {
300         case S_IFBLK:
301                 return ("block");
302         case S_IFCHR:
303                 return ("char");
304         case S_IFDIR:
305                 return ("dir");
306         case S_IFIFO:
307                 return ("fifo");
308         case S_IFREG:
309                 return ("file");
310         case S_IFLNK:
311                 return ("link");
312         case S_IFSOCK:
313                 return ("socket");
314         default:
315                 return ("unknown");
316         }
317         /* NOTREACHED */
318 }
319
320 static const char *
321 ftype(u_int type)
322 {
323         switch(type) {
324         case F_BLOCK:
325                 return ("block");
326         case F_CHAR:
327                 return ("char");
328         case F_DIR:
329                 return ("dir");
330         case F_FIFO:
331                 return ("fifo");
332         case F_FILE:
333                 return ("file");
334         case F_LINK:
335                 return ("link");
336         case F_SOCK:
337                 return ("socket");
338         default:
339                 return ("unknown");
340         }
341         /* NOTREACHED */
342 }
343
344 char *
345 rlink(char *name)
346 {
347         static char lbuf[MAXPATHLEN];
348         int len;
349
350         if ((len = readlink(name, lbuf, sizeof(lbuf) - 1)) == -1)
351                 err(1, "line %d: %s", lineno, name);
352         lbuf[len] = '\0';
353         return (lbuf);
354 }