From 68d4a494f072d5b9b01527b151cd3736aba156f0 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 25 Jan 2021 18:52:49 +0800 Subject: [PATCH] fix data1.sim error --- src/inc/tfs.h | 1 + src/tfs/src/tfs.c | 23 +++++++++++++++++++++++ src/tsdb/src/tsdbFile.c | 40 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 60 insertions(+), 4 deletions(-) diff --git a/src/inc/tfs.h b/src/inc/tfs.h index 23ca7ed6ac..c273be5678 100644 --- a/src/inc/tfs.h +++ b/src/inc/tfs.h @@ -75,6 +75,7 @@ void tfsdirname(const TFILE *pf, char *dest); // DIR APIs ==================================== int tfsMkdirAt(const char *rname, int level, int id); +int tfsMkdirRecurAt(const char *rname, int level, int id); int tfsMkdir(const char *rname); int tfsRmdir(const char *rname); int tfsRename(char *orname, char *nrname); diff --git a/src/tfs/src/tfs.c b/src/tfs/src/tfs.c index 207d28a4d7..6fc96b0c0e 100644 --- a/src/tfs/src/tfs.c +++ b/src/tfs/src/tfs.c @@ -250,6 +250,29 @@ int tfsMkdirAt(const char *rname, int level, int id) { return 0; } +int tfsMkdirRecurAt(const char *rname, int level, int id) { + if (tfsMkdirAt(rname, level, id) < 0) { + if (errno == ENOENT) { + // Try to create upper + char *s = strdup(rname); + + if (tfsMkdirRecurAt(dirname(s), level, id) < 0) { + tfree(s); + return -1; + } + tfree(s); + + if (tfsMkdirAt(rname, level, id) < 0) { + return -1; + } + } else { + return -1; + } + } + + return 0; +} + int tfsMkdir(const char *rname) { for (int level = 0; level < TFS_NLEVEL(); level++) { STier *pTier = TFS_TIER_AT(level); diff --git a/src/tsdb/src/tsdbFile.c b/src/tsdb/src/tsdbFile.c index 1193261cf1..9da9e7a2ab 100644 --- a/src/tsdb/src/tsdbFile.c +++ b/src/tsdb/src/tsdbFile.c @@ -110,8 +110,24 @@ int tsdbCreateMFile(SMFile *pMFile, bool updateHeader) { pMFile->fd = open(TSDB_FILE_FULL_NAME(pMFile), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0755); if (pMFile->fd < 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; + if (errno == ENOENT) { + // Try to create directory recursively + char *s = strdup(TFILE_REL_NAME(&(pMFile->f))); + if (tfsMkdirRecurAt(dirname(s), TSDB_FILE_LEVEL(pMFile), TSDB_FILE_ID(pMFile)) < 0) { + tfree(s); + return -1; + } + tfree(s); + + pMFile->fd = open(TSDB_FILE_FULL_NAME(pMFile), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0755); + if (pMFile->fd < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + } else { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } } if (!updateHeader) { @@ -323,8 +339,24 @@ int tsdbCreateDFile(SDFile *pDFile, bool updateHeader) { pDFile->fd = open(TSDB_FILE_FULL_NAME(pDFile), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0755); if (pDFile->fd < 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; + if (errno == ENOENT) { + // Try to create directory recursively + char *s = strdup(TFILE_REL_NAME(&(pDFile->f))); + if (tfsMkdirRecurAt(dirname(s), TSDB_FILE_LEVEL(pDFile), TSDB_FILE_ID(pDFile)) < 0) { + tfree(s); + return -1; + } + tfree(s); + + pDFile->fd = open(TSDB_FILE_FULL_NAME(pDFile), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0755); + if (pDFile->fd < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + } else { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } } if (!updateHeader) {