⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tdb.c

📁 samba最新软件
💻 C
📖 第 1 页 / 共 2 页
字号:
		return 0;	/* keep looking until we find the right record */	while (rec_ptr) {		if (tdb_rec_read(tdb, rec_ptr, r) == -1)			return 0;		if (TDB_DEAD(r) && r->rec_len >= length) {			/*			 * First fit for simple coding, TODO: change to best			 * fit			 */			return rec_ptr;		}		rec_ptr = r->next;	}	return 0;}/* store an element in the database, replacing any existing element   with the same key    return 0 on success, -1 on failure*/int tdb_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, int flag){	struct list_struct rec;	uint32_t hash;	tdb_off_t rec_ptr;	char *p = NULL;	int ret = -1;	if (tdb->read_only || tdb->traverse_read) {		tdb->ecode = TDB_ERR_RDONLY;		return -1;	}	/* find which hash bucket it is in */	hash = tdb->hash_fn(&key);	if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)		return -1;	/* check for it existing, on insert. */	if (flag == TDB_INSERT) {		if (tdb_exists_hash(tdb, key, hash)) {			tdb->ecode = TDB_ERR_EXISTS;			goto fail;		}	} else {		/* first try in-place update, on modify or replace. */		if (tdb_update_hash(tdb, key, hash, dbuf) == 0) {			goto done;		}		if (tdb->ecode == TDB_ERR_NOEXIST &&		    flag == TDB_MODIFY) {			/* if the record doesn't exist and we are in TDB_MODIFY mode then			 we should fail the store */			goto fail;		}	}	/* reset the error code potentially set by the tdb_update() */	tdb->ecode = TDB_SUCCESS;	/* 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. */	if (flag != TDB_INSERT)		tdb_delete_hash(tdb, key, hash);	/* Copy key+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 + dbuf.dsize))) {		tdb->ecode = TDB_ERR_OOM;		goto fail;	}	memcpy(p, key.dptr, key.dsize);	if (dbuf.dsize)		memcpy(p+key.dsize, dbuf.dptr, dbuf.dsize);	if (tdb->max_dead_records != 0) {		/*		 * Allow for some dead records per hash chain, look if we can		 * find one that can hold the new record. We need enough space		 * for key, data and tailer. If we find one, we don't have to		 * consult the central freelist.		 */		rec_ptr = tdb_find_dead(			tdb, hash, &rec,			key.dsize + dbuf.dsize + sizeof(tdb_off_t));		if (rec_ptr != 0) {			rec.key_len = key.dsize;			rec.data_len = dbuf.dsize;			rec.full_hash = hash;			rec.magic = TDB_MAGIC;			if (tdb_rec_write(tdb, rec_ptr, &rec) == -1			    || tdb->methods->tdb_write(				    tdb, rec_ptr + sizeof(rec),				    p, key.dsize + dbuf.dsize) == -1) {				goto fail;			}			goto done;		}	}	/*	 * We have to allocate some space from the freelist, so this means we	 * have to lock it. Use the chance to purge all the DEAD records from	 * the hash chain under the freelist lock.	 */	if (tdb_lock(tdb, -1, F_WRLCK) == -1) {		goto fail;	}	if ((tdb->max_dead_records != 0)	    && (tdb_purge_dead(tdb, hash) == -1)) {		tdb_unlock(tdb, -1, F_WRLCK);		goto fail;	}	/* we have to allocate some space */	rec_ptr = tdb_allocate(tdb, key.dsize + dbuf.dsize, &rec);	tdb_unlock(tdb, -1, F_WRLCK);	if (rec_ptr == 0) {		goto fail;	}	/* Read hash top into next ptr */	if (tdb_ofs_read(tdb, TDB_HASH_TOP(hash), &rec.next) == -1)		goto fail;	rec.key_len = key.dsize;	rec.data_len = dbuf.dsize;	rec.full_hash = hash;	rec.magic = TDB_MAGIC;	/* write out and point the top of the hash chain at it */	if (tdb_rec_write(tdb, rec_ptr, &rec) == -1	    || tdb->methods->tdb_write(tdb, rec_ptr+sizeof(rec), p, key.dsize+dbuf.dsize)==-1	    || tdb_ofs_write(tdb, TDB_HASH_TOP(hash), &rec_ptr) == -1) {		/* Need to tdb_unallocate() here */		goto fail;	} done:	ret = 0; fail:	if (ret == 0) {		tdb_increment_seqnum(tdb);	}	SAFE_FREE(p); 	tdb_unlock(tdb, BUCKET(hash), F_WRLCK);	return ret;}/* Append to an entry. Create if not exist. */int tdb_append(struct tdb_context *tdb, TDB_DATA key, TDB_DATA new_dbuf){	uint32_t hash;	TDB_DATA dbuf;	int ret = -1;	/* find which hash bucket it is in */	hash = tdb->hash_fn(&key);	if (tdb_lock(tdb, BUCKET(hash), F_WRLCK) == -1)		return -1;	dbuf = tdb_fetch(tdb, key);	if (dbuf.dptr == NULL) {		dbuf.dptr = (unsigned char *)malloc(new_dbuf.dsize);	} else {		unsigned char *new_dptr = (unsigned char *)realloc(dbuf.dptr,						     dbuf.dsize + new_dbuf.dsize);		if (new_dptr == NULL) {			free(dbuf.dptr);		}		dbuf.dptr = new_dptr;	}	if (dbuf.dptr == NULL) {		tdb->ecode = TDB_ERR_OOM;		goto failed;	}	memcpy(dbuf.dptr + dbuf.dsize, new_dbuf.dptr, new_dbuf.dsize);	dbuf.dsize += new_dbuf.dsize;	ret = tdb_store(tdb, key, dbuf, 0);	failed:	tdb_unlock(tdb, BUCKET(hash), F_WRLCK);	SAFE_FREE(dbuf.dptr);	return ret;}/*  return the name of the current tdb file  useful for external logging functions*/const char *tdb_name(struct tdb_context *tdb){	return tdb->name;}/*  return the underlying file descriptor being used by tdb, or -1  useful for external routines that want to check the device/inode  of the fd*/int tdb_fd(struct tdb_context *tdb){	return tdb->fd;}/*  return the current logging function  useful for external tdb routines that wish to log tdb errors*/tdb_log_func tdb_log_fn(struct tdb_context *tdb){	return tdb->log.log_fn;}/*  get the tdb sequence number. Only makes sense if the writers opened  with TDB_SEQNUM set. Note that this sequence number will wrap quite  quickly, so it should only be used for a 'has something changed'  test, not for code that relies on the count of the number of changes  made. If you want a counter then use a tdb record.  The aim of this sequence number is to allow for a very lightweight  test of a possible tdb change.*/int tdb_get_seqnum(struct tdb_context *tdb){	tdb_off_t seqnum=0;	tdb_ofs_read(tdb, TDB_SEQNUM_OFS, &seqnum);	return seqnum;}int tdb_hash_size(struct tdb_context *tdb){	return tdb->header.hash_size;}size_t tdb_map_size(struct tdb_context *tdb){	return tdb->map_size;}int tdb_get_flags(struct tdb_context *tdb){	return tdb->flags;}void tdb_add_flags(struct tdb_context *tdb, unsigned flags){	tdb->flags |= flags;}void tdb_remove_flags(struct tdb_context *tdb, unsigned flags){	tdb->flags &= ~flags;}/*  enable sequence number handling on an open tdb*/void tdb_enable_seqnum(struct tdb_context *tdb){	tdb->flags |= TDB_SEQNUM;}/*  add a region of the file to the freelist. Length is the size of the region in bytes,   which includes the free list header that needs to be added */static int tdb_free_region(struct tdb_context *tdb, tdb_off_t offset, ssize_t length){	struct list_struct rec;	if (length <= sizeof(rec)) {		/* the region is not worth adding */		return 0;	}	if (length + offset > tdb->map_size) {		TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: adding region beyond end of file\n"));		return -1;			}	memset(&rec,'\0',sizeof(rec));	rec.rec_len = length - sizeof(rec);	if (tdb_free(tdb, offset, &rec) == -1) {		TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_free_region: failed to add free record\n"));		return -1;	}	return 0;}/*  wipe the entire database, deleting all records. This can be done  very fast by using a global lock. The entire data portion of the  file becomes a single entry in the freelist.  This code carefully steps around the recovery area, leaving it alone */int tdb_wipe_all(struct tdb_context *tdb){	int i;	tdb_off_t offset = 0;	ssize_t data_len;	tdb_off_t recovery_head;	tdb_len_t recovery_size = 0;	if (tdb_lockall(tdb) != 0) {		return -1;	}	/* see if the tdb has a recovery area, and remember its size	   if so. We don't want to lose this as otherwise each	   tdb_wipe_all() in a transaction will increase the size of	   the tdb by the size of the recovery area */	if (tdb_ofs_read(tdb, TDB_RECOVERY_HEAD, &recovery_head) == -1) {		TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery head\n"));		goto failed;	}	if (recovery_head != 0) {		struct list_struct rec;		if (tdb->methods->tdb_read(tdb, recovery_head, &rec, sizeof(rec), DOCONV()) == -1) {			TDB_LOG((tdb, TDB_DEBUG_FATAL, "tdb_wipe_all: failed to read recovery record\n"));			return -1;		}			recovery_size = rec.rec_len + sizeof(rec);	}	/* wipe the hashes */	for (i=0;i<tdb->header.hash_size;i++) {		if (tdb_ofs_write(tdb, TDB_HASH_TOP(i), &offset) == -1) {			TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write hash %d\n", i));			goto failed;		}	}	/* wipe the freelist */	if (tdb_ofs_write(tdb, FREELIST_TOP, &offset) == -1) {		TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to write freelist\n"));		goto failed;	}	/* add all the rest of the file to the freelist, possibly leaving a gap 	   for the recovery area */	if (recovery_size == 0) {		/* the simple case - the whole file can be used as a freelist */		data_len = (tdb->map_size - TDB_DATA_START(tdb->header.hash_size));		if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {			goto failed;		}	} else {		/* we need to add two freelist entries - one on either		   side of the recovery area 		   Note that we cannot shift the recovery area during		   this operation. Only the transaction.c code may		   move the recovery area or we risk subtle data		   corruption		*/		data_len = (recovery_head - TDB_DATA_START(tdb->header.hash_size));		if (tdb_free_region(tdb, TDB_DATA_START(tdb->header.hash_size), data_len) != 0) {			goto failed;		}		/* and the 2nd free list entry after the recovery area - if any */		data_len = tdb->map_size - (recovery_head+recovery_size);		if (tdb_free_region(tdb, recovery_head+recovery_size, data_len) != 0) {			goto failed;		}	}	if (tdb_unlockall(tdb) != 0) {		TDB_LOG((tdb, TDB_DEBUG_FATAL,"tdb_wipe_all: failed to unlock\n"));		goto failed;	}	return 0;failed:	tdb_unlockall(tdb);	return -1;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -