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;
|
2015-10-04 17:20:14 +02:00
|
|
|
using blockstore::Key;
|
2015-09-30 13:21:07 +02:00
|
|
|
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();
|
2015-10-04 17:20:14 +02:00
|
|
|
return FileBlob::InitializeEmptyFile(std::move(blob));
|
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();
|
2015-10-04 17:20:14 +02:00
|
|
|
return DirBlob::InitializeEmptyDir(std::move(blob), _getLstatSize());
|
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();
|
2015-10-04 17:20:14 +02:00
|
|
|
return SymlinkBlob::InitializeSymlink(std::move(blob), target);
|
2015-09-30 13:21:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
boost::optional<unique_ref<FsBlob>> FsBlobStore::load(const blockstore::Key &key) {
|
|
|
|
auto blob = _baseBlobStore->load(key);
|
|
|
|
if (blob == none) {
|
|
|
|
return none;
|
|
|
|
}
|
|
|
|
unsigned char magicNumber = FsBlob::magicNumber(**blob);
|
|
|
|
if (magicNumber == MagicNumbers::FILE) {
|
2015-10-04 17:20:14 +02:00
|
|
|
return unique_ref<FsBlob>(make_unique_ref<FileBlob>(std::move(*blob)));
|
2015-09-30 13:21:07 +02:00
|
|
|
} else if (magicNumber == MagicNumbers::DIR) {
|
2015-10-04 17:20:14 +02:00
|
|
|
return unique_ref<FsBlob>(make_unique_ref<DirBlob>(std::move(*blob), _getLstatSize()));
|
2015-09-30 13:21:07 +02:00
|
|
|
} else if (magicNumber == MagicNumbers::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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FsBlobStore::remove(cpputils::unique_ref<FsBlob> blob) {
|
|
|
|
_baseBlobStore->remove(blob->releaseBaseBlob());
|
|
|
|
}
|
|
|
|
|
2015-10-04 17:20:14 +02:00
|
|
|
function<off_t (const Key &)> FsBlobStore::_getLstatSize() {
|
|
|
|
return [this] (const Key &key) {
|
|
|
|
auto blob = load(key);
|
|
|
|
ASSERT(blob != none, "Blob not found");
|
|
|
|
return (*blob)->lstat_size();
|
2015-09-30 14:05:05 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-09-30 13:21:07 +02:00
|
|
|
}
|
|
|
|
}
|