extracttar.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: extracttar.cc,v 1.6 2002/11/11 06:55:50 doogie 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. /*}}}*/
  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) : File(Fd),
  53. MaxInSize(Max)
  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. if (ExecWait(GZPid,_config->Find("dir::bin::gzip","/bin/gzip").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. Args[0] = _config->Find("dir::bin::gzip","/bin/gzip").c_str();
  122. Args[1] = "-d";
  123. Args[2] = 0;
  124. execv(Args[0],(char **)Args);
  125. cerr << "Failed to exec gzip " << Args[0] << endl;
  126. _exit(100);
  127. }
  128. // Fix up our FDs
  129. InFd.Fd(Pipes[0]);
  130. close(Pipes[1]);
  131. return true;
  132. }
  133. /*}}}*/
  134. // ExtractTar::Go - Perform extraction /*{{{*/
  135. // ---------------------------------------------------------------------
  136. /* This reads each 512 byte block from the archive and extracts the header
  137. information into the Item structure. Then it resolves the UID/GID and
  138. invokes the correct processing function. */
  139. bool ExtractTar::Go(pkgDirStream &Stream)
  140. {
  141. if (StartGzip() == false)
  142. return false;
  143. // Loop over all blocks
  144. string LastLongLink;
  145. string LastLongName;
  146. while (1)
  147. {
  148. bool BadRecord = false;
  149. unsigned char Block[512];
  150. if (InFd.Read(Block,sizeof(Block),true) == false)
  151. return false;
  152. if (InFd.Eof() == true)
  153. break;
  154. // Get the checksum
  155. TarHeader *Tar = (TarHeader *)Block;
  156. unsigned long CheckSum;
  157. if (StrToNum(Tar->Checksum,CheckSum,sizeof(Tar->Checksum),8) == false)
  158. return _error->Error("Corrupted archive");
  159. /* Compute the checksum field. The actual checksum is blanked out
  160. with spaces so it is not included in the computation */
  161. unsigned long NewSum = 0;
  162. memset(Tar->Checksum,' ',sizeof(Tar->Checksum));
  163. for (int I = 0; I != sizeof(Block); I++)
  164. NewSum += Block[I];
  165. /* Check for a block of nulls - in this case we kill gzip, GNU tar
  166. does this.. */
  167. if (NewSum == ' '*sizeof(Tar->Checksum))
  168. return Done(true);
  169. if (NewSum != CheckSum)
  170. return _error->Error("Tar Checksum failed, archive corrupted");
  171. // Decode all of the fields
  172. pkgDirStream::Item Itm;
  173. if (StrToNum(Tar->Mode,Itm.Mode,sizeof(Tar->Mode),8) == false ||
  174. StrToNum(Tar->UserID,Itm.UID,sizeof(Tar->UserID),8) == false ||
  175. StrToNum(Tar->GroupID,Itm.GID,sizeof(Tar->GroupID),8) == false ||
  176. StrToNum(Tar->Size,Itm.Size,sizeof(Tar->Size),8) == false ||
  177. StrToNum(Tar->MTime,Itm.MTime,sizeof(Tar->MTime),8) == false ||
  178. StrToNum(Tar->Major,Itm.Major,sizeof(Tar->Major),8) == false ||
  179. StrToNum(Tar->Minor,Itm.Minor,sizeof(Tar->Minor),8) == false)
  180. return _error->Error("Corrupted archive");
  181. // Grab the filename
  182. if (LastLongName.empty() == false)
  183. Itm.Name = (char *)LastLongName.c_str();
  184. else
  185. {
  186. Tar->Name[sizeof(Tar->Name)] = 0;
  187. Itm.Name = Tar->Name;
  188. }
  189. if (Itm.Name[0] == '.' && Itm.Name[1] == '/' && Itm.Name[2] != 0)
  190. Itm.Name += 2;
  191. // Grab the link target
  192. Tar->Name[sizeof(Tar->LinkName)] = 0;
  193. Itm.LinkTarget = Tar->LinkName;
  194. if (LastLongLink.empty() == false)
  195. Itm.LinkTarget = (char *)LastLongLink.c_str();
  196. // Convert the type over
  197. switch (Tar->LinkFlag)
  198. {
  199. case NormalFile0:
  200. case NormalFile:
  201. Itm.Type = pkgDirStream::Item::File;
  202. break;
  203. case HardLink:
  204. Itm.Type = pkgDirStream::Item::HardLink;
  205. break;
  206. case SymbolicLink:
  207. Itm.Type = pkgDirStream::Item::SymbolicLink;
  208. break;
  209. case CharacterDevice:
  210. Itm.Type = pkgDirStream::Item::CharDevice;
  211. break;
  212. case BlockDevice:
  213. Itm.Type = pkgDirStream::Item::BlockDevice;
  214. break;
  215. case Directory:
  216. Itm.Type = pkgDirStream::Item::Directory;
  217. break;
  218. case FIFO:
  219. Itm.Type = pkgDirStream::Item::FIFO;
  220. break;
  221. case GNU_LongLink:
  222. {
  223. unsigned long Length = Itm.Size;
  224. unsigned char Block[512];
  225. while (Length > 0)
  226. {
  227. if (InFd.Read(Block,sizeof(Block),true) == false)
  228. return false;
  229. if (Length <= sizeof(Block))
  230. {
  231. LastLongLink.append(Block,Block+sizeof(Block));
  232. break;
  233. }
  234. LastLongLink.append(Block,Block+sizeof(Block));
  235. Length -= sizeof(Block);
  236. }
  237. continue;
  238. }
  239. case GNU_LongName:
  240. {
  241. unsigned long Length = Itm.Size;
  242. unsigned char Block[512];
  243. while (Length > 0)
  244. {
  245. if (InFd.Read(Block,sizeof(Block),true) == false)
  246. return false;
  247. if (Length < sizeof(Block))
  248. {
  249. LastLongName.append(Block,Block+sizeof(Block));
  250. break;
  251. }
  252. LastLongName.append(Block,Block+sizeof(Block));
  253. Length -= sizeof(Block);
  254. }
  255. continue;
  256. }
  257. default:
  258. BadRecord = true;
  259. _error->Warning("Unkown TAR header type %u, member %s",(unsigned)Tar->LinkFlag,Tar->Name);
  260. break;
  261. }
  262. int Fd = -1;
  263. if (BadRecord == false)
  264. if (Stream.DoItem(Itm,Fd) == false)
  265. return false;
  266. // Copy the file over the FD
  267. unsigned long Size = Itm.Size;
  268. while (Size != 0)
  269. {
  270. unsigned char Junk[32*1024];
  271. unsigned long Read = MIN(Size,sizeof(Junk));
  272. if (InFd.Read(Junk,((Read+511)/512)*512) == false)
  273. return false;
  274. if (BadRecord == false)
  275. {
  276. if (Fd > 0)
  277. {
  278. if (write(Fd,Junk,Read) != (signed)Read)
  279. return Stream.Fail(Itm,Fd);
  280. }
  281. else
  282. {
  283. /* An Fd of -2 means to send to a special processing
  284. function */
  285. if (Fd == -2)
  286. if (Stream.Process(Itm,Junk,Read,Itm.Size - Size) == false)
  287. return Stream.Fail(Itm,Fd);
  288. }
  289. }
  290. Size -= Read;
  291. }
  292. // And finish up
  293. if (Itm.Size != 0 && BadRecord == false)
  294. if (Stream.FinishedFile(Itm,Fd) == false)
  295. return false;
  296. LastLongName.erase();
  297. LastLongLink.erase();
  298. }
  299. return Done(false);
  300. }
  301. /*}}}*/