2015-09-30 13:21:07 +02:00
|
|
|
#include "FsBlobStore.h"
|
|
|
|
#include "FileBlob.h"
|
|
|
|
#include "DirBlob.h"
|
|
|
|
#include "SymlinkBlob.h"
|
|
|
|
|
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
|
|
|
using blobstore::BlobStore;
|
2017-09-17 03:07:27 +02:00
|
|
|
using blockstore::BlockId;
|
2015-09-30 13:21:07 +02:00
|
|
|
using boost::none;
|
2016-06-28 05:03:44 +02:00
|
|
|
using std::vector;
|
2015-09-30 13:21:07 +02:00
|
|
|
|
|
|
|
namespace cryfs {
|
|
|
|
namespace fsblobstore {
|
|
|
|
|
2017-09-17 03:07:27 +02:00
|
|
|
boost::optional<unique_ref<FsBlob>> FsBlobStore::load(const blockstore::BlockId &blockId) {
|
|
|
|
auto blob = _baseBlobStore->load(blockId);
|
2015-09-30 13:21:07 +02:00
|
|
|
if (blob == none) {
|
|
|
|
return none;
|
|
|
|
}
|
2016-02-13 15:06:28 +01:00
|
|
|
FsBlobView::BlobType blobType = FsBlobView::blobType(**blob);
|
|
|
|
if (blobType == FsBlobView::BlobType::FILE) {
|
2015-10-04 17:20:14 +02:00
|
|
|
return unique_ref<FsBlob>(make_unique_ref<FileBlob>(std::move(*blob)));
|
2016-02-13 15:06:28 +01:00
|
|
|
} else if (blobType == FsBlobView::BlobType::DIR) {
|
2016-03-08 23:47:31 +01:00
|
|
|
return unique_ref<FsBlob>(make_unique_ref<DirBlob>(this, std::move(*blob), _getLstatSize()));
|
2016-02-13 15:06:28 +01:00
|
|
|
} else if (blobType == FsBlobView::BlobType::SYMLINK) {
|
2015-10-04 17:20:14 +02:00
|
|
|
return unique_ref<FsBlob>(make_unique_ref<SymlinkBlob>(std::move(*blob)));
|
2015-09-30 13:21:07 +02:00
|
|
|
} else {
|
|
|
|
ASSERT(false, "Unknown magic number");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-28 05:03:44 +02:00
|
|
|
#ifndef CRYFS_NO_COMPATIBILITY
|
2017-09-17 03:07:27 +02:00
|
|
|
unique_ref<FsBlobStore> FsBlobStore::migrateIfNeeded(unique_ref<BlobStore> blobStore, const blockstore::BlockId &rootBlobId) {
|
|
|
|
auto rootBlob = blobStore->load(rootBlobId);
|
2016-06-28 05:03:44 +02:00
|
|
|
ASSERT(rootBlob != none, "Could not load root blob");
|
|
|
|
uint16_t format = FsBlobView::getFormatVersionHeader(**rootBlob);
|
|
|
|
|
|
|
|
auto fsBlobStore = make_unique_ref<FsBlobStore>(std::move(blobStore));
|
|
|
|
if (format == 0) {
|
|
|
|
// migration needed
|
|
|
|
std::cout << "Migrating file system for conflict resolution features. Please don't interrupt this process. This can take a while..." << std::flush;
|
2017-09-17 03:07:27 +02:00
|
|
|
fsBlobStore->_migrate(std::move(*rootBlob), blockstore::BlockId::Null());
|
2016-06-28 05:03:44 +02:00
|
|
|
std::cout << "done" << std::endl;
|
|
|
|
}
|
|
|
|
return fsBlobStore;
|
|
|
|
}
|
|
|
|
|
2017-09-17 03:07:27 +02:00
|
|
|
void FsBlobStore::_migrate(unique_ref<blobstore::Blob> node, const blockstore::BlockId &parentId) {
|
|
|
|
FsBlobView::migrate(node.get(), parentId);
|
2016-06-28 05:03:44 +02:00
|
|
|
if (FsBlobView::blobType(*node) == FsBlobView::BlobType::DIR) {
|
|
|
|
DirBlob dir(this, std::move(node), _getLstatSize());
|
|
|
|
vector<fspp::Dir::Entry> children;
|
|
|
|
dir.AppendChildrenTo(&children);
|
|
|
|
for (const auto &child : children) {
|
|
|
|
auto childEntry = dir.GetChild(child.name);
|
|
|
|
ASSERT(childEntry != none, "Couldn't load child, although it was returned as a child in the lsit.");
|
2017-09-17 03:07:27 +02:00
|
|
|
auto childBlob = _baseBlobStore->load(childEntry->blockId());
|
2016-06-28 05:03:44 +02:00
|
|
|
ASSERT(childBlob != none, "Couldn't load child blob");
|
2017-09-17 03:07:27 +02:00
|
|
|
_migrate(std::move(*childBlob), dir.blockId());
|
2016-06-28 05:03:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
2015-09-30 13:21:07 +02:00
|
|
|
}
|