]> git.saurik.com Git - apt.git/blame_incremental - methods/aptmethod.h
reorganize server-states resetting in http/https
[apt.git] / methods / aptmethod.h
... / ...
CommitLineData
1#ifndef APT_APTMETHOD_H
2#define APT_APTMETHOD_H
3
4#include <apt-pkg/acquire-method.h>
5#include <apt-pkg/configuration.h>
6#include <apt-pkg/error.h>
7
8#include <algorithm>
9#include <locale>
10#include <string>
11#include <vector>
12
13#include <sys/time.h>
14#include <sys/types.h>
15#include <sys/stat.h>
16#include <unistd.h>
17
18#include <apti18n.h>
19
20static bool hasDoubleColon(std::string const &n)
21{
22 return n.find("::") != std::string::npos;
23}
24
25class aptMethod : public pkgAcqMethod
26{
27protected:
28 std::string const Binary;
29
30public:
31 virtual bool Configuration(std::string Message) APT_OVERRIDE
32 {
33 if (pkgAcqMethod::Configuration(Message) == false)
34 return false;
35
36 std::string const conf = std::string("Binary::") + Binary;
37 _config->MoveSubTree(conf.c_str(), NULL);
38
39 DropPrivsOrDie();
40
41 return true;
42 }
43
44 bool CalculateHashes(FetchItem const * const Itm, FetchResult &Res) const APT_NONNULL(2)
45 {
46 Hashes Hash(Itm->ExpectedHashes);
47 FileFd Fd;
48 if (Fd.Open(Res.Filename, FileFd::ReadOnly) == false || Hash.AddFD(Fd) == false)
49 return false;
50 Res.TakeHashes(Hash);
51 return true;
52 }
53
54 void Warning(const char *Format,...)
55 {
56 va_list args;
57 va_start(args,Format);
58 PrintStatus("104 Warning", Format, args);
59 va_end(args);
60 }
61
62 std::vector<std::string> methodNames;
63 void setPostfixForMethodNames(char const * const postfix) APT_NONNULL(2)
64 {
65 methodNames.erase(std::remove_if(methodNames.begin(), methodNames.end(), hasDoubleColon), methodNames.end());
66 decltype(methodNames) toAdd;
67 for (auto && name: methodNames)
68 toAdd.emplace_back(name + "::" + postfix);
69 std::move(toAdd.begin(), toAdd.end(), std::back_inserter(methodNames));
70 }
71 bool DebugEnabled() const
72 {
73 if (methodNames.empty())
74 return false;
75 auto const sni = std::find_if_not(methodNames.crbegin(), methodNames.crend(), hasDoubleColon);
76 if (unlikely(sni == methodNames.crend()))
77 return false;
78 auto const ln = methodNames[methodNames.size() - 1];
79 // worst case: all three are the same
80 std::string confln, confsn, confpn;
81 strprintf(confln, "Debug::Acquire::%s", ln.c_str());
82 strprintf(confsn, "Debug::Acquire::%s", sni->c_str());
83 auto const pni = sni->substr(0, sni->find('+'));
84 strprintf(confpn, "Debug::Acquire::%s", pni.c_str());
85 return _config->FindB(confln,_config->FindB(confsn, _config->FindB(confpn, false)));
86 }
87 std::string ConfigFind(char const * const postfix, std::string const &defValue) const APT_NONNULL(2)
88 {
89 for (auto && name: methodNames)
90 {
91 std::string conf;
92 strprintf(conf, "Acquire::%s::%s", name.c_str(), postfix);
93 auto const value = _config->Find(conf);
94 if (value.empty() == false)
95 return value;
96 }
97 return defValue;
98 }
99 std::string ConfigFind(std::string const &postfix, std::string const &defValue) const
100 {
101 return ConfigFind(postfix.c_str(), defValue);
102 }
103 bool ConfigFindB(char const * const postfix, bool const defValue) const APT_NONNULL(2)
104 {
105 return StringToBool(ConfigFind(postfix, defValue ? "yes" : "no"), defValue);
106 }
107 int ConfigFindI(char const * const postfix, int const defValue) const APT_NONNULL(2)
108 {
109 char *End;
110 std::string const value = ConfigFind(postfix, "");
111 auto const Res = strtol(value.c_str(), &End, 0);
112 if (value.c_str() == End)
113 return defValue;
114 return Res;
115 }
116
117 bool TransferModificationTimes(char const * const From, char const * const To, time_t &LastModified) APT_NONNULL(2, 3)
118 {
119 if (strcmp(To, "/dev/null") == 0)
120 return true;
121
122 struct stat Buf2;
123 if (lstat(To, &Buf2) != 0 || S_ISLNK(Buf2.st_mode))
124 return true;
125
126 struct stat Buf;
127 if (stat(From, &Buf) != 0)
128 return _error->Errno("stat",_("Failed to stat"));
129
130 // we don't use utimensat here for compatibility reasons: #738567
131 struct timeval times[2];
132 times[0].tv_sec = Buf.st_atime;
133 LastModified = times[1].tv_sec = Buf.st_mtime;
134 times[0].tv_usec = times[1].tv_usec = 0;
135 if (utimes(To, times) != 0)
136 return _error->Errno("utimes",_("Failed to set modification time"));
137 return true;
138 }
139
140 aptMethod(std::string &&Binary, char const * const Ver, unsigned long const Flags) APT_NONNULL(3) :
141 pkgAcqMethod(Ver, Flags), Binary(Binary), methodNames({Binary})
142 {
143 try {
144 std::locale::global(std::locale(""));
145 } catch (...) {
146 setlocale(LC_ALL, "");
147 }
148 }
149};
150
151#endif