bzip2.cc 4.1 KB

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