You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

172 lines
5.1 KiB

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