gzip.cc 3.7 KB

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