gzip.cc 2.7 KB

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