gzip.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // if the file is empty, just rename it and return
  43. if(From.Size() == 0)
  44. {
  45. rename(Path.c_str(), Itm->DestFile.c_str());
  46. return true;
  47. }
  48. int GzOut[2];
  49. if (pipe(GzOut) < 0)
  50. return _error->Errno("pipe",_("Couldn't open pipe for %s"),Prog);
  51. // Fork gzip
  52. pid_t Process = ExecFork();
  53. if (Process == 0)
  54. {
  55. close(GzOut[0]);
  56. dup2(From.Fd(),STDIN_FILENO);
  57. dup2(GzOut[1],STDOUT_FILENO);
  58. From.Close();
  59. close(GzOut[1]);
  60. SetCloseExec(STDIN_FILENO,false);
  61. SetCloseExec(STDOUT_FILENO,false);
  62. const char *Args[3];
  63. string Tmp = _config->Find(GzPathOption,Prog);
  64. Args[0] = Tmp.c_str();
  65. Args[1] = "-d";
  66. Args[2] = 0;
  67. execvp(Args[0],(char **)Args);
  68. _exit(100);
  69. }
  70. From.Close();
  71. close(GzOut[1]);
  72. FileFd FromGz(GzOut[0]); // For autoclose
  73. FileFd To(Itm->DestFile,FileFd::WriteEmpty);
  74. To.EraseOnFailure();
  75. if (_error->PendingError() == true)
  76. return false;
  77. // Read data from gzip, generate checksums and write
  78. Hashes Hash;
  79. bool Failed = false;
  80. while (1)
  81. {
  82. unsigned char Buffer[4*1024];
  83. unsigned long Count;
  84. Count = read(GzOut[0],Buffer,sizeof(Buffer));
  85. if (Count < 0 && errno == EINTR)
  86. continue;
  87. if (Count < 0)
  88. {
  89. _error->Errno("read", _("Read error from %s process"),Prog);
  90. Failed = true;
  91. break;
  92. }
  93. if (Count == 0)
  94. break;
  95. Hash.Add(Buffer,Count);
  96. if (To.Write(Buffer,Count) == false)
  97. {
  98. Failed = true;
  99. FromGz.Close();
  100. break;
  101. }
  102. }
  103. // Wait for gzip to finish
  104. if (ExecWait(Process,_config->Find(GzPathOption,Prog).c_str(),false) == false)
  105. {
  106. To.OpFail();
  107. return false;
  108. }
  109. To.Close();
  110. if (Failed == true)
  111. return false;
  112. // Transfer the modification times
  113. struct stat Buf;
  114. if (stat(Path.c_str(),&Buf) != 0)
  115. return _error->Errno("stat",_("Failed to stat"));
  116. struct utimbuf TimeBuf;
  117. TimeBuf.actime = Buf.st_atime;
  118. TimeBuf.modtime = Buf.st_mtime;
  119. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  120. return _error->Errno("utime",_("Failed to set modification time"));
  121. if (stat(Itm->DestFile.c_str(),&Buf) != 0)
  122. return _error->Errno("stat",_("Failed to stat"));
  123. // Return a Done response
  124. Res.LastModified = Buf.st_mtime;
  125. Res.Size = Buf.st_size;
  126. Res.TakeHashes(Hash);
  127. URIDone(Res);
  128. return true;
  129. }
  130. /*}}}*/
  131. int main(int argc, char *argv[])
  132. {
  133. setlocale(LC_ALL, "");
  134. GzipMethod Mth;
  135. Prog = strrchr(argv[0],'/');
  136. Prog++;
  137. return Mth.Run();
  138. }