| 1 | // SPDX-License-Identifier: GPL-3.0-or-latercxx . . $ `<file>/src/core/ccpro/zipdatasink.cpp`/ |
| 2 | // Copyright 2019 Juan Palacios <[email protected]> |
| 3 | |
| 4 | #include "zipdatasink.h"cxx . . $ `<file>/src/core/ccpro/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 = stdcxx . . $ std/::filesystem; |
| 18 | |
| 19 | ZipDataSinkcxx . . $ ZipDataSink#::ZipDataSinkcxx . . $ ZipDataSink#ZipDataSink(769d1bd79eb25b11).(stdcxx . . $ std/::filesystemcxx . . $ std/filesystem/::pathcxx . . $ std/filesystem/__cxx11/path# const &path) noexcept |
| 20 | : path_cxx . . $ ZipDataSink#path_.(path) |
| 21 | { |
| 22 | backupFilecxx . . $ ZipDataSink#backupFile(83f5f38750778ac8).(); |
| 23 | } |
| 24 | |
| 25 | ZipDataSinkcxx . . $ ZipDataSink#::~cxx . . $ ZipDataSink#`~ZipDataSink`(ced63f7c635d850d).ZipDataSinkcxx . . $ ZipDataSink#() |
| 26 | { |
| 27 | removeBackupFilecxx . . $ ZipDataSink#removeBackupFile(83f5f38750778ac8).(); |
| 28 | } |
| 29 | |
| 30 | stdcxx . . $ std/::stringcxx . . $ std/string# ZipDataSinkcxx . . $ ZipDataSink#::sinkcxx . . $ ZipDataSink#sink(219584694fe2b248).() const |
| 31 | { |
| 32 | return path_cxx . . $ ZipDataSink#path_..stringcxx . . $ std/filesystem/__cxx11/path#string(219584694fe2b248).(); |
| 33 | } |
| 34 | |
| 35 | bool ZipDataSinkcxx . . $ ZipDataSink#::writecxx . . $ ZipDataSink#write(4b2265c2a260e669).( |
| 36 | stdcxx . . $ std/::vectorcxx . . $ std/vector#<stdcxx . . $ std/::paircxx . . $ std/pair#<stdcxx . . $ std/::stringcxx . . $ std/string#, stdcxx . . $ std/::vectorcxx . . $ std/vector#<char>>> const &data) |
| 37 | { |
| 38 | if (!data.emptycxx . . $ std/vector#empty(3482b152b9333168).()) { |
| 39 | QuaZipcxx . . $ QuaZip# zipcxx . . $ QuaZip#QuaZip(3520e46b33348bf6).(QStringcxx . . $ QString#::fromStdStringcxx . . $ QString#fromStdString(24a2e9bdf24626eb).(sinkcxx . . $ ZipDataSink#sink(219584694fe2b248).())); |
| 40 | if (zip.opencxx . . $ QuaZip#open(fd0432b046ebdc00).(QuaZipcxx . . $ QuaZip#::mdCreatecxx . . $ QuaZip#mdCreate.)) { |
| 41 | |
| 42 | for (auto &[dataFilePath, fileData] : data) { |
| 43 | if (!dataFilePath.emptycxx . . $ std/__cxx11/basic_string#empty(3482b152b9333168).() && !fileData.emptycxx . . $ std/vector#empty(3482b152b9333168).()) { |
| 44 | |
| 45 | QuaZipFilecxx . . $ QuaZipFile# filecxx . . $ QuaZipFile#QuaZipFile(32ccc81e403473f6).(&zip); |
| 46 | if (!(file.opencxx . . $ QuaZipFile#open(6f19fa9e06e61f5).(QIODevicecxx . . $ QFlags#QFlags(93d577a3e9528307).::WriteOnlycxx . . $ QIODeviceBase#WriteOnly., |
| 47 | QuaZipNewInfocxx . . $ QuaZipNewInfo#(QStringcxx . . $ QString#::fromStdStringcxx . . $ QString#fromStdString(24a2e9bdf24626eb).(dataFilePath))) && |
| 48 | file.writecxx . . $ QIODevice#write(4ee2750a0be83987).(QByteArraycxx . . $ QByteArray#::fromRawDatacxx . . $ QByteArray#fromRawData(cc880211a6cf3fc9).(fileData.datacxx . . $ std/vector#data(8e614c7de0b1573e).(), |
| 49 | fileData.sizecxx . . $ std/vector#size(c9b1e749b64d5edc).())) >= 0)) { |
| 50 | |
| 51 | if (file.isOpencxx . . $ QIODevice#isOpen(50ce9a9e25b4a850).()) |
| 52 | file.closecxx . . $ QuaZipFile#close(49f6e7a06ebc5aa8).(); |
| 53 | zip.closecxx . . $ QuaZip#close(49f6e7a06ebc5aa8).(); |
| 54 | |
| 55 | restorePreWriteFileStatecxx . . $ ZipDataSink#restorePreWriteFileState(83f5f38750778ac8).(); |
| 56 | |
| 57 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#( |
| 58 | stdcxx . . $ std/::formatcxx . . $ std/format(7a9e6f4918cdc192).("Failed to write {} data to file {}"cxx . . $ std/basic_format_string#basic_format_string(edcd8b2679e9bd1a)., |
| 59 | dataFilePath.datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(), sinkcxx . . $ ZipDataSink#sink(219584694fe2b248).().datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).())); |
| 60 | } |
| 61 | |
| 62 | file.closecxx . . $ QuaZipFile#close(49f6e7a06ebc5aa8).(); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | zip.closecxx . . $ QuaZip#close(49f6e7a06ebc5aa8).(); |
| 67 | return true; |
| 68 | } |
| 69 | else { |
| 70 | throw stdcxx . . $ std/::runtime_errorcxx . . $ std/runtime_error#( |
| 71 | stdcxx . . $ std/::formatcxx . . $ std/format(7a9e6f4918cdc192).("Failed to open file {}"cxx . . $ std/basic_format_string#basic_format_string(edcd8b2679e9bd1a)., sinkcxx . . $ ZipDataSink#sink(219584694fe2b248).().datacxx . . $ std/__cxx11/basic_string#data(258ee1c9e0a18a5c).())); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return false; |
| 76 | } |
| 77 | |
| 78 | void ZipDataSinkcxx . . $ ZipDataSink#::backupFilecxx . . $ ZipDataSink#backupFile(83f5f38750778ac8).() const |
| 79 | { |
| 80 | try { |
| 81 | if (fs::existscxx . . $ std/filesystem/exists(c53a6ef3278968b1).(path_cxx . . $ ZipDataSink#path_.) && fs::is_regular_filecxx . . $ std/filesystem/is_regular_file(c53a6ef3278968b1).(path_cxx . . $ ZipDataSink#path_.)) |
| 82 | fs::copy_filecxx . . $ std/filesystem/copy_file(a0213926d45dbebc).(path_cxx . . $ ZipDataSink#path_., sinkcxx . . $ std/filesystem/__cxx11/path#path(2f6c7066048c5333).() +cxx . . $ std/operator+(900ec9581c410ad9). ".bak", fs::copy_optionscxx . . $ std/filesystem/copy_options#::overwrite_existingcxx . . $ std/filesystem/copy_options#overwrite_existing.); |
| 83 | } |
| 84 | catch (stdcxx . . $ std/::exception const &e) { |
| 85 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!(e.what()); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void ZipDataSinkcxx . . $ ZipDataSink#::removeBackupFilecxx . . $ ZipDataSink#removeBackupFile(83f5f38750778ac8).() const |
| 90 | { |
| 91 | try { |
| 92 | fs::removecxx . . $ std/filesystem/remove(c53a6ef3278968b1).(sinkcxx . . $ std/filesystem/__cxx11/path#path(2f6c7066048c5333).() +cxx . . $ std/operator+(900ec9581c410ad9). ".bak"); |
| 93 | } |
| 94 | catch (stdcxx . . $ std/::exception const &e) { |
| 95 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!(e.what()); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void ZipDataSinkcxx . . $ ZipDataSink#::restorePreWriteFileStatecxx . . $ ZipDataSink#restorePreWriteFileState(83f5f38750778ac8).() const |
| 100 | { |
| 101 | // remove the current file |
| 102 | try { |
| 103 | fs::removecxx . . $ std/filesystem/remove(c53a6ef3278968b1).(path_cxx . . $ std/filesystem/__cxx11/path#path(2f6c7066048c5333)..stringcxx . . $ std/filesystem/__cxx11/path#string(219584694fe2b248).()); |
| 104 | } |
| 105 | catch (stdcxx . . $ std/::exception const &e) { |
| 106 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!(e.what()); |
| 107 | } |
| 108 | |
| 109 | // restore the backup file |
| 110 | try { |
| 111 | if (fs::existscxx . . $ std/filesystem/exists(c53a6ef3278968b1).(sinkcxx . . $ std/filesystem/__cxx11/path#path(2f6c7066048c5333).() +cxx . . $ std/operator+(900ec9581c410ad9). ".bak") && fs::is_regular_filecxx . . $ std/filesystem/is_regular_file(c53a6ef3278968b1).(sinkcxx . . $ std/filesystem/__cxx11/path#path(2f6c7066048c5333).() +cxx . . $ std/operator+(900ec9581c410ad9). ".bak")) |
| 112 | fs::copy_filecxx . . $ std/filesystem/copy_file(a0213926d45dbebc).(sinkcxx . . $ std/filesystem/__cxx11/path#path(2f6c7066048c5333).() +cxx . . $ std/operator+(900ec9581c410ad9). ".bak", path_cxx . . $ ZipDataSink#path_., fs::copy_optionscxx . . $ std/filesystem/copy_options#::overwrite_existingcxx . . $ std/filesystem/copy_options#overwrite_existing.); |
| 113 | } |
| 114 | catch (stdcxx . . $ std/::exception const &e) { |
| 115 | SPDLOG_DEBUGcxx . . $ `<external>/13613935168682455490/spdlog.h:311:13`!(e.what()); |
| 116 | } |
| 117 | } |