gzip.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: gzip.cc,v 1.10 2000/03/18 07:39:33 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. string Path = Get.Host + Get.Path; // To account for relative paths
  32. FetchResult Res;
  33. Res.Filename = Itm->DestFile;
  34. URIStart(Res);
  35. // Open the source and destintation files
  36. FileFd From(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. if (ExecWait(Process,_config->Find("Dir::bin::gzip","gzip").c_str(),false) == false)
  64. {
  65. To.OpFail();
  66. return false;
  67. }
  68. To.Close();
  69. // Transfer the modification times
  70. struct stat Buf;
  71. if (stat(Path.c_str(),&Buf) != 0)
  72. return _error->Errno("stat","Failed to stat");
  73. struct utimbuf TimeBuf;
  74. TimeBuf.actime = Buf.st_atime;
  75. TimeBuf.modtime = Buf.st_mtime;
  76. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  77. return _error->Errno("utime","Failed to set modification time");
  78. if (stat(Itm->DestFile.c_str(),&Buf) != 0)
  79. return _error->Errno("stat","Failed to stat");
  80. // Return a Done response
  81. Res.LastModified = Buf.st_mtime;
  82. Res.Size = Buf.st_size;
  83. URIDone(Res);
  84. return true;
  85. }
  86. /*}}}*/
  87. int main()
  88. {
  89. GzipMethod Mth;
  90. return Mth.Run();
  91. }