Skip to content

Commit

Permalink
bsr: use unsigned integers when refering to file indices
Browse files Browse the repository at this point in the history
This is done because bsr->VolParams saves them as unsigned anyways.
  • Loading branch information
sebsura authored and BareosBot committed Aug 10, 2023
1 parent 10c2016 commit 4f2cb11
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
40 changes: 24 additions & 16 deletions core/src/dird/bsr.cc
Expand Up @@ -154,22 +154,31 @@ static void PrintBsrItem(std::string& buffer, const char* fmt, ...)
* for each Volume.
*/
uint32_t write_findex(RestoreBootstrapRecordFileIndex* fi,
int32_t& FirstIndex,
int32_t& LastIndex,
uint32_t& FirstIndex,
uint32_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;
struct minmax {
uint32_t min, max;
};
std::optional<minmax> min_max_index;

for (auto& range : fi->GetRanges()) {
auto first = std::max(range.first, FirstIndex);
auto last = std::min(range.second, LastIndex);
ASSERT(range.first >= 0);
ASSERT(range.second >= 0);
auto first = std::max(static_cast<uint32_t>(range.first), FirstIndex);
auto last = std::min(static_cast<uint32_t>(range.second), LastIndex);
if (first <= last) {
if (!actual_first.has_value()) { actual_first = first; }
actual_last = last;
if (!min_max_index.has_value()) {
min_max_index = {first, last};
} else {
// the ranges returned by GetRanges() are sorted!
ASSERT(last > min_max_index->max);
min_max_index->max = last;
}

if (first == last) {
bsrItems += "FileIndex=" + std::to_string(first) + "\n";
Expand All @@ -183,12 +192,11 @@ uint32_t write_findex(RestoreBootstrapRecordFileIndex* fi,
}
buffer += bsrItems.c_str();

if (actual_first.has_value()) {
if (min_max_index) {
// 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;
FirstIndex = min_max_index->min;
LastIndex = min_max_index->max;
}

return count;
Expand Down Expand Up @@ -383,7 +391,7 @@ static uint32_t write_bsr_item(RestoreBootstrapRecord* bsr,
RestoreContext& rx,
std::string& buffer,
bool& first,
int32_t& LastIndex)
uint32_t& LastIndex)
{
int i;
char ed1[50], ed2[50];
Expand Down Expand Up @@ -427,8 +435,8 @@ static uint32_t write_bsr_item(RestoreBootstrapRecord* bsr,
edit_uint64(bsr->VolParams[i].StartAddr, ed1),
edit_uint64(bsr->VolParams[i].EndAddr, ed2));

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

Expand Down Expand Up @@ -457,7 +465,7 @@ static uint32_t write_bsr_item(RestoreBootstrapRecord* bsr,
uint32_t WriteBsr(UaContext* ua, RestoreContext& rx, std::string& buffer)
{
bool first = true;
int32_t LastIndex = 0;
uint32_t LastIndex = 0;
uint32_t total_count = 0;
const char* p;
JobId_t JobId;
Expand Down
4 changes: 2 additions & 2 deletions core/src/dird/bsr.h
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,
uint32_t& FirstIndex,
uint32_t& LastIndex,
std::string& buffer);

} /* namespace directordaemon */
Expand Down
4 changes: 2 additions & 2 deletions core/src/tests/test_fileindex_list.cc
Expand Up @@ -116,9 +116,9 @@ static std::string ToBsrStringBareos(const itBegin& t_begin, const itEnd& t_end)
std::for_each(t_begin, t_end,
[&](int fid) { AddFindex(&bsr, kJobId_1, fid); });
});
auto maxId = *std::max_element(t_begin, t_end);
uint32_t maxId = *std::max_element(t_begin, t_end);
auto buffer = std::string{};
auto first_possible_file_index = 1;
uint32_t first_possible_file_index = 1;
TimedLambda("write_findex total", [&]() {
write_findex(bsr.fi.get(), first_possible_file_index, maxId, buffer);
});
Expand Down

0 comments on commit 4f2cb11

Please sign in to comment.