]> git.saurik.com Git - apt.git/blame - apt-inst/contrib/extracttar.cc
Speling
[apt.git] / apt-inst / contrib / extracttar.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
18255546 3// $Id: extracttar.cc,v 1.3 2001/05/27 23:47:09 jgg Exp $
b2e465d6
AL
4/* ######################################################################
5
6 Extract a Tar - Tar Extractor
7
8 Some performance measurements showed that zlib performed quite poorly
9 in comparision to a forked gzip process. This tar extractor makes use
10 of the fact that dup'd file descriptors have the same seek pointer
11 and that gzip will not read past the end of a compressed stream,
12 even if there is more data. We use the dup property to track extraction
13 progress and the gzip feature to just feed gzip a fd in the middle
14 of an AR file.
15
16 ##################################################################### */
17 /*}}}*/
18// Include Files /*{{{*/
19#ifdef __GNUG__
20#pragma implementation "apt-pkg/extracttar.h"
21#endif
22#include <apt-pkg/extracttar.h>
23
24#include <apt-pkg/error.h>
25#include <apt-pkg/strutl.h>
26#include <apt-pkg/configuration.h>
27#include <system.h>
28
29#include <stdlib.h>
30#include <unistd.h>
31#include <signal.h>
32#include <fcntl.h>
18255546 33#include <iostream.h>
b2e465d6
AL
34 /*}}}*/
35
36// The on disk header for a tar file.
37struct ExtractTar::TarHeader
38{
39 char Name[100];
40 char Mode[8];
41 char UserID[8];
42 char GroupID[8];
43 char Size[12];
44 char MTime[12];
45 char Checksum[8];
46 char LinkFlag;
47 char LinkName[100];
48 char MagicNumber[8];
49 char UserName[32];
50 char GroupName[32];
51 char Major[8];
52 char Minor[8];
53};
54
55// ExtractTar::ExtractTar - Constructor /*{{{*/
56// ---------------------------------------------------------------------
57/* */
58ExtractTar::ExtractTar(FileFd &Fd,unsigned long Max) : File(Fd),
59 MaxInSize(Max)
60
61{
62 GZPid = -1;
63 InFd = -1;
64 Eof = false;
65}
66 /*}}}*/
67// ExtractTar::ExtractTar - Destructor /*{{{*/
68// ---------------------------------------------------------------------
69/* */
70ExtractTar::~ExtractTar()
71{
72 Done(false);
73}
74 /*}}}*/
75// ExtractTar::Done - Reap the gzip sub process /*{{{*/
76// ---------------------------------------------------------------------
77/* If the force flag is given then error messages are suppressed - this
78 means we hit the end of the tar file but there was still gzip data. */
79bool ExtractTar::Done(bool Force)
80{
81 InFd.Close();
82 if (GZPid <= 0)
83 return true;
84
85 /* If there is a pending error then we are cleaning up gzip and are
86 not interested in it's failures */
87 if (_error->PendingError() == true)
88 Force = true;
89
90 // Make sure we clean it up!
91 kill(GZPid,SIGINT);
92 if (ExecWait(GZPid,_config->Find("dir::bin::gzip","/bin/gzip").c_str(),
93 Force) == false)
94 {
95 GZPid = -1;
96 return Force;
97 }
98
99 GZPid = -1;
100 return true;
101}
102 /*}}}*/
103// ExtractTar::StartGzip - Startup gzip /*{{{*/
104// ---------------------------------------------------------------------
105/* This creates a gzip sub process that has its input as the file itself.
106 If this tar file is embedded into something like an ar file then
107 gzip will efficiently ignore the extra bits. */
108bool ExtractTar::StartGzip()
109{
110 int Pipes[2];
111 if (pipe(Pipes) != 0)
112 return _error->Errno("pipe","Failed to create pipes");
113
114 // Fork off the process
115 GZPid = ExecFork();
116
117 // Spawn the subprocess
118 if (GZPid == 0)
119 {
120 // Setup the FDs
121 dup2(Pipes[1],STDOUT_FILENO);
122 dup2(File.Fd(),STDIN_FILENO);
123 int Fd = open("/dev/null",O_RDWR);
124 if (Fd == -1)
125 _exit(101);
126 dup2(Fd,STDERR_FILENO);
127 close(Fd);
128 SetCloseExec(STDOUT_FILENO,false);
129 SetCloseExec(STDIN_FILENO,false);
130 SetCloseExec(STDERR_FILENO,false);
131
132 const char *Args[3];
133 Args[0] = _config->Find("dir::bin::gzip","/bin/gzip").c_str();
134 Args[1] = "-d";
135 Args[2] = 0;
136 execv(Args[0],(char **)Args);
137 cerr << "Failed to exec gzip " << Args[0] << endl;
138 _exit(100);
139 }
140
141 // Fix up our FDs
142 InFd.Fd(Pipes[0]);
143 close(Pipes[1]);
144 return true;
145}
146 /*}}}*/
147// ExtractTar::Go - Perform extraction /*{{{*/
148// ---------------------------------------------------------------------
149/* This reads each 512 byte block from the archive and extracts the header
150 information into the Item structure. Then it resolves the UID/GID and
151 invokes the correct processing function. */
152bool ExtractTar::Go(pkgDirStream &Stream)
153{
154 if (StartGzip() == false)
155 return false;
156
157 // Loop over all blocks
158 string LastLongLink;
159 string LastLongName;
160 while (1)
161 {
162 bool BadRecord = false;
163 unsigned char Block[512];
164 if (InFd.Read(Block,sizeof(Block),true) == false)
165 return false;
166
167 if (InFd.Eof() == true)
168 break;
169
170 // Get the checksum
171 TarHeader *Tar = (TarHeader *)Block;
172 unsigned long CheckSum;
173 if (StrToNum(Tar->Checksum,CheckSum,sizeof(Tar->Checksum),8) == false)
174 return _error->Error("Corrupted archive");
175
176 /* Compute the checksum field. The actual checksum is blanked out
177 with spaces so it is not included in the computation */
178 unsigned long NewSum = 0;
179 memset(Tar->Checksum,' ',sizeof(Tar->Checksum));
180 for (int I = 0; I != sizeof(Block); I++)
181 NewSum += Block[I];
182
183 /* Check for a block of nulls - in this case we kill gzip, GNU tar
184 does this.. */
185 if (NewSum == ' '*sizeof(Tar->Checksum))
186 return Done(true);
187
188 if (NewSum != CheckSum)
189 return _error->Error("Tar Checksum failed, archive corrupted");
190
191 // Decode all of the fields
192 pkgDirStream::Item Itm;
193 unsigned long UID;
194 unsigned long GID;
195 if (StrToNum(Tar->Mode,Itm.Mode,sizeof(Tar->Mode),8) == false ||
196 StrToNum(Tar->UserID,UID,sizeof(Tar->UserID),8) == false ||
197 StrToNum(Tar->GroupID,GID,sizeof(Tar->GroupID),8) == false ||
198 StrToNum(Tar->Size,Itm.Size,sizeof(Tar->Size),8) == false ||
199 StrToNum(Tar->MTime,Itm.MTime,sizeof(Tar->MTime),8) == false ||
200 StrToNum(Tar->Major,Itm.Major,sizeof(Tar->Major),8) == false ||
201 StrToNum(Tar->Minor,Itm.Minor,sizeof(Tar->Minor),8) == false)
202 return _error->Error("Corrupted archive");
203
204 // Grab the filename
205 if (LastLongName.empty() == false)
206 Itm.Name = (char *)LastLongName.c_str();
207 else
208 {
209 Tar->Name[sizeof(Tar->Name)] = 0;
210 Itm.Name = Tar->Name;
211 }
212 if (Itm.Name[0] == '.' && Itm.Name[1] == '/' && Itm.Name[2] != 0)
213 Itm.Name += 2;
214
215 // Grab the link target
216 Tar->Name[sizeof(Tar->LinkName)] = 0;
217 Itm.LinkTarget = Tar->LinkName;
218
219 if (LastLongLink.empty() == false)
220 Itm.LinkTarget = (char *)LastLongLink.c_str();
221
222 // Convert the type over
223 switch (Tar->LinkFlag)
224 {
225 case NormalFile0:
226 case NormalFile:
227 Itm.Type = pkgDirStream::Item::File;
228 break;
229
230 case HardLink:
231 Itm.Type = pkgDirStream::Item::HardLink;
232 break;
233
234 case SymbolicLink:
235 Itm.Type = pkgDirStream::Item::SymbolicLink;
236 break;
237
238 case CharacterDevice:
239 Itm.Type = pkgDirStream::Item::CharDevice;
240 break;
241
242 case BlockDevice:
243 Itm.Type = pkgDirStream::Item::BlockDevice;
244 break;
245
246 case Directory:
247 Itm.Type = pkgDirStream::Item::Directory;
248 break;
249
250 case FIFO:
251 Itm.Type = pkgDirStream::Item::FIFO;
252 break;
253
254 case GNU_LongLink:
255 {
256 unsigned long Length = Itm.Size;
257 unsigned char Block[512];
258 while (Length > 0)
259 {
260 if (InFd.Read(Block,sizeof(Block),true) == false)
261 return false;
262 if (Length <= sizeof(Block))
263 {
264 LastLongLink.append(Block,Block+sizeof(Block));
265 break;
266 }
267 LastLongLink.append(Block,Block+sizeof(Block));
268 Length -= sizeof(Block);
269 }
270 continue;
271 }
272
273 case GNU_LongName:
274 {
275 unsigned long Length = Itm.Size;
276 unsigned char Block[512];
277 while (Length > 0)
278 {
279 if (InFd.Read(Block,sizeof(Block),true) == false)
280 return false;
281 if (Length < sizeof(Block))
282 {
283 LastLongName.append(Block,Block+sizeof(Block));
284 break;
285 }
286 LastLongName.append(Block,Block+sizeof(Block));
287 Length -= sizeof(Block);
288 }
289 continue;
290 }
291
292 default:
293 BadRecord = true;
294 _error->Warning("Unkown TAR header type %u, member %s",(unsigned)Tar->LinkFlag,Tar->Name);
295 break;
296 }
297
298 int Fd = -1;
299 if (BadRecord == false)
300 if (Stream.DoItem(Itm,Fd) == false)
301 return false;
302
303 // Copy the file over the FD
304 unsigned long Size = Itm.Size;
305 while (Size != 0)
306 {
307 unsigned char Junk[32*1024];
308 unsigned long Read = MIN(Size,sizeof(Junk));
309 if (InFd.Read(Junk,((Read+511)/512)*512) == false)
310 return false;
311
312 if (BadRecord == false)
313 {
314 if (Fd > 0)
315 {
316 if (write(Fd,Junk,Read) != (signed)Read)
317 return Stream.Fail(Itm,Fd);
318 }
319 else
320 {
321 /* An Fd of -2 means to send to a special processing
322 function */
323 if (Fd == -2)
324 if (Stream.Process(Itm,Junk,Read,Itm.Size - Size) == false)
325 return Stream.Fail(Itm,Fd);
326 }
327 }
328
329 Size -= Read;
330 }
331
332 // And finish up
333 if (Itm.Size != 0 && BadRecord == false)
334 if (Stream.FinishedFile(Itm,Fd) == false)
335 return false;
336
337 LastLongName.erase();
338 LastLongLink.erase();
339 }
340
341 return Done(false);
342}
343 /*}}}*/