gzip.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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);
  29. virtual bool Configuration(std::string Message);
  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, To;
  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. To.Open(Itm->DestFile, FileFd::WriteAtomic);
  65. }
  66. else
  67. {
  68. From.Open(Path, FileFd::ReadOnly);
  69. To.Open(Itm->DestFile, FileFd::WriteOnly | FileFd::Create | FileFd::Empty, *compressor);
  70. }
  71. To.EraseOnFailure();
  72. if (From.IsOpen() == false || From.Failed() == true ||
  73. To.IsOpen() == false || To.Failed() == true)
  74. return false;
  75. // Read data from source, generate checksums and write
  76. Hashes Hash(Itm->ExpectedHashes);
  77. bool Failed = false;
  78. while (1)
  79. {
  80. unsigned char Buffer[4*1024];
  81. unsigned long long Count = 0;
  82. if (!From.Read(Buffer,sizeof(Buffer),&Count))
  83. {
  84. To.OpFail();
  85. return false;
  86. }
  87. if (Count == 0)
  88. break;
  89. Hash.Add(Buffer,Count);
  90. if (To.Write(Buffer,Count) == false)
  91. {
  92. Failed = true;
  93. break;
  94. }
  95. }
  96. From.Close();
  97. Res.Size = To.FileSize();
  98. To.Close();
  99. if (Failed == true)
  100. return false;
  101. // Transfer the modification times
  102. struct stat Buf;
  103. if (stat(Path.c_str(),&Buf) != 0)
  104. return _error->Errno("stat",_("Failed to stat"));
  105. struct timeval times[2];
  106. times[0].tv_sec = Buf.st_atime;
  107. Res.LastModified = times[1].tv_sec = Buf.st_mtime;
  108. times[0].tv_usec = times[1].tv_usec = 0;
  109. if (utimes(Itm->DestFile.c_str(), times) != 0)
  110. return _error->Errno("utimes",_("Failed to set modification time"));
  111. // Return a Done response
  112. Res.TakeHashes(Hash);
  113. URIDone(Res);
  114. return true;
  115. }
  116. /*}}}*/
  117. int main(int, char *argv[])
  118. {
  119. setlocale(LC_ALL, "");
  120. Prog = strrchr(argv[0],'/');
  121. ++Prog;
  122. GzipMethod Mth;
  123. return Mth.Run();
  124. }