gzip.cc 3.7 KB

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