switch to c++20

This commit is contained in:
2024-03-18 22:39:10 +01:00
parent 8e9ed2b715
commit e996e93431
11 changed files with 50 additions and 43 deletions

7
.idea/misc.xml generated
View File

@@ -1,4 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" /> <component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
<component name="CidrRootsConfiguration">
<excludeRoots>
<file path="$PROJECT_DIR$/cmake-build-debug" />
<file path="$PROJECT_DIR$/cmake-build-relwithdebinfo" />
<file path="$PROJECT_DIR$/cmake-build-relwithsan" />
</excludeRoots>
</component>
</project> </project>

View File

@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.18) cmake_minimum_required(VERSION 3.18)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_subdirectory(change_detectors) add_subdirectory(change_detectors)

View File

@@ -16,13 +16,13 @@ ComparableFile::ComparableFile(const File &file, const Repository *repo)
contents([file, repo]() { return std::make_unique<FileBuffer>(repo, file.id); }) {} contents([file, repo]() { return std::make_unique<FileBuffer>(repo, file.id); }) {}
ComparableFile::ComparableFile(const std::filesystem::path &p, const std::filesystem::path &base) ComparableFile::ComparableFile(const std::filesystem::path &p, const std::filesystem::path &base)
: path(p.lexically_relative(base).u8string()), type(File::getFileType(p)), bytes(File::getFileSize(p)), : path(p.lexically_relative(base).string()), type(File::getFileType(p)), bytes(File::getFileSize(p)),
mtime(File::getFileMtime(p)), mtime(File::getFileMtime(p)),
contents([p, path = this->path, type = this->type]() -> std::unique_ptr<std::streambuf> { contents([p, path = this->path, type = this->type]() -> std::unique_ptr<std::streambuf> {
if (type == File::Type::Normal) { if (type == File::Type::Normal) {
auto fb = std::make_unique<std::filebuf>(); auto fb = std::make_unique<std::filebuf>();
fb->open(p, std::ios::in | std::ios::binary); fb->open(p, std::ios::in | std::ios::binary);
if (!fb->is_open()) throw Exception("Can't open " + p.u8string() + " for reading!"); if (!fb->is_open()) throw Exception("Can't open " + p.string() + " for reading!");
return fb; return fb;
} }

View File

@@ -43,7 +43,7 @@ void CommandDiff::run(Context ctx) {
std::map<std::filesystem::path, File> files;///< Files in the first archive std::map<std::filesystem::path, File> files;///< Files in the first archive
for (auto id: archiveO1.files) { for (auto id: archiveO1.files) {
auto file = Serialize::deserialize<File>(ctx.repo->getObject(id)); auto file = Serialize::deserialize<File>(ctx.repo->getObject(id));
auto path = std::filesystem::u8path(file.name); auto path = std::filesystem::path(file.name);
if (isSubpath(ctx.repo->getConfig().getStr("prefix"), path)) files.emplace(file.getKey(), std::move(file)); if (isSubpath(ctx.repo->getConfig().getStr("prefix"), path)) files.emplace(file.getKey(), std::move(file));
} }
@@ -83,7 +83,7 @@ void CommandDiff::run(Context ctx) {
/// Exit when asked to /// Exit when asked to
if (Signals::shouldQuit) throw Exception("Quitting"); if (Signals::shouldQuit) throw Exception("Quitting");
auto file = Serialize::deserialize<File>(ctx.repo->getObject(id)); auto file = Serialize::deserialize<File>(ctx.repo->getObject(id));
if (isSubpath(ctx.repo->getConfig().getStr("prefix"), std::filesystem::u8path(file.name))) if (isSubpath(ctx.repo->getConfig().getStr("prefix"), std::filesystem::path(file.name)))
threadPool.push([&, file]() { processFile(ComparableFile{file, ctx.repo}); }); threadPool.push([&, file]() { processFile(ComparableFile{file, ctx.repo}); });
if (Signals::shouldQuit) break; if (Signals::shouldQuit) break;
} }
@@ -115,7 +115,7 @@ void CommandDiff::run(Context ctx) {
std::map<std::filesystem::path, File> files2;///< Files in the first archive std::map<std::filesystem::path, File> files2;///< Files in the first archive
for (auto id: archiveO2->files) { for (auto id: archiveO2->files) {
auto file = Serialize::deserialize<File>(ctx.repo->getObject(id)); auto file = Serialize::deserialize<File>(ctx.repo->getObject(id));
auto path = std::filesystem::u8path(file.name); auto path = std::filesystem::path(file.name);
if (isSubpath(ctx.repo->getConfig().getStr("prefix"), path)) if (isSubpath(ctx.repo->getConfig().getStr("prefix"), path))
files2.emplace(file.getKey(), std::move(file)); files2.emplace(file.getKey(), std::move(file));
} }
@@ -144,5 +144,5 @@ void CommandDiff::run(Context ctx) {
std::unique_lock finishedLock(threadPool.finishedLock); std::unique_lock finishedLock(threadPool.finishedLock);
threadPool.finished.wait(finishedLock, [&threadPool] { return threadPool.empty(); }); threadPool.finished.wait(finishedLock, [&threadPool] { return threadPool.empty(); });
if (diffMode == "normal") if (diffMode == "normal")
for (auto const &s: files) { ctx.logger->write(s.first.u8string() + " is removed\n", 0); } for (auto const &s: files) { ctx.logger->write(s.first.string() + " is removed\n", 0); }
} }

View File

@@ -24,7 +24,7 @@ CommandRestore::CommandRestore() : Command() {}
void CommandRestore::run(Context ctx) { void CommandRestore::run(Context ctx) {
Object::idType archive = ctx.repo->getConfig().getInt("aid"); Object::idType archive = ctx.repo->getConfig().getInt("aid");
std::filesystem::path to = std::filesystem::u8path(ctx.repo->getConfig().getStr("to")); std::filesystem::path to = std::filesystem::path(ctx.repo->getConfig().getStr("to"));
std::atomic<unsigned long long> filesToRestoreCount = 0; std::atomic<unsigned long long> filesToRestoreCount = 0;
std::atomic<unsigned long long> bytesToRestore = 0; std::atomic<unsigned long long> bytesToRestore = 0;
@@ -95,21 +95,21 @@ void CommandRestore::run(Context ctx) {
std::string CommandRestore::backupRestoreFile(const File &file, const std::filesystem::path &baseDir, std::string CommandRestore::backupRestoreFile(const File &file, const std::filesystem::path &baseDir,
workerStatsFunction &callback, Context ctx) { workerStatsFunction &callback, Context ctx) {
auto fullpath = baseDir / std::filesystem::u8path(file.name); auto fullpath = baseDir / std::filesystem::path(file.name);
std::filesystem::create_directories(fullpath.parent_path()); std::filesystem::create_directories(fullpath.parent_path());
if (file.fileType == File::Type::Directory) { if (file.fileType == File::Type::Directory) {
std::filesystem::create_directory(fullpath); std::filesystem::create_directory(fullpath);
callback(0, 0, 1); callback(0, 0, 1);
return fullpath.u8string(); return fullpath.string();
} }
if (file.fileType == File::Type::Symlink) { if (file.fileType == File::Type::Symlink) {
auto dest = Serialize::deserialize<Chunk>(ctx.repo->getObject(file.chunks.at(0))); auto dest = Serialize::deserialize<Chunk>(ctx.repo->getObject(file.chunks.at(0)));
std::filesystem::create_symlink(std::filesystem::u8path(std::string{dest.data.begin(), dest.data.end()}), std::filesystem::create_symlink(std::filesystem::path(std::string{dest.data.begin(), dest.data.end()}),
fullpath); fullpath);
callback(0, 0, 1); callback(0, 0, 1);
return fullpath.u8string(); return fullpath.string();
} }
std::ofstream ostream(fullpath, std::ios::binary | std::ios::out | std::ios::trunc); std::ofstream ostream(fullpath, std::ios::binary | std::ios::out | std::ios::trunc);
@@ -124,5 +124,5 @@ std::string CommandRestore::backupRestoreFile(const File &file, const std::files
} }
callback(0, 0, 1); callback(0, 0, 1);
return fullpath.u8string(); return fullpath.string();
} }

View File

@@ -79,14 +79,14 @@ void CommandRun::run(Context ctx) {
File::getFileType(absPath) == File::Type::Normal ? std::filesystem::file_size(absPath) : 0; File::getFileType(absPath) == File::Type::Normal ? std::filesystem::file_size(absPath) : 0;
runnerStats.filesToSaveCount++; runnerStats.filesToSaveCount++;
threadPool.push([&, relPath, absPath]() { threadPool.push([&, relPath, absPath]() {
addFile(backupChunkFile(absPath, relPath.u8string(), workerCallback, ctx)); addFile(backupChunkFile(absPath, relPath.string(), workerCallback, ctx));
progress.print("Copied: " + relPath.u8string(), 1); progress.print("Copied: " + relPath.string(), 1);
}); });
}; };
/// Task to process an individual file in the backup /// Task to process an individual file in the backup
std::function<void(std::filesystem::path)> processFile = [&, this](const std::filesystem::path &p) { std::function<void(std::filesystem::path)> processFile = [&, this](const std::filesystem::path &p) {
auto relPath = p.lexically_relative(from).u8string(); auto relPath = p.lexically_relative(from).string();
if (ctx.repo->exists(Object::ObjectType::File, relPath) != 0) { if (ctx.repo->exists(Object::ObjectType::File, relPath) != 0) {
File repoFile = Serialize::deserialize<File>(ctx.repo->getObject(Object::ObjectType::File, relPath)); File repoFile = Serialize::deserialize<File>(ctx.repo->getObject(Object::ObjectType::File, relPath));
@@ -144,10 +144,10 @@ Object::idType CommandRun::backupChunkFile(const std::filesystem::path &orig, co
ctx.repo->putObject(f); ctx.repo->putObject(f);
return f.id; return f.id;
} }
if (!std::filesystem::is_regular_file(orig)) throw Exception(orig.u8string() + "is a special file, not saving"); if (!std::filesystem::is_regular_file(orig)) throw Exception(orig.string() + "is a special file, not saving");
std::ifstream ifstream(orig, std::ios::in | std::ios::binary); std::ifstream ifstream(orig, std::ios::in | std::ios::binary);
if (!ifstream) throw Exception("Couldn't open " + orig.u8string() + " for reading"); if (!ifstream) throw Exception("Couldn't open " + orig.string() + " for reading");
std::unique_ptr<Chunker> chunker = ChunkerFactory::getChunker(ctx.repo->getConfig(), ifstream.rdbuf()); std::unique_ptr<Chunker> chunker = ChunkerFactory::getChunker(ctx.repo->getConfig(), ifstream.rdbuf());
SHA fileHash; SHA fileHash;
@@ -179,7 +179,7 @@ Object::idType CommandRun::backupChunkFile(const std::filesystem::path &orig, co
/// We might have exited in the loop before, so we don't save an incomplete file /// We might have exited in the loop before, so we don't save an incomplete file
if (Signals::shouldQuit) throw Exception("Quitting!"); if (Signals::shouldQuit) throw Exception("Quitting!");
if (size != File::getFileSize(orig)) { if (size != File::getFileSize(orig)) {
throw Exception("Something really bad happened or file " + orig.u8string() + " changed during backup"); throw Exception("Something really bad happened or file " + orig.string() + " changed during backup");
} }
File f(ctx.repo->getId(), saveAs, size, File::getFileMtime(orig), fileHash.getHash(), fileChunks, File f(ctx.repo->getId(), saveAs, size, File::getFileMtime(orig), fileHash.getHash(), fileChunks,
File::getFileType(orig)); File::getFileType(orig));

View File

@@ -18,16 +18,16 @@ void CommandsCommon::workerCallback(unsigned long long int bytesWritten, unsigne
} }
bool CommandsCommon::isSubpath(const std::filesystem::path &prefix, const std::filesystem::path &p) { bool CommandsCommon::isSubpath(const std::filesystem::path &prefix, const std::filesystem::path &p) {
if (prefix.u8string().size() > p.u8string().size()) return false; if (prefix.string().size() > p.string().size()) return false;
for (int i = 0; i < prefix.u8string().size(); i++) for (int i = 0; i < prefix.string().size(); i++)
if (p.u8string()[i] != prefix.u8string()[i]) return false; if (p.string()[i] != prefix.string()[i]) return false;
return true; return true;
} }
void CommandsCommon::processDirWithIgnore(const std::filesystem::path &dir, std::vector<std::string> ignore, void CommandsCommon::processDirWithIgnore(const std::filesystem::path &dir, std::vector<std::string> ignore,
const std::function<void(std::function<void()>)> &spawner, const std::function<void(std::function<void()>)> &spawner,
std::function<void(std::filesystem::directory_entry)> processFile) { std::function<void(std::filesystem::directory_entry)> processFile) {
if (!std::filesystem::is_directory(dir)) throw Exception(dir.u8string() + " is not a directory!"); if (!std::filesystem::is_directory(dir)) throw Exception(dir.string() + " is not a directory!");
/// Don't process the directory if it has a ".nobackup" file /// Don't process the directory if it has a ".nobackup" file
if (std::filesystem::exists(dir / ".nobackup")) return; if (std::filesystem::exists(dir / ".nobackup")) return;
@@ -47,7 +47,7 @@ void CommandsCommon::processDirWithIgnore(const std::filesystem::path &dir, std:
/// Don't process the entry if it matches any of the ignore rules /// Don't process the entry if it matches any of the ignore rules
if (std::any_of(ignore.begin(), ignore.end(), [dirEntry](auto pred) { if (std::any_of(ignore.begin(), ignore.end(), [dirEntry](auto pred) {
std::smatch m; std::smatch m;
auto s = dirEntry.path().filename().u8string(); auto s = dirEntry.path().filename().string();
return std::regex_match(s, m, std::regex(pred)); return std::regex_match(s, m, std::regex(pred));
})) }))
continue; continue;

View File

@@ -15,7 +15,7 @@
#include "objects/Chunk.h" #include "objects/Chunk.h"
DirEntry *getf(std::string path) { DirEntry *getf(std::string path) {
auto p = std::filesystem::relative(std::filesystem::u8path(path), "/"); auto p = std::filesystem::relative(std::filesystem::path(path), "/");
DirEntry *entry = RepoFS::root.get(); DirEntry *entry = RepoFS::root.get();
if (p != ".") if (p != ".")
for (auto const &subp: p) { entry = entry->children.at(subp).get(); } for (auto const &subp: p) { entry = entry->children.at(subp).get(); }
@@ -142,7 +142,7 @@ void RepoFS::start(Repository *repo, std::string path) {
auto a = Serialize::deserialize<Archive>(repo->getObject(r.second)); auto a = Serialize::deserialize<Archive>(repo->getObject(r.second));
for (auto const &f: a.files) { for (auto const &f: a.files) {
auto file = Serialize::deserialize<File>(repo->getObject(f)); auto file = Serialize::deserialize<File>(repo->getObject(f));
auto path = std::filesystem::u8path(file.name); auto path = std::filesystem::path(file.name);
DirEntry *entry = root->children[std::to_string(a.id)].get() DirEntry *entry = root->children[std::to_string(a.id)].get()
? root->children[std::to_string(a.id)].get() ? root->children[std::to_string(a.id)].get()
: (root->children[std::to_string(a.id)] = std::make_unique<DirEntry>()).get(); : (root->children[std::to_string(a.id)] = std::make_unique<DirEntry>()).get();
@@ -153,7 +153,7 @@ void RepoFS::start(Repository *repo, std::string path) {
: (entry->children[subp] = std::make_unique<DirEntry>()).get(); : (entry->children[subp] = std::make_unique<DirEntry>()).get();
} }
entry->file.emplace(file); entry->file.emplace(file);
entry->name = std::filesystem::u8path(file.name).filename().u8string(); entry->name = std::filesystem::path(file.name).filename().string();
} }
} }

View File

@@ -58,7 +58,7 @@ bool FileRepository::init() {
if (exists()) throw Exception("Trying to initialize already existing repository!"); if (exists()) throw Exception("Trying to initialize already existing repository!");
if (!std::filesystem::is_directory(root) && !std::filesystem::create_directories(root)) if (!std::filesystem::is_directory(root) && !std::filesystem::create_directories(root))
throw Exception("Can't create directory " + root.u8string()); throw Exception("Can't create directory " + root.string());
writeFile(root / "info", CheckFilter::filterWriteStatic(Serialize::serialize(config))); writeFile(root / "info", CheckFilter::filterWriteStatic(Serialize::serialize(config)));
@@ -157,23 +157,23 @@ bool FileRepository::deleteObject(const Object &obj) {
std::vector<char> FileRepository::readFile(const std::filesystem::path &file, unsigned long long offset, std::vector<char> FileRepository::readFile(const std::filesystem::path &file, unsigned long long offset,
unsigned long long size) const { unsigned long long size) const {
if (size > absoluteMaxFileLimit) if (size > absoluteMaxFileLimit)
throw Exception("Tried to read " + std::to_string(size) + " bytes from " + file.u8string() + throw Exception("Tried to read " + std::to_string(size) + " bytes from " + file.string() +
" which is more than absoluteMaxFileLimit"); " which is more than absoluteMaxFileLimit");
std::ifstream ifstream(file, std::ios::binary | std::ios::in); std::ifstream ifstream(file, std::ios::binary | std::ios::in);
if (!ifstream.is_open()) throw Exception("Can't open file " + file.u8string() + " for reading!"); if (!ifstream.is_open()) throw Exception("Can't open file " + file.string() + " for reading!");
std::vector<char> buf(size); std::vector<char> buf(size);
if (ifstream.rdbuf()->pubseekpos(offset) == std::streampos(std::streamoff(-1))) if (ifstream.rdbuf()->pubseekpos(offset) == std::streampos(std::streamoff(-1)))
throw Exception("Unexpected end of file " + file.u8string()); throw Exception("Unexpected end of file " + file.string());
if (ifstream.rdbuf()->sgetn(buf.data(), size) != size) throw Exception("Unexpected end of file " + file.u8string()); if (ifstream.rdbuf()->sgetn(buf.data(), size) != size) throw Exception("Unexpected end of file " + file.string());
return buf; return buf;
} }
std::vector<char> FileRepository::readFile(const std::filesystem::path &file) const { std::vector<char> FileRepository::readFile(const std::filesystem::path &file) const {
if (!std::filesystem::is_regular_file(file)) throw Exception("File " + file.u8string() + " is not a regular file!"); if (!std::filesystem::is_regular_file(file)) throw Exception("File " + file.string() + " is not a regular file!");
auto fileSize = std::filesystem::file_size(file); auto fileSize = std::filesystem::file_size(file);
if (fileSize == 0) return {}; if (fileSize == 0) return {};
return readFile(file, 0, fileSize); return readFile(file, 0, fileSize);
@@ -181,10 +181,10 @@ std::vector<char> FileRepository::readFile(const std::filesystem::path &file) co
bool FileRepository::writeFile(const std::filesystem::path &file, const std::vector<char> &data) { bool FileRepository::writeFile(const std::filesystem::path &file, const std::vector<char> &data) {
std::ofstream ofstream(file, std::ios::binary | std::ios::trunc | std::ios::out); std::ofstream ofstream(file, std::ios::binary | std::ios::trunc | std::ios::out);
if (!ofstream.is_open()) throw Exception("Can't open file " + file.u8string() + " for writing!"); if (!ofstream.is_open()) throw Exception("Can't open file " + file.string() + " for writing!");
if (ofstream.rdbuf()->sputn(data.data(), data.size()) != data.size()) if (ofstream.rdbuf()->sputn(data.data(), data.size()) != data.size())
throw Exception("Couldn't write all the data for " + file.u8string()); throw Exception("Couldn't write all the data for " + file.string());
return true; return true;
} }

View File

@@ -42,20 +42,20 @@ File::Type File::getFileType(const std::filesystem::path &p) {
if (std::filesystem::is_symlink(p)) return Type::Symlink; if (std::filesystem::is_symlink(p)) return Type::Symlink;
if (std::filesystem::is_directory(p)) return Type::Directory; if (std::filesystem::is_directory(p)) return Type::Directory;
if (std::filesystem::is_regular_file(p)) return Type::Normal; if (std::filesystem::is_regular_file(p)) return Type::Normal;
throw Exception("Unsupported file type! " + p.u8string()); throw Exception("Unsupported file type! " + p.string());
} }
std::vector<char> File::getFileContents(const std::filesystem::path &p) { std::vector<char> File::getFileContents(const std::filesystem::path &p) {
auto type = getFileType(p); auto type = getFileType(p);
if (type == Type::Normal) throw Exception(p.u8string() + " is a normal file!"); if (type == Type::Normal) throw Exception(p.string() + " is a normal file!");
if (type == Type::Directory) { return {}; } if (type == Type::Directory) { return {}; }
if (type == Type::Symlink) { if (type == Type::Symlink) {
auto target = std::filesystem::read_symlink(p).u8string(); auto target = std::filesystem::read_symlink(p).string();
std::vector<char> target_null_term = {target.begin(), target.end()}; std::vector<char> target_null_term = {target.begin(), target.end()};
target_null_term.emplace_back('\0'); target_null_term.emplace_back('\0');
return target_null_term; return target_null_term;
} }
throw Exception("Error with file " + p.u8string()); throw Exception("Error with file " + p.string());
} }
unsigned long long File::getFileMtime(const std::filesystem::path &p) { unsigned long long File::getFileMtime(const std::filesystem::path &p) {
@@ -65,16 +65,16 @@ unsigned long long File::getFileMtime(const std::filesystem::path &p) {
std::chrono::duration_cast<std::chrono::seconds>(std::filesystem::last_write_time(p).time_since_epoch()) std::chrono::duration_cast<std::chrono::seconds>(std::filesystem::last_write_time(p).time_since_epoch())
.count()); .count());
else if (type == Type::Symlink) { else if (type == Type::Symlink) {
auto path = p.u8string(); auto path = p.string();
struct stat sb; struct stat sb;
if (lstat(path.c_str(), &sb) != 0) throw Exception("Error reading mtime for " + p.u8string()); if (lstat(path.c_str(), &sb) != 0) throw Exception("Error reading mtime for " + p.string());
#ifdef __APPLE__ #ifdef __APPLE__
return sb.st_mtimespec.tv_sec; return sb.st_mtimespec.tv_sec;
#else #else
return sb.st_mtime; return sb.st_mtime;
#endif #endif
} }
throw Exception("Error with file " + p.u8string()); throw Exception("Error with file " + p.string());
} }
unsigned long long File::getFileSize(const std::filesystem::path &p) { unsigned long long File::getFileSize(const std::filesystem::path &p) {

View File

@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.18) cmake_minimum_required(VERSION 3.18)
# GoogleTest requires at least C++14 # GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0") if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")