extracttar.cc 9.7 KB

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