bzip2.cc 4.0 KB

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