📄 tdb.c
字号:
*/static int tdb_append_inplace(TDB_CONTEXT *tdb, TDB_DATA key, u32 hash, TDB_DATA new_dbuf){ struct list_struct rec; tdb_off rec_ptr; /* find entry */ if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) return -1; /* Append of 0 is always ok. */ if (new_dbuf.dsize == 0) return 0; /* must be long enough for key, old data + new data and tailer */ if (rec.rec_len < key.dsize + rec.data_len + new_dbuf.dsize + sizeof(tdb_off)) { /* No room. */ tdb->ecode = TDB_SUCCESS; /* Not really an error */ return -1; } if (tdb_write(tdb, rec_ptr + sizeof(rec) + rec.key_len + rec.data_len, new_dbuf.dptr, new_dbuf.dsize) == -1) return -1; /* update size */ rec.data_len += new_dbuf.dsize; return rec_write(tdb, rec_ptr, &rec);}/* Append to an entry. Create if not exist. */int tdb_append(TDB_CONTEXT *tdb, TDB_DATA key, TDB_DATA new_dbuf){ struct list_struct rec; u32 hash; tdb_off rec_ptr; char *p = NULL; int ret = 0; size_t new_data_size = 0; /* find which hash bucket it is in */ hash = tdb->hash_fn(&key); if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1) return -1; /* first try in-place. */ if (tdb_append_inplace(tdb, key, hash, new_dbuf) == 0) goto out; /* reset the error code potentially set by the tdb_append_inplace() */ tdb->ecode = TDB_SUCCESS; /* find entry */ if (!(rec_ptr = tdb_find(tdb, key, hash, &rec))) { if (tdb->ecode != TDB_ERR_NOEXIST) goto fail; /* Not found - create. */ ret = tdb_store(tdb, key, new_dbuf, TDB_INSERT); goto out; } new_data_size = rec.data_len + new_dbuf.dsize; /* Copy key+old_value+value *before* allocating free space in case malloc fails and we are left with a dead spot in the tdb. */ if (!(p = (char *)malloc(key.dsize + new_data_size))) { tdb->ecode = TDB_ERR_OOM; goto fail; } /* Copy the key in place. */ memcpy(p, key.dptr, key.dsize); /* Now read the old data into place. */ if (rec.data_len && tdb_read(tdb, rec_ptr + sizeof(rec) + rec.key_len, p + key.dsize, rec.data_len, 0) == -1) goto fail; /* Finally append the new data. */ if (new_dbuf.dsize) memcpy(p+key.dsize+rec.data_len, new_dbuf.dptr, new_dbuf.dsize); /* delete any existing record - if it doesn't exist we don't care. Doing this first reduces fragmentation, and avoids coalescing with `allocated' block before it's updated. */ tdb_delete_hash(tdb, key, hash); if (!(rec_ptr = tdb_allocate(tdb, key.dsize + new_data_size, &rec))) goto fail; /* Read hash top into next ptr */ if (ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1) goto fail; rec.key_len = key.dsize; rec.data_len = new_data_size; rec.full_hash = hash; rec.magic = TDB_MAGIC; /* write out and point the top of the hash chain at it */ if (rec_write(tdb, rec_ptr, &rec) == -1 || tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+new_data_size)==-1 || ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) { /* Need to tdb_unallocate() here */ goto fail; } out: SAFE_FREE(p); tdb_unlock(tdb, BUCKET(hash), F_WRLCK); return ret;fail: ret = -1; goto out;}static int tdb_already_open(dev_t device, ino_t ino){ TDB_CONTEXT *i; for (i = tdbs; i; i = i->next) { if (i->device == device && i->inode == ino) { return 1; } } return 0;}/* This is based on the hash algorithm from gdbm */static u32 default_tdb_hash(TDB_DATA *key){ u32 value; /* Used to compute the hash value. */ u32 i; /* Used to cycle through random values. */ /* Set the initial value from the key size. */ for (value = 0x238F13AF * key->dsize, i=0; i < key->dsize; i++) value = (value + (key->dptr[i] << (i*5 % 24))); return (1103515243 * value + 12345); }/* open the database, creating it if necessary The open_flags and mode are passed straight to the open call on the database file. A flags value of O_WRONLY is invalid. The hash size is advisory, use zero for a default value. Return is NULL on error, in which case errno is also set. Don't try to call tdb_error or tdb_errname, just do strerror(errno). @param name may be NULL for internal databases. */TDB_CONTEXT *tdb_open(const char *name, int hash_size, int tdb_flags, int open_flags, mode_t mode){ return tdb_open_ex(name, hash_size, tdb_flags, open_flags, mode, NULL, NULL);}TDB_CONTEXT *tdb_open_ex(const char *name, int hash_size, int tdb_flags, int open_flags, mode_t mode, tdb_log_func log_fn, tdb_hash_func hash_fn){ TDB_CONTEXT *tdb; struct stat st; int rev = 0, locked = 0; unsigned char *vp; u32 vertest; if (!(tdb = calloc(1, sizeof *tdb))) { /* Can't log this */ errno = ENOMEM; goto fail; } tdb->fd = -1; tdb->name = NULL; tdb->map_ptr = NULL; tdb->flags = tdb_flags; tdb->open_flags = open_flags; tdb->log_fn = log_fn; tdb->hash_fn = hash_fn ? hash_fn : default_tdb_hash; if ((open_flags & O_ACCMODE) == O_WRONLY) { TDB_LOG((tdb, 0, "tdb_open_ex: can't open tdb %s write-only\n", name)); errno = EINVAL; goto fail; } if (hash_size == 0) hash_size = DEFAULT_HASH_SIZE; if ((open_flags & O_ACCMODE) == O_RDONLY) { tdb->read_only = 1; /* read only databases don't do locking or clear if first */ tdb->flags |= TDB_NOLOCK; tdb->flags &= ~TDB_CLEAR_IF_FIRST; } /* internal databases don't mmap or lock, and start off cleared */ if (tdb->flags & TDB_INTERNAL) { tdb->flags |= (TDB_NOLOCK | TDB_NOMMAP); tdb->flags &= ~TDB_CLEAR_IF_FIRST; if (tdb_new_database(tdb, hash_size) != 0) { TDB_LOG((tdb, 0, "tdb_open_ex: tdb_new_database failed!")); goto fail; } goto internal; } if ((tdb->fd = open(name, open_flags, mode)) == -1) { TDB_LOG((tdb, 5, "tdb_open_ex: could not open file %s: %s\n", name, strerror(errno))); goto fail; /* errno set by open(2) */ } /* ensure there is only one process initialising at once */ if (tdb_brlock(tdb, GLOBAL_LOCK, F_WRLCK, F_SETLKW, 0) == -1) { TDB_LOG((tdb, 0, "tdb_open_ex: failed to get global lock on %s: %s\n", name, strerror(errno))); goto fail; /* errno set by tdb_brlock */ } /* we need to zero database if we are the only one with it open */ if ((tdb_flags & TDB_CLEAR_IF_FIRST) && (locked = (tdb_brlock(tdb, ACTIVE_LOCK, F_WRLCK, F_SETLK, 0) == 0))) { open_flags |= O_CREAT; if (ftruncate(tdb->fd, 0) == -1) { TDB_LOG((tdb, 0, "tdb_open_ex: " "failed to truncate %s: %s\n", name, strerror(errno))); goto fail; /* errno set by ftruncate */ } } if (read(tdb->fd, &tdb->header, sizeof(tdb->header)) != sizeof(tdb->header) || strcmp(tdb->header.magic_food, TDB_MAGIC_FOOD) != 0 || (tdb->header.version != TDB_VERSION && !(rev = (tdb->header.version==TDB_BYTEREV(TDB_VERSION))))) { /* its not a valid database - possibly initialise it */ if (!(open_flags & O_CREAT) || tdb_new_database(tdb, hash_size) == -1) { errno = EIO; /* ie bad format or something */ goto fail; } rev = (tdb->flags & TDB_CONVERT); } vp = (unsigned char *)&tdb->header.version; vertest = (((u32)vp[0]) << 24) | (((u32)vp[1]) << 16) | (((u32)vp[2]) << 8) | (u32)vp[3]; tdb->flags |= (vertest==TDB_VERSION) ? TDB_BIGENDIAN : 0; if (!rev) tdb->flags &= ~TDB_CONVERT; else { tdb->flags |= TDB_CONVERT; convert(&tdb->header, sizeof(tdb->header)); } if (fstat(tdb->fd, &st) == -1) goto fail; /* Is it already in the open list? If so, fail. */ if (tdb_already_open(st.st_dev, st.st_ino)) { TDB_LOG((tdb, 2, "tdb_open_ex: " "%s (%d,%d) is already open in this process\n", name, (int)st.st_dev, (int)st.st_ino)); errno = EBUSY; goto fail; } if (!(tdb->name = (char *)strdup(name))) { errno = ENOMEM; goto fail; } tdb->map_size = st.st_size; tdb->device = st.st_dev; tdb->inode = st.st_ino; tdb->locked = calloc(tdb->header.hash_size+1, sizeof(tdb->locked[0])); if (!tdb->locked) { TDB_LOG((tdb, 2, "tdb_open_ex: " "failed to allocate lock structure for %s\n", name)); errno = ENOMEM; goto fail; } tdb_mmap(tdb); if (locked) { if (!tdb->read_only) if (tdb_clear_spinlocks(tdb) != 0) { TDB_LOG((tdb, 0, "tdb_open_ex: " "failed to clear spinlock\n")); goto fail; } if (tdb_brlock(tdb, ACTIVE_LOCK, F_UNLCK, F_SETLK, 0) == -1) { TDB_LOG((tdb, 0, "tdb_open_ex: " "failed to take ACTIVE_LOCK on %s: %s\n", name, strerror(errno))); goto fail; } } /* We always need to do this if the CLEAR_IF_FIRST flag is set, even if we didn't get the initial exclusive lock as we need to let all other users know we're using it. */ if (tdb_flags & TDB_CLEAR_IF_FIRST) { /* leave this lock in place to indicate it's in use */ if (tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0) == -1) goto fail; } internal: /* Internal (memory-only) databases skip all the code above to * do with disk files, and resume here by releasing their * global lock and hooking into the active list. */ if (tdb_brlock(tdb, GLOBAL_LOCK, F_UNLCK, F_SETLKW, 0) == -1) goto fail; tdb->next = tdbs; tdbs = tdb; return tdb; fail: { int save_errno = errno; if (!tdb) return NULL; if (tdb->map_ptr) { if (tdb->flags & TDB_INTERNAL) SAFE_FREE(tdb->map_ptr); else tdb_munmap(tdb); } SAFE_FREE(tdb->name); if (tdb->fd != -1) if (close(tdb->fd) != 0) TDB_LOG((tdb, 5, "tdb_open_ex: failed to close tdb->fd on error!\n")); SAFE_FREE(tdb->locked); SAFE_FREE(tdb); errno = save_errno; return NULL; }}/** * Close a database. * * @returns -1 for error; 0 for success. **/int tdb_close(TDB_CONTEXT *tdb){ TDB_CONTEXT **i; int ret = 0; if (tdb->map_ptr) { if (tdb->flags & TDB_INTERNAL) SAFE_FREE(tdb->map_ptr); else tdb_munmap(tdb); } SAFE_FREE(tdb->name); if (tdb->fd != -1) ret = close(tdb->fd); SAFE_FREE(tdb->locked); /* Remove from contexts list */ for (i = &tdbs; *i; i = &(*i)->next) { if (*i == tdb) { *i = tdb->next; break; } } memset(tdb, 0, sizeof(*tdb)); SAFE_FREE(tdb); return ret;}/* lock/unlock entire database */int tdb_lockall(TDB_CONTEXT *tdb){ u32 i; /* There are no locks on read-only dbs */ if (tdb->read_only) return TDB_ERRCODE(TDB_ERR_LOCK, -1); for (i = 0; i < tdb->header.hash_size; i++) if (tdb_lock(tdb, i, F_WRLCK)) break; /* If error, release locks we have... */ if (i < tdb->header.hash_size) { u32 j; for ( j = 0; j < i; j++) tdb_unlock(tdb, j, F_WRLCK); return TDB_ERRCODE(TDB_ERR_NOLOCK, -1); } return 0;}void tdb_unlockall(TDB_CONTEXT *tdb){ u32 i; for (i=0; i < tdb->header.hash_size; i++) tdb_unlock(tdb, i, F_WRLCK);}/* lock/unlock one hash chain. This is meant to be used to reduce contention - it cannot guarantee how many records will be locked */int tdb_chainlock(TDB_CONTEXT *tdb, TDB_DATA key){ return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);}int tdb_chainunlock(TDB_CONTEXT *tdb, TDB_DATA key){ return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_WRLCK);}int tdb_chainlock_read(TDB_CONTEXT *tdb, TDB_DATA key){ return tdb_lock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);}int tdb_chainunlock_read(TDB_CONTEXT *tdb, TDB_DATA key){ return tdb_unlock(tdb, BUCKET(tdb->hash_fn(&key)), F_RDLCK);}/* register a loging function */void tdb_logging_function(TDB_CONTEXT *tdb, void (*fn)(TDB_CONTEXT *, int , const char *, ...)){ tdb->log_fn = fn;}/* reopen a tdb - this can be used after a fork to ensure that we have an independent seek pointer from our parent and to re-establish locks */int tdb_reopen(TDB_CONTEXT *tdb){ struct stat st; if (tdb->flags & TDB_INTERNAL) return 0; /* Nothing to do. */ if (tdb_munmap(tdb) != 0) { TDB_LOG((tdb, 0, "tdb_reopen: munmap failed (%s)\n", strerror(errno))); goto fail; } if (close(tdb->fd) != 0) TDB_LOG((tdb, 0, "tdb_reopen: WARNING closing tdb->fd failed!\n")); tdb->fd = open(tdb->name, tdb->open_flags & ~(O_CREAT|O_TRUNC), 0); if (tdb->fd == -1) { TDB_LOG((tdb, 0, "tdb_reopen: open failed (%s)\n", strerror(errno))); goto fail; } if (fstat(tdb->fd, &st) != 0) { TDB_LOG((tdb, 0, "tdb_reopen: fstat failed (%s)\n", strerror(errno))); goto fail; } if (st.st_ino != tdb->inode || st.st_dev != tdb->device) { TDB_LOG((tdb, 0, "tdb_reopen: file dev/inode has changed!\n")); goto fail; } tdb_mmap(tdb); if ((tdb->flags & TDB_CLEAR_IF_FIRST) && (tdb_brlock(tdb, ACTIVE_LOCK, F_RDLCK, F_SETLKW, 0) == -1)) { TDB_LOG((tdb, 0, "tdb_reopen: failed to obtain active lock\n")); goto fail; } return 0;fail: tdb_close(tdb); return -1;}/* reopen all tdb's */int tdb_reopen_all(void){ TDB_CONTEXT *tdb; for (tdb=tdbs; tdb; tdb = tdb->next) { /* Ensure no clear-if-first. */ tdb->flags &= ~TDB_CLEAR_IF_FIRST; if (tdb_reopen(tdb) != 0) return -1; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -