Skip to content

Commit

Permalink
If file system migration encounters files or folders with the wrong f…
Browse files Browse the repository at this point in the history
…ormat in the base directory, it now just ignores them instead of crashing.
  • Loading branch information
smessmer committed Feb 9, 2019
1 parent 4ca139c commit 97c9ac4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Version 0.10.1 (unreleased)
---------------
Fixed bugs:
* If file system migration encounters files or folders with the wrong format in the base directory, it now just ignores them instead of crashing.


Version 0.10.0
---------------
New Features & Improvements:
Expand Down
29 changes: 23 additions & 6 deletions src/blockstore/implementations/ondisk/OnDiskBlockStore2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ namespace ondisk {

const string OnDiskBlockStore2::FORMAT_VERSION_HEADER_PREFIX = "cryfs;block;";
const string OnDiskBlockStore2::FORMAT_VERSION_HEADER = OnDiskBlockStore2::FORMAT_VERSION_HEADER_PREFIX + "0";
namespace {
constexpr size_t PREFIX_LENGTH = 3;
constexpr size_t POSTFIX_LENGTH = BlockId::STRING_LENGTH - PREFIX_LENGTH;
constexpr const char* ALLOWED_BLOCKID_CHARACTERS = "0123456789ABCDEF";
}

boost::filesystem::path OnDiskBlockStore2::_getFilepath(const BlockId &blockId) const {
std::string blockIdStr = blockId.ToString();
return _rootDir / blockIdStr.substr(0,3) / blockIdStr.substr(3);
return _rootDir / blockIdStr.substr(0, PREFIX_LENGTH) / blockIdStr.substr(PREFIX_LENGTH);
}

Data OnDiskBlockStore2::_checkAndRemoveHeader(const Data &data) {
Expand Down Expand Up @@ -112,12 +117,24 @@ uint64_t OnDiskBlockStore2::blockSizeFromPhysicalBlockSize(uint64_t blockSize) c

void OnDiskBlockStore2::forEachBlock(std::function<void (const BlockId &)> callback) const {
for (auto prefixDir = boost::filesystem::directory_iterator(_rootDir); prefixDir != boost::filesystem::directory_iterator(); ++prefixDir) {
if (boost::filesystem::is_directory(prefixDir->path())) {
std::string blockIdPrefix = prefixDir->path().filename().string();
for (auto block = boost::filesystem::directory_iterator(prefixDir->path()); block != boost::filesystem::directory_iterator(); ++block) {
std::string blockIdPostfix = block->path().filename().string();
callback(BlockId::FromString(blockIdPrefix + blockIdPostfix));
if (!boost::filesystem::is_directory(prefixDir->path())) {
continue;
}

std::string blockIdPrefix = prefixDir->path().filename().string();
if (blockIdPrefix.size() != PREFIX_LENGTH || std::string::npos != blockIdPrefix.find_first_not_of(ALLOWED_BLOCKID_CHARACTERS)) {
// directory has wrong length or an invalid character
continue;
}

for (auto block = boost::filesystem::directory_iterator(prefixDir->path()); block != boost::filesystem::directory_iterator(); ++block) {
std::string blockIdPostfix = block->path().filename().string();
if (blockIdPostfix.size() != POSTFIX_LENGTH || std::string::npos != blockIdPostfix.find_first_not_of(ALLOWED_BLOCKID_CHARACTERS)) {
// filename has wrong length or an invalid character
continue;
}

callback(BlockId::FromString(blockIdPrefix + blockIdPostfix));
}
}
}
Expand Down

0 comments on commit 97c9ac4

Please sign in to comment.