gzip.cc 2.8 KB

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