gzip.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 "aptmethod.h"
  19. #include <string.h>
  20. #include <sys/stat.h>
  21. #include <sys/time.h>
  22. #include <string>
  23. #include <vector>
  24. #include <apti18n.h>
  25. /*}}}*/
  26. class GzipMethod : public aptMethod
  27. {
  28. std::string const Prog;
  29. virtual bool Fetch(FetchItem *Itm) APT_OVERRIDE;
  30. public:
  31. explicit GzipMethod(std::string const &pProg) : aptMethod(pProg.c_str(),"1.1",SingleInstance | SendConfig), Prog(pProg) {};
  32. };
  33. // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* */
  36. bool GzipMethod::Fetch(FetchItem *Itm)
  37. {
  38. URI Get = Itm->Uri;
  39. std::string Path = Get.Host + Get.Path; // To account for relative paths
  40. FetchResult Res;
  41. Res.Filename = Itm->DestFile;
  42. URIStart(Res);
  43. std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
  44. std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin();
  45. for (; compressor != compressors.end(); ++compressor)
  46. if (compressor->Name == Prog)
  47. break;
  48. if (compressor == compressors.end())
  49. return _error->Error("Extraction of file %s requires unknown compressor %s", Path.c_str(), Prog.c_str());
  50. // Open the source and destination files
  51. FileFd From;
  52. if (_config->FindB("Method::Compress", false) == false)
  53. {
  54. From.Open(Path, FileFd::ReadOnly, *compressor);
  55. if(From.FileSize() == 0)
  56. return _error->Error(_("Empty files can't be valid archives"));
  57. }
  58. else
  59. From.Open(Path, FileFd::ReadOnly);
  60. if (From.IsOpen() == false || From.Failed() == true)
  61. return false;
  62. FileFd To;
  63. if (Itm->DestFile != "/dev/null")
  64. {
  65. if (_config->FindB("Method::Compress", false) == false)
  66. To.Open(Itm->DestFile, FileFd::WriteAtomic);
  67. else
  68. To.Open(Itm->DestFile, FileFd::WriteOnly | FileFd::Create | FileFd::Empty, *compressor);
  69. if (To.IsOpen() == false || To.Failed() == true)
  70. return false;
  71. To.EraseOnFailure();
  72. }
  73. // Read data from source, generate checksums and write
  74. Hashes Hash(Itm->ExpectedHashes);
  75. bool Failed = false;
  76. Res.Size = 0;
  77. while (1)
  78. {
  79. unsigned char Buffer[4*1024];
  80. unsigned long long Count = 0;
  81. if (!From.Read(Buffer,sizeof(Buffer),&Count))
  82. {
  83. if (To.IsOpen())
  84. To.OpFail();
  85. return false;
  86. }
  87. if (Count == 0)
  88. break;
  89. Res.Size += Count;
  90. Hash.Add(Buffer,Count);
  91. if (To.IsOpen() && To.Write(Buffer,Count) == false)
  92. {
  93. Failed = true;
  94. break;
  95. }
  96. }
  97. From.Close();
  98. To.Close();
  99. if (Failed == true)
  100. return false;
  101. // Transfer the modification times
  102. if (Itm->DestFile != "/dev/null")
  103. {
  104. struct stat Buf;
  105. if (stat(Path.c_str(),&Buf) != 0)
  106. return _error->Errno("stat",_("Failed to stat"));
  107. struct timeval times[2];
  108. times[0].tv_sec = Buf.st_atime;
  109. Res.LastModified = times[1].tv_sec = Buf.st_mtime;
  110. times[0].tv_usec = times[1].tv_usec = 0;
  111. if (utimes(Itm->DestFile.c_str(), times) != 0)
  112. return _error->Errno("utimes",_("Failed to set modification time"));
  113. }
  114. // Return a Done response
  115. Res.TakeHashes(Hash);
  116. URIDone(Res);
  117. return true;
  118. }
  119. /*}}}*/
  120. int main(int, char *argv[])
  121. {
  122. setlocale(LC_ALL, "");
  123. GzipMethod Mth(flNotDir(argv[0]));
  124. return Mth.Run();
  125. }