gzip.cc 3.6 KB

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