File: exporttodir.cc

package info (click to toggle)
signalbackup-tools 20250313.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,752 kB
  • sloc: cpp: 47,042; sh: 477; ansic: 399; ruby: 19; makefile: 3
file content (237 lines) | stat: -rw-r--r-- 7,766 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
  Copyright (C) 2019-2025  Selwin van Dijk

  This file is part of signalbackup-tools.

  signalbackup-tools is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  signalbackup-tools is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with signalbackup-tools.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "signalbackup.ih"

bool SignalBackup::exportBackupToDir(std::string const &directory, bool overwrite, bool keepattachmentdatainmemory, bool onlydb)
{
  Logger::message("\nExporting backup into '", directory, "/'");

  if (!prepareOutputDirectory(directory, overwrite))
    return false;

  bool exportok = true;

  // export headerframe:
  if (!onlydb)
  {
    Logger::message("Writing HeaderFrame...");
    if (!writeRawFrameDataToFile(directory + "/Header.sbf", d_headerframe)) [[unlikely]]
    {
      Logger::error("Failed to write headerframe");
      exportok = false;
    }

    // export databaseversionframe
    Logger::message("Writing DatabaseVersionFrame...");
    if (!writeRawFrameDataToFile(directory + "/DatabaseVersion.sbf", d_databaseversionframe)) [[unlikely]]
    {
      Logger::error("Failed to write databaseversionframe");
      exportok = false;
    }

    // export attachments
    Logger::message("Writing Attachments...");
    for (auto const &aframe : d_attachments)
    {
      AttachmentFrame *a = aframe.second.get();
      uint64_t rowid = a->rowId();
      int64_t uniqueid = a->attachmentId();
      if (uniqueid == 0)
        uniqueid = -1;
      std::string attachment_basefilename = directory + "/Attachment_" + bepaald::toString(rowid) + "_" + bepaald::toString(uniqueid);

      // write frame
      if (!writeRawFrameDataToFile(attachment_basefilename + ".sbf", a)) [[unlikely]]
      {
        Logger::error("Failed to write attachmentframe");
        exportok = false;
        continue;
      }

      // write actual attachment:
      std::ofstream attachmentstream(attachment_basefilename + ".bin", std::ios_base::binary);
      if (!attachmentstream.is_open()) [[unlikely]]
      {
        Logger::error("Failed to open file for writing: ", directory, attachment_basefilename, ".bin");
        exportok = false;
        continue;
      }
      else
      {
        unsigned char const *data = a->attachmentData();
        if (!data) [[unlikely]]
        {
          Logger::error("Failed to retrieve attachment data for attachment (rowid: ", rowid, " uniqueid: ", uniqueid, ")");
          exportok = false;
          continue;
        }
        if (!attachmentstream.write(reinterpret_cast<char const *>(data), a->attachmentSize())) [[unlikely]]
        {
          Logger::error("Failed write attachmentdata");
          exportok = false;
          continue;
        }
      }

      if (!keepattachmentdatainmemory && a)
      {
        MEMINFO("BEFORE DROPPING ATTACHMENT DATA");
        a->clearData();
        MEMINFO("AFTER DROPPING ATTACHMENT DATA");
      }
    }

    // export avatars
    Logger::message("Writing Avatars...");
#if __cplusplus > 201703L
    for (int count = 1; auto const &aframe : d_avatars)
#else
      int count = 1;
    for (auto const &aframe : d_avatars)
#endif
    {
      AvatarFrame *a = aframe.second.get();
      std::string avatar_basefilename = directory + "/Avatar_" +
        std::string(bepaald::numDigits(d_avatars.size()) - bepaald::numDigits(count), '0') + bepaald::toString(count)
        + "_" + ((d_databaseversion < 33) ? a->name() : a->recipient());
      ++count;

      // write frame
      if (!writeRawFrameDataToFile(avatar_basefilename + ".sbf", a)) [[unlikely]]
      {
        Logger::error("Failed to write avatarframe");
        exportok = false;
        continue;
      }

      // write actual avatar:
      std::ofstream avatarstream(avatar_basefilename + ".bin", std::ios_base::binary);
      if (!avatarstream.is_open()) [[unlikely]]
      {
        Logger::error("Failed to open file for writing: ", directory, avatar_basefilename, ".bin");
        exportok = false;
        continue;
      }
      else
        if (!avatarstream.write(reinterpret_cast<char *>(a->attachmentData()), a->attachmentSize())) [[unlikely]]
        {
          Logger::error("Failed to write avatar data");
          exportok = false;
          continue;
        }
    }

    // export sharedpreferences
    Logger::message("Writing SharedPrefFrame(s)...");
#if __cplusplus > 201703L
    for (int count = 1; auto const &spframe : d_sharedpreferenceframes)
#else
      count = 1;
    for (auto const &spframe : d_sharedpreferenceframes)
#endif
      if (!writeRawFrameDataToFile(directory + "/SharedPreference_" + bepaald::toString(count++) + ".sbf", spframe)) [[unlikely]]
      {
        Logger::error("Failed to write sharedpreferenceframe");
        exportok = false;
        continue;
      }

    // export keyvalues
    Logger::message("Writing KeyValueFrame(s)...");
#if __cplusplus > 201703L
    for (int count = 1; auto const &kvframe : d_keyvalueframes)
#else
      count = 1;
    for (auto const &kvframe : d_keyvalueframes)
#endif
      if (!writeRawFrameDataToFile(directory + "/KeyValue_" + bepaald::toString(count++) + ".sbf", kvframe)) [[unlikely]]
      {
        Logger::error("Failed to write keyvalueframe");
        exportok = false;
        continue;
      }

    // export stickers
    Logger::message("Writing StickerFrames...");
#if __cplusplus > 201703L
    for (int count = 1; auto const &sframe : d_stickers)
#else
      count = 1;
    for (auto const &sframe : d_stickers)
#endif
    {
      StickerFrame *s = sframe.second.get();
      std::string sticker_basefilename = directory + "/Sticker_" + bepaald::toString(count++) + "_" + bepaald::toString(s->rowId());

      // write frame
      if (!writeRawFrameDataToFile(sticker_basefilename + ".sbf", s)) [[unlikely]]
      {
        Logger::error("Failed to write stickerframe");
        exportok = false;
        continue;
      }

      // write actual sticker data
      std::ofstream stickerstream(sticker_basefilename + ".bin", std::ios_base::binary);
      if (!stickerstream.is_open()) [[unlikely]]
      {
        Logger::error("Failed to open file for writing: ", directory, sticker_basefilename, ".bin");
        exportok = false;
        continue;
      }
      else
        if (!stickerstream.write(reinterpret_cast<char *>(s->attachmentData()), s->attachmentSize())) [[unlikely]]
        {
          Logger::error("Failed to write sticker data");
          exportok = false;
          continue;
        }
    }

    // export endframe
    Logger::message("Writing EndFrame...");
    if (!writeRawFrameDataToFile(directory + "/End.sbf", d_endframe)) [[unlikely]]
    {
      Logger::error("Failed to write endframe");
      exportok = false;
    }
  }

  // export database
  Logger::message("Writing database...");
  if (!d_database.saveToFile(directory + "/database.sqlite")) [[unlikely]]
  {
    Logger::error("Failed to write SQLite database");
    exportok = false;
  }

#ifdef BUILT_FOR_TESTING
  // if d_found_sqlite_sequence_in_database
  //   write('BUILT_FOR_TESTING_FOUND_SQLITE_SEQUENCE');
  if (d_found_sqlite_sequence_in_backup)
  {
    std::ofstream bft(directory + "/BUILT_FOR_TESTING_FOUND_SQLITE_SEQUENCE", std::ios_base::binary);
    bft.close();
  }
#endif

  Logger::message("Done!");
  return exportok;
}