2015-09-30 13:21:07 +02:00
|
|
|
#include "FsBlobStore.h"
|
|
|
|
#include "FileBlob.h"
|
|
|
|
#include "DirBlob.h"
|
|
|
|
#include "SymlinkBlob.h"
|
|
|
|
#include "MagicNumbers.h"
|
|
|
|
|
|
|
|
namespace bf = boost::filesystem;
|
|
|
|
using cpputils::unique_ref;
|
|
|
|
using cpputils::make_unique_ref;
|
|
|
|
using blobstore::BlobStore;
|
|
|
|
using boost::none;
|
2015-09-30 14:05:05 +02:00
|
|
|
using std::function;
|
2015-09-30 13:21:07 +02:00
|
|
|
|
|
|
|
namespace cryfs {
|
|
|
|
namespace fsblobstore {
|
|
|
|
|
|
|
|
FsBlobStore::FsBlobStore(unique_ref<BlobStore> baseBlobStore): _baseBlobStore(std::move(baseBlobStore)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
unique_ref<FileBlob> FsBlobStore::createFileBlob() {
|
2015-09-30 14:05:05 +02:00
|
|
|
auto blob = _baseBlobStore->create();
|
|
|
|
auto key = blob->key();
|
|
|
|
_openBlobs.lock(key);
|
|
|
|
return FileBlob::InitializeEmptyFile(std::move(blob), freeLockFunction(key));
|
2015-09-30 13:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unique_ref<DirBlob> FsBlobStore::createDirBlob() {
|
2015-09-30 14:05:05 +02:00
|
|
|
auto blob = _baseBlobStore->create();
|
|
|
|
auto key = blob->key();
|
|
|
|
_openBlobs.lock(key);
|
2015-09-30 13:21:07 +02:00
|
|
|
//TODO Passed in fsBlobStore should be ParallelAccessFsBlobStore later
|
2015-09-30 14:05:05 +02:00
|
|
|
return DirBlob::InitializeEmptyDir(std::move(blob), this, freeLockFunction(key));
|
2015-09-30 13:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
unique_ref<SymlinkBlob> FsBlobStore::createSymlinkBlob(const bf::path &target) {
|
2015-09-30 14:05:05 +02:00
|
|
|
auto blob = _baseBlobStore->create();
|
|
|
|
auto key = blob->key();
|
|
|
|
_openBlobs.lock(key);
|
|
|
|
return SymlinkBlob::InitializeSymlink(std::move(blob), target, freeLockFunction(key));
|
2015-09-30 13:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<unique_ref<FsBlob>> FsBlobStore::load(const blockstore::Key &key) {
|
2015-09-30 14:05:05 +02:00
|
|
|
_openBlobs.lock(key);
|
|
|
|
|
2015-09-30 13:21:07 +02:00
|
|
|
auto blob = _baseBlobStore->load(key);
|
|
|
|
if (blob == none) {
|
|
|
|
return none;
|
|
|
|
}
|
|
|
|
unsigned char magicNumber = FsBlob::magicNumber(**blob);
|
|
|
|
if (magicNumber == MagicNumbers::FILE) {
|
2015-09-30 14:05:05 +02:00
|
|
|
return unique_ref<FsBlob>(make_unique_ref<FileBlob>(std::move(*blob), freeLockFunction(key)));
|
2015-09-30 13:21:07 +02:00
|
|
|
} else if (magicNumber == MagicNumbers::DIR) {
|
|
|
|
//TODO Passed in fsBlobStore should be ParallelAccessFsBlobStore later
|
2015-09-30 14:05:05 +02:00
|
|
|
return unique_ref<FsBlob>(make_unique_ref<DirBlob>(std::move(*blob), this, freeLockFunction(key)));
|
2015-09-30 13:21:07 +02:00
|
|
|
} else if (magicNumber == MagicNumbers::SYMLINK) {
|
2015-09-30 14:05:05 +02:00
|
|
|
return unique_ref<FsBlob>(make_unique_ref<SymlinkBlob>(std::move(*blob), freeLockFunction(key)));
|
2015-09-30 13:21:07 +02:00
|
|
|
} else {
|
|
|
|
ASSERT(false, "Unknown magic number");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FsBlobStore::remove(cpputils::unique_ref<FsBlob> blob) {
|
|
|
|
_baseBlobStore->remove(blob->releaseBaseBlob());
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:05:05 +02:00
|
|
|
function<void()> FsBlobStore::freeLockFunction(const blockstore::Key &key) {
|
|
|
|
return [this, key] {
|
|
|
|
_openBlobs.release(key);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-09-30 13:21:07 +02:00
|
|
|
}
|
|
|
|
}
|