summaryrefslogtreecommitdiff
path: root/pkgtools/pkg_filecheck/files/pkg_filecheck.c
blob: 6bc3a8f73ae066c5da2125f50d5043621002d0c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
  $NetBSD: pkg_filecheck.c,v 1.4 2006/10/24 19:23:38 rillig Exp $

  pkg_filecheck.c -- check for files not owned by any package
  Copyright (C) 2001 Dieter Baron

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in
     the documentation and/or other materials provided with the
     distribution.
  3. The name of the author may not be used to endorse or promote
     products derived from this software without specific prior
     written permission.
 
  THIS SOFTWARE IS PROVIDED BY DIETER BARON ``AS IS'' AND ANY EXPRESS
  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  ARE DISCLAIMED.  IN NO EVENT SHALL DIETER BARON BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
  IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#if defined(USE_LIBNBCOMPAT)
#include <nbcompat.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <db.h>
#include <errno.h>
#include <fcntl.h>
#if !defined(USE_LIBNBCOMPAT) || defined(HAVE_FTS_H)
#include <fts.h>
#endif
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

struct array {
    void **p;
    int len, alen;
};

char *prg;

static int check(char **dir, DB **db, char **ignore);
static int fts_sort(const FTSENT **e1, const FTSENT **e2);
static void push(struct array *a, void *el);
static int read_conf(char *fname, char ***dirp, DB ***dbp, char ***ignorep);



int
main(int argc, char *argv[])
{
    DB **db;
    char **dir, **ignore;

    char *cfgfile;
    int c, i, err;

    prg = argv[0];
    opterr = 0;

    cfgfile = "@PKG_SYSCONFDIR@/pkg_filecheck.conf";

    while ((c=getopt(argc, argv, "c:h")) != -1) {
	switch (c) {
	case 'c':
	    cfgfile = optarg;
	    break;

	case 'h':
	    printf("usage: %s [-h] [-c config-file]\n", prg);
	    exit(0);

	default:
	    fprintf(stderr, "usage: %s [-h] [-c config-file]\n", prg);
	    exit(1);
	}
    }

    if (read_conf(cfgfile, &dir, &db, &ignore) < 0)
	exit(1);

    err = check(dir, db, ignore);

    for (i=0; db[i]; i++)
	db[i]->close(db[i]);
    free(dir);
    free(db);
    free(ignore);

    exit(err ? 1 : 0);
}



static int
check(char **dir, DB **db, char **ignore)
{
    FTS *fts;
    FTSENT *ent;
    DBT key, val;
    int i, found;

    if ((fts=fts_open(dir, FTS_PHYSICAL, fts_sort)) == NULL) {
	fprintf(stderr, "%s: cannot walk direcotry hierarchy: %s",
		prg, strerror(errno));
	return -1;
    }

    while ((ent=fts_read(fts))) {
	switch (ent->fts_info) {
	case FTS_F:
	    key.data = ent->fts_path;
	    key.size = ent->fts_pathlen+1;
	    
	    found = 0;
	    for (i=0; db[i]; i++)
		if (db[i]->get(db[i], &key, &val, 0) == 0) {
		    found = 1;
		    break;
		}

	    if (!found)
		printf("%s\n", ent->fts_path);
	    break;

	case FTS_D:
	    if (ignore)
		for (i=0; ignore[i]; i++)
		    if (strcmp(ignore[i], ent->fts_path) == 0) {
			fts_set(fts, ent, FTS_SKIP);
			break;
		    }
	    break;

	case FTS_DNR:
	    fprintf(stderr, "%s: cannot read directory `%s', skipped: %s\n",
		    prg, ent->fts_path, strerror(ent->fts_errno));
	    break;

	case FTS_NS:
	    fprintf(stderr, "%s: cannot stat file `%s', skipped: %s\n",
		    prg, ent->fts_path, strerror(ent->fts_errno));
	    break;

	case FTS_ERR:
	    fprintf(stderr, "%s: directory traversal error: %s\n",
		    prg, strerror(ent->fts_errno));
	    break;

	case FTS_DC:
	case FTS_DP:
	case FTS_SL:
	case FTS_SLNONE:
	case FTS_DEFAULT:
	    /* ignore */
	    break;

	default:
	    fprintf(stderr, "%s: unknown FTSENT type %d ignored\n",
		    prg, ent->fts_info);
	    break;
	}
    }

    fts_close(fts);
    return 0;
}



