extracttar.cc 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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/macros.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <signal.h>
  26. #include <fcntl.h>
  27. #include <iostream>
  28. #include <apti18n.h>
  29. /*}}}*/
  30. using namespace std;
  31. // The on disk header for a tar file.
  32. struct ExtractTar::TarHeader
  33. {
  34. char Name[100];
  35. char Mode[8];
  36. char UserID[8];
  37. char GroupID[8];
  38. char Size[12];
  39. char MTime[12];
  40. char Checksum[8];
  41. char LinkFlag;
  42. char LinkName[100];
  43. char MagicNumber[8];
  44. char UserName[32];
  45. char GroupName[32];
  46. char Major[8];
  47. char Minor[8];
  48. };
  49. // ExtractTar::ExtractTar - Constructor /*{{{*/
  50. // ---------------------------------------------------------------------
  51. /* */
  52. ExtractTar::ExtractTar(FileFd &Fd,unsigned long Max,string DecompressionProgram) : File(Fd),
  53. MaxInSize(Max), DecompressProg(DecompressionProgram)
  54. {
  55. GZPid = -1;
  56. Eof = false;
  57. }
  58. /*}}}*/
  59. // ExtractTar::ExtractTar - Destructor /*{{{*/
  60. // ---------------------------------------------------------------------
  61. /* */
  62. ExtractTar::~ExtractTar()
  63. {
  64. // Error close
  65. Done(true);
  66. }
  67. /*}}}*/
  68. // ExtractTar::Done - Reap the gzip sub process /*{{{*/
  69. // ---------------------------------------------------------------------
  70. /* If the force flag is given then error messages are suppressed - this
  71. means we hit the end of the tar file but there was still gzip data. */
  72. bool ExtractTar::Done(bool Force)
  73. {
  74. InFd.Close();
  75. if (GZPid <= 0)
  76. return true;
  77. /* If there is a pending error then we are cleaning up gzip and are
  78. not interested in it's failures */
  79. if (_error->PendingError() == true)
  80. Force = true;
  81. // Make sure we clean it up!
  82. kill(GZPid,SIGINT);
  83. string confvar = string("dir::bin::") + DecompressProg;
  84. if (ExecWait(GZPid,_config->Find(confvar.c_str(),DecompressProg.c_str()).c_str(),
  85. Force) == false)
  86. {
  87. GZPid = -1;
  88. return Force;
  89. }
  90. GZPid = -1;
  91. return true;
  92. }
  93. /*}}}*/
  94. // ExtractTar::StartGzip - Startup gzip /*{{{*/
  95. // ---------------------------------------------------------------------
  96. /* This creates a gzip sub process that has its input as the file itself.
  97. If this tar file is embedded into something like an ar file then
  98. gzip will efficiently ignore the extra bits. */
  99. bool ExtractTar::StartGzip()
  100. {
  101. int Pipes[2];
  102. if (pipe(Pipes) != 0)
  103. return _error->Errno("pipe",_("Failed to create pipes"));
  104. // Fork off the process
  105. GZPid = ExecFork();
  106. // Spawn the subprocess
  107. if (GZPid == 0)
  108. {
  109. // Setup the FDs
  110. dup2(Pipes[1],STDOUT_FILENO);
  111. dup2(File.Fd(),STDIN_FILENO);
  112. int Fd = open("/dev/null",O_RDWR);
  113. if (Fd == -1)
  114. _exit(101);
  115. dup2(Fd,STDERR_FILENO);
  116. close(Fd);
  117. SetCloseExec(STDOUT_FILENO,false);
  118. SetCloseExec(STDIN_FILENO,false);
  119. SetCloseExec(STDERR_FILENO,false);
  120. const char *Args[3];
  121. string confvar = string("dir::bin::") + DecompressProg;
  122. string argv0 = _config->Find(confvar.c_str(),DecompressProg.c_str());
  123. Args[0] = argv0.c_str();
  124. Args[1] = "-d";
  125. Args[2] = 0;
  126. execvp(Args[0],(char **)Args);
  127. cerr << _("Failed to exec gzip ") << Args[0] << endl;
  128. _exit(100);
  129. }
  130. // Fix up our FDs
  131. InFd.OpenDescriptor(Pipes[0], FileFd::ReadOnly, FileFd::None, true);
  132. close(Pipes[1]);
  133. return true;
  134. }
  135. /*}}}*/
  136. // ExtractTar::Go - Perform extraction /*{{{*/
  137. // ---------------------------------------------------------------------
  138. /* This reads each 512 byte block from the archive and extracts the header
  139. information into the Item structure. Then it resolves the UID/GID and
  140. invokes the correct processing function. */
  141. bool ExtractTar::Go(pkgDirStream &Stream)
  142. {
  143. if (StartGzip() == false)
  144. return false;
  145. // Loop over all blocks
  146. string LastLongLink, ItemLink;
  147. string LastLongName, ItemName;
  148. while (1)
  149. {
  150. bool BadRecord = false;
  151. unsigned char Block[512];
  152. if (InFd.Read(Block,sizeof(Block),true) == false)
  153. return false;
  154. if (InFd.Eof() == true)
  155. break;
  156. // Get the checksum
  157. TarHeader *Tar = (TarHeader *)Block;
  158. unsigned long CheckSum;
  159. if (StrToNum(Tar->Checksum,CheckSum,sizeof(Tar->Checksum),8) == false)
  160. return _error->Error(_("Corrupted archive"));
  161. /* Compute the checksum field. The actual checksum is blanked out
  162. with spaces so it is not included in the computation */
  163. unsigned long NewSum = 0;
  164. memset(Tar->Checksum,' ',sizeof(Tar->Checksum));
  165. for (int I = 0; I != sizeof(Block); I++)
  166. NewSum += Block[I];
  167. /* Check for a block of nulls - in this case we kill gzip, GNU tar
  168. does this.. */
  169. if (NewSum == ' '*sizeof(Tar->Checksum))
  170. return Done(true);
  171. if (NewSum != CheckSum)
  172. return _error->Error(_("Tar checksum failed, archive corrupted"));
  173. // Decode all of the fields
  174. pkgDirStream::Item Itm;
  175. if (StrToNum(Tar->Mode,Itm.Mode,sizeof(Tar->Mode),8) == false ||
  176. (Base256ToNum(Tar->UserID,Itm.UID,8) == false &&
  177. StrToNum(Tar->UserID,Itm.UID,sizeof(Tar->UserID),8) == false) ||
  178. (Base256ToNum(Tar->GroupID,Itm.GID,8) == false &&
  179. StrToNum(Tar->GroupID,Itm.GID,sizeof(Tar->GroupID),8) == false) ||
  180. (Base256ToNum(Tar->Size,Itm.Size,12) == false &&
  181. StrToNum(Tar->Size,Itm.Size,sizeof(Tar->Size),8) == false) ||
  182. (Base256ToNum(Tar->MTime,Itm.MTime,12) == false &&
  183. StrToNum(Tar->MTime,Itm.MTime,sizeof(Tar->MTime),8) == false) ||
  184. StrToNum(Tar->Major,Itm.Major,sizeof(Tar->Major),8) == false ||
  185. StrToNum(Tar->Minor,Itm.Minor,sizeof(Tar->Minor),8) == false)
  186. return _error->Error(_("Corrupted archive"));
  187. // Grab the filename and link target: use last long name if one was
  188. // set, otherwise use the header value as-is, but remember that it may
  189. // fill the entire 100-byte block and needs to be zero-terminated.
  190. // See Debian Bug #689582.
  191. if (LastLongName.empty() == false)
  192. Itm.Name = (char *)LastLongName.c_str();
  193. else
  194. Itm.Name = (char *)ItemName.assign(Tar->Name, sizeof(Tar->Name)).c_str();
  195. if (Itm.Name[0] == '.' && Itm.Name[1] == '/' && Itm.Name[2] != 0)
  196. Itm.Name += 2;
  197. if (LastLongLink.empty() == false)
  198. Itm.LinkTarget = (char *)LastLongLink.c_str();
  199. else
  200. Itm.LinkTarget = (char *)ItemLink.assign(Tar->LinkName, sizeof(Tar->LinkName)).c_str();
  201. // Convert the type over
  202. switch (Tar->LinkFlag)
  203. {
  204. case NormalFile0:
  205. case NormalFile:
  206. Itm.Type = pkgDirStream::Item::File;
  207. break;
  208. case HardLink:
  209. Itm.Type = pkgDirStream::Item::HardLink;
  210. break;
  211. case SymbolicLink:
  212. Itm.Type = pkgDirStream::Item::SymbolicLink;
  213. break;
  214. case CharacterDevice:
  215. Itm.Type = pkgDirStream::Item::CharDevice;
  216. break;
  217. case BlockDevice:
  218. Itm.Type = pkgDirStream::Item::BlockDevice;
  219. break;
  220. case Directory:
  221. Itm.Type = pkgDirStream::Item::Directory;
  222. break;
  223. case FIFO:
  224. Itm.Type = pkgDirStream::Item::FIFO;
  225. break;
  226. case GNU_LongLink:
  227. {
  228. unsigned long Length = Itm.Size;
  229. unsigned char Block[512];
  230. while (Length > 0)
  231. {
  232. if (InFd.Read(Block,sizeof(Block),true) == false)
  233. return false;
  234. if (Length <= sizeof(Block))
  235. {
  236. LastLongLink.append(Block,Block+sizeof(Block));
  237. break;
  238. }
  239. LastLongLink.append(Block,Block+sizeof(Block));
  240. Length -= sizeof(Block);
  241. }
  242. continue;
  243. }
  244. case GNU_LongName:
  245. {
  246. unsigned long Length = Itm.Size;
  247. unsigned char Block[512];
  248. while (Length > 0)
  249. {
  250. if (InFd.Read(Block,sizeof(Block),true) == false)
  251. return false;
  252. if (Length < sizeof(Block))
  253. {
  254. LastLongName.append(Block,Block+sizeof(Block));
  255. break;
  256. }
  257. LastLongName.append(Block,Block+sizeof(Block));
  258. Length -= sizeof(Block);
  259. }
  260. continue;
  261. }
  262. default:
  263. BadRecord = true;
  264. _error->Warning(_("Unknown TAR header type %u, member %s"),(unsigned)Tar->LinkFlag,Tar->Name);
  265. break;
  266. }
  267. int Fd = -1;
  268. if (BadRecord == false)
  269. if (Stream.DoItem(Itm,Fd) == false)
  270. return false;
  271. // Copy the file over the FD
  272. unsigned long Size = Itm.Size;
  273. while (Size != 0)
  274. {
  275. unsigned char Junk[32*1024];
  276. unsigned long Read = min(Size,(unsigned long)sizeof(Junk));
  277. if (InFd.Read(Junk,((Read+511)/512)*512) == false)
  278. return false;
  279. if (BadRecord == false)
  280. {
  281. if (Fd > 0)
  282. {
  283. if (write(Fd,Junk,Read) != (signed)Read)
  284. return Stream.Fail(Itm,Fd);
  285. }
  286. else
  287. {
  288. /* An Fd of -2 means to send to a special processing
  289. function */
  290. if (Fd == -2)
  291. if (Stream.Process(Itm,Junk,Read,Itm.Size - Size) == false)
  292. return Stream.Fail(Itm,Fd);
  293. }
  294. }
  295. Size -= Read;
  296. }
  297. // And finish up
  298. if (BadRecord == false)
  299. if (Stream.FinishedFile(Itm,Fd) == false)
  300. return false;
  301. LastLongName.erase();
  302. LastLongLink.erase();
  303. }
  304. return Done(false);
  305. }
  306. /*}}}*/