gzip.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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/fileutl.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/acquire-method.h>
  14. #include <apt-pkg/strutl.h>
  15. #include <apt-pkg/hashes.h>
  16. #include <sys/stat.h>
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <errno.h>
  20. #include <apti18n.h>
  21. /*}}}*/
  22. const char *Prog;
  23. class GzipMethod : public pkgAcqMethod
  24. {
  25. virtual bool Fetch(FetchItem *Itm);
  26. public:
  27. GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
  28. };
  29. // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
  30. // ---------------------------------------------------------------------
  31. /* */
  32. bool GzipMethod::Fetch(FetchItem *Itm)
  33. {
  34. URI Get = Itm->Uri;
  35. std::string Path = Get.Host + Get.Path; // To account for relative paths
  36. FetchResult Res;
  37. Res.Filename = Itm->DestFile;
  38. URIStart(Res);
  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 == Prog)
  43. break;
  44. if (compressor == compressors.end())
  45. return _error->Error("Extraction of file %s requires unknown compressor %s", Path.c_str(), Prog);
  46. // Open the source and destination files
  47. FileFd From;
  48. From.Open(Path, FileFd::ReadOnly, *compressor);
  49. if(From.FileSize() == 0)
  50. return _error->Error(_("Empty files can't be valid archives"));
  51. FileFd To(Itm->DestFile,FileFd::WriteAtomic);
  52. To.EraseOnFailure();
  53. if (_error->PendingError() == true)
  54. return false;
  55. // Read data from source, generate checksums and write
  56. Hashes Hash;
  57. bool Failed = false;
  58. while (1)
  59. {
  60. unsigned char Buffer[4*1024];
  61. unsigned long long Count = 0;
  62. if (!From.Read(Buffer,sizeof(Buffer),&Count))
  63. {
  64. To.OpFail();
  65. return false;
  66. }
  67. if (Count == 0)
  68. break;
  69. Hash.Add(Buffer,Count);
  70. if (To.Write(Buffer,Count) == false)
  71. {
  72. Failed = true;
  73. break;
  74. }
  75. }
  76. From.Close();
  77. if (Failed == true)
  78. return false;
  79. // Transfer the modification times
  80. struct stat Buf;
  81. if (stat(Path.c_str(),&Buf) != 0)
  82. return _error->Errno("stat",_("Failed to stat"));
  83. struct timespec times[2];
  84. times[0].tv_sec = Buf.st_atime;
  85. times[1].tv_sec = Buf.st_mtime;
  86. times[0].tv_nsec = times[1].tv_nsec = 0;
  87. if (futimens(To.Fd(), times) != 0)
  88. {
  89. To.OpFail();
  90. return _error->Errno("futimens",_("Failed to set modification time"));
  91. }
  92. Res.Size = To.FileSize();
  93. To.Close();
  94. if (stat(Itm->DestFile.c_str(),&Buf) != 0)
  95. return _error->Errno("stat",_("Failed to stat"));
  96. // Return a Done response
  97. Res.LastModified = Buf.st_mtime;
  98. Res.TakeHashes(Hash);
  99. URIDone(Res);
  100. return true;
  101. }
  102. /*}}}*/
  103. int main(int argc, char *argv[])
  104. {
  105. setlocale(LC_ALL, "");
  106. Prog = strrchr(argv[0],'/');
  107. ++Prog;
  108. GzipMethod Mth;
  109. return Mth.Run();
  110. }