2 * Copyright (c) 1997 Robert Nordier
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
12 * the documentation and/or other materials provided with the
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 static const char rcsid[] =
33 #include <sys/types.h>
47 extern int crc(int fd, uint32_t *cval, off_t *clen);
49 #define DISTMD5 1 /* MD5 format */
50 #define DISTINF 2 /* .inf format */
51 #define DISTTYPES 2 /* types supported */
53 #define E_UNKNOWN 1 /* Unknown format */
54 #define E_BADMD5 2 /* Invalid MD5 format */
55 #define E_BADINF 3 /* Invalid .inf format */
56 #define E_NAME 4 /* Can't derive component name */
57 #define E_LENGTH 5 /* Length mismatch */
58 #define E_CHKSUM 6 /* Checksum mismatch */
59 #define E_ERRNO 7 /* sys_errlist[errno] */
61 #define isfatal(err) ((err) && (err) <= E_NAME)
63 #define NAMESIZE 256 /* filename buffer size */
64 #define MDSUMLEN 32 /* length of MD5 message digest */
66 #define isstdin(path) ((path)[0] == '-' && !(path)[1])
68 static const char *opt_dir; /* where to look for components */
69 static const char *opt_name; /* name for accessing components */
70 static int opt_all; /* report on all components */
71 static int opt_ignore; /* ignore missing components */
72 static int opt_recurse; /* search directories recursively */
73 static int opt_silent; /* silent about inaccessible files */
74 static int opt_type; /* dist type: md5 or inf */
75 static int opt_exist; /* just verify existence */
77 static int ckdist(const char *path, int type);
78 static int chkmd5(FILE * fp, const char *path);
79 static int chkinf(FILE * fp, const char *path);
80 static int report(const char *path, const char *name, int error);
81 static const char *distname(const char *path, const char *name,
83 static const char *stripath(const char *path);
84 static int distfile(const char *path);
85 static int disttype(const char *name);
86 static int fail(const char *path, const char *msg);
87 static void usage(void);
90 main(int argc, char *argv[])
98 while ((c = getopt(argc, argv, "ad:in:rst:x")) != -1)
119 if ((opt_type = disttype(optarg)) == 0) {
120 warnx("illegal argument to -t option");
135 if (stat(opt_dir, &sb))
136 err(2, "%s", opt_dir);
137 if (!S_ISDIR(sb.st_mode))
138 errx(2, "%s: not a directory", opt_dir);
143 rval |= ckdist(*argv, opt_type);
144 else if (stat(*argv, &sb))
145 rval |= fail(*argv, NULL);
146 else if (S_ISREG(sb.st_mode))
147 rval |= ckdist(*argv, opt_type);
150 if ((ftsp = fts_open(arg, FTS_LOGICAL, NULL)) == NULL)
152 while ((f = fts_read(ftsp)) != NULL)
153 switch (f->fts_info) {
155 rval = fail(f->fts_path, "Directory causes a cycle");
160 rval = fail(f->fts_path, sys_errlist[f->fts_errno]);
163 if (!opt_recurse && f->fts_level > FTS_ROOTLEVEL &&
164 fts_set(ftsp, f, FTS_SKIP))
168 if ((type = distfile(f->fts_name)) != 0 &&
169 (!opt_type || type == opt_type))
170 rval |= ckdist(f->fts_path, type);
184 ckdist(const char *path, int type)
192 } else if ((fp = fopen(path, "r")) == NULL)
193 return fail(path, NULL);
196 type = distfile(path);
198 if ((c = fgetc(fp)) != EOF) {
199 type = c == 'M' ? DISTMD5 : c == 'P' ? DISTINF : 0;
205 rval = chkmd5(fp, path);
208 rval = chkinf(fp, path);
211 rval = report(path, NULL, E_UNKNOWN);
215 if (fp != stdin && fclose(fp))
221 chkmd5(FILE * fp, const char *path)
223 char buf[298]; /* "MD5 (NAMESIZE = MDSUMLEN" */
224 char name[NAMESIZE + 1];
225 char sum[MDSUMLEN + 1], chk[MDSUMLEN + 1];
228 int rval, error, c, fd;
232 while (fgets(buf, sizeof(buf), fp)) {
235 if (((c = sscanf(buf, "MD5 (%256s = %32s%c", name, sum,
236 &ch)) != 3 && (!feof(fp) || c != 2)) ||
237 (c == 3 && ch != '\n') ||
238 (s = strrchr(name, ')')) == NULL ||
239 strlen(sum) != MDSUMLEN)
243 if ((dname = distname(path, name, NULL)) == NULL)
245 else if (opt_exist) {
246 if ((fd = open(dname, O_RDONLY)) == -1)
250 } else if (!MD5File(dname, chk))
252 else if (strcmp(chk, sum))
255 if (opt_ignore && error == E_ERRNO && errno == ENOENT)
257 if (error || opt_all)
258 rval |= report(path, dname, error);
266 chkinf(FILE * fp, const char *path)
268 char buf[30]; /* "cksum.2 = 10 6" */
276 int rval, error, c, pieces, cnt, fd;
280 for (cnt = -1; fgets(buf, sizeof(buf), fp); cnt++) {
285 if ((c = sscanf(buf, "Pieces = %d%c", &pieces, &ch)) != 2 ||
286 ch != '\n' || pieces < 1)
288 } else if (((c = sscanf(buf, "cksum.%2s = %lu %jd%c", ext, &sum,
289 &sumlen, &ch)) != 4 &&
290 (!feof(fp) || c != 3)) || (c == 4 && ch != '\n') ||
291 ext[0] != 'a' + cnt / 26 || ext[1] != 'a' + cnt % 26)
293 else if ((dname = distname(fp == stdin ? NULL : path, NULL,
296 else if ((fd = open(dname, O_RDONLY)) == -1)
298 else if (fstat(fd, &sb))
300 else if (sb.st_size != (off_t)sumlen)
302 else if (!opt_exist) {
303 if (crc(fd, &chk, &len))
308 if (fd != -1 && close(fd))
310 if (opt_ignore && error == E_ERRNO && errno == ENOENT)
312 if (error || (opt_all && cnt >= 0))
313 rval |= report(path, dname, error);
321 report(const char *path, const char *name, int error)
324 name = stripath(name);
327 printf("%s: Unknown format\n", path);
330 printf("%s: Invalid MD5 format\n", path);
333 printf("%s: Invalid .inf format\n", path);
336 printf("%s: Can't derive component name\n", path);
339 printf("%s: %s: Size mismatch\n", path, name);
342 printf("%s: %s: Checksum mismatch\n", path, name);
345 printf("%s: %s: %s\n", path, name, sys_errlist[errno]);
348 printf("%s: %s: OK\n", path, name);
354 distname(const char *path, const char *name, const char *ext)
356 static char buf[NAMESIZE];
365 name = stripath(path);
368 if (ext && nlen > 4 && name[nlen - 4] == '.' &&
369 disttype(name + nlen - 3) == DISTINF)
375 plen = path && (s = strrchr(path, '/')) != NULL ?
376 (size_t)(s - path) : 0;
377 if (plen + (plen > 0) + nlen + (ext ? 3 : 0) >= sizeof(buf))
381 memcpy(s, path, plen);
385 memcpy(s, name, nlen);
397 stripath(const char *path)
401 return ((s = strrchr(path, '/')) != NULL && s[1] ?
406 distfile(const char *path)
411 if ((type = disttype(path)) == DISTMD5 ||
412 ((s = strrchr(path, '.')) != NULL && s > path &&
413 (type = disttype(s + 1)) != 0))
419 disttype(const char *name)
421 static const char dname[DISTTYPES][4] = {"md5", "inf"};
424 for (i = 0; i < DISTTYPES; i++)
425 if (!strcmp(dname[i], name))
431 fail(const char *path, const char *msg)
435 warnx("%s: %s", path, msg ? msg : sys_errlist[errno]);
443 "usage: ckdist [-airsx] [-d dir] [-n name] [-t type] file ...\n");