bzip2.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Bzip2 method - Take a file URI in and decompress it into the target
  5. file.
  6. While the method is named "bzip2" it handles also other compression
  7. types as it calls binaries based on the name of the method,
  8. so it can also be used to handle gzip, lzma and others if named
  9. correctly.
  10. ##################################################################### */
  11. /*}}}*/
  12. // Include Files /*{{{*/
  13. #include <config.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/error.h>
  16. #include <apt-pkg/acquire-method.h>
  17. #include <apt-pkg/strutl.h>
  18. #include <apt-pkg/hashes.h>
  19. #include <apt-pkg/configuration.h>
  20. #include <sys/stat.h>
  21. #include <unistd.h>
  22. #include <utime.h>
  23. #include <stdio.h>
  24. #include <errno.h>
  25. #include <apti18n.h>
  26. /*}}}*/
  27. const char *Prog;
  28. class Bzip2Method : public pkgAcqMethod
  29. {
  30. virtual bool Fetch(FetchItem *Itm);
  31. public:
  32. Bzip2Method() : pkgAcqMethod("1.1",SingleInstance | SendConfig) {};
  33. };
  34. // Bzip2Method::Fetch - Decompress the passed URI /*{{{*/
  35. // ---------------------------------------------------------------------
  36. /* */
  37. bool Bzip2Method::Fetch(FetchItem *Itm)
  38. {
  39. URI Get = Itm->Uri;
  40. std::string Path = Get.Host + Get.Path; // To account for relative paths
  41. std::string GzPathOption = "Dir::bin::" + std::string(Prog);
  42. FetchResult Res;
  43. Res.Filename = Itm->DestFile;
  44. URIStart(Res);
  45. // Open the source and destination files
  46. FileFd From(Path,FileFd::ReadOnly);
  47. if(From.FileSize() == 0)
  48. return _error->Error(_("Empty files can't be valid archives"));
  49. int GzOut[2];
  50. if (pipe(GzOut) < 0)
  51. return _error->Errno("pipe",_("Couldn't open pipe for %s"),Prog);
  52. // Fork bzip2
  53. pid_t Process = ExecFork();
  54. if (Process == 0)
  55. {
  56. close(GzOut[0]);
  57. dup2(From.Fd(),STDIN_FILENO);
  58. dup2(GzOut[1],STDOUT_FILENO);
  59. From.Close();
  60. close(GzOut[1]);
  61. SetCloseExec(STDIN_FILENO,false);
  62. SetCloseExec(STDOUT_FILENO,false);
  63. const char *Args[3];
  64. std::string Tmp = _config->Find(GzPathOption,Prog);
  65. Args[0] = Tmp.c_str();
  66. Args[1] = "-d";
  67. Args[2] = 0;
  68. execvp(Args[0],(char **)Args);
  69. _exit(100);
  70. }
  71. From.Close();
  72. close(GzOut[1]);
  73. FileFd FromGz(GzOut[0]); // For autoclose
  74. FileFd To(Itm->DestFile,FileFd::WriteAtomic);
  75. To.EraseOnFailure();
  76. if (_error->PendingError() == true)
  77. return false;
  78. // Read data from bzip2, generate checksums and write
  79. Hashes Hash;
  80. bool Failed = false;
  81. while (1)
  82. {
  83. unsigned char Buffer[4*1024];
  84. ssize_t Count = read(GzOut[0],Buffer,sizeof(Buffer));
  85. if (Count < 0 && errno == EINTR)
  86. continue;
  87. if (Count < 0)
  88. {
  89. _error->Errno("read", _("Read error from %s process"),Prog);
  90. Failed = true;
  91. break;
  92. }
  93. if (Count == 0)
  94. break;
  95. Hash.Add(Buffer,Count);
  96. if (To.Write(Buffer,Count) == false)
  97. {
  98. Failed = true;
  99. FromGz.Close();
  100. break;
  101. }
  102. }
  103. // Wait for bzip2 to finish
  104. if (ExecWait(Process,_config->Find(GzPathOption,Prog).c_str(),false) == false)
  105. {
  106. To.OpFail();
  107. return false;
  108. }
  109. To.Close();
  110. if (Failed == true)
  111. return false;
  112. // Transfer the modification times
  113. struct stat Buf;
  114. if (stat(Path.c_str(),&Buf) != 0)
  115. return _error->Errno("stat",_("Failed to stat"));
  116. struct utimbuf TimeBuf;
  117. TimeBuf.actime = Buf.st_atime;
  118. TimeBuf.modtime = Buf.st_mtime;
  119. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  120. return _error->Errno("utime",_("Failed to set modification time"));
  121. if (stat(Itm->DestFile.c_str(),&Buf) != 0)
  122. return _error->Errno("stat",_("Failed to stat"));
  123. // Return a Done response
  124. Res.LastModified = Buf.st_mtime;
  125. Res.Size = Buf.st_size;
  126. Res.TakeHashes(Hash);
  127. URIDone(Res);
  128. return true;
  129. }
  130. /*}}}*/
  131. int main(int argc, char *argv[])
  132. {
  133. setlocale(LC_ALL, "");
  134. Bzip2Method Mth;
  135. Prog = strrchr(argv[0],'/');
  136. Prog++;
  137. return Mth.Run();
  138. }