diff --git a/src/fileutils.cpp b/src/fileutils.cpp index c805ea71ccca436fe138f05891744aa6c21b2372..dc59b8705e275b580b1ee7474f4d0824a147232a 100644 --- a/src/fileutils.cpp +++ b/src/fileutils.cpp @@ -371,8 +371,8 @@ readArchive(const std::filesystem::path& path, std::string_view scheme, const st try { fileContent = dhtnet::fileutils::loadFile(path); } catch (const std::exception& e) { - JAMI_ERR("Error loading archive: %s", e.what()); - throw e; + JAMI_ERROR("Error loading archive: {}", e.what()); + throw; } if (isUnencryptedGzip(fileContent)) { @@ -393,7 +393,7 @@ readArchive(const std::filesystem::path& path, std::string_view scheme, const st base64::decode(pwd)); } catch (const std::exception& e) { JAMI_ERROR("Error decrypting archive: {}", e.what()); - throw e; + throw; } } else if (scheme == ARCHIVE_AUTH_SCHEME_PASSWORD) { try { @@ -401,7 +401,7 @@ readArchive(const std::filesystem::path& path, std::string_view scheme, const st fileContent = dht::crypto::aesDecrypt(fileContent, pwd); } catch (const std::exception& e) { JAMI_ERROR("Error decrypting archive: {}", e.what()); - throw e; + throw; } } decompress(fileContent); @@ -413,14 +413,14 @@ readArchive(const std::filesystem::path& path, std::string_view scheme, const st return ret; } -void +bool writeArchive(const std::string& archive_str, const std::filesystem::path& path, std::string_view scheme, const std::string& password, const std::vector<uint8_t>& password_salt) { - JAMI_LOG("Writing archive to {}", path); + JAMI_LOG("Writing archive to {} using scheme '{}'", path, scheme); if (scheme == ARCHIVE_AUTH_SCHEME_KEY) { // Encrypt using provided key @@ -430,7 +430,7 @@ writeArchive(const std::string& archive_str, saveFile(path, dht::crypto::aesBuildEncrypted(newArchive, password_salt)); } catch (const std::runtime_error& ex) { JAMI_ERROR("Export failed: {}", ex.what()); - return; + return false; } } else if (scheme == ARCHIVE_AUTH_SCHEME_PASSWORD and not password.empty()) { // Encrypt using provided password @@ -441,12 +441,16 @@ writeArchive(const std::string& archive_str, password_salt)); } catch (const std::runtime_error& ex) { JAMI_ERROR("Export failed: {}", ex.what()); - return; + return false; } - } else { + } else if (scheme == ARCHIVE_AUTH_SCHEME_NONE || (scheme == ARCHIVE_AUTH_SCHEME_PASSWORD && password.empty())) { JAMI_WARNING("Unsecured archiving (no password)"); archiver::compressGzip(archive_str, path.string()); + } else { + JAMI_ERROR("Unsupported scheme: {}", scheme); + return false; } + return true; } std::filesystem::path diff --git a/src/fileutils.h b/src/fileutils.h index a901a2655efbfa25ac2f83265867c77d28e003fe..a8e700501fbe88a7d879715ce234a9bbdfd282b4 100644 --- a/src/fileutils.h +++ b/src/fileutils.h @@ -134,7 +134,7 @@ ArchiveStorageData readArchive(const std::filesystem::path& path, std::string_view scheme, const std::string& pwd); -void writeArchive(const std::string& data, +bool writeArchive(const std::string& data, const std::filesystem::path& path, std::string_view scheme, const std::string& password = {}, diff --git a/src/jamidht/accountarchive.h b/src/jamidht/accountarchive.h index e863db8bca17a0dc6273e6c774e96c303224205a..a5d0ccaaa3ee34c95669a5ba53b1480a60db3ff5 100644 --- a/src/jamidht/accountarchive.h +++ b/src/jamidht/accountarchive.h @@ -89,11 +89,11 @@ struct AccountArchive } /** Save archive to file, optionally encrypted with provided password. */ - void save(const std::filesystem::path& path, + bool save(const std::filesystem::path& path, std::string_view scheme, const std::string& password) const { - fileutils::writeArchive(serialize(), path, scheme, password, password_salt); + return fileutils::writeArchive(serialize(), path, scheme, password, password_salt); } }; diff --git a/src/jamidht/archive_account_manager.cpp b/src/jamidht/archive_account_manager.cpp index 092be925aa41739be84dc8bf6edb6bc3fe8b9441..38f2822d164ebc432a724d6c7c8cf8a1cb81162e 100644 --- a/src/jamidht/archive_account_manager.cpp +++ b/src/jamidht/archive_account_manager.cpp @@ -1728,7 +1728,8 @@ ArchiveAccountManager::exportArchive(const std::string& destinationPath, AccountArchive archive = readArchive(scheme, password); updateArchive(archive); auto archivePath = fileutils::getFullPath(path_, archivePath_); - archive.save(archivePath, scheme, password); + if (!archive.save(archivePath, scheme, password)) + return false; // Export the file std::error_code ec;