gzip.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: gzip.cc,v 1.17.2.1 2004/01/16 18:58:50 mdz Exp $
  4. /* ######################################################################
  5. GZip method - Take a file URI in and decompress it into the target
  6. file.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <config.h>
  11. #include <apt-pkg/configuration.h>
  12. #include <apt-pkg/acquire-method.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/hashes.h>
  16. #include <apt-pkg/strutl.h>
  17. #include <apt-pkg/aptconfiguration.h>
  18. #include <string.h>
  19. #include <sys/stat.h>
  20. #include <sys/time.h>
  21. #include <string>
  22. #include <vector>
  23. #include <apti18n.h>
  24. /*}}}*/
  25. const char *Prog;
  26. class GzipMethod : public pkgAcqMethod
  27. {
  28. virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
  29. virtual bool Configuration(std::string Message) APT_OVERRIDE;
  30. public:
  31. GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
  32. };
  33. bool GzipMethod::Configuration(std::string Message)
  34. {
  35. if (pkgAcqMethod::Configuration(Message) == false)
  36. return false;
  37. DropPrivsOrDie();
  38. return true;
  39. }
  40. // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
  41. // ---------------------------------------------------------------------
  42. /* */
  43. bool GzipMethod::Fetch(FetchItem *Itm)
  44. {
  45. URI Get = Itm->Uri;
  46. std::string Path = Get.Host + Get.Path; // To account for relative paths
  47. FetchResult Res;
  48. Res.Filename = Itm->DestFile;
  49. URIStart(Res);
  50. std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
  51. std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin();
  52. for (; compressor != compressors.end(); ++compressor)
  53. if (compressor->Name == Prog)
  54. break;
  55. if (compressor == compressors.end())
  56. return _error->Error("Extraction of file %s requires unknown compressor %s", Path.c_str(), Prog);
  57. // Open the source and destination files
  58. FileFd From;
  59. if (_config->FindB("Method::Compress", false) == false)
  60. {
  61. From.Open(Path, FileFd::ReadOnly, *compressor);
  62. if(From.FileSize() == 0)
  63. return _error->Error(_("Empty files can't be valid archives"));
  64. }
  65. else
  66. From.Open(Path, FileFd::ReadOnly);
  67. if (From.IsOpen() == false || From.Failed() == true)
  68. return false;
  69. FileFd To;
  70. if (Itm->DestFile != "/dev/null")
  71. {
  72. if (_config->FindB("Method::Compress", false) == false)
  73. To.Open(Itm->DestFile, FileFd::WriteAtomic);
  74. else
  75. To.Open(Itm->DestFile, FileFd::WriteOnly | FileFd::Create | FileFd::Empty, *compressor);
  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. Prog = strrchr(argv[0],'/');
  131. ++Prog;
  132. GzipMethod Mth;
  133. return Mth.Run();
  134. }