From f60c0729dab0f55a3531681b95cca37f6b9d6b80 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 7 Feb 2022 07:20:56 +0000 Subject: [PATCH] more work --- source/libs/tdb/src/db/pgfile.c | 44 +++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/source/libs/tdb/src/db/pgfile.c b/source/libs/tdb/src/db/pgfile.c index 1c017657c1..b98d10e6a6 100644 --- a/source/libs/tdb/src/db/pgfile.c +++ b/source/libs/tdb/src/db/pgfile.c @@ -17,12 +17,52 @@ int pgFileOpen(const char *fname, SPgCache *pPgCache, SPgFile **ppPgFile) { SPgFile *pPgFile; - // TODO + + *ppPgFile = NULL; + + pPgFile = (SPgFile *)calloc(1, sizeof(*pPgFile)); + if (pPgFile == NULL) { + return -1; + } + + pPgFile->fd = -1; + + pPgFile->fname = strdup(fname); + if (pPgFile->fname == NULL) { + pgFileClose(pPgFile); + return -1; + } + + pPgFile->pPgCache = pPgCache; + + pPgFile->fd = open(fname, O_RDWR, 0755); + if (pPgFile->fd < 0) { + pgFileClose(pPgFile); + return -1; + } + + if (tdbGnrtFileID(fname, pPgFile->fileid) < 0) { + pgFileClose(pPgFile); + return -1; + } + + // TODO: get file size + pPgFile->pgFileSize = 0; + + *ppPgFile = pPgFile; return 0; } int pgFileClose(SPgFile *pPgFile) { - // TODO + if (pPgFile) { + if (pPgFile->fd >= 0) { + close(pPgFile->fd); + } + + tfree(pPgFile->fname); + free(pPgFile); + } + return 0; } -- GitLab