| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "zipdatasink.h" |
| 5 | |
| 6 | #include <QByteArray> |
| 7 | #include <QIODevice> |
| 8 | #include <QString> |
| 9 | #include <exception> |
| 10 | #include <format> |
| 11 | #include <quazip.h> |
| 12 | #include <quazipfile.h> |
| 13 | #include <quazipnewinfo.h> |
| 14 | #include <spdlog/spdlog.h> |
| 15 | #include <stdexcept> |
| 16 | |
| 17 | namespace fs = std::filesystem; |
| 18 | |
| 19 | ZipDataSink::ZipDataSink(std::filesystem::path const &path) noexcept |
| 20 | : path_(path) |
| 21 | { |
| 22 | backupFile(); |
| 23 | } |
| 24 | |
| 25 | ZipDataSink::~ZipDataSink() |
| 26 | { |
| 27 | removeBackupFile(); |
| 28 | } |
| 29 | |
| 30 | std::string ZipDataSink::sink() const |
| 31 | { |
| 32 | return path_.string(); |
| 33 | } |
| 34 | |
| 35 | bool ZipDataSink::write( |
| 36 | std::vector<std::pair<std::string, std::vector<char>>> const &data) |
| 37 | { |
| 38 | if (!data.empty()) { |
| 39 | QuaZip zip(QString::fromStdString(sink())); |
| 40 | if (zip.open(QuaZip::mdCreate)) { |
| 41 | |
| 42 | for (auto &[dataFilePath, fileData] : data) { |
| 43 | if (!dataFilePath.empty() && !fileData.empty()) { |
| 44 | |
| 45 | QuaZipFile file(&zip); |
| 46 | if (!(file.open(QIODevice::WriteOnly, |
| 47 | QuaZipNewInfo(QString::fromStdString(dataFilePath))) && |
| 48 | file.write(QByteArray::fromRawData(fileData.data(), |
| 49 | fileData.size())) >= 0)) { |
| 50 | |
| 51 | if (file.isOpen()) |
| 52 | file.close(); |
| 53 | zip.close(); |
| 54 | |
| 55 | restorePreWriteFileState(); |
| 56 | |
| 57 | throw std::runtime_error( |
| 58 | std::format("Failed to write {} data to file {}", |
| 59 | dataFilePath.data(), sink().data())); |
| 60 | } |
| 61 | |
| 62 | file.close(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | zip.close(); |
| 67 | return true; |
| 68 | } |
| 69 | else { |
| 70 | throw std::runtime_error( |
| 71 | std::format("Failed to open file {}", sink().data())); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | void ZipDataSink::backupFile() const |
| 79 | { |
| 80 | try { |
| 81 | if (fs::exists(path_) && fs::is_regular_file(path_)) |
| 82 | fs::copy_file(path_, sink() + ".bak", fs::copy_options::overwrite_existing); |
| 83 | } |
| 84 | catch (std::exception const &e) { |
| 85 | SPDLOG_DEBUG(e.what()); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void ZipDataSink::removeBackupFile() const |
| 90 | { |
| 91 | try { |
| 92 | fs::remove(sink() + ".bak"); |
| 93 | } |
| 94 | catch (std::exception const &e) { |
| 95 | SPDLOG_DEBUG(e.what()); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void ZipDataSink::restorePreWriteFileState() const |
| 100 | { |
| 101 | // remove the current file |
| 102 | try { |
| 103 | fs::remove(path_.string()); |
| 104 | } |
| 105 | catch (std::exception const &e) { |
| 106 | SPDLOG_DEBUG(e.what()); |
| 107 | } |
| 108 | |
| 109 | // restore the backup file |
| 110 | try { |
| 111 | if (fs::exists(sink() + ".bak") && fs::is_regular_file(sink() + ".bak")) |
| 112 | fs::copy_file(sink() + ".bak", path_, fs::copy_options::overwrite_existing); |
| 113 | } |
| 114 | catch (std::exception const &e) { |
| 115 | SPDLOG_DEBUG(e.what()); |
| 116 | } |
| 117 | } |