Skip to content

Commit

Permalink
cyrusdb: handle CONVERT without noise
Browse files Browse the repository at this point in the history
This adds a new "BADFORMAT" return, and only tries to convert if the
existing file has BADFORMAT
  • Loading branch information
brong committed Jan 30, 2025
1 parent b1c1382 commit 089b8c4
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 29 deletions.
16 changes: 7 additions & 9 deletions lib/cyrusdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,18 @@ static int _myopen(const char *backend, const char *fname,

/* check if it opens normally. Horray */
r = db->backend->open(fname, flags, &db->engine, tid);
if (r == CYRUSDB_NOTFOUND) goto done; /* no open flags */
if (!r) goto done;

r = _detect_or_convert(db, backend, fname, flags);
if (r == CYRUSDB_BADFORMAT) {
r = _detect_or_convert(db, backend, fname, flags);
if (r) goto done;
r = db->backend->open(fname, flags, &db->engine, tid);
}
if (r) goto done;

r = db->backend->open(fname, flags, &db->engine, tid);

#ifdef DEBUGDB
syslog(LOG_NOTICE, "DEBUGDB open(%s, %d) => %llx", fname, flags, (long long unsigned)db->engine);
syslog(LOG_NOTICE, "DEBUGDB open(%s, %d) => %d, %llx", fname, flags, r, (long long unsigned)db->engine);
#endif

done:

done:
if (r) free(db);
else *ret = db;

Expand Down
3 changes: 2 additions & 1 deletion lib/cyrusdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ enum cyrusdb_ret {
CYRUSDB_LOCKED = -6,
CYRUSDB_NOTIMPLEMENTED = -7,
CYRUSDB_FULL = -8,
CYRUSDB_READONLY = -9
CYRUSDB_READONLY = -9,
CYRUSDB_BADFORMAT = -10,
};

enum cyrusdb_initflags {
Expand Down
11 changes: 7 additions & 4 deletions lib/cyrusdb_skiplist.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,16 +528,19 @@ static int read_header(struct dbengine *db)
{
const char *dptr;

assert(db && db->map_len && db->fname && db->map_base
&& db->is_open && db->lock_status);
assert(db);

if (db->map_len < HEADER_SIZE) {
syslog(LOG_ERR,
"skiplist: file not large enough for header: %s", db->fname);
return CYRUSDB_BADFORMAT;
}

assert(db->fname && db->map_base && db->is_open && db->lock_status);

if (memcmp(db->map_base, HEADER_MAGIC, HEADER_MAGIC_SIZE)) {
syslog(LOG_ERR, "skiplist: invalid magic header: %s", db->fname);
return CYRUSDB_IOERROR;
return CYRUSDB_BADFORMAT;
}

db->version = ntohl(*((uint32_t *)(db->map_base + OFFSET_VERSION)));
Expand All @@ -546,7 +549,7 @@ static int read_header(struct dbengine *db)
if (db->version != SKIPLIST_VERSION) {
syslog(LOG_ERR, "skiplist: version mismatch: %s has version %d.%d",
db->fname, db->version, db->version_minor);
return CYRUSDB_IOERROR;
return CYRUSDB_BADFORMAT;
}

db->maxlevel = ntohl(*((uint32_t *)(db->map_base + OFFSET_MAXLEVEL)));
Expand Down
3 changes: 3 additions & 0 deletions lib/cyrusdb_twom.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ static int _errormap(int r) {
case TWOM_LOCKED: return CYRUSDB_LOCKED;
case TWOM_NOTFOUND: return CYRUSDB_NOTFOUND;
case TWOM_READONLY: return CYRUSDB_READONLY;
case TWOM_BADFORMAT: return CYRUSDB_BADFORMAT;
case TWOM_BADUSAGE: return CYRUSDB_INTERNAL;
case TWOM_BADCHECKSUM: return CYRUSDB_IOERROR;
// must be a foreach result
default: return r;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/cyrusdb_twoskip.c
Original file line number Diff line number Diff line change
Expand Up @@ -479,12 +479,12 @@ static int read_header(struct dbengine *db)
if (SIZE(db) < HEADER_SIZE) {
syslog(LOG_ERR,
"twoskip: file not large enough for header: %s", FNAME(db));
return CYRUSDB_IOERROR;
return CYRUSDB_BADFORMAT;
}

if (memcmp(base, HEADER_MAGIC, HEADER_MAGIC_SIZE)) {
syslog(LOG_ERR, "twoskip: invalid magic header: %s", FNAME(db));
return CYRUSDB_IOERROR;
return CYRUSDB_BADFORMAT;
}

db->header.version
Expand All @@ -493,7 +493,7 @@ static int read_header(struct dbengine *db)
if (db->header.version > VERSION) {
syslog(LOG_ERR, "twoskip: version mismatch: %s has version %d",
FNAME(db), db->header.version);
return CYRUSDB_IOERROR;
return CYRUSDB_BADFORMAT;
}

db->header.generation
Expand Down
20 changes: 8 additions & 12 deletions lib/twom.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ static inline int check_headcsum(struct twom_txn *txn, struct tm_file *file, con
txn->db->error("invalid head checksum",
"filename=<%s> offset=<%08llX>",
txn->db->fname, (LLU)offset);
return TWOM_IOERROR;
return TWOM_BADCHECKSUM;
}

return 0;
Expand All @@ -460,7 +460,7 @@ static inline int check_tailcsum(struct twom_txn *txn, struct tm_file *file, con
txn->db->error("invalid tail checksum",
"filename=<%s> offset=<%08llX>",
txn->db->fname, (LLU)offset);
return TWOM_IOERROR;
return TWOM_BADCHECKSUM;
}

return 0;
Expand Down Expand Up @@ -585,15 +585,11 @@ static int read_header(struct twom_db *db, struct tm_file *file, struct tm_heade
const char *base = file->base;

if (file->size < HEADER_SIZE) {
db->error("file not large enough for header",
"filename=<%s>", db->fname);
return TWOM_IOERROR;
return TWOM_BADFORMAT;
}

if (memcmp(base, HEADER_MAGIC, HEADER_MAGIC_SIZE)) {
db->error("invalid magic header",
"filename=<%s>", db->fname);
return TWOM_IOERROR;
return TWOM_BADFORMAT;
}

memcpy(header->uuid, base + OFFSET_UUID, 16);
Expand All @@ -605,7 +601,7 @@ static int read_header(struct twom_db *db, struct tm_file *file, struct tm_heade
db->error("invalid version",
"filename=<%s> version=<%d>",
db->fname, header->version);
return TWOM_IOERROR;
return TWOM_BADFORMAT;
}

header->flags
Expand All @@ -615,7 +611,7 @@ static int read_header(struct twom_db *db, struct tm_file *file, struct tm_heade
if (!db->external_csum) {
db->error("missing external csum function",
"filename=<%s>", db->fname);
return TWOM_IOERROR;
return TWOM_BADUSAGE;
}
}
set_csum_engine(db, file, header->flags);
Expand All @@ -625,7 +621,7 @@ static int read_header(struct twom_db *db, struct tm_file *file, struct tm_heade
if (!db->external_compar) {
db->error("missing external compar function",
"filename=<%s>", db->fname);
return TWOM_IOERROR;
return TWOM_BADUSAGE;
}
file->compar = db->external_compar;
}
Expand Down Expand Up @@ -660,7 +656,7 @@ static int read_header(struct twom_db *db, struct tm_file *file, struct tm_heade
if (file->csum(base, OFFSET_CSUM) != csum) {
db->error("header checksum failure",
"filename=<%s>", db->fname);
return TWOM_IOERROR;
return TWOM_BADCHECKSUM;
}

return 0;
Expand Down
3 changes: 3 additions & 0 deletions lib/twom.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ enum twom_ret {
TWOM_LOCKED = -4,
TWOM_NOTFOUND = -5,
TWOM_READONLY = -6,
TWOM_BADFORMAT = -7,
TWOM_BADUSAGE = -8,
TWOM_BADCHECKSUM = -9,
};

// we don't reuse flags for different operations (e.g. open, fetch, foreach), as
Expand Down

0 comments on commit 089b8c4

Please sign in to comment.