From 2394e0e0ffb168a1c10cc1a0b11eb711fa65a4e9 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 20 Jan 2022 07:28:58 +0000 Subject: [PATCH] more --- source/libs/tdb/src/db/tdb_mpool.c | 26 ++++++++++++++++++++++++-- source/libs/tdb/src/inc/tdb_mpool.h | 4 ++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/source/libs/tdb/src/db/tdb_mpool.c b/source/libs/tdb/src/db/tdb_mpool.c index 60829ede94..6191d8d4d1 100644 --- a/source/libs/tdb/src/db/tdb_mpool.c +++ b/source/libs/tdb/src/db/tdb_mpool.c @@ -104,6 +104,7 @@ int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) { } mpf->fd = -1; + mpf->mp = mp; if ((mpf->fname = strdup(fname)) == NULL) { goto _err; @@ -113,6 +114,10 @@ int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) { goto _err; } + if (tdbGnrtFileID(fname, mpf->fileid) < 0) { + goto _err; + } + *mpfp = mpf; return 0; @@ -123,11 +128,28 @@ _err: } int tdbMPoolFileClose(TDB_MPFILE *mpf) { - // TODO + if (mpf) { + if (mpf->fd > 0) { + close(mpf->fd); + } + tfree(mpf->fname); + free(mpf); + } return 0; } static int tdbGnrtFileID(const char *fname, uint8_t *fileid) { - // TODO + struct stat statbuf; + + if (stat(fname, &statbuf) < 0) { + return -1; + } + + memset(fileid, 0, TDB_FILE_ID_LEN); + + ((uint64_t *)fileid)[0] = (uint64_t)statbuf.st_ino; + ((uint64_t *)fileid)[1] = (uint64_t)statbuf.st_dev; + ((uint64_t *)fileid)[2] = rand(); + return 0; } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdb_mpool.h b/source/libs/tdb/src/inc/tdb_mpool.h index 4bd4d12108..dd1d802fa5 100644 --- a/source/libs/tdb/src/inc/tdb_mpool.h +++ b/source/libs/tdb/src/inc/tdb_mpool.h @@ -56,10 +56,10 @@ struct TDB_MPOOL { #define MP_PAGE_AT(mp, idx) (mp)->pages[idx] struct TDB_MPFILE { - uint8_t fileid[TDB_FILE_ID_LEN]; // file ID - TDB_MPOOL *mp; // underlying memory pool char * fname; // file name int fd; // fd + uint8_t fileid[TDB_FILE_ID_LEN]; // file ID + TDB_MPOOL *mp; // underlying memory pool }; /*=================================================== Exposed apis ==================================================*/ -- GitLab