aptmethod.h 2.1 KB

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