1 /*
2  * Copyright (C) 2018 Red Hat, Inc.
3  *
4  * Licensed under the GNU Lesser General Public License Version 2.1
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 #include "OptionPath.hpp"
22 
23 #include "bgettext/bgettext-lib.h"
24 #include "tinyformat/tinyformat.hpp"
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 
30 namespace libdnf {
31 
removeFileProt(const std::string & value)32 static std::string removeFileProt(const std::string & value)
33 {
34     if (value.compare(0, 7, "file://") == 0)
35         return value.substr(7);
36     return value;
37 }
38 
OptionPath(const std::string & defaultValue,bool exists,bool absPath)39 OptionPath::OptionPath(const std::string & defaultValue, bool exists, bool absPath)
40 : OptionString(defaultValue), exists(exists), absPath(absPath)
41 {
42     this->defaultValue = removeFileProt(this->defaultValue);
43     test(this->defaultValue);
44     this->value = this->defaultValue;
45 }
46 
OptionPath(const char * defaultValue,bool exists,bool absPath)47 OptionPath::OptionPath(const char * defaultValue, bool exists, bool absPath)
48 : OptionString(defaultValue), exists(exists), absPath(absPath)
49 {
50     if (defaultValue) {
51         this->defaultValue = removeFileProt(this->defaultValue);
52         test(this->defaultValue);
53         this->value = this->defaultValue;
54     }
55 }
56 
OptionPath(const std::string & defaultValue,const std::string & regex,bool icase,bool exists,bool absPath)57 OptionPath::OptionPath(const std::string & defaultValue, const std::string & regex, bool icase, bool exists, bool absPath)
58 : OptionString(removeFileProt(defaultValue), regex, icase), exists(exists), absPath(absPath)
59 {
60     this->defaultValue = removeFileProt(this->defaultValue);
61     test(this->defaultValue);
62     this->value = this->defaultValue;
63 }
64 
OptionPath(const char * defaultValue,const std::string & regex,bool icase,bool exists,bool absPath)65 OptionPath::OptionPath(const char * defaultValue, const std::string & regex, bool icase, bool exists, bool absPath)
66 : OptionString(defaultValue, regex, icase), exists(exists), absPath(absPath)
67 {
68     if (defaultValue) {
69         this->defaultValue = removeFileProt(this->defaultValue);
70         test(this->defaultValue);
71         this->value = this->defaultValue;
72     }
73 }
74 
test(const std::string & value) const75 void OptionPath::test(const std::string & value) const
76 {
77     if (absPath && value[0] != '/')
78         throw InvalidValue(tfm::format(_("given path '%s' is not absolute."), value));
79 
80     struct stat buffer;
81     if (exists && stat(value.c_str(), &buffer))
82         throw InvalidValue(tfm::format(_("given path '%s' does not exist."), value));
83 }
84 
set(Priority priority,const std::string & value)85 void OptionPath::set(Priority priority, const std::string & value)
86 {
87     if (priority >= this->priority) {
88         OptionString::test(value);
89         auto val = removeFileProt(value);
90         test(val);
91         this->value = val;
92         this->priority = priority;
93     }
94 }
95 
96 }
97