bzip2.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 the file is empty, just rename it and return
  46. if(From.Size() == 0)
  47. {
  48. rename(Path.c_str(), Itm->DestFile.c_str());
  49. return true;
  50. }
  51. int GzOut[2];
  52. if (pipe(GzOut) < 0)
  53. return _error->Errno("pipe",_("Couldn't open pipe for %s"),Prog);
  54. // Fork bzip2
  55. pid_t Process = ExecFork();
  56. if (Process == 0)
  57. {
  58. close(GzOut[0]);
  59. dup2(From.Fd(),STDIN_FILENO);
  60. dup2(GzOut[1],STDOUT_FILENO);
  61. From.Close();
  62. close(GzOut[1]);
  63. SetCloseExec(STDIN_FILENO,false);
  64. SetCloseExec(STDOUT_FILENO,false);
  65. const char *Args[3];
  66. string Tmp = _config->Find(GzPathOption,Prog);
  67. Args[0] = Tmp.c_str();
  68. Args[1] = "-d";
  69. Args[2] = 0;
  70. execvp(Args[0],(char **)Args);
  71. _exit(100);
  72. }
  73. From.Close();
  74. close(GzOut[1]);
  75. FileFd FromGz(GzOut[0]); // For autoclose
  76. FileFd To(Itm->DestFile,FileFd::WriteEmpty);
  77. To.EraseOnFailure();
  78. if (_error->PendingError() == true)
  79. return false;
  80. // Read data from bzip2, generate checksums and write
  81. Hashes Hash;
  82. bool Failed = false;
  83. while (1)
  84. {
  85. unsigned char Buffer[4*1024];
  86. unsigned long Count;
  87. Count = read(GzOut[0],Buffer,sizeof(Buffer));
  88. if (Count < 0 && errno == EINTR)
  89. continue;
  90. if (Count < 0)
  91. {
  92. _error->Errno("read", _("Read error from %s process"),Prog);
  93. Failed = true;
  94. break;
  95. }
  96. if (Count == 0)
  97. break;
  98. Hash.Add(Buffer,Count);
  99. if (To.Write(Buffer,Count) == false)
  100. {
  101. Failed = true;
  102. FromGz.Close();
  103. break;
  104. }
  105. }
  106. // Wait for bzip2 to finish
  107. if (ExecWait(Process,_config->Find(GzPathOption,Prog).c_str(),false) == false)
  108. {
  109. To.OpFail();
  110. return false;
  111. }
  112. To.Close();
  113. if (Failed == true)
  114. return false;
  115. // Transfer the modification times
  116. struct stat Buf;
  117. if (stat(Path.c_str(),&Buf) != 0)
  118. return _error->Errno("stat",_("Failed to stat"));
  119. struct utimbuf TimeBuf;
  120. TimeBuf.actime = Buf.st_atime;
  121. TimeBuf.modtime = Buf.st_mtime;
  122. if (utime(Itm->DestFile.c_str(),&TimeBuf) != 0)
  123. return _error->Errno("utime",_("Failed to set modification time"));
  124. if (stat(Itm->DestFile.c_str(),&Buf) != 0)
  125. return _error->Errno("stat",_("Failed to stat"));
  126. // Return a Done response
  127. Res.LastModified = Buf.st_mtime;
  128. Res.Size = Buf.st_size;
  129. Res.TakeHashes(Hash);
  130. URIDone(Res);
  131. return true;
  132. }
  133. /*}}}*/
  134. int main(int argc, char *argv[])
  135. {
  136. setlocale(LC_ALL, "");
  137. Bzip2Method Mth;
  138. Prog = strrchr(argv[0],'/');
  139. Prog++;
  140. return Mth.Run();
  141. }