extracttar.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: extracttar.cc,v 1.8.2.1 2004/01/16 18:58:50 mdz Exp $
  4. /* ######################################################################
  5. Extract a Tar - Tar Extractor
  6. Some performance measurements showed that zlib performed quite poorly
  7. in comparison to a forked gzip process. This tar extractor makes use
  8. of the fact that dup'd file descriptors have the same seek pointer
  9. and that gzip will not read past the end of a compressed stream,
  10. even if there is more data. We use the dup property to track extraction
  11. progress and the gzip feature to just feed gzip a fd in the middle
  12. of an AR file.
  13. ##################################################################### */
  14. /*}}}*/
  15. // Include Files /*{{{*/
  16. #include<config.h>
  17. #include <apt-pkg/dirstream.h>
  18. #include <apt-pkg/extracttar.h>
  19. #include <apt-pkg/error.h>
  20. #include <apt-pkg/strutl.h>
  21. #include <apt-pkg/configuration.h>
  22. #include <apt-pkg/fileutl.h>
  23. #include <string.h>
  24. #include <algorithm>
  25. #include <string>
  26. #include <unistd.h>
  27. #include <signal.h>
  28. #include <fcntl.h>
  29. #include <iostream>
  30. #include <apti18n.h>
  31. /*}}}*/
  32. using namespace std;
  33. // The on disk header for a tar file.
  34. struct ExtractTar::TarHeader
  35. {
  36. char Name[100];
  37. char Mode[8];
  38. char UserID[8];
  39. char GroupID[8];
  40. char Size[12];
  41. char MTime[12];
  42. char Checksum[8];
  43. char LinkFlag;
  44. char LinkName[100];
  45. char MagicNumber[8];
  46. char UserName[32];
  47. char GroupName[32];
  48. char Major[8];
  49. char Minor[8];
  50. };
  51. // ExtractTar::ExtractTar - Constructor /*{{{*/
  52. // ---------------------------------------------------------------------
  53. /* */
  54. ExtractTar::ExtractTar(FileFd &Fd,unsigned long long Max,string DecompressionProgram)
  55. : File(Fd), MaxInSize(Max), DecompressProg(DecompressionProgram)
  56. {
  57. GZPid = -1;
  58. Eof = false;
  59. }
  60. /*}}}*/
  61. // ExtractTar::ExtractTar - Destructor /*{{{*/
  62. // ---------------------------------------------------------------------
  63. /* */
  64. ExtractTar::~ExtractTar()
  65. {
  66. // Error close
  67. Done();
  68. }
  69. /*}}}*/
  70. // ExtractTar::Done - Reap the gzip sub process /*{{{*/
  71. bool ExtractTar::Done(bool)
  72. {
  73. return Done();
  74. }
  75. bool ExtractTar::Done()
  76. {
  77. return InFd.Close();
  78. }
  79. /*}}}*/
  80. // ExtractTar::StartGzip - Startup gzip /*{{{*/
  81. // ---------------------------------------------------------------------
  82. /* This creates a gzip sub process that has its input as the file itself.
  83. If this tar file is embedded into something like an ar file then
  84. gzip will efficiently ignore the extra bits. */
  85. bool ExtractTar::StartGzip()
  86. {
  87. if (DecompressProg.empty())
  88. {
  89. InFd.OpenDescriptor(File.Fd(), FileFd::ReadOnly, FileFd::None, false);
  90. return true;
  91. }
  92. std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
  93. std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin();
  94. for (; compressor != compressors.end(); ++compressor) {
  95. if (compressor->Name == DecompressProg) {
  96. return InFd.OpenDescriptor(File.Fd(), FileFd::ReadOnly, *compressor, false);
  97. }
  98. }
  99. return _error->Error(_("Cannot find a configured compressor for '%s'"),
  100. DecompressProg.c_str());
  101. }
  102. /*}}}*/
  103. // ExtractTar::Go - Perform extraction /*{{{*/
  104. // ---------------------------------------------------------------------
  105. /* This reads each 512 byte block from the archive and extracts the header
  106. information into the Item structure. Then it resolves the UID/GID and
  107. invokes the correct processing function. */
  108. bool ExtractTar::Go(pkgDirStream &Stream)
  109. {
  110. if (StartGzip() == false)
  111. return false;
  112. // Loop over all blocks
  113. string LastLongLink, ItemLink;
  114. string LastLongName, ItemName;
  115. while (1)
  116. {
  117. bool BadRecord = false;
  118. unsigned char Block[512];
  119. if (InFd.Read(Block,sizeof(Block),true) == false)
  120. return false;
  121. if (InFd.Eof() == true)
  122. break;
  123. // Get the checksum
  124. TarHeader *Tar = (TarHeader *)Block;
  125. unsigned long CheckSum;
  126. if (StrToNum(Tar->Checksum,CheckSum,sizeof(Tar->Checksum),8) == false)
  127. return _error->Error(_("Corrupted archive"));
  128. /* Compute the checksum field. The actual checksum is blanked out
  129. with spaces so it is not included in the computation */
  130. unsigned long NewSum = 0;
  131. memset(Tar->Checksum,' ',sizeof(Tar->Checksum));
  132. for (int I = 0; I != sizeof(Block); I++)
  133. NewSum += Block[I];
  134. /* Check for a block of nulls - in this case we kill gzip, GNU tar
  135. does this.. */
  136. if (NewSum == ' '*sizeof(Tar->Checksum))
  137. return Done();
  138. if (NewSum != CheckSum)
  139. return _error->Error(_("Tar checksum failed, archive corrupted"));
  140. // Decode all of the fields
  141. pkgDirStream::Item Itm;
  142. if (StrToNum(Tar->Mode,Itm.Mode,sizeof(Tar->Mode),8) == false ||
  143. (Base256ToNum(Tar->UserID,Itm.UID,8) == false &&
  144. StrToNum(Tar->UserID,Itm.UID,sizeof(Tar->UserID),8) == false) ||
  145. (Base256ToNum(Tar->GroupID,Itm.GID,8) == false &&
  146. StrToNum(Tar->GroupID,Itm.GID,sizeof(Tar->GroupID),8) == false) ||
  147. (Base256ToNum(Tar->Size,Itm.Size,12) == false &&
  148. StrToNum(Tar->Size,Itm.Size,sizeof(Tar->Size),8) == false) ||
  149. (Base256ToNum(Tar->MTime,Itm.MTime,12) == false &&
  150. StrToNum(Tar->MTime,Itm.MTime,sizeof(Tar->MTime),8) == false) ||
  151. StrToNum(Tar->Major,Itm.Major,sizeof(Tar->Major),8) == false ||
  152. StrToNum(Tar->Minor,Itm.Minor,sizeof(Tar->Minor),8) == false)
  153. return _error->Error(_("Corrupted archive"));
  154. // Grab the filename and link target: use last long name if one was
  155. // set, otherwise use the header value as-is, but remember that it may
  156. // fill the entire 100-byte block and needs to be zero-terminated.
  157. // See Debian Bug #689582.
  158. if (LastLongName.empty() == false)
  159. Itm.Name = (char *)LastLongName.c_str();
  160. else
  161. Itm.Name = (char *)ItemName.assign(Tar->Name, sizeof(Tar->Name)).c_str();
  162. if (Itm.Name[0] == '.' && Itm.Name[1] == '/' && Itm.Name[2] != 0)
  163. Itm.Name += 2;
  164. if (LastLongLink.empty() == false)
  165. Itm.LinkTarget = (char *)LastLongLink.c_str();
  166. else
  167. Itm.LinkTarget = (char *)ItemLink.assign(Tar->LinkName, sizeof(Tar->LinkName)).c_str();
  168. // Convert the type over
  169. switch (Tar->LinkFlag)
  170. {
  171. case NormalFile0:
  172. case NormalFile:
  173. Itm.Type = pkgDirStream::Item::File;
  174. break;
  175. case HardLink:
  176. Itm.Type = pkgDirStream::Item::HardLink;
  177. break;
  178. case SymbolicLink:
  179. Itm.Type = pkgDirStream::Item::SymbolicLink;
  180. break;
  181. case CharacterDevice:
  182. Itm.Type = pkgDirStream::Item::CharDevice;
  183. break;
  184. case BlockDevice:
  185. Itm.Type = pkgDirStream::Item::BlockDevice;
  186. break;
  187. case Directory:
  188. Itm.Type = pkgDirStream::Item::Directory;
  189. break;
  190. case FIFO:
  191. Itm.Type = pkgDirStream::Item::FIFO;
  192. break;
  193. case GNU_LongLink:
  194. {
  195. unsigned long long Length = Itm.Size;
  196. unsigned char Block[512];
  197. while (Length > 0)
  198. {
  199. if (InFd.Read(Block,sizeof(Block),true) == false)
  200. return false;
  201. if (Length <= sizeof(Block))
  202. {
  203. LastLongLink.append(Block,Block+sizeof(Block));
  204. break;
  205. }
  206. LastLongLink.append(Block,Block+sizeof(Block));
  207. Length -= sizeof(Block);
  208. }
  209. continue;
  210. }
  211. case GNU_LongName:
  212. {
  213. unsigned long long Length = Itm.Size;
  214. unsigned char Block[512];
  215. while (Length > 0)
  216. {
  217. if (InFd.Read(Block,sizeof(Block),true) == false)
  218. return false;
  219. if (Length < sizeof(Block))
  220. {
  221. LastLongName.append(Block,Block+sizeof(Block));
  222. break;
  223. }
  224. LastLongName.append(Block,Block+sizeof(Block));
  225. Length -= sizeof(Block);
  226. }
  227. continue;
  228. }
  229. default:
  230. BadRecord = true;
  231. _error->Warning(_("Unknown TAR header type %u, member %s"),(unsigned)Tar->LinkFlag,Tar->Name);
  232. break;
  233. }
  234. int Fd = -1;
  235. if (BadRecord == false)
  236. if (Stream.DoItem(Itm,Fd) == false)
  237. return false;
  238. // Copy the file over the FD
  239. unsigned long long Size = Itm.Size;
  240. while (Size != 0)
  241. {
  242. unsigned char Junk[32*1024];
  243. unsigned long Read = min(Size, (unsigned long long)sizeof(Junk));
  244. if (InFd.Read(Junk,((Read+511)/512)*512) == false)
  245. return false;
  246. if (BadRecord == false)
  247. {
  248. if (Fd > 0)
  249. {
  250. if (write(Fd,Junk,Read) != (signed)Read)
  251. return Stream.Fail(Itm,Fd);
  252. }
  253. else
  254. {
  255. /* An Fd of -2 means to send to a special processing
  256. function */
  257. if (Fd == -2)
  258. if (Stream.Process(Itm,Junk,Read,Itm.Size - Size) == false)
  259. return Stream.Fail(Itm,Fd);
  260. }
  261. }
  262. Size -= Read;
  263. }
  264. // And finish up
  265. if (BadRecord == false)
  266. if (Stream.FinishedFile(Itm,Fd) == false)
  267. return false;
  268. LastLongName.erase();
  269. LastLongLink.erase();
  270. }
  271. return Done();
  272. }
  273. /*}}}*/