diff --git a/src/inc/tfs.h b/src/inc/tfs.h index 23ca7ed6acd3288f545a2e4b4afc73a942d8e5f6..c273be56789d515e7fcbf37882b1200886394376 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 207d28a4d790ba8217438c50acbe5851a5216b2b..6fc96b0c0ea50af9f3588c056979faed58cc0c5e 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 1193261cf15b900527843c64106b720448524e0d..9da9e7a2abf3b1c45d7a8e8c8761295c8b0e0b45 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) {