gzip.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: gzip.cc,v 1.16 2001/05/27 04:29:30 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. string Tmp = _config->Find(GzPathOption,Prog);
  57. Args[0] = Tmp.c_str();
  58. Args[1] = "-d";
  59. Args[2] = 0;
  60. execvp(Args[0],(char **)Args);
  61. _exit(100);
  62. }
  63. From.Close();
  64. close(GzOut[1]);
  65. FileFd FromGz(GzOut[0]); // For autoclose
  66. FileFd To(Itm->DestFile,FileFd::WriteEmpty);
  67. To.EraseOnFailure();
  68. if (_error->PendingError() == true)
  69. return false;
  70. // Read data from gzip, generate checksums and write
  71. Hashes Hash;
  72. bool Failed = false;
  73. while (1)
  74. {
  75. unsigned char Buffer[4*1024];
  76. unsigned long Count;
  77. Count = read(GzOut[0],Buffer,sizeof(Buffer));
  78. if (Count < 0 && errno == EINTR)
  79. continue;
  80. if (Count < 0)
  81. {
  82. _error->Errno("read", "Read error from %s process",Prog);
  83. Failed = true;
  84. break;
  85. }
  86. if (Count == 0)
  87. break;
  88. Hash.Add(Buffer,Count);
  89. if (To.Write(Buffer,Count) == false)
  90. {
  91. Failed = true;
  92. break;
  93. }
  94. }
  95. // Wait for gzip to finish
  96. if (ExecWait(Process,_config->Find(GzPathOption,Prog).c_str(),false) == false)
  97. {
  98. To.OpFail();
  99. return false;
  100. }
  101. To.Close();
  102. if (Failed == true)
  103. return false;
  104. // Transfer the modification times
  105. struct stat Buf;
  106. if (stat(Path.c_str(),&Buf) != 0)
  107. return _error->Errno("stat","Failed to stat");
  108. struct utimbuf TimeBuf;
  109. TimeBuf.actime = Buf.st_atime;
  110. TimeBuf.modtime = Buf.st_mtime;
  111. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  112. return _error->Errno("utime","Failed to set modification time");
  113. if (stat(Itm->DestFile.c_str(),&Buf) != 0)
  114. return _error->Errno("stat","Failed to stat");
  115. // Return a Done response
  116. Res.LastModified = Buf.st_mtime;
  117. Res.Size = Buf.st_size;
  118. Res.TakeHashes(Hash);
  119. URIDone(Res);
  120. return true;
  121. }
  122. /*}}}*/
  123. int main(int argc, char *argv[])
  124. {
  125. GzipMethod Mth;
  126. Prog = strrchr(argv[0],'/');
  127. Prog++;
  128. return Mth.Run();
  129. }