gzip.cc 3.3 KB

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