gzip.cc 3.8 KB

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