You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

133 lines
3.2 KiB

  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 <sys/time.h>
  18. #include <unistd.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. Res.Size = To.FileSize();
  79. To.Close();
  80. if (Failed == true)
  81. return false;
  82. // Transfer the modification times
  83. struct stat Buf;
  84. if (stat(Path.c_str(),&Buf) != 0)
  85. return _error->Errno("stat",_("Failed to stat"));
  86. struct timeval times[2];
  87. times[0].tv_sec = Buf.st_atime;
  88. Res.LastModified = times[1].tv_sec = Buf.st_mtime;
  89. times[0].tv_usec = times[1].tv_usec = 0;
  90. if (utimes(Itm->DestFile.c_str(), times) != 0)
  91. return _error->Errno("utimes",_("Failed to set modification time"));
  92. // Return a Done response
  93. Res.TakeHashes(Hash);
  94. URIDone(Res);
  95. return true;
  96. }
  97. /*}}}*/
  98. int main(int argc, char *argv[])
  99. {
  100. setlocale(LC_ALL, "");
  101. Prog = strrchr(argv[0],'/');
  102. ++Prog;
  103. GzipMethod Mth;
  104. return Mth.Run();
  105. }