aptmethod.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 <locale>
  7. #include <string>
  8. #include <sys/time.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <unistd.h>
  12. #include <apti18n.h>
  13. class aptMethod : public pkgAcqMethod
  14. {
  15. protected:
  16. std::string Binary;
  17. public:
  18. virtual bool Configuration(std::string Message) APT_OVERRIDE
  19. {
  20. if (pkgAcqMethod::Configuration(Message) == false)
  21. return false;
  22. std::string const conf = std::string("Binary::") + Binary;
  23. _config->MoveSubTree(conf.c_str(), NULL);
  24. DropPrivsOrDie();
  25. return true;
  26. }
  27. bool CalculateHashes(FetchItem const * const Itm, FetchResult &Res) const
  28. {
  29. Hashes Hash(Itm->ExpectedHashes);
  30. FileFd Fd;
  31. if (Fd.Open(Res.Filename, FileFd::ReadOnly) == false || Hash.AddFD(Fd) == false)
  32. return false;
  33. Res.TakeHashes(Hash);
  34. return true;
  35. }
  36. void Warning(const char *Format,...)
  37. {
  38. va_list args;
  39. va_start(args,Format);
  40. PrintStatus("104 Warning", Format, args);
  41. va_end(args);
  42. }
  43. bool TransferModificationTimes(char const * const From, char const * const To, time_t &LastModified)
  44. {
  45. if (strcmp(To, "/dev/null") == 0)
  46. return true;
  47. struct stat Buf2;
  48. if (lstat(To, &Buf2) != 0 || S_ISLNK(Buf2.st_mode))
  49. return true;
  50. struct stat Buf;
  51. if (stat(From, &Buf) != 0)
  52. return _error->Errno("stat",_("Failed to stat"));
  53. // we don't use utimensat here for compatibility reasons: #738567
  54. struct timeval times[2];
  55. times[0].tv_sec = Buf.st_atime;
  56. LastModified = times[1].tv_sec = Buf.st_mtime;
  57. times[0].tv_usec = times[1].tv_usec = 0;
  58. if (utimes(To, times) != 0)
  59. return _error->Errno("utimes",_("Failed to set modification time"));
  60. return true;
  61. }
  62. aptMethod(char const * const Binary, char const * const Ver, unsigned long const Flags) :
  63. pkgAcqMethod(Ver, Flags), Binary(Binary)
  64. {
  65. try {
  66. std::locale::global(std::locale(""));
  67. } catch (...) {
  68. setlocale(LC_ALL, "");
  69. }
  70. }
  71. };
  72. #endif