X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;ds=sidebyside;f=ccan%2Ftdb2%2Ftdb.c;h=8079dad7887d89a649d3ec8aa67daf215d0af041;hb=7d6312518bd3a8e4cbad69b1f6b1800f7bcdf68c;hp=58a62cb400da905dba1fbae3845ed4a0d1f47dbe;hpb=587982955ca9c61363d6e3004622ee97eda80e4b;p=ccan diff --git a/ccan/tdb2/tdb.c b/ccan/tdb2/tdb.c index 58a62cb4..8079dad7 100644 --- a/ccan/tdb2/tdb.c +++ b/ccan/tdb2/tdb.c @@ -173,6 +173,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, ssize_t rlen; struct tdb_header hdr; struct tdb_attribute_seed *seed = NULL; + enum TDB_ERROR ecode; tdb = malloc(sizeof(*tdb)); if (!tdb) { @@ -265,9 +266,8 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, fcntl(tdb->fd, F_SETFD, v | FD_CLOEXEC); /* ensure there is only one process initialising at once */ - if (tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK) == -1) { - /* errno set by tdb_brlock */ - saved_errno = errno; + tdb->ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK); + if (tdb->ecode != TDB_SUCCESS) { goto fail; } @@ -324,7 +324,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, /* Is it already in the open list? If so, fail. */ if (tdb_already_open(st.st_dev, st.st_ino)) { /* FIXME */ - tdb_logerr(tdb, TDB_ERR_NESTING, TDB_LOG_USE_ERROR, + tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_USE_ERROR, "tdb_open: %s (%d,%d) is already open in this" " process", name, (int)st.st_dev, (int)st.st_ino); @@ -346,8 +346,12 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, tdb->methods->oob(tdb, tdb->map_size + 1, true); /* Now it's fully formed, recover if necessary. */ - if (tdb_needs_recovery(tdb) && tdb_lock_and_recover(tdb) == -1) { - goto fail; + if (tdb_needs_recovery(tdb)) { + ecode = tdb_lock_and_recover(tdb); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; + goto fail; + } } if (tdb_ftable_init(tdb) == -1) @@ -374,9 +378,6 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags, case TDB_ERR_EINVAL: saved_errno = EINVAL; break; - case TDB_ERR_NESTING: - saved_errno = EBUSY; - break; default: saved_errno = EINVAL; break; @@ -412,12 +413,18 @@ static int update_rec_hdr(struct tdb_context *tdb, uint64_t h) { uint64_t dataroom = rec_data_length(rec) + rec_extra_padding(rec); + enum TDB_ERROR ecode; if (set_header(tdb, rec, TDB_USED_MAGIC, keylen, datalen, keylen + dataroom, h)) return -1; - return tdb_write_convert(tdb, off, rec, sizeof(*rec)); + ecode = tdb_write_convert(tdb, off, rec, sizeof(*rec)); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; + return -1; + } + return 0; } /* Returns -1 on error, 0 on OK */ @@ -428,6 +435,7 @@ static int replace_data(struct tdb_context *tdb, bool growing) { tdb_off_t new_off; + enum TDB_ERROR ecode; /* Allocate a new record. */ new_off = alloc(tdb, key.dsize, dbuf.dsize, h->h, TDB_USED_MAGIC, @@ -449,12 +457,18 @@ static int replace_data(struct tdb_context *tdb, } new_off += sizeof(struct tdb_used_record); - if (tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize) == -1) + ecode = tdb->methods->twrite(tdb, new_off, key.dptr, key.dsize); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; return -1; + } new_off += key.dsize; - if (tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize) == -1) + ecode = tdb->methods->twrite(tdb, new_off, dbuf.dptr, dbuf.dsize); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; return -1; + } /* FIXME: tdb_increment_seqnum(tdb); */ return 0; @@ -468,6 +482,7 @@ int tdb_store(struct tdb_context *tdb, tdb_len_t old_room = 0; struct tdb_used_record rec; int ret; + enum TDB_ERROR ecode; off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL); if (unlikely(off == TDB_OFF_ERR)) @@ -489,10 +504,15 @@ int tdb_store(struct tdb_context *tdb, key.dsize, dbuf.dsize, &rec, h.h)) goto fail; - if (tdb->methods->twrite(tdb, off + sizeof(rec) - + key.dsize, - dbuf.dptr, dbuf.dsize)) + ecode = tdb->methods->twrite(tdb, + off + sizeof(rec) + + key.dsize, + dbuf.dptr, + dbuf.dsize); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; goto fail; + } tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, F_WRLCK); return 0; @@ -527,6 +547,7 @@ int tdb_append(struct tdb_context *tdb, tdb_len_t old_room = 0, old_dlen; unsigned char *newdata; struct tdb_data new_dbuf; + enum TDB_ERROR ecode; int ret; off = find_and_lock(tdb, key, F_WRLCK, &h, &rec, NULL); @@ -544,9 +565,12 @@ int tdb_append(struct tdb_context *tdb, goto fail; off += sizeof(rec) + key.dsize + old_dlen; - if (tdb->methods->twrite(tdb, off, dbuf.dptr, - dbuf.dsize) == -1) + ecode = tdb->methods->twrite(tdb, off, dbuf.dptr, + dbuf.dsize); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; goto fail; + } /* FIXME: tdb_increment_seqnum(tdb); */ tdb_unlock_hashes(tdb, h.hlock_start, h.hlock_range, @@ -562,8 +586,10 @@ int tdb_append(struct tdb_context *tdb, (size_t)(key.dsize+old_dlen+dbuf.dsize)); goto fail; } - if (tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize, - newdata, old_dlen) != 0) { + ecode = tdb->methods->tread(tdb, off + sizeof(rec) + key.dsize, + newdata, old_dlen); + if (ecode != TDB_SUCCESS) { + tdb->ecode = ecode; free(newdata); goto fail; } @@ -702,7 +728,6 @@ const char *tdb_errorstr(const struct tdb_context *tdb) case TDB_ERR_LOCK: return "Locking error"; case TDB_ERR_OOM: return "Out of memory"; case TDB_ERR_EXISTS: return "Record exists"; - case TDB_ERR_NESTING: return "Transaction already started"; case TDB_ERR_EINVAL: return "Invalid parameter"; case TDB_ERR_NOEXIST: return "Record does not exist"; case TDB_ERR_RDONLY: return "write not permitted"; @@ -710,10 +735,10 @@ const char *tdb_errorstr(const struct tdb_context *tdb) return "Invalid error code"; } -void COLD tdb_logerr(struct tdb_context *tdb, - enum TDB_ERROR ecode, - enum tdb_log_level level, - const char *fmt, ...) +enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb, + enum TDB_ERROR ecode, + enum tdb_log_level level, + const char *fmt, ...) { char *message; va_list ap; @@ -724,7 +749,7 @@ void COLD tdb_logerr(struct tdb_context *tdb, tdb->ecode = ecode; if (!tdb->logfn) - return; + return ecode; /* FIXME: Doesn't assume asprintf. */ va_start(ap, fmt); @@ -736,7 +761,7 @@ void COLD tdb_logerr(struct tdb_context *tdb, tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private, "out of memory formatting message:"); tdb->logfn(tdb, level, tdb->log_private, fmt); - return; + return ecode; } va_start(ap, fmt); len = vsprintf(message, fmt, ap); @@ -744,4 +769,5 @@ void COLD tdb_logerr(struct tdb_context *tdb, tdb->logfn(tdb, level, tdb->log_private, message); free(message); errno = saved_errno; + return ecode; }