gzip.cc 2.7 KB

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