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
|
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: LGPL-2.1-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
//
// Libdnf 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with libdnf. If not, see <https://www.gnu.org/licenses/>.
#include "libdnf5/conf/option_path.hpp"
#include "option_string_private.hpp"
#include "libdnf5/utils/bgettext/bgettext-mark-domain.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
namespace libdnf5 {
class OptionPath::Impl {
public:
Impl(bool exists, bool abs_path) : exists(exists), abs_path(abs_path) {};
private:
friend OptionPath;
bool exists;
bool abs_path;
};
static std::string remove_file_prot(const std::string & value) {
const int prefix_len = 7;
if (value.compare(0, prefix_len, "file://") == 0) {
return value.substr(prefix_len);
}
return value;
}
OptionPath::OptionPath(std::string default_value, bool exists, bool abs_path)
: OptionString(default_value),
p_impl(new Impl(exists, abs_path)) {
OptionString::p_impl->default_value = remove_file_prot(this->get_default_value());
test(this->get_default_value());
OptionString::p_impl->value = this->get_default_value();
}
OptionPath::OptionPath(const char * default_value, bool exists, bool abs_path)
: OptionString(default_value),
p_impl(new Impl(exists, abs_path)) {
if (default_value) {
OptionString::p_impl->default_value = remove_file_prot(this->get_default_value());
test(this->get_default_value());
OptionString::p_impl->value = this->get_default_value();
}
}
OptionPath::OptionPath(std::string default_value, std::string regex, bool icase, bool exists, bool abs_path)
: OptionString(remove_file_prot(default_value), regex, icase),
p_impl(new Impl(exists, abs_path)) {
OptionString::p_impl->default_value = remove_file_prot(this->get_default_value());
test(this->get_default_value());
OptionString::p_impl->value = this->get_default_value();
}
OptionPath::OptionPath(const char * default_value, std::string regex, bool icase, bool exists, bool abs_path)
: OptionString(default_value, regex, icase),
p_impl(new Impl(exists, abs_path)) {
if (default_value) {
OptionString::p_impl->default_value = remove_file_prot(this->get_default_value());
test(this->get_default_value());
OptionString::p_impl->value = this->get_default_value();
}
}
OptionPath::~OptionPath() = default;
OptionPath::OptionPath(const OptionPath & src) = default;
void OptionPath::test(const std::string & value) const {
if (p_impl->abs_path && value[0] != '/') {
throw OptionValueNotAllowedError(M_("Only absolute paths allowed, relative path \"{}\" detected"), value);
}
struct stat buffer;
if (p_impl->exists && stat(value.c_str(), &buffer) != 0) {
throw OptionPathNotFoundError(M_("Path \"{}\" does not exist"), value);
}
}
void OptionPath::set(Priority priority, const std::string & value) {
assert_not_locked();
if (priority >= get_priority()) {
OptionString::test(value);
auto val = remove_file_prot(value);
test(val);
OptionString::p_impl->value = val;
set_priority(priority);
}
}
void OptionPath::set(const std::string & value) {
set(Priority::RUNTIME, value);
}
OptionPath * OptionPath::OptionPath::clone() const {
return new OptionPath(*this);
}
} // namespace libdnf5
|