mirror of
https://github.com/usatiuk/backup.git
synced 2025-10-27 09:47:48 +01:00
proper cmake
This commit is contained in:
35
src/repo/srcs/objects/Archive.cpp
Normal file
35
src/repo/srcs/objects/Archive.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 14.04.2023.
|
||||
//
|
||||
|
||||
#include "../../includes/objects/Archive.h"
|
||||
|
||||
#include "../../../utils/includes/Exception.h"
|
||||
#include "../../includes/Serialize.h"
|
||||
|
||||
Archive::Archive(Object::idType id, std::string name, unsigned long long mtime, std::vector<idType> files, bool full)
|
||||
: Object(id, ObjectType::Archive), name(name), mtime(mtime), files(files), isFull(full) {}
|
||||
|
||||
Archive::Archive(std::vector<char>::const_iterator &in, const std::vector<char>::const_iterator &end)
|
||||
: Object(in, end),
|
||||
name(Serialize::deserialize<std::string>(in, end)),
|
||||
mtime(Serialize::deserialize<unsigned long long>(in, end)),
|
||||
files(Serialize::deserialize<std::remove_const<decltype(files)>::type>(in, end)),
|
||||
isFull(Serialize::deserialize<bool>(in, end)) {
|
||||
if (type != ObjectType::Archive) throw Exception("Type mismatch for Archive!");
|
||||
auto filesN = Serialize::deserialize<decltype(files.size())>(in, end);
|
||||
if (files.size() != filesN) throw Exception("Number of files recorded doesn't match the number of files read!");
|
||||
}
|
||||
|
||||
void Archive::serialize(std::vector<char> &out) const {
|
||||
Object::serialize(out);
|
||||
Serialize::serialize(name, out);
|
||||
Serialize::serialize(mtime, out);
|
||||
Serialize::serialize(files, out);
|
||||
Serialize::serialize(isFull, out);
|
||||
Serialize::serialize(files.size(), out);
|
||||
}
|
||||
|
||||
std::string Archive::getKey() const {
|
||||
return name;
|
||||
}
|
||||
30
src/repo/srcs/objects/Chunk.cpp
Normal file
30
src/repo/srcs/objects/Chunk.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 14.04.2023.
|
||||
//
|
||||
|
||||
#include "../../includes/objects/Chunk.h"
|
||||
|
||||
#include "../../../utils/includes/Exception.h"
|
||||
#include "../../includes/Serialize.h"
|
||||
|
||||
Chunk::Chunk(idType id, std::string md5, std::vector<char> data) : Object(id, ObjectType::Chunk), data(std::move(data)), md5(std::move(md5)), length(this->data.size()) {}
|
||||
|
||||
Chunk::Chunk(std::vector<char>::const_iterator &in, const std::vector<char>::const_iterator &end)
|
||||
: Object(in, end),
|
||||
md5(Serialize::deserialize<std::remove_const<decltype(md5)>::type>(in, end)),
|
||||
data(Serialize::deserialize<std::remove_const<decltype(data)>::type>(in, end)),
|
||||
length(Serialize::deserialize<std::remove_const<decltype(length)>::type>(in, end)) {
|
||||
if (type != ObjectType::Chunk) throw Exception("Type mismatch for Chunk!");
|
||||
if (length != data.size()) throw Exception("Recorded length and actual length don't match for Chunk!");
|
||||
}
|
||||
|
||||
void Chunk::serialize(std::vector<char> &out) const {
|
||||
Object::serialize(out);
|
||||
Serialize::serialize(md5, out);
|
||||
Serialize::serialize(data, out);
|
||||
Serialize::serialize(length, out);
|
||||
}
|
||||
|
||||
std::string Chunk::getKey() const {
|
||||
return md5;
|
||||
}
|
||||
84
src/repo/srcs/objects/File.cpp
Normal file
84
src/repo/srcs/objects/File.cpp
Normal file
@@ -0,0 +1,84 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 14.04.2023.
|
||||
//
|
||||
|
||||
#include "../../includes/objects/File.h"
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "../../../utils/includes/Exception.h"
|
||||
#include "../../includes/Serialize.h"
|
||||
|
||||
File::File(Object::idType id, std::string name, unsigned long long bytes, unsigned long long mtime, std::string md5, std::vector<idType> chunks, Type fileType)
|
||||
: Object(id, ObjectType::File), name(name), bytes(bytes), mtime(mtime), md5(md5), fileType(fileType), chunks(chunks) {}
|
||||
|
||||
File::File(std::vector<char>::const_iterator &in, const std::vector<char>::const_iterator &end)
|
||||
: Object(in, end),
|
||||
name(Serialize::deserialize<std::string>(in, end)),
|
||||
bytes(Serialize::deserialize<unsigned long long>(in, end)),
|
||||
mtime(Serialize::deserialize<unsigned long long>(in, end)),
|
||||
md5(Serialize::deserialize<std::remove_const<decltype(md5)>::type>(in, end)),
|
||||
fileType(Serialize::deserialize<std::remove_const<decltype(fileType)>::type>(in, end)),
|
||||
chunks(Serialize::deserialize<std::remove_const<decltype(chunks)>::type>(in, end)) {
|
||||
if (type != ObjectType::File) throw Exception("Type mismatch for File!");
|
||||
}
|
||||
|
||||
void File::serialize(std::vector<char> &out) const {
|
||||
Object::serialize(out);
|
||||
Serialize::serialize(name, out);
|
||||
Serialize::serialize(bytes, out);
|
||||
Serialize::serialize(mtime, out);
|
||||
Serialize::serialize(md5, out);
|
||||
Serialize::serialize(fileType, out);
|
||||
Serialize::serialize(chunks, out);
|
||||
}
|
||||
|
||||
std::string File::getKey() const {
|
||||
return name;
|
||||
}
|
||||
|
||||
File::Type File::getFileType(const std::filesystem::path &p) {
|
||||
if (std::filesystem::is_symlink(p)) return Type::Symlink;
|
||||
if (std::filesystem::is_directory(p)) return Type::Directory;
|
||||
if (std::filesystem::is_regular_file(p)) return Type::Normal;
|
||||
throw Exception("Unsupported file type! " + p.u8string());
|
||||
}
|
||||
|
||||
std::vector<char> File::getFileContents(const std::filesystem::path &p) {
|
||||
auto type = getFileType(p);
|
||||
if (type == Type::Normal) throw Exception(p.u8string() + " is a normal file!");
|
||||
if (type == Type::Directory) {
|
||||
return {};
|
||||
}
|
||||
if (type == Type::Symlink) {
|
||||
auto target = std::filesystem::read_symlink(p).u8string();
|
||||
return {target.begin(), target.end()};
|
||||
}
|
||||
throw Exception("Error with file " + p.u8string());
|
||||
}
|
||||
|
||||
unsigned long long File::getFileMtime(const std::filesystem::path &p) {
|
||||
auto type = getFileType(p);
|
||||
if (type == Type::Normal || type == Type::Directory)
|
||||
return static_cast<const unsigned long long int>(std::chrono::duration_cast<std::chrono::seconds>(std::filesystem::last_write_time(p).time_since_epoch()).count());
|
||||
else if (type == Type::Symlink) {
|
||||
auto path = p.u8string();
|
||||
struct stat sb;
|
||||
if (lstat(path.c_str(), &sb) != 0) throw Exception("Error reading mtime for " + p.u8string());
|
||||
#ifdef __APPLE__
|
||||
return sb.st_mtimespec.tv_sec;
|
||||
#else
|
||||
return sb.st_mtime;
|
||||
#endif
|
||||
}
|
||||
throw Exception("Error with file " + p.u8string());
|
||||
}
|
||||
|
||||
unsigned long long File::getFileSize(const std::filesystem::path &p) {
|
||||
auto type = getFileType(p);
|
||||
if (type == Type::Normal) return std::filesystem::file_size(p);
|
||||
else
|
||||
return getFileContents(p).size();
|
||||
}
|
||||
51
src/repo/srcs/objects/FileBuffer.cpp
Normal file
51
src/repo/srcs/objects/FileBuffer.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
//
|
||||
// Created by Stepan Usatiuk on 23.04.2023.
|
||||
//
|
||||
|
||||
#include "../../includes/objects/FileBuffer.h"
|
||||
|
||||
#include "../../includes/Serialize.h"
|
||||
|
||||
FileBuffer::FileBuffer(const Repository *repo, Object::idType fileId) : repo(repo), file(Serialize::deserialize<File>(repo->getObject(fileId))), chunksQueue() {
|
||||
for (auto const &id: file.chunks) chunksQueue.emplace(id);
|
||||
};
|
||||
|
||||
int FileBuffer::sync() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::streamsize FileBuffer::xsgetn(char *s, std::streamsize countr) {
|
||||
if (underflow() == std::char_traits<char>::eof()) return 0;
|
||||
for (int i = 0; i < countr; i++) {
|
||||
auto c = uflow();
|
||||
if (c != traits_type::eof()) {
|
||||
s[i] = traits_type::to_char_type(c);
|
||||
} else
|
||||
return i;
|
||||
}
|
||||
return countr;
|
||||
}
|
||||
|
||||
int FileBuffer::uflow() {
|
||||
auto out = underflow();
|
||||
if (out != traits_type::eof())
|
||||
curGetBufPos++;
|
||||
return out;
|
||||
}
|
||||
|
||||
int FileBuffer::underflow() {
|
||||
if (getBuf.empty() || curGetBufPos == getBuf.size()) {
|
||||
if (chunksQueue.empty()) return traits_type::eof();
|
||||
else {
|
||||
auto chunk = Serialize::deserialize<Chunk>(repo->getObject(chunksQueue.front()));
|
||||
getBuf = chunk.data;
|
||||
chunksQueue.pop();
|
||||
curGetBufPos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (!getBuf.empty())
|
||||
return traits_type::to_int_type(getBuf[curGetBufPos]);
|
||||
else
|
||||
return traits_type::eof();
|
||||
}
|
||||
Reference in New Issue
Block a user