]>
git.saurik.com Git - apt.git/blob - methods/aptmethod.h
1 #ifndef APT_APTMETHOD_H
2 #define APT_APTMETHOD_H
4 #include <apt-pkg/acquire-method.h>
5 #include <apt-pkg/configuration.h>
6 #include <apt-pkg/error.h>
14 #include <sys/types.h>
20 static bool hasDoubleColon(std::string
const &n
)
22 return n
.find("::") != std::string::npos
;
25 class aptMethod
: public pkgAcqMethod
28 std::string
const Binary
;
31 virtual bool Configuration(std::string Message
) APT_OVERRIDE
33 if (pkgAcqMethod::Configuration(Message
) == false)
36 std::string
const conf
= std::string("Binary::") + Binary
;
37 _config
->MoveSubTree(conf
.c_str(), NULL
);
44 bool CalculateHashes(FetchItem
const * const Itm
, FetchResult
&Res
) const APT_NONNULL(2)
46 Hashes
Hash(Itm
->ExpectedHashes
);
48 if (Fd
.Open(Res
.Filename
, FileFd::ReadOnly
) == false || Hash
.AddFD(Fd
) == false)
54 void Warning(const char *Format
,...)
57 va_start(args
,Format
);
58 PrintStatus("104 Warning", Format
, args
);
62 std::vector
<std::string
> methodNames
;
63 void setPostfixForMethodNames(char const * const postfix
) APT_NONNULL(2)
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
));
71 bool DebugEnabled() const
73 if (methodNames
.empty())
75 auto const sni
= std::find_if_not(methodNames
.crbegin(), methodNames
.crend(), hasDoubleColon
);
76 if (unlikely(sni
== methodNames
.crend()))
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)));
87 std::string
ConfigFind(char const * const postfix
, std::string
const &defValue
) const APT_NONNULL(2)
89 for (auto name
= methodNames
.rbegin(); name
!= methodNames
.rend(); ++name
)
92 strprintf(conf
, "Acquire::%s::%s", name
->c_str(), postfix
);
93 auto const value
= _config
->Find(conf
);
94 if (value
.empty() == false)
99 std::string
ConfigFind(std::string
const &postfix
, std::string
const &defValue
) const
101 return ConfigFind(postfix
.c_str(), defValue
);
103 bool ConfigFindB(char const * const postfix
, bool const defValue
) const APT_NONNULL(2)
105 return StringToBool(ConfigFind(postfix
, defValue
? "yes" : "no"), defValue
);
107 int ConfigFindI(char const * const postfix
, int const defValue
) const APT_NONNULL(2)
110 std::string
const value
= ConfigFind(postfix
, "");
111 auto const Res
= strtol(value
.c_str(), &End
, 0);
112 if (value
.c_str() == End
)
117 bool TransferModificationTimes(char const * const From
, char const * const To
, time_t &LastModified
) APT_NONNULL(2, 3)
119 if (strcmp(To
, "/dev/null") == 0)
123 if (lstat(To
, &Buf2
) != 0 || S_ISLNK(Buf2
.st_mode
))
127 if (stat(From
, &Buf
) != 0)
128 return _error
->Errno("stat",_("Failed to stat"));
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"));
140 aptMethod(std::string
&&Binary
, char const * const Ver
, unsigned long const Flags
) APT_NONNULL(3) :
141 pkgAcqMethod(Ver
, Flags
), Binary(Binary
), methodNames({Binary
})
144 std::locale::global(std::locale(""));
146 setlocale(LC_ALL
, "");