File: buildindex.c

package info (click to toggle)
workman 1.3a-8
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 944 kB
  • ctags: 745
  • sloc: ansic: 9,810; makefile: 137; sh: 27
file content (197 lines) | stat: -rw-r--r-- 3,794 bytes parent folder | download
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
/*
 * %W%	%G%
 *
 * Build a WorkMan database index file from a flat text file.  Requires
 * 4.4BSD libdb library.
 */
#include <stdio.h>
#include <db.h>
#include <fcntl.h>
#include <errno.h>
#include <netinet/in.h>		/* for htonl() */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <unistd.h>

char *strrchr();

main(argc, argv)
	int	argc;
	char	**argv;
{
	DB	*db;
	DBT	key, data;
	FILE	*fp;
	int	lock = 1, i = 0, locked, frame;
	char	buf[1000], indname[MAXPATHLEN + 100], framebuf[8], *c;
	unsigned long	pos;
	BTREEINFO	bt;
	struct stat	st;

	if (argc > 2 && !strcmp(argv[1], "-n"))
	{
		lock = 0;
		i++;
	}

	if (argc < i + 2)
	{
		fprintf(stderr, "Usage: %s [-n] dbfile [dbfile ...]\n",
			argv[0]);
		exit(1);
	}

	data.data = &pos;
	data.size = sizeof(pos);
	key.data = framebuf;
	key.size = 7;		/* %07d */

	while (++i < argc)
	{
		fprintf(stderr, "Building index for %s\n", argv[i]);

		if ((fp = fopen(argv[i], "r")) == NULL)
		{
			perror(argv[i]);
			continue;
		}

		/*
		 * Figure out the file's mode, uid, gid, so we can set the
		 * permissions on the index file to the same thing.
		 */
		if (fstat(fileno(fp), &st))
		{
			sprintf(indname, "%s: fstat", argv[i]);
			perror(indname);
			fclose(fp);
			continue;
		}

		if (lock && lockit(fileno(fp), F_WRLCK))
		{
			sprintf(indname, "%s: Warning: Couldn't lock", argv[i]);
			perror(indname);
			locked = 0;
		}
		else
			locked = 1;

		/*
		 * Create a database file.
		 */
		bt.flags = R_DUP;	/* allow duplicate keys */
		bt.cachesize = 0;
		bt.psize = 0;
		bt.lorder = 4321;
		bt.minkeypage = 0;
		bt.compare = NULL;	/* use lexical comparison */
		bt.prefix = NULL;	/* no prefix comparisons */

		/* Index files have ".ind" extensions */
		sprintf(indname, "%s.ind", argv[i]);
		if ((db = dbopen(indname, O_CREAT | O_RDWR | O_TRUNC,
			st.st_mode, DB_BTREE, &bt)) == NULL)
		{
			perror(indname);
			if (locked)
				lockit(fileno(fp), F_UNLCK);
			fclose(fp);
			continue;
		}

		/*
		 * Now loop through the text file, inserting a record into
		 * the index file for each "tracks" line.
		 */
		while (! feof(fp))
		{
			pos = ftell(fp);
			buf[0] = '\0';
			if (fgets(buf, sizeof(buf), fp) == NULL || ! buf[0])
			{
				/* End of file? */
				if (feof(fp))
					break;
				
				/* Nope.  A read error.  Unlink the database. */
				perror(argv[i]);
				(void) unlink(indname);
				break;
			}

			if (strncmp(buf, "tracks ", 7))
				continue;
			
			/*
			 * Found the start of a record.  Figure out the start
			 * time of the last track and put an entry in the
			 * index file with that as the key.
			 */
			c = strrchr(buf, ' ');	/* this will always succeed */
			*c = '\0';
			c = strrchr(buf, ' ');	/* this should too, but... */
			if (c == NULL)
			{
				fprintf(stderr,
					"%s: Malformed tracks line at %lu\n",
					argv[i], pos);
				continue;
			}
			sscanf(c+1, "%d", &frame);
			sprintf(framebuf, "%07d", frame);
			pos = htonl(pos);

			if ((db->put)(db, &key, &data, 0))
			{
				perror(indname);
				unlink(indname);
				break;
			}
		}

		/*
		 * Clean up.
		 */
		(void) (db->close)(db);
		if (locked)
			lockit(fileno(fp), F_UNLCK);
	}
}

/*
 * Lock a file.  Time out after a little while if we can't get a lock;
 * this usually means the locking system is broken.
 *
 * Unfortunately, if there are lots of people contending for a lock,
 * this can result in the file not getting locked when it probably should.
 */
int
lockit(fd, type)
	int	fd;
	int	type;
{
	struct flock	fl;
	int		result, timer = 0;

	fl.l_type = type;
	fl.l_whence = 0;
	fl.l_start = 0;
	fl.l_len = 0;

	while ((result = fcntl(fd, F_SETLK, &fl)) < 0)
	{
		if (errno != EACCES || errno != EAGAIN)
			break;
		if (timer++ == 30)
		{
			errno = ETIMEDOUT;
			break;
		}

		sleep(1);
	}

	return (result);
}