aptmethod.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. //return true; //comment this in for more debug info
  61. if (methodNames.empty())
  62. return false;
  63. auto const sni = std::find_if_not(methodNames.crbegin(), methodNames.crend(), hasDoubleColon);
  64. if (unlikely(sni == methodNames.crend()))
  65. return false;
  66. auto const ln = methodNames[methodNames.size() - 1];
  67. // worst case: all three are the same
  68. std::string confln, confsn, confpn;
  69. strprintf(confln, "Debug::Acquire::%s", ln.c_str());
  70. strprintf(confsn, "Debug::Acquire::%s", sni->c_str());
  71. auto const pni = sni->substr(0, sni->find('+'));
  72. strprintf(confpn, "Debug::Acquire::%s", pni.c_str());
  73. return _config->FindB(confln,_config->FindB(confsn, _config->FindB(confpn, false)));
  74. }
  75. std::string ConfigFind(char const * const postfix, std::string const &defValue) const APT_NONNULL(2)
  76. {
  77. for (auto name = methodNames.rbegin(); name != methodNames.rend(); ++name)
  78. {
  79. std::string conf;
  80. strprintf(conf, "Acquire::%s::%s", name->c_str(), postfix);
  81. auto const value = _config->Find(conf);
  82. if (value.empty() == false)
  83. return value;
  84. }
  85. return defValue;
  86. }
  87. std::string ConfigFind(std::string const &postfix, std::string const &defValue) const
  88. {
  89. return ConfigFind(postfix.c_str(), defValue);
  90. }
  91. bool ConfigFindB(char const * const postfix, bool const defValue) const APT_NONNULL(2)
  92. {
  93. return StringToBool(ConfigFind(postfix, defValue ? "yes" : "no"), defValue);
  94. }
  95. int ConfigFindI(char const * const postfix, int const defValue) const APT_NONNULL(2)
  96. {
  97. char *End;
  98. std::string const value = ConfigFind(postfix, "");
  99. auto const Res = strtol(value.c_str(), &End, 0);
  100. if (value.c_str() == End)
  101. return defValue;
  102. return Res;
  103. }
  104. bool TransferModificationTimes(char const * const From, char const * const To, time_t &LastModified) APT_NONNULL(2, 3)
  105. {
  106. if (strcmp(To, "/dev/null") == 0)
  107. return true;
  108. struct stat Buf2;
  109. if (lstat(To, &Buf2) != 0 || S_ISLNK(Buf2.st_mode))
  110. return true;
  111. struct stat Buf;
  112. if (stat(From, &Buf) != 0)
  113. return _error->Errno("stat",_("Failed to stat"));
  114. // we don't use utimensat here for compatibility reasons: #738567
  115. struct timeval times[2];
  116. times[0].tv_sec = Buf.st_atime;
  117. LastModified = times[1].tv_sec = Buf.st_mtime;
  118. times[0].tv_usec = times[1].tv_usec = 0;
  119. if (utimes(To, times) != 0)
  120. return _error->Errno("utimes",_("Failed to set modification time"));
  121. return true;
  122. }
  123. aptMethod(std::string &&Binary, char const * const Ver, unsigned long const Flags) APT_NONNULL(3) :
  124. pkgAcqMethod(Ver, Flags), Binary(Binary), methodNames({Binary})
  125. {
  126. try {
  127. std::locale::global(std::locale(""));
  128. } catch (...) {
  129. setlocale(LC_ALL, "");
  130. }
  131. }
  132. };
  133. #endif