gzip.cc 3.3 KB

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