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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*
* $Id: index.c,v 1.3.4.1 1999/01/15 08:47:35 dirk Exp $
*
* This file is part of WorkMan, the civilized CD player program
* (c) 1991-1997 by Steven Grimm (original author)
* (c) by Dirk F"orsterling (current 'author' = maintainer)
* The maintainer can be contacted by his e-mail address:
* milliByte@DeathsDoor.com
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Maintain an external index file for a WorkMan CD database.
* Uses the Berkeley libdb library, available from ftp.cs.berkeley.edu.
*/
static char index_id[] = "$Id: index.c,v 1.3.4.1 1999/01/15 08:47:35 dirk Exp $";
#ifdef LIBDB
#include <stdio.h>
#include <stdlib.h>
#include <db.h>
#include <fcntl.h>
#include <string.h>
#include <netinet/in.h> /* for htonl() */
#include "config.h"
#include "proto.h"
extern int suppress_locking;
/*
* idx_find_entry()
*
* Find an entry in the index file.
*
* Input:
* file Name of database file (text version).
* ntracks Number of tracks on the CD we're looking for.
* tracks Array of track start times.
* len CD length in frames.
* fuzz Fuzz factor (tolerance value).
* pos Pointer to return value.
*
* Output:
* 1 No matching record found.
* 0 Record found; *pos contains offset into text file.
* -1 Index file out of date or inaccessible, or another error.
*/
int
idx_find_entry( char *file, int ntracks, int *tracks,
int len, int fuzz, unsigned long *pos )
{
unsigned long dbpos;
char *indexname = NULL, keyval[8];
int c;
FILE *text;
DB *index;
DBT key, data;
BTREEINFO bti;
/*
* First, see if the text file is accessible. Lock it if so.
*/
text = fopen(file, "r");
if (text == NULL)
return (-1);
if ((c = getc(text)) == EOF)
{
fclose(text);
return (-1);
}
if (! suppress_locking)
if (lockit(fileno(text), F_RDLCK))
{
fclose(text);
return (-1);
}
/*
* Open the index file.
*/
indexname = malloc(strlen(file) + sizeof(".ind"));
if (indexname == NULL)
{
fclose(text);
return (-1);
}
strcpy(indexname, file);
strcat(indexname, ".ind");
bti.flags = 0;
bti.cachesize = 0;
bti.minkeypage = bti.maxkeypage = 0;
bti.psize = bti.lorder = 0;
bti.compare = NULL;
bti.prefix = NULL;
index = dbopen(indexname, O_RDONLY, 0666, DB_BTREE, &bti);
free(indexname);
if (index == NULL)
{
fclose(text);
return (-1);
}
/*
* Search for the first matching entry.
*/
sprintf(keyval, "%07d", tracks[ntracks - 1] - fuzz);
key.data = keyval;
key.size = 7;
if (c = (index->seq)(index, &key, &data, R_CURSOR))
{
(index->close)(index);
fclose(text);
return (c);
}
/*
* Now loop through all the possible matches, collecting them into
* memory.
*/
do {
char tracksline[750], *s;
int i, val;
/* Hit the end of the valid entries? */
sscanf(key.data, "%d", &val);
if (val > tracks[ntracks - 1] + fuzz)
break;
dbpos = ntohl(*((unsigned long *) data.data));
if (fseek(text, dbpos, 0))
break;
fgets(tracksline, sizeof(tracksline), text);
if (strncmp(tracksline, "tracks ", 7))
break;
(void) strtok(tracksline, " \t");
/* Got a valid tracks line. See if it matches the CD. */
s = strtok(NULL, " \t");
if (s == NULL)
break;
if (atoi(s) != ntracks)
continue;
for (i = 0; i < ntracks; i++)
{
s = strtok(NULL, " \t");
if (s == NULL)
break;
val = atoi(s);
if (val + fuzz < tracks[i] || val - fuzz > tracks[i])
break;
}
if (i != ntracks)
continue;
s = strtok(NULL, " \t");
if (s == NULL)
continue;
val = atoi(s);
if (val + fuzz / 75 < len / 75 || val + fuzz / 75 > len / 75)
continue;
/* XXX - add to sorted list! */
*pos = dbpos;
(index->close)(index);
fclose(text);
return (0);
} while ((c = (index->seq)(index, &key, &data, R_NEXT)) == 0);
if (c == 0)
{
/* An error. */
(index->close)(index);
fclose(text);
return (-1);
}
(index->close)(index);
fclose(text);
return (1);
}
/*
* idx_delete_entry()
*
* Delete an entry from the index file.
*
* Input:
* file Name of database file (text version).
* track Last track's start time (database key).
* fuzz Fuzz factor (tolerance value).
* pos Position of CD in database file.
*
* Output:
* 1 No matching record found.
* 0 Record deleted.
* -1 Index file out of date or inaccessible, or another error.
*
* Note: it is the caller's responsibility to do locking, as it's assumed
* that this operation will accompany a modification of the main database
* file and will need to be atomic with that modification.
*/
int
idx_delete_entry(char *file, int track, int fuzz, unsigned long pos )
{
unsigned long dbpos;
char *indexname = NULL, keyval[8];
int c, status;
DB *index;
DBT key, data;
BTREEINFO bti;
/*
* Open the index file.
*/
indexname = malloc(strlen(file) + sizeof(".ind"));
if (indexname == NULL)
return (-1);
strcpy(indexname, file);
strcat(indexname, ".ind");
bti.flags = 0;
bti.cachesize = 0;
bti.minkeypage = bti.maxkeypage = 0;
bti.psize = bti.lorder = 0;
bti.compare = NULL;
bti.prefix = NULL;
index = dbopen(indexname, O_RDWR, 0666, DB_BTREE, &bti);
free(indexname);
if (index == NULL)
return (-1);
/*
* Search for the first matching entry.
*/
sprintf(keyval, "%07d", track - fuzz);
key.data = keyval;
key.size = 7;
if (c = (index->seq)(index, &key, &data, R_CURSOR))
{
/*
* Nothing matched!
*/
(index->close)(index);
return (c);
}
/*
* Look for the entry the user wants to delete.
*/
do {
int val;
/* Hit the end of the valid entries? */
sscanf(key.data, "%d", &val);
if (val > track + fuzz)
break;
/* Is this the entry we want? */
if (pos == ntohl(*((unsigned long *) data.data)))
{
/*
* Yep! Delete it.
*/
status = (index->del)(index, &key, R_CURSOR);
(index->close)(index);
return (status);
}
} while ((c = (index->seq)(index, &key, &data, R_NEXT)) == 0);
if (c == 0)
{
/* An error. */
(index->close)(index);
return (-1);
}
(index->close)(index);
return (1);
}
/*
* idx_write_entry()
*
* Write out an index file entry.
*
* Input:
* file Name of database file (text version).
* track Start time of last track (database key).
* pos Position of entry in text file.
*
* Output:
* 0 Record written.
* -1 Index file inaccessible, or another error.
*
* Note: it is the caller's responsibility to do locking, as it's assumed
* that this operation will accompany a modification of the main database
* file and will need to be atomic with that modification.
*/
int
idx_write_entry( char *file, int track, unsigned long pos )
{
char *indexname, keyval[8];
int status;
DB *index;
DBT key, data;
BTREEINFO bti;
/*
* Open the index file.
*/
indexname = malloc(strlen(file) + sizeof(".ind"));
if (indexname == NULL)
return (-1);
strcpy(indexname, file);
strcat(indexname, ".ind");
bti.flags = R_DUP;
bti.cachesize = 0;
bti.minkeypage = 0;
bti.maxkeypage = 0;
bti.psize = 0;
bti.lorder = 4321; /* network byte order */
bti.compare = NULL;
bti.prefix = NULL;
index = dbopen(indexname, O_RDWR, 0666, DB_BTREE, &bti);
free(indexname);
if (index == NULL)
return (-1);
/*
* Create a new key and value.
*/
pos = htonl(pos);
data.data = &pos;
data.size = sizeof(pos);
key.data = keyval;
key.size = 7;
sprintf(keyval, "%07d", track);
status = (index->put)(index, &key, &data, 0);
(index->close)(index);
return (status);
}
#else /* LIBDB */
int
idx_find_entry()
{
return (1); /* no record found; text file will be searched. */
}
int
idx_delete_entry()
{
return (0);
}
int
idx_write_entry()
{
return (0);
}
#endif /* LIBDB */
|