You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

307 lines
8.8 KiB

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