gzip.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: gzip.cc,v 1.12 2001/03/06 07:15:29 jgg 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 <apt-pkg/fileutl.h>
  11. #include <apt-pkg/error.h>
  12. #include <apt-pkg/acquire-method.h>
  13. #include <apt-pkg/strutl.h>
  14. #include <apt-pkg/hashes.h>
  15. #include <sys/stat.h>
  16. #include <unistd.h>
  17. #include <utime.h>
  18. #include <stdio.h>
  19. #include <errno.h>
  20. /*}}}*/
  21. class GzipMethod : public pkgAcqMethod
  22. {
  23. virtual bool Fetch(FetchItem *Itm);
  24. public:
  25. GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
  26. };
  27. // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
  28. // ---------------------------------------------------------------------
  29. /* */
  30. bool GzipMethod::Fetch(FetchItem *Itm)
  31. {
  32. URI Get = Itm->Uri;
  33. string Path = Get.Host + Get.Path; // To account for relative paths
  34. FetchResult Res;
  35. Res.Filename = Itm->DestFile;
  36. URIStart(Res);
  37. // Open the source and destination files
  38. FileFd From(Path,FileFd::ReadOnly);
  39. int GzOut[2];
  40. if (pipe(GzOut) < 0)
  41. return _error->Errno("pipe","Couldn't open pipe for gzip");
  42. // Fork gzip
  43. int Process = ExecFork();
  44. if (Process == 0)
  45. {
  46. close(GzOut[0]);
  47. dup2(From.Fd(),STDIN_FILENO);
  48. dup2(GzOut[1],STDOUT_FILENO);
  49. From.Close();
  50. close(GzOut[1]);
  51. SetCloseExec(STDIN_FILENO,false);
  52. SetCloseExec(STDOUT_FILENO,false);
  53. const char *Args[3];
  54. Args[0] = _config->Find("Dir::bin::gzip","gzip").c_str();
  55. Args[1] = "-d";
  56. Args[2] = 0;
  57. execvp(Args[0],(char **)Args);
  58. _exit(100);
  59. }
  60. From.Close();
  61. close(GzOut[1]);
  62. FileFd FromGz(GzOut[0]); // For autoclose
  63. FileFd To(Itm->DestFile,FileFd::WriteEmpty);
  64. To.EraseOnFailure();
  65. if (_error->PendingError() == true)
  66. return false;
  67. // Read data from gzip, generate checksums and write
  68. Hashes Hash;
  69. bool Failed = false;
  70. while (1)
  71. {
  72. unsigned char Buffer[4*1024];
  73. unsigned long Count;
  74. Count = read(GzOut[0],Buffer,sizeof(Buffer));
  75. if (Count < 0 && errno == EINTR)
  76. continue;
  77. if (Count < 0)
  78. {
  79. _error->Errno("read", "Read error from gzip process");
  80. Failed = true;
  81. break;
  82. }
  83. if (Count == 0)
  84. break;
  85. Hash.Add(Buffer,Count);
  86. To.Write(Buffer,Count);
  87. }
  88. // Wait for gzip to finish
  89. if (ExecWait(Process,_config->Find("Dir::bin::gzip","gzip").c_str(),false) == false)
  90. {
  91. To.OpFail();
  92. return false;
  93. }
  94. To.Close();
  95. if (Failed == true)
  96. return false;
  97. // Transfer the modification times
  98. struct stat Buf;
  99. if (stat(Path.c_str(),&Buf) != 0)
  100. return _error->Errno("stat","Failed to stat");
  101. struct utimbuf TimeBuf;
  102. TimeBuf.actime = Buf.st_atime;
  103. TimeBuf.modtime = Buf.st_mtime;
  104. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  105. return _error->Errno("utime","Failed to set modification time");
  106. if (stat(Itm->DestFile.c_str(),&Buf) != 0)
  107. return _error->Errno("stat","Failed to stat");
  108. // Return a Done response
  109. Res.LastModified = Buf.st_mtime;
  110. Res.Size = Buf.st_size;
  111. Res.MD5Sum = Hash.MD5.Result();
  112. URIDone(Res);
  113. return true;
  114. }
  115. /*}}}*/
  116. int main()
  117. {
  118. GzipMethod Mth;
  119. return Mth.Run();
  120. }