Skip to content

Commit

Permalink
bsr: fix bad decrementing
Browse files Browse the repository at this point in the history
We should only decrement the expected file counter if a file we are
actually restoring is split over two volumes.  That means we need to
keep track of which FileIndices we actually selected to be restored
instead of just assuming that we are restoring the whole volume.
  • Loading branch information
sebsura authored and BareosBot committed Aug 10, 2023
1 parent 4f48442 commit 4dc2c58
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 14 deletions.
32 changes: 24 additions & 8 deletions core/src/dird/bsr.cc
Expand Up @@ -154,13 +154,16 @@ static void PrintBsrItem(std::string& buffer, const char* fmt, ...)
* for each Volume.
*/
uint32_t write_findex(RestoreBootstrapRecordFileIndex* fi,
int32_t FirstIndex,
int32_t LastIndex,
int32_t& FirstIndex,
int32_t& LastIndex,
std::string& buffer)
{
auto count = uint32_t{0};
auto bsrItems = std::string{};

std::optional<int32_t> actual_first;
std::optional<int32_t> actual_last;

for (auto& range : fi->GetRanges()) {
auto first = range.first;
auto last = range.second;
Expand All @@ -170,6 +173,9 @@ uint32_t write_findex(RestoreBootstrapRecordFileIndex* fi,
first = std::max(first, FirstIndex);
last = std::min(last, LastIndex);

if (!actual_first.has_value()) { actual_first = first; }
actual_last = last;

if (first == last) {
bsrItems += "FileIndex=" + std::to_string(first) + "\n";
count++;
Expand All @@ -181,6 +187,15 @@ uint32_t write_findex(RestoreBootstrapRecordFileIndex* fi,
}
}
buffer += bsrItems.c_str();

if (actual_first.has_value()) {
// if we did not restore anything
// we should just leave the first/last index alone!
ASSERT(actual_last.has_value());
FirstIndex = *actual_first;
LastIndex = *actual_last;
}

return count;
}

Expand Down Expand Up @@ -379,7 +394,7 @@ static uint32_t write_bsr_item(RestoreBootstrapRecord* bsr,
RestoreContext& rx,
std::string& buffer,
bool& first,
uint32_t& LastIndex)
int32_t& LastIndex)
{
int i;
char ed1[50], ed2[50];
Expand Down Expand Up @@ -423,18 +438,19 @@ static uint32_t write_bsr_item(RestoreBootstrapRecord* bsr,
edit_uint64(bsr->VolParams[i].StartAddr, ed1),
edit_uint64(bsr->VolParams[i].EndAddr, ed2));

count = write_findex(bsr->fi.get(), bsr->VolParams[i].FirstIndex,
bsr->VolParams[i].LastIndex, buffer);
int32_t start = bsr->VolParams[i].FirstIndex;
int32_t end = bsr->VolParams[i].LastIndex;
count = write_findex(bsr->fi.get(), start, end, buffer);
if (count) { PrintBsrItem(buffer, "Count=%u\n", count); }

total_count += count;

/* If the same file is present on two tapes or in two files
* on a tape, it is a continuation, and should not be treated
* twice in the totals. */
if (!first && LastIndex == bsr->VolParams[i].FirstIndex) { total_count--; }
if (!first && count > 0 && LastIndex == start) { total_count--; }
first = false;
LastIndex = bsr->VolParams[i].LastIndex;
LastIndex = end;
}

return total_count;
Expand All @@ -452,7 +468,7 @@ static uint32_t write_bsr_item(RestoreBootstrapRecord* bsr,
uint32_t WriteBsr(UaContext* ua, RestoreContext& rx, std::string& buffer)
{
bool first = true;
uint32_t LastIndex = 0;
int32_t LastIndex = 0;
uint32_t total_count = 0;
const char* p;
JobId_t JobId;
Expand Down
6 changes: 3 additions & 3 deletions core/src/dird/bsr.h
Expand Up @@ -2,7 +2,7 @@
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2002-2010 Free Software Foundation Europe e.V.
Copyright (C) 2013-2019 Bareos GmbH & Co. KG
Copyright (C) 2013-2023 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
Expand Down Expand Up @@ -102,8 +102,8 @@ bool SendBootstrapFile(JobControlRecord* jcr,
bootstrap_info& info);
void CloseBootstrapFile(bootstrap_info& info);
uint32_t write_findex(RestoreBootstrapRecordFileIndex* fi,
int32_t FirstIndex,
int32_t LastIndex,
int32_t& FirstIndex,
int32_t& LastIndex,
std::string& buffer);

} /* namespace directordaemon */
Expand Down
8 changes: 5 additions & 3 deletions core/src/tests/test_fileindex_list.cc
@@ -1,7 +1,7 @@
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2019-2022 Bareos GmbH & Co. KG
Copyright (C) 2019-2023 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
Expand Down Expand Up @@ -118,8 +118,10 @@ static std::string ToBsrStringBareos(const itBegin& t_begin, const itEnd& t_end)
});
auto maxId = *std::max_element(t_begin, t_end);
auto buffer = std::string{};
TimedLambda("write_findex total",
[&]() { write_findex(bsr.fi.get(), 1, maxId, buffer); });
auto first_possible_file_index = 1;
TimedLambda("write_findex total", [&]() {
write_findex(bsr.fi.get(), first_possible_file_index, maxId, buffer);
});
return std::string{buffer.c_str()};
}

Expand Down

0 comments on commit 4dc2c58

Please sign in to comment.