Welcome to mirror list, hosted at ThFree Co, Russian Federation.

accurate.cc « filed « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a169418d9ca1ed16c7c3b1272140c381cdca123d (plain)
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
   Copyright (C) 2013-2014 Planets Communications B.V.
   Copyright (C) 2013-2022 Bareos GmbH & Co. KG

   This program is Free Software; you can redistribute it and/or
   modify it under the terms of version three of the GNU Affero General Public
   License as published by the Free Software Foundation and included
   in the file LICENSE.

   This program 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
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/

#include "include/bareos.h"
#include "filed/filed.h"
#include "filed/accurate.h"
#include "filed/filed_globals.h"
#include "filed/filed_jcr_impl.h"
#include "filed/verify.h"
#include "lib/attribs.h"
#include "lib/bsock.h"
#include "lib/edit.h"

namespace filedaemon {

static int debuglevel = 100;

bool AccurateMarkFileAsSeen(JobControlRecord* jcr, char* fname)
{
  accurate_payload* temp;

  if (!jcr->accurate || !jcr->fd_impl->file_list) { return false; }

  temp = jcr->fd_impl->file_list->lookup_payload(fname);
  if (temp) {
    jcr->fd_impl->file_list->MarkFileAsSeen(temp);
    Dmsg1(debuglevel, "marked <%s> as seen\n", fname);
  } else {
    Dmsg1(debuglevel, "<%s> not found to be marked as seen\n", fname);
  }

  return true;
}

bool accurate_unMarkFileAsSeen(JobControlRecord* jcr, char* fname)
{
  accurate_payload* temp;

  if (!jcr->accurate || !jcr->fd_impl->file_list) { return false; }

  temp = jcr->fd_impl->file_list->lookup_payload(fname);
  if (temp) {
    jcr->fd_impl->file_list->UnmarkFileAsSeen(temp);
    Dmsg1(debuglevel, "unmarked <%s> as seen\n", fname);
  } else {
    Dmsg1(debuglevel, "<%s> not found to be unmarked as seen\n", fname);
  }

  return true;
}

bool AccurateMarkAllFilesAsSeen(JobControlRecord* jcr)
{
  if (!jcr->accurate || !jcr->fd_impl->file_list) { return false; }

  jcr->fd_impl->file_list->MarkAllFilesAsSeen();
  return true;
}

bool accurate_unMarkAllFilesAsSeen(JobControlRecord* jcr)
{
  if (!jcr->accurate || !jcr->fd_impl->file_list) { return false; }

  jcr->fd_impl->file_list->UnmarkAllFilesAsSeen();
  return true;
}

static inline bool AccurateLookup(JobControlRecord* jcr,
                                  char* fname,
                                  accurate_payload** payload)
{
  bool found = false;

  *payload = jcr->fd_impl->file_list->lookup_payload(fname);
  if (*payload) {
    found = true;
    Dmsg1(debuglevel, "lookup <%s> ok\n", fname);
  }

  return found;
}

void AccurateFree(JobControlRecord* jcr)
{
  if (jcr->fd_impl->file_list) {
    delete jcr->fd_impl->file_list;
    jcr->fd_impl->file_list = NULL;
  }
}

// Send the deleted or the base file list and cleanup.
bool AccurateFinish(JobControlRecord* jcr)
{
  bool retval = true;

  if (jcr->IsCanceled() || jcr->IsIncomplete()) {
    AccurateFree(jcr);
    return retval;
  }

  if (jcr->accurate && jcr->fd_impl->file_list) {
    if (jcr->is_JobLevel(L_FULL)) {
      if (!jcr->rerunning) {
        retval = jcr->fd_impl->file_list->SendBaseFileList();
      }
    } else {
      retval = jcr->fd_impl->file_list->SendDeletedList();
    }

    AccurateFree(jcr);
    if (jcr->is_JobLevel(L_FULL)) {
      Jmsg(jcr, M_INFO, 0, _("Space saved with Base jobs: %lld MB\n"),
           jcr->fd_impl->base_size / (1024 * 1024));
    }
  }

  return retval;
}

/**
 * This function is called for each file seen in fileset.
 * We check in file_list hash if fname have been backed up
 * the last time. After we can compare Lstat field.
 * Full Lstat usage have been removed on 6612
 *
 * Returns: true   if file has changed (must be backed up)
 *          false  file not changed
 */
bool AccurateCheckFile(JobControlRecord* jcr, FindFilesPacket* ff_pkt)
{
  char* opts;
  char* fname;
  int32_t LinkFIc;
  struct stat statc;
  bool status = false;
  accurate_payload* payload;

  ff_pkt->delta_seq = 0;
  ff_pkt->accurate_found = false;

  if (!jcr->accurate && !jcr->rerunning) { return true; }

  if (!jcr->fd_impl->file_list) { return true; /** Not initialized properly */ }

  // Apply path stripping for lookup in accurate data.
  StripPath(ff_pkt);

  if (S_ISDIR(ff_pkt->statp.st_mode)) {
    fname = ff_pkt->link;
  } else {
    fname = ff_pkt->fname;
  }

  if (!AccurateLookup(jcr, fname, &payload)) {
    Dmsg1(debuglevel, "accurate %s (not found)\n", fname);
    status = true;
    UnstripPath(ff_pkt);
    goto bail_out;
  }

  /**
   * Restore original name so we can check the actual file when we check
   * the accurate options later on. This is mostly important for the
   * CalculateAndCompareFileChksum() function as that needs to calulate
   * the checksum of the real file and not try to open the stripped pathname.
   */
  UnstripPath(ff_pkt);

  ff_pkt->accurate_found = true;
  ff_pkt->delta_seq = payload->delta_seq;

  DecodeStat(payload->lstat, &statc, sizeof(statc),
             &LinkFIc); /** decode catalog stat */

  if (!jcr->rerunning && (jcr->getJobLevel() == L_FULL)) {
    opts = ff_pkt->BaseJobOpts;
  } else {
    opts = ff_pkt->AccurateOpts;
  }

  // Loop over options supplied by user and verify the fields he requests.
  for (char* p = opts; !status && *p; p++) {
    char ed1[30], ed2[30];
    switch (*p) {
      case 'i': /** Compare INODE numbers */
        if (statc.st_ino != ff_pkt->statp.st_ino) {
          Dmsg3(debuglevel - 1, "%s      st_ino   differ. Cat: %s File: %s\n",
                fname, edit_uint64((uint64_t)statc.st_ino, ed1),
                edit_uint64((uint64_t)ff_pkt->statp.st_ino, ed2));
          status = true;
        }
        break;
      case 'p': /** Permissions bits */
        /**
         * TODO: If something change only in perm, user, group
         * Backup only the attribute stream
         */
        if (statc.st_mode != ff_pkt->statp.st_mode) {
          Dmsg3(debuglevel - 1,
                "%s     st_mode  differ. Cat: %04o File: %04o\n", fname,
                (uint32_t)(statc.st_mode & ~S_IFMT),
                (uint32_t)(ff_pkt->statp.st_mode & ~S_IFMT));
          status = true;
        }
        break;
      case 'n': /** Number of links */
        if (statc.st_nlink != ff_pkt->statp.st_nlink) {
          Dmsg3(debuglevel - 1, "%s      st_nlink differ. Cat: %d File: %d\n",
                fname, (uint32_t)statc.st_nlink,
                (uint32_t)ff_pkt->statp.st_nlink);
          status = true;
        }
        break;
      case 'u': /** User id */
        if (statc.st_uid != ff_pkt->statp.st_uid) {
          Dmsg3(debuglevel - 1, "%s      st_uid   differ. Cat: %u File: %u\n",
                fname, (uint32_t)statc.st_uid, (uint32_t)ff_pkt->statp.st_uid);
          status = true;
        }
        break;
      case 'g': /** Group id */
        if (statc.st_gid != ff_pkt->statp.st_gid) {
          Dmsg3(debuglevel - 1, "%s      st_gid   differ. Cat: %u File: %u\n",
                fname, (uint32_t)statc.st_gid, (uint32_t)ff_pkt->statp.st_gid);
          status = true;
        }
        break;
      case 's': /** Size */
        if (statc.st_size != ff_pkt->statp.st_size) {
          Dmsg3(debuglevel - 1, "%s      st_size  differ. Cat: %s File: %s\n",
                fname, edit_uint64((uint64_t)statc.st_size, ed1),
                edit_uint64((uint64_t)ff_pkt->statp.st_size, ed2));
          status = true;
        }
        break;
      case 'a': /** Access time */
        if (statc.st_atime != ff_pkt->statp.st_atime) {
          Dmsg1(debuglevel - 1, "%s      st_atime differs\n", fname);
          status = true;
        }
        break;
      case 'm': /** Modification time */
        if (statc.st_mtime != ff_pkt->statp.st_mtime) {
          Dmsg1(debuglevel - 1, "%s      st_mtime differs\n", fname);
          status = true;
        }
        break;
      case 'c': /** Change time */
        if (statc.st_ctime != ff_pkt->statp.st_ctime) {
          Dmsg1(debuglevel - 1, "%s      st_ctime differs\n", fname);
          status = true;
        }
        break;
      case 'd': /** File size decrease */
        if (statc.st_size > ff_pkt->statp.st_size) {
          Dmsg3(debuglevel - 1, "%s      st_size  decrease. Cat: %s File: %s\n",
                fname, edit_uint64((uint64_t)statc.st_size, ed1),
                edit_uint64((uint64_t)ff_pkt->statp.st_size, ed2));
          status = true;
        }
        break;
      case 'A': /** Always backup a file */
        status = true;
        break;
      case '5': /** Compare MD5 */
      case '1': /** Compare SHA1 */
        if (!status && ff_pkt->type != FT_LNKSAVED
            && (S_ISREG(ff_pkt->statp.st_mode)
                && (BitIsSet(FO_MD5, ff_pkt->flags)
                    || BitIsSet(FO_SHA1, ff_pkt->flags)
                    || BitIsSet(FO_SHA256, ff_pkt->flags)
                    || BitIsSet(FO_SHA512, ff_pkt->flags)))) {
          if (!*payload->chksum && !jcr->rerunning) {
            Jmsg(jcr, M_WARNING, 0, _("Cannot verify checksum for %s\n"),
                 ff_pkt->fname);
            status = true;
          } else {
            status = !CalculateAndCompareFileChksum(jcr, ff_pkt, fname,
                                                    payload->chksum);
          }
        }
        break;
      case ':':
      case 'J':
      case 'C':
        break;
      default:
        break;
    }
  }

  /**
   * In Incr/Diff accurate mode, we mark all files as seen
   * When in Full+Base mode, we mark only if the file match exactly
   */
  if (jcr->getJobLevel() == L_FULL) {
    if (!status) {
      // Compute space saved with basefile.
      jcr->fd_impl->base_size += ff_pkt->statp.st_size;
      jcr->fd_impl->file_list->MarkFileAsSeen(payload);
    }
  } else {
    jcr->fd_impl->file_list->MarkFileAsSeen(payload);
  }

bail_out:
  return status;
}

bool AccurateCmd(JobControlRecord* jcr)
{
  uint32_t number_of_previous_files;
  int fname_length, lstat_length, chksum_length;
  char *fname, *lstat, *chksum;
  uint16_t delta_seq;
  BareosSocket* dir = jcr->dir_bsock;

  if (JobCanceled(jcr)) { return true; }

  if (sscanf(dir->msg, "accurate files=%u", &number_of_previous_files) != 1) {
    dir->fsend(_("2991 Bad accurate command\n"));
    return false;
  }

#ifdef HAVE_LMDB
  if (me->always_use_lmdb
      || (me->lmdb_threshold > 0
          && number_of_previous_files >= me->lmdb_threshold)) {
    jcr->fd_impl->file_list
        = new BareosAccurateFilelistLmdb(jcr, number_of_previous_files);
  } else {
    jcr->fd_impl->file_list
        = new BareosAccurateFilelistHtable(jcr, number_of_previous_files);
  }
#else
  jcr->fd_impl->file_list
      = new BareosAccurateFilelistHtable(jcr, number_of_previous_files);
#endif

  if (!jcr->fd_impl->file_list->init()) { return false; }

  jcr->accurate = true;

  // dirmsg = fname + \0 + lstat + \0 + checksum + \0 + delta_seq + \0
  while (dir->recv() >= 0) {
    fname = dir->msg;
    fname_length = strlen(fname);
    lstat = dir->msg + fname_length + 1;
    lstat_length = strlen(lstat);

    // No checksum.
    if ((fname_length + lstat_length + 2) >= dir->message_length) {
      chksum = NULL;
      chksum_length = 0;
      delta_seq = 0;
    } else {
      chksum = lstat + lstat_length + 1;
      chksum_length = strlen(chksum);
      delta_seq = str_to_int32(chksum + chksum_length + 1);

      /**
       * Sanity check total length of the received msg must be at least
       * total of the 3 lengths calculated + 3 (\0)
       */
      if ((fname_length + lstat_length + chksum_length + 3)
          > dir->message_length) {
        continue;
      }
    }

    jcr->fd_impl->file_list->AddFile(fname, fname_length, lstat, lstat_length,
                                     chksum, chksum_length, delta_seq);
  }

  if (!jcr->fd_impl->file_list->EndLoad()) { return false; }

  return true;
}

} /* namespace filedaemon */