summaryrefslogtreecommitdiff
path: root/math/mkdb.c
blob: 83c98c0a1a9cf36f6144468c081c0c6dccca3cae (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
/*	$Id$ */

/*
 * Copyright (c) 2006 Dimitri A. Sokolyuk <demon@vhost.dyndns.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <fcntl.h>
#include <fts.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "crc.h"
#include "metadb.h"

void twiddle(void);
void walk(char **);
void fillauthor(char *);

void
twiddle(void)
{
	static int i;

	putchar("|/-\\"[i++ & 3]);
	putchar('\b');
	fflush(stdout);
}

void
walk(char **fname)
{
	FTS	*fts;
	FTSENT	*ftp, *chld, *cp;
	char	*p, pathmeta[BUFSIZ], pathset[BUFSIZ];
	int	parent, id;
	struct	meta *mp;

	fts = fts_open(fname, FTS_LOGICAL, NULL);
	assert(fts);

	printf("Scanning: ");

//	db_wipe();

	while ((ftp = fts_read(fts)) != NULL) {
		twiddle();
		if (ftp->fts_info == FTS_D) {
			p = ftp->fts_parent->fts_name;
			if (ftp->fts_parent->fts_level <= 0)
				parent = 0;
			else
				parent = crc32(p, strlen(p));

			p = ftp->fts_name;
			if (ftp->fts_level <= 0)
				id = 0;
			else
				id = crc32(p, strlen(p));

			snprintf(pathmeta, sizeof(pathmeta), "%s/%s", ftp->fts_accpath, METADATA);
			snprintf(pathset, sizeof(pathset), "%s/%s", ftp->fts_accpath, SECTION);

			if ((mp = openmeta(pathmeta)) != NULL) {
				mp->dir = strdup(p);
				mp->date = ftp->fts_statp->st_ctimespec.tv_sec;
				mp->type = M_RECORD;

				chld = fts_children(fts, 0);
				for (cp = chld; cp != NULL; cp = cp->fts_link) {
					if (cp->fts_info == FTS_F) {
						p = cp->fts_name;
						if (strstr(p, ".htm") != NULL)
							mp->html = strdup(p);
						else if (strstr(p, ".mws") != NULL)
							mp->mws = strdup(p);
						else if (strstr(p, ".mw") != NULL)
							mp->mw = strdup(p);
						else if (strstr(p, ".pdf") != NULL)
							mp->pdf = strdup(p);
						else if (strstr(p, ".zip") != NULL)
							mp->code = strdup(p);
						/* TODO: add doc, rtf, etc. */
					}
				}

				/* fallbacks */
				if (mp->title == NULL)
					mp->title = mp->dir;
				if (mp->author == NULL)
					mp->author = strdup("A. N. Other");
				if (mp->language == NULL)
					mp->language = strdup("en");

				db_add(mp, parent, id);
				closemeta(mp);
			} else if ((mp = openmeta(pathset)) != NULL) {
				mp->dir = strdup(p);
				mp->date = ftp->fts_statp->st_ctimespec.tv_sec;
				mp->type = M_SET;
				db_add(mp, parent, id);
			}
		}
	}

	fts_close(fts);

	printf("done\n");
}

void
fillauthor(char *fname)
{
	FILE	*fd;
	struct	author a;
	char	buf[BUFSIZ], *p, *key;
	char	sep[] = ":\n";

	fd = fopen(fname, "r");

	while ((p = fgets(buf, sizeof(buf), fd)) != NULL) {
		if (*p == '#')
			continue;
		key = strsep(&p, sep);
		a.name = strsep(&p, sep);
		a.mail = strsep(&p, sep);
		db_putauthor(&a, key);
	}

	fclose(fd);
}
		

int
main(int argc, char **argv)
{
	if (argc != 2)
		return -1;

	db_open(O_CREAT|O_RDWR);
	fillauthor(AUTHORDATA);
	walk(&argv[1]);
	db_close();

	return 0;
}