From c6634976c88980649866a65f6baa976c9ffa7e8d Mon Sep 17 00:00:00 2001 From: cutecutecat Date: Wed, 7 Jul 2021 11:54:02 -0500 Subject: [PATCH] Fix the forbidden symbols when using unpackdb (#467) Fix unpackdb trying to produce files with forbidden filenames #466 <>:"/ \| ?* is not allowed in file path of Windows(all) and Linux(only /) when created separated database files with sequence names. Therefore, these symbols should be substituted with others. Co-authored-by: Milot Mirdita --- src/commons/FileUtil.cpp | 18 ++++++++++++++++++ src/commons/FileUtil.h | 3 +++ src/util/unpackdb.cpp | 3 ++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/commons/FileUtil.cpp b/src/commons/FileUtil.cpp index f772abda8..c50421abc 100644 --- a/src/commons/FileUtil.cpp +++ b/src/commons/FileUtil.cpp @@ -465,3 +465,21 @@ void FileUtil::fixRlimitNoFile() { } } } + +std::string FileUtil::sanitizeFilename(std::string name){ + static const std::vector> symbolTable = + {{'\\', '@'}, + {'/', '@'}, + {':', '@'}, + {'*', '@'}, + {'?', '@'}, + {'<', '@'}, + {'>', '@'}, + {'|', '!'}}; + + std::vector>::const_iterator it; + for (it = symbolTable.begin(); it != symbolTable.end(); ++it) { + std::replace(name.begin(), name.end(), it->first, it->second); + } + return name; +} diff --git a/src/commons/FileUtil.h b/src/commons/FileUtil.h index 53d088422..019783bcc 100644 --- a/src/commons/FileUtil.h +++ b/src/commons/FileUtil.h @@ -64,6 +64,9 @@ class FileUtil { static std::string createTemporaryDirectory(const std::string& basePath, const std::string& subDirectory); static void fixRlimitNoFile(); + + // remove forbidden symbols in from filenames + static std::string sanitizeFilename(std::string name); }; diff --git a/src/util/unpackdb.cpp b/src/util/unpackdb.cpp index e7f54d264..b576145e6 100644 --- a/src/util/unpackdb.cpp +++ b/src/util/unpackdb.cpp @@ -27,6 +27,7 @@ int unpackdb(int argc, const char **argv, const Command& command) { size_t entries = reader.getSize(); Debug::Progress progress(entries); + #pragma omp parallel { unsigned int thread_idx = 0; @@ -44,7 +45,7 @@ int unpackdb(int argc, const char **argv, const Command& command) { } if (par.unpackNameMode == Parameters::UNPACK_NAME_ACCESSION) { size_t lookupId = reader.getLookupIdByKey(key); - name.append(reader.getLookupEntryName(lookupId)); + name.append(FileUtil::sanitizeFilename(reader.getLookupEntryName(lookupId))); } else { name.append(SSTR(key)); }