gzip.cc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: gzip.cc,v 1.11 2001/03/06 03:11:22 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 <sys/stat.h>
  15. #include <unistd.h>
  16. #include <utime.h>
  17. #include <stdio.h>
  18. /*}}}*/
  19. class GzipMethod : public pkgAcqMethod
  20. {
  21. virtual bool Fetch(FetchItem *Itm);
  22. public:
  23. GzipMethod() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
  24. };
  25. // GzipMethod::Fetch - Decompress the passed URI /*{{{*/
  26. // ---------------------------------------------------------------------
  27. /* */
  28. bool GzipMethod::Fetch(FetchItem *Itm)
  29. {
  30. URI Get = Itm->Uri;
  31. string Path = Get.Host + Get.Path; // To account for relative paths
  32. string GzipPath = _config->Find("Dir::bin::gzip","gzip");
  33. FetchResult Res;
  34. Res.Filename = Itm->DestFile;
  35. URIStart(Res);
  36. // Open the source and destintation files
  37. FileFd From(Path,FileFd::ReadOnly);
  38. FileFd To(Itm->DestFile,FileFd::WriteEmpty);
  39. To.EraseOnFailure();
  40. if (_error->PendingError() == true)
  41. return false;
  42. // Fork gzip
  43. int Process = fork();
  44. if (Process < 0)
  45. return _error->Errno("fork",string("Couldn't fork "+GzipPath).c_str());
  46. // The child
  47. if (Process == 0)
  48. {
  49. dup2(From.Fd(),STDIN_FILENO);
  50. dup2(To.Fd(),STDOUT_FILENO);
  51. From.Close();
  52. To.Close();
  53. SetCloseExec(STDIN_FILENO,false);
  54. SetCloseExec(STDOUT_FILENO,false);
  55. const char *Args[3];
  56. Args[0] = GzipPath.c_str();
  57. Args[1] = "-d";
  58. Args[2] = 0;
  59. execvp(Args[0],(char **)Args);
  60. exit(100);
  61. }
  62. From.Close();
  63. // Wait for gzip to finish
  64. if (ExecWait(Process,GzipPath.c_str(),false) == false)
  65. {
  66. To.OpFail();
  67. return false;
  68. }
  69. To.Close();
  70. // Transfer the modification times
  71. struct stat Buf;
  72. if (stat(Path.c_str(),&Buf) != 0)
  73. return _error->Errno("stat","Failed to stat");
  74. struct utimbuf TimeBuf;
  75. TimeBuf.actime = Buf.st_atime;
  76. TimeBuf.modtime = Buf.st_mtime;
  77. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  78. return _error->Errno("utime","Failed to set modification time");
  79. if (stat(Itm->DestFile.c_str(),&Buf) != 0)
  80. return _error->Errno("stat","Failed to stat");
  81. // Return a Done response
  82. Res.LastModified = Buf.st_mtime;
  83. Res.Size = Buf.st_size;
  84. URIDone(Res);
  85. return true;
  86. }
  87. /*}}}*/
  88. int main()
  89. {
  90. GzipMethod Mth;
  91. return Mth.Run();
  92. }