static int readpagel(Aflinst *t) { if (fseeko(t->f.udict, (off_t) (t->udictp + 1), SEEK_SET) < 0) return aferr(AFEDBIO); if (fread(&(t->pagel), 1, sizeof t->pagel, t->f.udict) < sizeof t->pagel) return aferr(AFEDBIO); return 0; }
static int truncfile(const char *db, int type) { FILE *f; if (!(f = afopendbf(db, type, "wb"))) return aferr(AFEDBIO); if (fclose(f) != 0) return aferr(AFEDBIO); return 0; }
static int readuword(Aflinst *t) { if (fseeko(t->f.uword, (off_t) ( ((off_t) (t->uwordp - 1)) * ((off_t) (sizeof t->uword)) ), SEEK_SET) < 0) return aferr(AFEDBIO); if (fread(&(t->uword), 1, sizeof t->uword, t->f.uword) < sizeof t->uword) return aferr(AFEDBIO); return 0; }
static int fillbuf() { int x; if (fseeko(fbuf, 0, SEEK_SET) < 0) return aferr(AFEDBIO); for (x = 0; x < linbufn; x++) { if (fread(linbuf[x], 1, linbuflen[x], fbuf) < linbuflen[x]) return aferr(AFEDBIO); } return 0; }
int _aflinear(const Aflinear *rq) { Aflinst t; t.rq = rq; afprintv(rq->verbose, 2, "Linearizing"); afprintv(rq->verbose, 3, "Opening database"); if (linopen(&t) < 0) return -1; afprintv(rq->verbose, 4, "Checking if database is linearized"); /* exit if db is already linearized */ if (t.info.optimized) { if (afclosedbf(&t.f) < 0) return -1; if (freelock(t.rq->db) < 0) return -1; return aferr(AFELINEAR); } afprintv(rq->verbose, 3, "Performing linearize process"); if (linearize(&t) < 0) return -1; afprintv(rq->verbose, 3, "Closing database"); if (linclose(&t) < 0) return -1; return 0; }
static int writelpost(Aflinst *t) { if (fwrite(&(t->lpost), 1, sizeof t->lpost, t->f.lpost) < sizeof t->lpost) return aferr(AFEDBIO); return 0; }
static int writelword(Aflinst *t) { if (fwrite(&(t->lword), 1, sizeof t->lword, t->f.lword) < sizeof t->lword) return aferr(AFEDBIO); return 0; }
static int closefile(FILE *f) { if (f) { if (fclose(f) != 0) return aferr(AFEDBIO); } return 0; }
static int getlock(const char *db) { if (!etymon_db_ready(db)) return aferr(AFEDBLOCK); if (etymon_db_lock(db, NULL) < 0) return -1; return 0; }
static int seekleftleaf(Aflinst *t) { Uint1 leaf; ETYMON_INDEX_PAGE_NL pagenl; t->udictp = t->info.udict_root; while (1) { if (fseeko(t->f.udict, (off_t) t->udictp, SEEK_SET) < 0) return aferr(AFEDBIO); if (fread(&leaf, 1, 1, t->f.udict) < 0) return aferr(AFEDBIO); if (leaf) return 0; if (fread(&pagenl, 1, sizeof pagenl, t->f.udict) < sizeof pagenl) return aferr(AFEDBIO); t->udictp = pagenl.p[0]; } }
static int readupost(Aflinst *t) { if (t->rq->nobuffer) { if (fseeko(t->f.upost, (off_t) ( ((off_t) (t->upostp - 1)) * ((off_t) (sizeof t->upost)) ), SEEK_SET) < 0) return aferr(AFEDBIO); if (fread(&(t->upost), 1, sizeof t->upost, t->f.upost) < sizeof t->upost) return aferr(AFEDBIO); } else { if (aflinread(&(t->upost), (off_t) ( ((off_t) (t->upostp - 1)) * ((off_t) (sizeof t->upost)) ), sizeof t->upost) < 0) return -1; } return 0; }
/* returns 1 if lock was obtained */ int etymon_db_lock(const char* dbname, ETYMON_LOG* log) { char fn[ETYMON_MAX_PATH_SIZE]; int lock_fd; etymon_db_construct_path(ETYMON_DBF_LOCK, dbname, fn); lock_fd = open(fn, O_RDONLY | ETYMON_AF_O_LARGEFILE, ETYMON_DB_PERM); if (lock_fd != -1) { if (log) { int e; char s[ETYMON_MAX_MSG_SIZE]; sprintf(s, "%s: Database not ready", dbname); e = log->error(s, 1); close(lock_fd); if (e != 0) { exit(e); } return 0; } else { return aferr(AFEDBLOCK); } } lock_fd = open(fn, O_WRONLY | O_CREAT | O_TRUNC | ETYMON_AF_O_LARGEFILE, ETYMON_DB_PERM); if (lock_fd == -1) { if (log) { int e; char s[ETYMON_MAX_MSG_SIZE]; sprintf(s, "%s: Error writing to database", dbname); e = log->error(s, 1); if (e != 0) { exit(e); } return 0; } else { return aferr(AFEDBIO); } } close(lock_fd); return 1; }
static int createbuf() { int x; size_t z; linbufn = (bufsize / LINBUFSEGSIZE) + 1; linbuf = (unsigned char **) malloc(linbufn * sizeof (unsigned char *)); if (!linbuf) return aferr(AFEMEM); linbuflen = (size_t *) malloc(linbufn * sizeof (size_t)); if (!linbuflen) return aferr(AFEMEM); for (z = bufsize, x = 0; z > 0; z -= LINBUFSEGSIZE, x++) { linbuf[x] = (unsigned char *) malloc( linbuflen[x] = ( z < LINBUFSEGSIZE ? z : LINBUFSEGSIZE) ); if (!linbuf[x]) return aferr(AFEMEM); if (z < LINBUFSEGSIZE) break; } return 0; }
int aflinread(void *ptr, off_t offset, size_t size) { if ((offset + size) <= bufsize) { /* printf("."); fflush(stdout);*/ bufcpy((unsigned char *)ptr, offset, size); } else { /* printf("\n%lu %lu %lu\n", (unsigned long) offset, (unsigned long) size, (unsigned long) bufsize);*/ /* printf("*"); fflush(stdout);*/ fseeko(fbuf, offset, SEEK_SET); if (fread(ptr, 1, size, fbuf) < size) return aferr(AFEDBIO); } return 0; }