X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftdb2%2Ftdb1_check.c;h=51b8b594f414bb5539ddaa09dacbccee2ab9b220;hb=8458811a4126c22635b974718bfbf2876c893c37;hp=d62a4b927108a6e4d432517b157e5b2e8328a2ad;hpb=19e6c1a250ade1e7204ada17163294855585e825;p=ccan diff --git a/ccan/tdb2/tdb1_check.c b/ccan/tdb2/tdb1_check.c index d62a4b92..51b8b594 100644 --- a/ccan/tdb2/tdb1_check.c +++ b/ccan/tdb2/tdb1_check.c @@ -32,7 +32,7 @@ static bool tdb1_check_header(struct tdb1_context *tdb, tdb1_off_t *recovery) if (tdb->methods->tdb1_read(tdb, 0, &hdr, sizeof(hdr), 0) == -1) return false; - if (strcmp(hdr.magic_food, TDB1_MAGIC_FOOD) != 0) + if (strcmp(hdr.magic_food, TDB_MAGIC_FOOD) != 0) goto corrupt; TDB1_CONV(hdr); @@ -61,8 +61,8 @@ static bool tdb1_check_header(struct tdb1_context *tdb, tdb1_off_t *recovery) return true; corrupt: - tdb->ecode = TDB1_ERR_CORRUPT; - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, "Header is corrupt\n")); + tdb->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Header is corrupt\n"); return false; } @@ -75,21 +75,21 @@ static bool tdb1_check_record(struct tdb1_context *tdb, /* Check rec->next: 0 or points to record offset, aligned. */ if (rec->next > 0 && rec->next < TDB1_DATA_START(tdb->header.hash_size)){ - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Record offset %d too small next %d\n", - off, rec->next)); + tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Record offset %d too small next %d\n", + off, rec->next); goto corrupt; } if (rec->next + sizeof(*rec) < rec->next) { - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Record offset %d too large next %d\n", - off, rec->next)); + tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Record offset %d too large next %d\n", + off, rec->next); goto corrupt; } if ((rec->next % TDB1_ALIGNMENT) != 0) { - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Record offset %d misaligned next %d\n", - off, rec->next)); + tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Record offset %d misaligned next %d\n", + off, rec->next); goto corrupt; } if (tdb->methods->tdb1_oob(tdb, rec->next+sizeof(*rec), 0)) @@ -97,16 +97,16 @@ static bool tdb1_check_record(struct tdb1_context *tdb, /* Check rec_len: similar to rec->next, implies next record. */ if ((rec->rec_len % TDB1_ALIGNMENT) != 0) { - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Record offset %d misaligned length %d\n", - off, rec->rec_len)); + tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Record offset %d misaligned length %d\n", + off, rec->rec_len); goto corrupt; } /* Must fit tailer. */ if (rec->rec_len < sizeof(tailer)) { - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Record offset %d too short length %d\n", - off, rec->rec_len)); + tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Record offset %d too short length %d\n", + off, rec->rec_len); goto corrupt; } /* OOB allows "right at the end" access, so this works for last rec. */ @@ -118,38 +118,38 @@ static bool tdb1_check_record(struct tdb1_context *tdb, &tailer) == -1) goto corrupt; if (tailer != sizeof(*rec) + rec->rec_len) { - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Record offset %d invalid tailer\n", off)); + tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Record offset %d invalid tailer\n", off); goto corrupt; } return true; corrupt: - tdb->ecode = TDB1_ERR_CORRUPT; + tdb->last_error = TDB_ERR_CORRUPT; return false; } /* Grab some bytes: may copy if can't use mmap. Caller has already done bounds check. */ -static TDB1_DATA get_bytes(struct tdb1_context *tdb, +static TDB_DATA get_bytes(struct tdb1_context *tdb, tdb1_off_t off, tdb1_len_t len) { - TDB1_DATA d; + TDB_DATA d; d.dsize = len; - if (tdb->transaction == NULL && tdb->map_ptr != NULL) - d.dptr = (unsigned char *)tdb->map_ptr + off; + if (tdb->transaction == NULL && tdb->file->map_ptr != NULL) + d.dptr = (unsigned char *)tdb->file->map_ptr + off; else d.dptr = tdb1_alloc_read(tdb, off, d.dsize); return d; } /* Frees data if we're not able to simply use mmap. */ -static void put_bytes(struct tdb1_context *tdb, TDB1_DATA d) +static void put_bytes(struct tdb1_context *tdb, TDB_DATA d) { - if (tdb->transaction == NULL && tdb->map_ptr != NULL) + if (tdb->transaction == NULL && tdb->file->map_ptr != NULL) return; free(d.dptr); } @@ -236,18 +236,18 @@ static bool tdb1_check_used_record(struct tdb1_context *tdb, tdb1_off_t off, const struct tdb1_record *rec, unsigned char **hashes, - int (*check)(TDB1_DATA, TDB1_DATA, void *), + int (*check)(TDB_DATA, TDB_DATA, void *), void *private_data) { - TDB1_DATA key, data; + TDB_DATA key, data; if (!tdb1_check_record(tdb, off, rec)) return false; /* key + data + tailer must fit in record */ if (rec->key_len + rec->data_len + sizeof(tdb1_off_t) > rec->rec_len) { - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Record offset %d too short for contents\n", off)); + tdb->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Record offset %d too short for contents\n", off); return false; } @@ -256,8 +256,8 @@ static bool tdb1_check_used_record(struct tdb1_context *tdb, return false; if (tdb->hash_fn(&key) != rec->full_hash) { - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Record offset %d has incorrect hash\n", off)); + tdb->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Record offset %d has incorrect hash\n", off); goto fail_put_key; } @@ -312,7 +312,7 @@ size_t tdb1_dead_space(struct tdb1_context *tdb, tdb1_off_t off) { size_t len; - for (len = 0; off + len < tdb->map_size; len++) { + for (len = 0; off + len < tdb->file->map_size; len++) { char c; if (tdb->methods->tdb1_read(tdb, off, &c, 1, 0)) return 0; @@ -322,8 +322,8 @@ size_t tdb1_dead_space(struct tdb1_context *tdb, tdb1_off_t off) return len; } -_PUBLIC_ int tdb1_check(struct tdb1_context *tdb, - int (*check)(TDB1_DATA key, TDB1_DATA data, void *private_data), +int tdb1_check(struct tdb1_context *tdb, + int (*check)(TDB_DATA key, TDB_DATA data, void *private_data), void *private_data) { unsigned int h; @@ -336,7 +336,7 @@ _PUBLIC_ int tdb1_check(struct tdb1_context *tdb, /* Read-only databases use no locking at all: it's best-effort. * We may have a write lock already, so skip that case too. */ - if (tdb->read_only || tdb->allrecord_lock.count != 0) { + if (tdb->read_only || tdb->file->allrecord_lock.count != 0) { locked = false; } else { if (tdb1_lockall_read(tdb) == -1) @@ -345,16 +345,16 @@ _PUBLIC_ int tdb1_check(struct tdb1_context *tdb, } /* Make sure we know true size of the underlying file. */ - tdb->methods->tdb1_oob(tdb, tdb->map_size + 1, 1); + tdb->methods->tdb1_oob(tdb, tdb->file->map_size + 1, 1); /* Header must be OK: also gets us the recovery ptr, if any. */ if (!tdb1_check_header(tdb, &recovery_start)) goto unlock; /* We should have the whole header, too. */ - if (tdb->map_size < TDB1_DATA_START(tdb->header.hash_size)) { - tdb->ecode = TDB1_ERR_CORRUPT; - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, "File too short for hashes\n")); + if (tdb->file->map_size < TDB1_DATA_START(tdb->header.hash_size)) { + tdb->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "File too short for hashes\n"); goto unlock; } @@ -363,7 +363,7 @@ _PUBLIC_ int tdb1_check(struct tdb1_context *tdb, 1, sizeof(hashes[0]) * (1+tdb->header.hash_size) + BITMAP_BITS / CHAR_BIT * (1+tdb->header.hash_size)); if (!hashes) { - tdb->ecode = TDB1_ERR_OOM; + tdb->last_error = TDB_ERR_OOM; goto unlock; } @@ -383,7 +383,7 @@ _PUBLIC_ int tdb1_check(struct tdb1_context *tdb, /* For each record, read it in and check it's ok. */ for (off = TDB1_DATA_START(tdb->header.hash_size); - off < tdb->map_size; + off < tdb->file->map_size; off += sizeof(rec) + rec.rec_len) { if (tdb->methods->tdb1_read(tdb, off, &rec, sizeof(rec), TDB1_DOCONV()) == -1) @@ -410,26 +410,25 @@ _PUBLIC_ int tdb1_check(struct tdb1_context *tdb, if (dead < sizeof(rec)) goto corrupt; - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Dead space at %d-%d (of %u)\n", - off, off + dead, tdb->map_size)); + tdb_logerr(tdb, TDB_SUCCESS, TDB_LOG_WARNING, + "Dead space at %d-%d (of %u)\n", + off, off + dead, tdb->file->map_size); rec.rec_len = dead - sizeof(rec); break; case TDB1_RECOVERY_MAGIC: if (recovery_start != off) { - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Unexpected recovery record at offset %d\n", - off)); + tdb->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Unexpected recovery record at offset %d\n", + off); goto free; } found_recovery = true; break; default: ; corrupt: - tdb->ecode = TDB1_ERR_CORRUPT; - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Bad magic 0x%x at offset %d\n", - rec.magic, off)); + tdb->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Bad magic 0x%x at offset %d\n", + rec.magic, off); goto free; } } @@ -440,9 +439,8 @@ _PUBLIC_ int tdb1_check(struct tdb1_context *tdb, unsigned int i; for (i = 0; i < BITMAP_BITS / CHAR_BIT; i++) { if (hashes[h][i] != 0) { - tdb->ecode = TDB1_ERR_CORRUPT; - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Hashes do not match records\n")); + tdb->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Hashes do not match records\n"); goto free; } } @@ -450,9 +448,9 @@ _PUBLIC_ int tdb1_check(struct tdb1_context *tdb, /* We must have found recovery area if there was one. */ if (recovery_start != 0 && !found_recovery) { - TDB1_LOG((tdb, TDB1_DEBUG_ERROR, - "Expected a recovery area at %u\n", - recovery_start)); + tdb->last_error = tdb_logerr(tdb, TDB_ERR_CORRUPT, TDB_LOG_ERROR, + "Expected a recovery area at %u\n", + recovery_start); goto free; }