store.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Store method - Takes a file URI and stores its content (for which it will
  5. calculate the hashes) in the given destination. The input file will be
  6. extracted based on its file extension (or with the given compressor if
  7. called with one of the compatible symlinks) and potentially recompressed
  8. based on the file extension of the destination filename.
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #include <config.h>
  13. #include <apt-pkg/configuration.h>
  14. #include <apt-pkg/acquire-method.h>
  15. #include <apt-pkg/error.h>
  16. #include <apt-pkg/fileutl.h>
  17. #include <apt-pkg/hashes.h>
  18. #include <apt-pkg/strutl.h>
  19. #include <apt-pkg/aptconfiguration.h>
  20. #include "aptmethod.h"
  21. #include <string.h>
  22. #include <sys/stat.h>
  23. #include <sys/time.h>
  24. #include <string>
  25. #include <vector>
  26. #include <apti18n.h>
  27. /*}}}*/
  28. class StoreMethod : public aptMethod
  29. {
  30. std::string const Prog;
  31. virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
  32. public:
  33. explicit StoreMethod(std::string const &pProg) : aptMethod(pProg.c_str(),"1.2",SingleInstance | SendConfig), Prog(pProg) {};
  34. };
  35. static bool OpenFileWithCompressorByName(FileFd &fileFd, std::string const &Filename, unsigned int const Mode, std::string const &Name)
  36. {
  37. if (Name == "store")
  38. return fileFd.Open(Filename, Mode, FileFd::Extension);
  39. std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
  40. std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin();
  41. for (; compressor != compressors.end(); ++compressor)
  42. if (compressor->Name == Name)
  43. break;
  44. if (compressor == compressors.end())
  45. return _error->Error("Extraction of file %s requires unknown compressor %s", Filename.c_str(), Name.c_str());
  46. return fileFd.Open(Filename, Mode, *compressor);
  47. }
  48. /*}}}*/
  49. bool StoreMethod::Fetch(FetchItem *Itm) /*{{{*/
  50. {
  51. URI Get = Itm->Uri;
  52. std::string Path = Get.Host + Get.Path; // To account for relative paths
  53. FetchResult Res;
  54. Res.Filename = Itm->DestFile;
  55. URIStart(Res);
  56. // Open the source and destination files
  57. FileFd From;
  58. if (_config->FindB("Method::Compress", false) == false)
  59. {
  60. if (OpenFileWithCompressorByName(From, Path, FileFd::ReadOnly, Prog) == false)
  61. return false;
  62. if(From.IsCompressed() && From.FileSize() == 0)
  63. return _error->Error(_("Empty files can't be valid archives"));
  64. }
  65. else
  66. From.Open(Path, FileFd::ReadOnly, FileFd::Extension);
  67. if (From.IsOpen() == false || From.Failed() == true)
  68. return false;
  69. FileFd To;
  70. if (Itm->DestFile != "/dev/null" && Itm->DestFile != Path)
  71. {
  72. if (_config->FindB("Method::Compress", false) == false)
  73. To.Open(Itm->DestFile, FileFd::WriteOnly | FileFd::Create | FileFd::Atomic, FileFd::Extension);
  74. else if (OpenFileWithCompressorByName(To, Itm->DestFile, FileFd::WriteOnly | FileFd::Create | FileFd::Empty, Prog) == false)
  75. return false;
  76. if (To.IsOpen() == false || To.Failed() == true)
  77. return false;
  78. To.EraseOnFailure();
  79. }
  80. // Read data from source, generate checksums and write
  81. Hashes Hash(Itm->ExpectedHashes);
  82. bool Failed = false;
  83. Res.Size = 0;
  84. while (1)
  85. {
  86. unsigned char Buffer[4*1024];
  87. unsigned long long Count = 0;
  88. if (!From.Read(Buffer,sizeof(Buffer),&Count))
  89. {
  90. if (To.IsOpen())
  91. To.OpFail();
  92. return false;
  93. }
  94. if (Count == 0)
  95. break;
  96. Res.Size += Count;
  97. Hash.Add(Buffer,Count);
  98. if (To.IsOpen() && To.Write(Buffer,Count) == false)
  99. {
  100. Failed = true;
  101. break;
  102. }
  103. }
  104. From.Close();
  105. To.Close();
  106. if (Failed == true)
  107. return false;
  108. // Transfer the modification times
  109. if (Itm->DestFile != "/dev/null")
  110. {
  111. struct stat Buf;
  112. if (stat(Path.c_str(),&Buf) != 0)
  113. return _error->Errno("stat",_("Failed to stat"));
  114. struct timeval times[2];
  115. times[0].tv_sec = Buf.st_atime;
  116. Res.LastModified = times[1].tv_sec = Buf.st_mtime;
  117. times[0].tv_usec = times[1].tv_usec = 0;
  118. if (utimes(Itm->DestFile.c_str(), times) != 0)
  119. return _error->Errno("utimes",_("Failed to set modification time"));
  120. }
  121. // Return a Done response
  122. Res.TakeHashes(Hash);
  123. URIDone(Res);
  124. return true;
  125. }
  126. /*}}}*/
  127. int main(int, char *argv[])
  128. {
  129. setlocale(LC_ALL, "");
  130. StoreMethod Mth(flNotDir(argv[0]));
  131. return Mth.Run();
  132. }