gzip.cc 2.7 KB

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