Skip to content

Commit

Permalink
Fix the forbidden symbols when using unpackdb (#467)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
cutecutecat and milot-mirdita authored Jul 7, 2021
1 parent 488df86 commit c663497
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/commons/FileUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -465,3 +465,21 @@ void FileUtil::fixRlimitNoFile() {
}
}
}

std::string FileUtil::sanitizeFilename(std::string name){
static const std::vector<std::pair<char, char>> symbolTable =
{{'\\', '@'},
{'/', '@'},
{':', '@'},
{'*', '@'},
{'?', '@'},
{'<', '@'},
{'>', '@'},
{'|', '!'}};

std::vector<std::pair<char, char>>::const_iterator it;
for (it = symbolTable.begin(); it != symbolTable.end(); ++it) {
std::replace(name.begin(), name.end(), it->first, it->second);
}
return name;
}
3 changes: 3 additions & 0 deletions src/commons/FileUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};


Expand Down
3 changes: 2 additions & 1 deletion src/util/unpackdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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));
}
Expand Down

0 comments on commit c663497

Please sign in to comment.