extracttar.cc 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: extracttar.cc,v 1.5 2002/03/26 07:38:58 jgg 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. // The on disk header for a tar file.
  31. struct ExtractTar::TarHeader
  32. {
  33. char Name[100];
  34. char Mode[8];
  35. char UserID[8];
  36. char GroupID[8];
  37. char Size[12];
  38. char MTime[12];
  39. char Checksum[8];
  40. char LinkFlag;
  41. char LinkName[100];
  42. char MagicNumber[8];
  43. char UserName[32];
  44. char GroupName[32];
  45. char Major[8];
  46. char Minor[8];
  47. };
  48. // ExtractTar::ExtractTar - Constructor /*{{{*/
  49. // ---------------------------------------------------------------------
  50. /* */
  51. ExtractTar::ExtractTar(FileFd &Fd,unsigned long Max) : File(Fd),
  52. MaxInSize(Max)
  53. {
  54. GZPid = -1;
  55. InFd = -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. if (ExecWait(GZPid,_config->Find("dir::bin::gzip","/bin/gzip").c_str(),
  84. Force) == false)
  85. {
  86. GZPid = -1;
  87. return Force;
  88. }
  89. GZPid = -1;
  90. return true;
  91. }
  92. /*}}}*/
  93. // ExtractTar::StartGzip - Startup gzip /*{{{*/
  94. // ---------------------------------------------------------------------
  95. /* This creates a gzip sub process that has its input as the file itself.
  96. If this tar file is embedded into something like an ar file then
  97. gzip will efficiently ignore the extra bits. */
  98. bool ExtractTar::StartGzip()
  99. {
  100. int Pipes[2];
  101. if (pipe(Pipes) != 0)
  102. return _error->Errno("pipe","Failed to create pipes");
  103. // Fork off the process
  104. GZPid = ExecFork();
  105. // Spawn the subprocess
  106. if (GZPid == 0)
  107. {
  108. // Setup the FDs
  109. dup2(Pipes[1],STDOUT_FILENO);
  110. dup2(File.Fd(),STDIN_FILENO);
  111. int Fd = open("/dev/null",O_RDWR);
  112. if (Fd == -1)
  113. _exit(101);
  114. dup2(Fd,STDERR_FILENO);
  115. close(Fd);
  116. SetCloseExec(STDOUT_FILENO,false);
  117. SetCloseExec(STDIN_FILENO,false);
  118. SetCloseExec(STDERR_FILENO,false);
  119. const char *Args[3];
  120. Args[0] = _config->Find("dir::bin::gzip","/bin/gzip").c_str();
  121. Args[1] = "-d";
  122. Args[2] = 0;
  123. execv(Args[0],(char **)Args);
  124. cerr << "Failed to exec gzip " << Args[0] << endl;
  125. _exit(100);
  126. }
  127. // Fix up our FDs
  128. InFd.Fd(Pipes[0]);
  129. close(Pipes[1]);
  130. return true;
  131. }
  132. /*}}}*/
  133. // ExtractTar::Go - Perform extraction /*{{{*/
  134. // ---------------------------------------------------------------------
  135. /* This reads each 512 byte block from the archive and extracts the header
  136. information into the Item structure. Then it resolves the UID/GID and
  137. invokes the correct processing function. */
  138. bool ExtractTar::Go(pkgDirStream &Stream)
  139. {
  140. if (StartGzip() == false)
  141. return false;
  142. // Loop over all blocks
  143. string LastLongLink;
  144. string LastLongName;
  145. while (1)
  146. {
  147. bool BadRecord = false;
  148. unsigned char Block[512];
  149. if (InFd.Read(Block,sizeof(Block),true) == false)
  150. return false;
  151. if (InFd.Eof() == true)
  152. break;
  153. // Get the checksum
  154. TarHeader *Tar = (TarHeader *)Block;
  155. unsigned long CheckSum;
  156. if (StrToNum(Tar->Checksum,CheckSum,sizeof(Tar->Checksum),8) == false)
  157. return _error->Error("Corrupted archive");
  158. /* Compute the checksum field. The actual checksum is blanked out
  159. with spaces so it is not included in the computation */
  160. unsigned long NewSum = 0;
  161. memset(Tar->Checksum,' ',sizeof(Tar->Checksum));
  162. for (int I = 0; I != sizeof(Block); I++)
  163. NewSum += Block[I];
  164. /* Check for a block of nulls - in this case we kill gzip, GNU tar
  165. does this.. */
  166. if (NewSum == ' '*sizeof(Tar->Checksum))
  167. return Done(true);
  168. if (NewSum != CheckSum)
  169. return _error->Error("Tar Checksum failed, archive corrupted");
  170. // Decode all of the fields
  171. pkgDirStream::Item Itm;
  172. if (StrToNum(Tar->Mode,Itm.Mode,sizeof(Tar->Mode),8) == false ||
  173. StrToNum(Tar->UserID,Itm.UID,sizeof(Tar->UserID),8) == false ||
  174. StrToNum(Tar->GroupID,Itm.GID,sizeof(Tar->GroupID),8) == false ||
  175. StrToNum(Tar->Size,Itm.Size,sizeof(Tar->Size),8) == false ||
  176. StrToNum(Tar->MTime,Itm.MTime,sizeof(Tar->MTime),8) == false ||
  177. StrToNum(Tar->Major,Itm.Major,sizeof(Tar->Major),8) == false ||
  178. StrToNum(Tar->Minor,Itm.Minor,sizeof(Tar->Minor),8) == false)
  179. return _error->Error("Corrupted archive");
  180. // Grab the filename
  181. if (LastLongName.empty() == false)
  182. Itm.Name = (char *)LastLongName.c_str();
  183. else
  184. {
  185. Tar->Name[sizeof(Tar->Name)] = 0;
  186. Itm.Name = Tar->Name;
  187. }
  188. if (Itm.Name[0] == '.' && Itm.Name[1] == '/' && Itm.Name[2] != 0)
  189. Itm.Name += 2;
  190. // Grab the link target
  191. Tar->Name[sizeof(Tar->LinkName)] = 0;
  192. Itm.LinkTarget = Tar->LinkName;
  193. if (LastLongLink.empty() == false)
  194. Itm.LinkTarget = (char *)LastLongLink.c_str();
  195. // Convert the type over
  196. switch (Tar->LinkFlag)
  197. {
  198. case NormalFile0:
  199. case NormalFile:
  200. Itm.Type = pkgDirStream::Item::File;
  201. break;
  202. case HardLink:
  203. Itm.Type = pkgDirStream::Item::HardLink;
  204. break;
  205. case SymbolicLink:
  206. Itm.Type = pkgDirStream::Item::SymbolicLink;
  207. break;
  208. case CharacterDevice:
  209. Itm.Type = pkgDirStream::Item::CharDevice;
  210. break;
  211. case BlockDevice:
  212. Itm.Type = pkgDirStream::Item::BlockDevice;
  213. break;
  214. case Directory:
  215. Itm.Type = pkgDirStream::Item::Directory;
  216. break;
  217. case FIFO:
  218. Itm.Type = pkgDirStream::Item::FIFO;
  219. break;
  220. case GNU_LongLink:
  221. {
  222. unsigned long Length = Itm.Size;
  223. unsigned char Block[512];
  224. while (Length > 0)
  225. {
  226. if (InFd.Read(Block,sizeof(Block),true) == false)
  227. return false;
  228. if (Length <= sizeof(Block))
  229. {
  230. LastLongLink.append(Block,Block+sizeof(Block));
  231. break;
  232. }
  233. LastLongLink.append(Block,Block+sizeof(Block));
  234. Length -= sizeof(Block);
  235. }
  236. continue;
  237. }
  238. case GNU_LongName:
  239. {
  240. unsigned long Length = Itm.Size;
  241. unsigned char Block[512];
  242. while (Length > 0)
  243. {
  244. if (InFd.Read(Block,sizeof(Block),true) == false)
  245. return false;
  246. if (Length < sizeof(Block))
  247. {
  248. LastLongName.append(Block,Block+sizeof(Block));
  249. break;
  250. }
  251. LastLongName.append(Block,Block+sizeof(Block));
  252. Length -= sizeof(Block);
  253. }
  254. continue;
  255. }
  256. default:
  257. BadRecord = true;
  258. _error->Warning("Unkown TAR header type %u, member %s",(unsigned)Tar->LinkFlag,Tar->Name);
  259. break;
  260. }
  261. int Fd = -1;
  262. if (BadRecord == false)
  263. if (Stream.DoItem(Itm,Fd) == false)
  264. return false;
  265. // Copy the file over the FD
  266. unsigned long Size = Itm.Size;
  267. while (Size != 0)
  268. {
  269. unsigned char Junk[32*1024];
  270. unsigned long Read = MIN(Size,sizeof(Junk));
  271. if (InFd.Read(Junk,((Read+511)/512)*512) == false)
  272. return false;
  273. if (BadRecord == false)
  274. {
  275. if (Fd > 0)
  276. {
  277. if (write(Fd,Junk,Read) != (signed)Read)
  278. return Stream.Fail(Itm,Fd);
  279. }
  280. else
  281. {
  282. /* An Fd of -2 means to send to a special processing
  283. function */
  284. if (Fd == -2)
  285. if (Stream.Process(Itm,Junk,Read,Itm.Size - Size) == false)
  286. return Stream.Fail(Itm,Fd);
  287. }
  288. }
  289. Size -= Read;
  290. }
  291. // And finish up
  292. if (Itm.Size != 0 && BadRecord == false)
  293. if (Stream.FinishedFile(Itm,Fd) == false)
  294. return false;
  295. LastLongName.erase();
  296. LastLongLink.erase();
  297. }
  298. return Done(false);
  299. }
  300. /*}}}*/