file_cmds-45.tar.gz
[apple/file_cmds.git] / mtree / compare.c
1 /* $NetBSD: compare.c,v 1.15 1998/08/27 18:03:45 ross Exp $ */
2
3 /*-
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)compare.c 8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: compare.c,v 1.15 1998/08/27 18:03:45 ross Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #include <fts.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <time.h>
52 #include <unistd.h>
53 #include "mtree.h"
54 #include "extern.h"
55
56 extern int tflag, uflag;
57
58 static char *ftype __P((u_int));
59
60 #define INDENTNAMELEN 8
61 #define LABEL \
62 if (!label++) { \
63 len = printf("%s: ", RP(p)); \
64 if (len > INDENTNAMELEN) { \
65 tab = "\t"; \
66 (void)printf("\n"); \
67 } else { \
68 tab = ""; \
69 (void)printf("%*s", INDENTNAMELEN - (int)len, ""); \
70 } \
71 }
72
73 int
74 compare(name, s, p)
75 char *name;
76 NODE *s;
77 FTSENT *p;
78 {
79 u_int32_t len, val;
80 int fd, label;
81 char *cp, *tab;
82
83 tab = NULL;
84 label = 0;
85 switch(s->type) {
86 case F_BLOCK:
87 if (!S_ISBLK(p->fts_statp->st_mode))
88 goto typeerr;
89 break;
90 case F_CHAR:
91 if (!S_ISCHR(p->fts_statp->st_mode))
92 goto typeerr;
93 break;
94 case F_DIR:
95 if (!S_ISDIR(p->fts_statp->st_mode))
96 goto typeerr;
97 break;
98 case F_FIFO:
99 if (!S_ISFIFO(p->fts_statp->st_mode))
100 goto typeerr;
101 break;
102 case F_FILE:
103 if (!S_ISREG(p->fts_statp->st_mode))
104 goto typeerr;
105 break;
106 case F_LINK:
107 if (!S_ISLNK(p->fts_statp->st_mode))
108 goto typeerr;
109 break;
110 case F_SOCK:
111 if (!S_ISSOCK(p->fts_statp->st_mode)) {
112 typeerr: LABEL;
113 (void)printf("\ttype (%s, %s)\n",
114 ftype(s->type), inotype(p->fts_statp->st_mode));
115 }
116 break;
117 }
118 /* Set the uid/gid first, then set the mode. */
119 if (s->flags & (F_UID | F_UNAME) && s->st_uid != p->fts_statp->st_uid) {
120 LABEL;
121 (void)printf("%suser (%u, %u",
122 tab, s->st_uid, p->fts_statp->st_uid);
123 if (uflag)
124 if (chown(p->fts_accpath, s->st_uid, -1))
125 (void)printf(", not modified: %s)\n",
126 strerror(errno));
127 else
128 (void)printf(", modified)\n");
129 else
130 (void)printf(")\n");
131 tab = "\t";
132 }
133 if (s->flags & (F_GID | F_GNAME) && s->st_gid != p->fts_statp->st_gid) {
134 LABEL;
135 (void)printf("%sgid (%u, %u",
136 tab, s->st_gid, p->fts_statp->st_gid);
137 if (uflag)
138 if (chown(p->fts_accpath, -1, s->st_gid))
139 (void)printf(", not modified: %s)\n",
140 strerror(errno));
141 else
142 (void)printf(", modified)\n");
143 else
144 (void)printf(")\n");
145 tab = "\t";
146 }
147 if (s->flags & F_MODE &&
148 s->st_mode != (p->fts_statp->st_mode & MBITS)) {
149 LABEL;
150 (void)printf("%spermissions (%#o, %#o",
151 tab, s->st_mode, p->fts_statp->st_mode & MBITS);
152 if (uflag)
153 if (chmod(p->fts_accpath, s->st_mode))
154 (void)printf(", not modified: %s)\n",
155 strerror(errno));
156 else
157 (void)printf(", modified)\n");
158 else
159 (void)printf(")\n");
160 tab = "\t";
161 }
162 if (s->flags & F_NLINK && s->type != F_DIR &&
163 s->st_nlink != p->fts_statp->st_nlink) {
164 LABEL;
165 (void)printf("%slink count (%u, %u)\n",
166 tab, s->st_nlink, p->fts_statp->st_nlink);
167 tab = "\t";
168 }
169 if (s->flags & F_SIZE && s->st_size != p->fts_statp->st_size) {
170 LABEL;
171 (void)printf("%ssize (%qd, %qd)\n",
172 tab, (long long)s->st_size,
173 (long long)p->fts_statp->st_size);
174 tab = "\t";
175 }
176 /*
177 * XXX
178 * Since utimes(2) only takes a timeval, there's no point in
179 * comparing the low bits of the timespec nanosecond field. This
180 * will only result in mismatches that we can never fix.
181 *
182 * Doesn't display microsecond differences.
183 */
184 if (s->flags & F_TIME) {
185 struct timeval tv[2];
186
187 TIMESPEC_TO_TIMEVAL(&tv[0], &s->st_mtimespec);
188 TIMESPEC_TO_TIMEVAL(&tv[1], &p->fts_statp->st_mtimespec);
189 if (tv[0].tv_sec != tv[1].tv_sec ||
190 tv[0].tv_usec != tv[1].tv_usec) {
191 LABEL;
192 (void)printf("%smodification time (%.24s, ",
193 tab, ctime(&s->st_mtimespec.tv_sec));
194 (void)printf("%.24s",
195 ctime(&p->fts_statp->st_mtimespec.tv_sec));
196 if (tflag) {
197 tv[1] = tv[0];
198 if (utimes(p->fts_accpath, tv))
199 (void)printf(", not modified: %s)\n",
200 strerror(errno));
201 else
202 (void)printf(", modified)\n");
203 } else
204 (void)printf(")\n");
205 tab = "\t";
206 }
207 }
208 if (s->flags & F_CKSUM) {
209 if ((fd = open(p->fts_accpath, O_RDONLY, 0)) < 0) {
210 LABEL;
211 (void)printf("%scksum: %s: %s\n",
212 tab, p->fts_accpath, strerror(errno));
213 tab = "\t";
214 } else if (crc(fd, &val, &len)) {
215 (void)close(fd);
216 LABEL;
217 (void)printf("%scksum: %s: %s\n",
218 tab, p->fts_accpath, strerror(errno));
219 tab = "\t";
220 } else {
221 (void)close(fd);
222 if (s->cksum != val) {
223 LABEL;
224 (void)printf("%scksum (%lu, %lu)\n",
225 tab, s->cksum, (unsigned long)val);
226 }
227 tab = "\t";
228 }
229 }
230 if (s->flags & F_SLINK && strcmp(cp = rlink(name), s->slink)) {
231 LABEL;
232 (void)printf("%slink ref (%s, %s)\n", tab, cp, s->slink);
233 }
234 return (label);
235 }
236
237 char *
238 inotype(type)
239 u_int type;
240 {
241 switch(type & S_IFMT) {
242 case S_IFBLK:
243 return ("block");
244 case S_IFCHR:
245 return ("char");
246 case S_IFDIR:
247 return ("dir");
248 case S_IFIFO:
249 return ("fifo");
250 case S_IFREG:
251 return ("file");
252 case S_IFLNK:
253 return ("link");
254 case S_IFSOCK:
255 return ("socket");
256 default:
257 return ("unknown");
258 }
259 /* NOTREACHED */
260 }
261
262 static char *
263 ftype(type)
264 u_int type;
265 {
266 switch(type) {
267 case F_BLOCK:
268 return ("block");
269 case F_CHAR:
270 return ("char");
271 case F_DIR:
272 return ("dir");
273 case F_FIFO:
274 return ("fifo");
275 case F_FILE:
276 return ("file");
277 case F_LINK:
278 return ("link");
279 case F_SOCK:
280 return ("socket");
281 default:
282 return ("unknown");
283 }
284 /* NOTREACHED */
285 }
286
287 char *
288 rlink(name)
289 char *name;
290 {
291 static char lbuf[MAXPATHLEN];
292 int len;
293
294 if ((len = readlink(name, lbuf, sizeof(lbuf))) == -1)
295 mtree_err("%s: %s", name, strerror(errno));
296 lbuf[len] = '\0';
297 return (lbuf);
298 }