Skip to content

Commit

Permalink
stored: lower case device type during validation
Browse files Browse the repository at this point in the history
Since other parts of the system are actually using equality
comparision, we should ensure that any validated resource is actually
usable by the rest of the system.  In this case we do this by
lower_case-ing the device type before we start the validation process.
  • Loading branch information
sebsura authored and arogge committed Mar 2, 2023
1 parent 561280c commit 946143a
Showing 1 changed file with 39 additions and 50 deletions.
89 changes: 39 additions & 50 deletions core/src/stored/device_resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2022 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 @@ -273,46 +273,35 @@ void DeviceResource::CreateAndAssignSerialNumber(uint16_t number)
resource_name_ = strdup(tmp_name.c_str());
}

static bool CaseInsensitiveComparision(std::string_view a,
std::string_view b)
{
return std::equal(a.begin(), a.end(),
b.begin(), b.end(),
[](char a, char b) {
return tolower(a) == tolower(b);
});
}

static void WarnOnNonZeroBlockSize(int max_block_size, std::string_view name)
{
if (max_block_size > 0)
{
my_config->AddWarning(fmt::format(
FMT_STRING(
"Device {:s}: Setting 'Maximum Block Size' is only supported on "
"tape devices"),
name));
}
if (max_block_size > 0) {
my_config->AddWarning(fmt::format(
FMT_STRING(
"Device {:s}: Setting 'Maximum Block Size' is only supported on "
"tape devices"),
name));
}
}

static void WarnOnZeroMaxConcurrentJobs(int max_concurrent_jobs, std::string_view name)
static void WarnOnZeroMaxConcurrentJobs(int max_concurrent_jobs,
std::string_view name)
{
if (max_concurrent_jobs == 0)
{
my_config->AddWarning(fmt::format(
FMT_STRING("Device {:s}: unlimited (0) 'Maximum Concurrent Jobs' (the "
"default) reduces the restore peformance."),
name));
my_config->AddWarning(fmt::format(
FMT_STRING("Device {:s}: the default value for 'Maximum Concurrent "
"Jobs' will change from 0 (unlimited) to 1 in Bareos 23."),
name));
}
if (max_concurrent_jobs == 0) {
my_config->AddWarning(fmt::format(
FMT_STRING("Device {:s}: unlimited (0) 'Maximum Concurrent Jobs' (the "
"default) reduces the restore peformance."),
name));
my_config->AddWarning(fmt::format(
FMT_STRING("Device {:s}: the default value for 'Maximum Concurrent "
"Jobs' will change from 0 (unlimited) to 1 in Bareos 23."),
name));
}
}

static void WarnOnGtOneMaxConcurrentJobs(int max_concurrent_jobs, std::string_view name)
static void WarnOnGtOneMaxConcurrentJobs(int max_concurrent_jobs,
std::string_view name)
{

if (max_concurrent_jobs > 1) {
my_config->AddWarning(fmt::format(
FMT_STRING(
Expand All @@ -325,34 +314,34 @@ static void WarnOnGtOneMaxConcurrentJobs(int max_concurrent_jobs, std::string_vi

static bool ValidateTapeDevice(const DeviceResource& resource)
{
ASSERT(CaseInsensitiveComparision(resource.device_type, DeviceType::B_TAPE_DEV))
ASSERT(resource.device_type == DeviceType::B_TAPE_DEV);

WarnOnZeroMaxConcurrentJobs(resource.max_concurrent_jobs,
resource.resource_name_);
WarnOnZeroMaxConcurrentJobs(resource.max_concurrent_jobs,
resource.resource_name_);

return true;
return true;
}

static bool ValidateGenericDevice(const DeviceResource& resource)
{
WarnOnNonZeroBlockSize(resource.max_block_size, resource.resource_name_);
WarnOnZeroMaxConcurrentJobs(resource.max_concurrent_jobs, resource.resource_name_);
WarnOnGtOneMaxConcurrentJobs(resource.max_concurrent_jobs, resource.resource_name_);
return true;
WarnOnNonZeroBlockSize(resource.max_block_size, resource.resource_name_);
WarnOnZeroMaxConcurrentJobs(resource.max_concurrent_jobs,
resource.resource_name_);
WarnOnGtOneMaxConcurrentJobs(resource.max_concurrent_jobs,
resource.resource_name_);
return true;
}


bool DeviceResource::Validate()
{
//if (CaseInsensitiveComparision(device_type, DeviceType::B_TAPE_DEV))
if (CaseInsensitiveComparision(device_type, DeviceType::B_TAPE_DEV))
{
return ValidateTapeDevice(*this);
} else
{
return ValidateGenericDevice(*this);
}
return true;
to_lower(device_type);
if (device_type == DeviceType::B_TAPE_DEV) {
return ValidateTapeDevice(*this);
} else {
return ValidateGenericDevice(*this);
}
return true;
}

} /* namespace storagedaemon */

0 comments on commit 946143a

Please sign in to comment.