Browse Source

* chg imdb_t->errstr from pointer to buffer

master
Alex 'AdUser' Z 8 years ago
parent
commit
f9b0ebb6d2
  1. 20
      src/database.c
  2. 4
      src/database.h
  3. 4
      src/imdb-query.c
  4. 6
      src/imdb-write.c

20
src/database.c

@ -23,7 +23,7 @@
memset((buf), 0x0, (len)); \ memset((buf), 0x0, (len)); \
bytes = pread((db)->fd, (buf), (len), (off)); \ bytes = pread((db)->fd, (buf), (len), (off)); \
if (errno) { \ if (errno) { \
(db)->errstr = strerror(errno); \ strncpy((db)->error, strerror(errno), sizeof(db->error)); \
return -1; \ return -1; \
} }
@ -31,7 +31,7 @@
errno = 0; \ errno = 0; \
bytes = pwrite((db)->fd, (buf), (len), (off)); \ bytes = pwrite((db)->fd, (buf), (len), (off)); \
if (errno) { \ if (errno) { \
(db)->errstr = strerror(errno); \ strncpy((db)->error, strerror(errno), sizeof(db->error)); \
return -1; \ return -1; \
} }
@ -75,13 +75,13 @@ int imdb_open(imdb_db_t *db, const char *path, int write)
errno = 0; errno = 0;
if (stat(path, &st) == -1) { if (stat(path, &st) == -1) {
db->errstr = strerror(errno); strncpy(db->error, strerror(errno), sizeof(db->error));
return -1; return -1;
} }
errno = 0; errno = 0;
if ((db->fd = open(path, write ? O_RDWR : O_RDONLY)) == -1) { if ((db->fd = open(path, write ? O_RDWR : O_RDONLY)) == -1) {
db->errstr = strerror(errno); strncpy(db->error, strerror(errno), sizeof(db->error));
return -1; return -1;
} }
db->write = write; db->write = write;
@ -90,19 +90,19 @@ int imdb_open(imdb_db_t *db, const char *path, int write)
DB_READ(db, buf, IMDB_REC_LEN, 0); DB_READ(db, buf, IMDB_REC_LEN, 0);
if (bytes != IMDB_REC_LEN) { if (bytes != IMDB_REC_LEN) {
db->errstr = "Empty or damaged database file"; strncpy(db->error, "Empty or damaged database file", sizeof(db->error));
return -1; return -1;
} }
if (memcmp("IMDB", buf, 4) != 0) { if (memcmp("IMDB", buf, 4) != 0) {
db->errstr = "Not a database file"; strncpy(db->error, "Not a database file", sizeof(db->error));
return -1; return -1;
} }
if (atoi(buf + 6) != IMDB_VERSION) { if (atoi(buf + 6) != IMDB_VERSION) {
db->errstr = "Database version mismatch"; strncpy(db->error, "Database version mismatch", sizeof(db->error));
return -1; return -1;
} }
if (memcmp("CAPS", buf + 10, 4) != 0) { if (memcmp("CAPS", buf + 10, 4) != 0) {
db->errstr = "Can't read database capabilities"; strncpy(db->error, "Can't read database capabilities", sizeof(db->error));
return -1; return -1;
} }
memcpy(db->caps, buf + 16, sizeof(char) * 8); memcpy(db->caps, buf + 16, sizeof(char) * 8);
@ -116,7 +116,7 @@ int imdb_close(imdb_db_t *db)
errno = 0; errno = 0;
if (close(db->fd) != 0) { if (close(db->fd) != 0) {
db->errstr = strerror(errno); strncpy(db->error, strerror(errno), sizeof(db->error));
return -1; return -1;
} }
db->fd = -1; db->fd = -1;
@ -227,7 +227,7 @@ imdb_search(imdb_db_t * const db,
blk.records = blk_size; blk.records = blk_size;
if (imdb_read_rec(db, sample) < 1) { if (imdb_read_rec(db, sample) < 1) {
db->errstr = "Can't read source sample"; strncpy(db->error, "Can't read source sample", sizeof(db->error));
return -1; return -1;
} }

4
src/database.h

@ -8,8 +8,8 @@ typedef struct {
int fd; int fd;
int write; int write;
char path[PATH_MAX]; char path[PATH_MAX];
const char *errstr; char error[256];
unsigned char caps[8]; char caps[8];
} imdb_db_t; } imdb_db_t;
/** /**

4
src/imdb-query.c

@ -54,7 +54,7 @@ int search_similar(imdb_db_t *db, uint64_t number, float maxdiff)
sample.num = number; sample.num = number;
if ((ret = imdb_search(db, &sample, &search, &matches)) < 0) { if ((ret = imdb_search(db, &sample, &search, &matches)) < 0) {
fprintf(stderr, "%s\n", db->errstr); fprintf(stderr, "%s\n", db->error);
return 1; return 1;
} }
@ -244,7 +244,7 @@ int main(int argc, char **argv)
} }
if (imdb_open(&db, db_path, 0) == -1) { if (imdb_open(&db, db_path, 0) == -1) {
fprintf(stderr, "database open: %s\n", db.errstr); fprintf(stderr, "database open: %s\n", db.error);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

6
src/imdb-write.c

@ -68,7 +68,7 @@ int main(int argc, char **argv)
} }
if (mode != init && imdb_open(&db, db_path, 1) != 0) { if (mode != init && imdb_open(&db, db_path, 1) != 0) {
fprintf(stderr, "can't open database: %s\n", db.errstr); fprintf(stderr, "can't open database: %s\n", db.error);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
@ -81,13 +81,13 @@ int main(int argc, char **argv)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (imdb_write_rec(&db, &rec) < 1) { if (imdb_write_rec(&db, &rec) < 1) {
fprintf(stderr, "%s\n", db.errstr); fprintf(stderr, "%s\n", db.error);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
break; break;
case del : case del :
if (imdb_write_rec(&db, &rec) < 1) { if (imdb_write_rec(&db, &rec) < 1) {
fprintf(stderr, "%s\n", db.errstr); fprintf(stderr, "%s\n", db.error);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
break; break;

Loading…
Cancel
Save