extracttar.cc 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. #ifdef __GNUG__
  17. #pragma implementation "apt-pkg/extracttar.h"
  18. #endif
  19. #include <apt-pkg/extracttar.h>
  20. #include <apt-pkg/error.h>
  21. #include <apt-pkg/strutl.h>
  22. #include <apt-pkg/configuration.h>
  23. #include <system.h>
  24. #include <stdlib.h>
  25. #include <unistd.h>
  26. #include <signal.h>
  27. #include <fcntl.h>
  28. #include <iostream>
  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 Max,string DecompressionProgram) : File(Fd),
  54. MaxInSize(Max), DecompressProg(DecompressionProgram)
  55. {
  56. GZPid = -1;
  57. InFd = -1;
  58. Eof = false;
  59. }
  60. /*}}}*/
  61. // ExtractTar::ExtractTar - Destructor /*{{{*/
  62. // ---------------------------------------------------------------------
  63. /* */
  64. ExtractTar::~ExtractTar()
  65. {
  66. // Error close
  67. Done(true);
  68. }
  69. /*}}}*/
  70. // ExtractTar::Done - Reap the gzip sub process /*{{{*/
  71. // ---------------------------------------------------------------------
  72. /* If the force flag is given then error messages are suppressed - this
  73. means we hit the end of the tar file but there was still gzip data. */
  74. bool ExtractTar::Done(bool Force)
  75. {
  76. InFd.Close();
  77. if (GZPid <= 0)
  78. return true;
  79. /* If there is a pending error then we are cleaning up gzip and are
  80. not interested in it's failures */
  81. if (_error->PendingError() == true)
  82. Force = true;
  83. // Make sure we clean it up!
  84. kill(GZPid,SIGINT);
  85. string confvar = string("dir::bin::") + DecompressProg;
  86. if (ExecWait(GZPid,_config->Find(confvar.c_str(),DecompressProg.c_str()).c_str(),
  87. Force) == false)
  88. {
  89. GZPid = -1;
  90. return Force;
  91. }
  92. GZPid = -1;
  93. return true;
  94. }
  95. /*}}}*/
  96. // ExtractTar::StartGzip - Startup gzip /*{{{*/
  97. // ---------------------------------------------------------------------
  98. /* This creates a gzip sub process that has its input as the file itself.
  99. If this tar file is embedded into something like an ar file then
  100. gzip will efficiently ignore the extra bits. */
  101. bool ExtractTar::StartGzip()
  102. {
  103. int Pipes[2];
  104. if (pipe(Pipes) != 0)
  105. return _error->Errno("pipe",_("Failed to create pipes"));
  106. // Fork off the process
  107. GZPid = ExecFork();
  108. // Spawn the subprocess
  109. if (GZPid == 0)
  110. {
  111. // Setup the FDs
  112. dup2(Pipes[1],STDOUT_FILENO);
  113. dup2(File.Fd(),STDIN_FILENO);
  114. int Fd = open("/dev/null",O_RDWR);
  115. if (Fd == -1)
  116. _exit(101);
  117. dup2(Fd,STDERR_FILENO);
  118. close(Fd);
  119. SetCloseExec(STDOUT_FILENO,false);
  120. SetCloseExec(STDIN_FILENO,false);
  121. SetCloseExec(STDERR_FILENO,false);
  122. const char *Args[3];
  123. string confvar = string("dir::bin::") + DecompressProg;
  124. Args[0] = _config->Find(confvar.c_str(),DecompressProg.c_str()).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.Fd(Pipes[0]);
  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. StrToNum(Tar->UserID,Itm.UID,sizeof(Tar->UserID),8) == false ||
  178. StrToNum(Tar->GroupID,Itm.GID,sizeof(Tar->GroupID),8) == false ||
  179. StrToNum(Tar->Size,Itm.Size,sizeof(Tar->Size),8) == false ||
  180. StrToNum(Tar->MTime,Itm.MTime,sizeof(Tar->MTime),8) == false ||
  181. StrToNum(Tar->Major,Itm.Major,sizeof(Tar->Major),8) == false ||
  182. StrToNum(Tar->Minor,Itm.Minor,sizeof(Tar->Minor),8) == false)
  183. return _error->Error(_("Corrupted archive"));
  184. // Grab the filename
  185. if (LastLongName.empty() == false)
  186. Itm.Name = (char *)LastLongName.c_str();
  187. else
  188. {
  189. Tar->Name[sizeof(Tar->Name)] = 0;
  190. Itm.Name = Tar->Name;
  191. }
  192. if (Itm.Name[0] == '.' && Itm.Name[1] == '/' && Itm.Name[2] != 0)
  193. Itm.Name += 2;
  194. // Grab the link target
  195. Tar->Name[sizeof(Tar->LinkName)] = 0;
  196. Itm.LinkTarget = Tar->LinkName;
  197. if (LastLongLink.empty() == false)
  198. Itm.LinkTarget = (char *)LastLongLink.c_str();
  199. // Convert the type over
  200. switch (Tar->LinkFlag)
  201. {
  202. case NormalFile0:
  203. case NormalFile:
  204. Itm.Type = pkgDirStream::Item::File;
  205. break;
  206. case HardLink:
  207. Itm.Type = pkgDirStream::Item::HardLink;
  208. break;
  209. case SymbolicLink:
  210. Itm.Type = pkgDirStream::Item::SymbolicLink;
  211. break;
  212. case CharacterDevice:
  213. Itm.Type = pkgDirStream::Item::CharDevice;
  214. break;
  215. case BlockDevice:
  216. Itm.Type = pkgDirStream::Item::BlockDevice;
  217. break;
  218. case Directory:
  219. Itm.Type = pkgDirStream::Item::Directory;
  220. break;
  221. case FIFO:
  222. Itm.Type = pkgDirStream::Item::FIFO;
  223. break;
  224. case GNU_LongLink:
  225. {
  226. unsigned long Length = Itm.Size;
  227. unsigned char Block[512];
  228. while (Length > 0)
  229. {
  230. if (InFd.Read(Block,sizeof(Block),true) == false)
  231. return false;
  232. if (Length <= sizeof(Block))
  233. {
  234. LastLongLink.append(Block,Block+sizeof(Block));
  235. break;
  236. }
  237. LastLongLink.append(Block,Block+sizeof(Block));
  238. Length -= sizeof(Block);
  239. }
  240. continue;
  241. }
  242. case GNU_LongName:
  243. {
  244. unsigned long Length = Itm.Size;
  245. unsigned char Block[512];
  246. while (Length > 0)
  247. {
  248. if (InFd.Read(Block,sizeof(Block),true) == false)
  249. return false;
  250. if (Length < sizeof(Block))
  251. {
  252. LastLongName.append(Block,Block+sizeof(Block));
  253. break;
  254. }
  255. LastLongName.append(Block,Block+sizeof(Block));
  256. Length -= sizeof(Block);
  257. }
  258. continue;
  259. }
  260. default:
  261. BadRecord = true;
  262. _error->Warning(_("Unknown TAR header type %u, member %s"),(unsigned)Tar->LinkFlag,Tar->Name);
  263. break;
  264. }
  265. int Fd = -1;
  266. if (BadRecord == false)
  267. if (Stream.DoItem(Itm,Fd) == false)
  268. return false;
  269. // Copy the file over the FD
  270. unsigned long Size = Itm.Size;
  271. while (Size != 0)
  272. {
  273. unsigned char Junk[32*1024];
  274. unsigned long Read = min(Size,(unsigned long)sizeof(Junk));
  275. if (InFd.Read(Junk,((Read+511)/512)*512) == false)
  276. return false;
  277. if (BadRecord == false)
  278. {
  279. if (Fd > 0)
  280. {
  281. if (write(Fd,Junk,Read) != (signed)Read)
  282. return Stream.Fail(Itm,Fd);
  283. }
  284. else
  285. {
  286. /* An Fd of -2 means to send to a special processing
  287. function */
  288. if (Fd == -2)
  289. if (Stream.Process(Itm,Junk,Read,Itm.Size - Size) == false)
  290. return Stream.Fail(Itm,Fd);
  291. }
  292. }
  293. Size -= Read;
  294. }
  295. // And finish up
  296. if (Itm.Size != 0 && BadRecord == false)
  297. if (Stream.FinishedFile(Itm,Fd) == false)
  298. return false;
  299. LastLongName.erase();
  300. LastLongLink.erase();
  301. }
  302. return Done(false);
  303. }
  304. /*}}}*/