static int
fts_sort(const FTSENT **e1, const FTSENT **e2)
{
    return strcmp((*e1)->fts_name, (*e2)->fts_name);
}



static int
read_conf(char *fname, char ***dirp, DB ***dbp, char ***ignorep)
{
    struct array dir, db, ignore;
    FILE *f;
    DB *cdb;
    char b[8192], *curd, *dbname, *cmd, *p;
    int lineno, err;

    dir.p = db.p = ignore.p = NULL;
    dir.len = db.len = ignore.len = 0;
    dir.alen = db.alen = ignore.alen = 0;

    if ((f=fopen(fname, "r")) == NULL) {
	fprintf(stderr, "%s: cannot open config file `%s': %s\n",
		prg, fname, strerror(errno));
	return -1;
    }

    curd = NULL;

    lineno = 0;
    while (fgets(b, 8192, f)) {
	lineno++;
	if (b[0] == '#')
	    continue;

	cmd = b+strspn(b, " \t");
	p = cmd+strcspn(cmd, " \t\n");
	*(p++) = '\0';
	p += strspn(p, " \t");
	if (*p == '\n' || *p == '\0')
	    continue;

	if (p[strlen(p)-1] == '\n')
	    p[strlen(p)-1] = '\0';

	if (strcmp(cmd, "dir") == 0) {
	    free(curd);
	    curd = strdup(p);
	}
	else if (strcmp(cmd, "db") == 0) {
	    if (curd) {
		dbname = malloc(strlen(curd) + strlen(p) + 2);
		sprintf(dbname, "%s/%s", curd, p);
	    }
	    else
		dbname = p;
	    if ((cdb=dbopen(dbname, O_RDONLY, 0, DB_BTREE, NULL)) == NULL) {
		fprintf(stderr, "%s:%s:%d: cannot open data base `%s': %s\n",
			prg, fname, lineno, dbname, strerror(errno));
		free(dir.p);
		free(db.p);
		free(ignore.p);
		return -1;
	    }
	    else
		push(&db, cdb);

	    if (curd)
		free(dbname);
	}
	else if (strcmp(cmd, "check") == 0)
	    push(&dir, strdup(p));
	else if (strcmp(cmd, "ignore") == 0)
	    push(&ignore, strdup(p));
	else {
	    fprintf(stderr, "%s:%s:%d: unrecognized command `%s'\n",
		    prg, fname, lineno, cmd);
	}
    }

    fclose(f);

    err = 0;

    if (dir.p == NULL) {
	fprintf(stderr, "%s: no directories to check\n",
		prg);
	err = 1;
    }
    if (db.p == NULL) {
	fprintf(stderr, "%s: no file data bases given\n",
		prg);
	err = 1;
    }

    if (err) {
	free(dir.p);
	free(db.p);
	free(ignore.p);
	return -1;
    }

    *dirp = (char **)dir.p;
    *dbp = (DB **)db.p;
    *ignorep = (char **)ignore.p;

    return 0;
}



static void
push(struct array *a, void *el)
{
    int alen;
    void *p;

    if (a->len + 2 > a->alen) {
	alen = (a->alen == 0) ? 8
	     : (a->alen < 1024) ? (a->alen * 2)
	     : (a->alen + 1024);

	if ((p=realloc(a->p, alen*sizeof(void *))) == NULL) {
	    fprintf(stderr, "%s: malloc failure\n", prg);
	    exit(1);
	}
	
	a->p = p;
	a->alen = alen;
    }

    a->p[a->len++] = el;
    a->p[a->len] = NULL;
}