extracttar.cc 9.2 KB

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