1 // -*- mode: cpp; mode: fold -*-
3 // $Id: extracttar.cc,v 1.8.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
6 Extract a Tar - Tar Extractor
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
16 ##################################################################### */
18 // Include Files /*{{{*/
21 #include <apt-pkg/dirstream.h>
22 #include <apt-pkg/extracttar.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/strutl.h>
25 #include <apt-pkg/configuration.h>
26 #include <apt-pkg/macros.h>
39 // The on disk header for a tar file.
40 struct ExtractTar::TarHeader
58 // ExtractTar::ExtractTar - Constructor /*{{{*/
59 // ---------------------------------------------------------------------
61 ExtractTar::ExtractTar(FileFd
&Fd
,unsigned long Max
,string DecompressionProgram
) : File(Fd
),
62 MaxInSize(Max
), DecompressProg(DecompressionProgram
)
69 // ExtractTar::ExtractTar - Destructor /*{{{*/
70 // ---------------------------------------------------------------------
72 ExtractTar::~ExtractTar()
78 // ExtractTar::Done - Reap the gzip sub process /*{{{*/
79 // ---------------------------------------------------------------------
80 /* If the force flag is given then error messages are suppressed - this
81 means we hit the end of the tar file but there was still gzip data. */
82 bool ExtractTar::Done(bool Force
)
88 /* If there is a pending error then we are cleaning up gzip and are
89 not interested in it's failures */
90 if (_error
->PendingError() == true)
93 // Make sure we clean it up!
95 string confvar
= string("dir::bin::") + DecompressProg
;
96 if (ExecWait(GZPid
,_config
->Find(confvar
.c_str(),DecompressProg
.c_str()).c_str(),
107 // ExtractTar::StartGzip - Startup gzip /*{{{*/
108 // ---------------------------------------------------------------------
109 /* This creates a gzip sub process that has its input as the file itself.
110 If this tar file is embedded into something like an ar file then
111 gzip will efficiently ignore the extra bits. */
112 bool ExtractTar::StartGzip()
115 if (pipe(Pipes
) != 0)
116 return _error
->Errno("pipe",_("Failed to create pipes"));
118 // Fork off the process
121 // Spawn the subprocess
125 dup2(Pipes
[1],STDOUT_FILENO
);
126 dup2(File
.Fd(),STDIN_FILENO
);
127 int Fd
= open("/dev/null",O_RDWR
);
130 dup2(Fd
,STDERR_FILENO
);
132 SetCloseExec(STDOUT_FILENO
,false);
133 SetCloseExec(STDIN_FILENO
,false);
134 SetCloseExec(STDERR_FILENO
,false);
137 string confvar
= string("dir::bin::") + DecompressProg
;
138 string argv0
= _config
->Find(confvar
.c_str(),DecompressProg
.c_str());
139 Args
[0] = argv0
.c_str();
142 execvp(Args
[0],(char **)Args
);
143 cerr
<< _("Failed to exec gzip ") << Args
[0] << endl
;
148 InFd
.OpenDescriptor(Pipes
[0], FileFd::ReadOnly
, FileFd::None
, true);
153 // ExtractTar::Go - Perform extraction /*{{{*/
154 // ---------------------------------------------------------------------
155 /* This reads each 512 byte block from the archive and extracts the header
156 information into the Item structure. Then it resolves the UID/GID and
157 invokes the correct processing function. */
158 bool ExtractTar::Go(pkgDirStream
&Stream
)
160 if (StartGzip() == false)
163 // Loop over all blocks
168 bool BadRecord
= false;
169 unsigned char Block
[512];
170 if (InFd
.Read(Block
,sizeof(Block
),true) == false)
173 if (InFd
.Eof() == true)
177 TarHeader
*Tar
= (TarHeader
*)Block
;
178 unsigned long CheckSum
;
179 if (StrToNum(Tar
->Checksum
,CheckSum
,sizeof(Tar
->Checksum
),8) == false)
180 return _error
->Error(_("Corrupted archive"));
182 /* Compute the checksum field. The actual checksum is blanked out
183 with spaces so it is not included in the computation */
184 unsigned long NewSum
= 0;
185 memset(Tar
->Checksum
,' ',sizeof(Tar
->Checksum
));
186 for (int I
= 0; I
!= sizeof(Block
); I
++)
189 /* Check for a block of nulls - in this case we kill gzip, GNU tar
191 if (NewSum
== ' '*sizeof(Tar
->Checksum
))
194 if (NewSum
!= CheckSum
)
195 return _error
->Error(_("Tar checksum failed, archive corrupted"));
197 // Decode all of the fields
198 pkgDirStream::Item Itm
;
199 if (StrToNum(Tar
->Mode
,Itm
.Mode
,sizeof(Tar
->Mode
),8) == false ||
200 (Base256ToNum(Tar
->UserID
,Itm
.UID
,8) == false &&
201 StrToNum(Tar
->UserID
,Itm
.UID
,sizeof(Tar
->UserID
),8) == false) ||
202 (Base256ToNum(Tar
->GroupID
,Itm
.GID
,8) == false &&
203 StrToNum(Tar
->GroupID
,Itm
.GID
,sizeof(Tar
->GroupID
),8) == false) ||
204 (Base256ToNum(Tar
->Size
,Itm
.Size
,12) == false &&
205 StrToNum(Tar
->Size
,Itm
.Size
,sizeof(Tar
->Size
),8) == false) ||
206 (Base256ToNum(Tar
->MTime
,Itm
.MTime
,12) == false &&
207 StrToNum(Tar
->MTime
,Itm
.MTime
,sizeof(Tar
->MTime
),8) == false) ||
208 StrToNum(Tar
->Major
,Itm
.Major
,sizeof(Tar
->Major
),8) == false ||
209 StrToNum(Tar
->Minor
,Itm
.Minor
,sizeof(Tar
->Minor
),8) == false)
210 return _error
->Error(_("Corrupted archive"));
213 if (LastLongName
.empty() == false)
214 Itm
.Name
= (char *)LastLongName
.c_str();
217 Tar
->Name
[sizeof(Tar
->Name
)-1] = 0;
218 Itm
.Name
= Tar
->Name
;
220 if (Itm
.Name
[0] == '.' && Itm
.Name
[1] == '/' && Itm
.Name
[2] != 0)
223 // Grab the link target
224 Tar
->Name
[sizeof(Tar
->LinkName
)-1] = 0;
225 Itm
.LinkTarget
= Tar
->LinkName
;
227 if (LastLongLink
.empty() == false)
228 Itm
.LinkTarget
= (char *)LastLongLink
.c_str();
230 // Convert the type over
231 switch (Tar
->LinkFlag
)
235 Itm
.Type
= pkgDirStream::Item::File
;
239 Itm
.Type
= pkgDirStream::Item::HardLink
;
243 Itm
.Type
= pkgDirStream::Item::SymbolicLink
;
246 case CharacterDevice
:
247 Itm
.Type
= pkgDirStream::Item::CharDevice
;
251 Itm
.Type
= pkgDirStream::Item::BlockDevice
;
255 Itm
.Type
= pkgDirStream::Item::Directory
;
259 Itm
.Type
= pkgDirStream::Item::FIFO
;
264 unsigned long Length
= Itm
.Size
;
265 unsigned char Block
[512];
268 if (InFd
.Read(Block
,sizeof(Block
),true) == false)
270 if (Length
<= sizeof(Block
))
272 LastLongLink
.append(Block
,Block
+sizeof(Block
));
275 LastLongLink
.append(Block
,Block
+sizeof(Block
));
276 Length
-= sizeof(Block
);
283 unsigned long Length
= Itm
.Size
;
284 unsigned char Block
[512];
287 if (InFd
.Read(Block
,sizeof(Block
),true) == false)
289 if (Length
< sizeof(Block
))
291 LastLongName
.append(Block
,Block
+sizeof(Block
));
294 LastLongName
.append(Block
,Block
+sizeof(Block
));
295 Length
-= sizeof(Block
);
302 _error
->Warning(_("Unknown TAR header type %u, member %s"),(unsigned)Tar
->LinkFlag
,Tar
->Name
);
307 if (BadRecord
== false)
308 if (Stream
.DoItem(Itm
,Fd
) == false)
311 // Copy the file over the FD
312 unsigned long Size
= Itm
.Size
;
315 unsigned char Junk
[32*1024];
316 unsigned long Read
= min(Size
,(unsigned long)sizeof(Junk
));
317 if (InFd
.Read(Junk
,((Read
+511)/512)*512) == false)
320 if (BadRecord
== false)
324 if (write(Fd
,Junk
,Read
) != (signed)Read
)
325 return Stream
.Fail(Itm
,Fd
);
329 /* An Fd of -2 means to send to a special processing
332 if (Stream
.Process(Itm
,Junk
,Read
,Itm
.Size
- Size
) == false)
333 return Stream
.Fail(Itm
,Fd
);
341 if (BadRecord
== false)
342 if (Stream
.FinishedFile(Itm
,Fd
) == false)
345 LastLongName
.erase();
346 LastLongLink
.erase();