aptmethod.h 4.5 KB

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