]>
git.saurik.com Git - apt-legacy.git/blob - apt-inst/deb/debfile.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: debfile.cc,v 1.3.2.1 2004/01/16 18:58:50 mdz Exp $
4 /* ######################################################################
6 Debian Archive File (.deb)
8 .DEB archives are AR files containing two tars and an empty marker
9 member called 'debian-binary'. The two tars contain the meta data and
10 the actual archive contents. Thus this class is a very simple wrapper
11 around ar/tar to simply extract the right tar files.
13 It also uses the deb package list parser to parse the control file
16 ##################################################################### */
18 // Include Files /*{{{*/
20 #pragma implementation "apt-pkg/debfile.h"
23 #include <apt-pkg/debfile.h>
24 #include <apt-pkg/extracttar.h>
25 #include <apt-pkg/error.h>
26 #include <apt-pkg/deblistparser.h>
33 // DebFile::debDebFile - Constructor /*{{{*/
34 // ---------------------------------------------------------------------
35 /* Open the AR file and check for consistency */
36 debDebFile::debDebFile(FileFd
&File
) : File(File
), AR(File
)
38 if (_error
->PendingError() == true)
41 if (!CheckMember("debian-binary")) {
42 _error
->Error(_("This is not a valid DEB archive, missing '%s' member"), "debian-binary");
46 if (!CheckMember("control.tar.gz")) {
47 _error
->Error(_("This is not a valid DEB archive, missing '%s' member"), "control.tar.gz");
51 if (!CheckMember("data.tar.gz") && !CheckMember("data.tar.bz2")) {
52 _error
->Error(_("This is not a valid DEB archive, it has no '%s' or '%s' member"), "data.tar.gz", "data.tar.bz2");
57 // DebFile::CheckMember - Check if a named member is in the archive /*{{{*/
58 // ---------------------------------------------------------------------
59 /* This is used to check for a correct deb and to give nicer error messages
60 for people playing around. */
61 bool debDebFile::CheckMember(const char *Name
)
63 if (AR
.FindMember(Name
) == 0)
68 // DebFile::GotoMember - Jump to a Member /*{{{*/
69 // ---------------------------------------------------------------------
70 /* Jump in the file to the start of a named member and return the information
71 about that member. The caller can then read from the file up to the
72 returned size. Note, since this relies on the file position this is
73 a destructive operation, it also changes the last returned Member
74 structure - so don't nest them! */
75 const ARArchive::Member
*debDebFile::GotoMember(const char *Name
)
77 // Get the archive member and positition the file
78 const ARArchive::Member
*Member
= AR
.FindMember(Name
);
83 if (File
.Seek(Member
->Start
) == false)
89 // DebFile::ExtractControl - Extract Control information /*{{{*/
90 // ---------------------------------------------------------------------
91 /* Extract the control information into the Database's temporary
93 bool debDebFile::ExtractControl(pkgDataBase
&DB
)
95 // Get the archive member and positition the file
96 const ARArchive::Member
*Member
= GotoMember("control.tar.gz");
101 ControlExtract Extract
;
102 ExtractTar
Tar(File
,Member
->Size
,"gzip");
103 if (_error
->PendingError() == true)
106 // Get into the temporary directory
107 string Cwd
= SafeGetCWD();
109 if (DB
.GetMetaTmp(Tmp
) == false)
111 if (chdir(Tmp
.c_str()) != 0)
112 return _error
->Errno("chdir",_("Couldn't change to %s"),Tmp
.c_str());
115 if (Tar
.Go(Extract
) == false)
118 // Switch out of the tmp directory.
119 if (chdir(Cwd
.c_str()) != 0)
125 // DebFile::ExtractArchive - Extract the archive data itself /*{{{*/
126 // ---------------------------------------------------------------------
127 /* Simple wrapper around tar.. */
128 bool debDebFile::ExtractArchive(pkgDirStream
&Stream
)
130 // Get the archive member and positition the file
131 const ARArchive::Member
*Member
= AR
.FindMember("data.tar.gz");
132 const char *Compressor
= "gzip";
134 Member
= AR
.FindMember("data.tar.bz2");
135 Compressor
= "bzip2";
138 return _error
->Error(_("Internal error, could not locate member"));
139 if (File
.Seek(Member
->Start
) == false)
143 ExtractTar
Tar(File
,Member
->Size
,Compressor
);
144 if (_error
->PendingError() == true)
146 return Tar
.Go(Stream
);
149 // DebFile::MergeControl - Merge the control information /*{{{*/
150 // ---------------------------------------------------------------------
151 /* This reads the extracted control file into the cache and returns the
152 version that was parsed. All this really does is select the correct
153 parser and correct file to parse. */
154 pkgCache::VerIterator
debDebFile::MergeControl(pkgDataBase
&DB
)
156 // Open the control file
158 if (DB
.GetMetaTmp(Tmp
) == false)
159 return pkgCache::VerIterator(DB
.GetCache());
160 FileFd
Fd(Tmp
+ "control",FileFd::ReadOnly
);
161 if (_error
->PendingError() == true)
162 return pkgCache::VerIterator(DB
.GetCache());
165 debListParser
Parse(&Fd
);
166 pkgCache::VerIterator
Ver(DB
.GetCache());
167 if (DB
.GetGenerator().MergeList(Parse
,&Ver
) == false)
168 return pkgCache::VerIterator(DB
.GetCache());
170 if (Ver
.end() == true)
171 _error
->Error(_("Failed to locate a valid control file"));
176 // DebFile::ControlExtract::DoItem - Control Tar Extraction /*{{{*/
177 // ---------------------------------------------------------------------
178 /* This directory stream handler for the control tar handles extracting
179 it into the temporary meta directory. It only extracts files, it does
180 not create directories, links or anything else. */
181 bool debDebFile::ControlExtract::DoItem(Item
&Itm
,int &Fd
)
183 if (Itm
.Type
!= Item::File
)
186 /* Cleanse the file name, prevent people from trying to unpack into
187 absolute paths, .., etc */
188 for (char *I
= Itm
.Name
; *I
!= 0; I
++)
192 /* Force the ownership to be root and ensure correct permissions,
193 go-w, the rest are left untouched */
196 Itm
.Mode
&= ~(S_IWGRP
| S_IWOTH
);
198 return pkgDirStream::DoItem(Itm
,Fd
);
202 // MemControlExtract::DoItem - Check if it is the control file /*{{{*/
203 // ---------------------------------------------------------------------
204 /* This sets up to extract the control block member file into a memory
205 block of just the right size. All other files go into the bit bucket. */
206 bool debDebFile::MemControlExtract::DoItem(Item
&Itm
,int &Fd
)
208 // At the control file, allocate buffer memory.
209 if (Member
== Itm
.Name
)
212 Control
= new char[Itm
.Size
+2];
214 Fd
= -2; // Signal to pass to Process
223 // MemControlExtract::Process - Process extracting the control file /*{{{*/
224 // ---------------------------------------------------------------------
225 /* Just memcopy the block from the tar extractor and put it in the right
226 place in the pre-allocated memory block. */
227 bool debDebFile::MemControlExtract::Process(Item
&Itm
,const unsigned char *Data
,
228 unsigned long Size
,unsigned long Pos
)
230 memcpy(Control
+ Pos
, Data
,Size
);
234 // MemControlExtract::Read - Read the control information from the deb /*{{{*/
235 // ---------------------------------------------------------------------
236 /* This uses the internal tar extractor to fetch the control file, and then
237 it parses it into a tag section parser. */
238 bool debDebFile::MemControlExtract::Read(debDebFile
&Deb
)
240 // Get the archive member and positition the file
241 const ARArchive::Member
*Member
= Deb
.GotoMember("control.tar.gz");
246 ExtractTar
Tar(Deb
.GetFile(),Member
->Size
,"gzip");
247 if (Tar
.Go(*this) == false)
253 Control
[Length
] = '\n';
254 Control
[Length
+1] = '\n';
255 if (Section
.Scan(Control
,Length
+2) == false)
256 return _error
->Error(_("Unparsable control file"));
260 // MemControlExtract::TakeControl - Parse a memory block /*{{{*/
261 // ---------------------------------------------------------------------
262 /* The given memory block is loaded into the parser and parsed as a control
264 bool debDebFile::MemControlExtract::TakeControl(const void *Data
,unsigned long Size
)
267 Control
= new char[Size
+2];
269 memcpy(Control
,Data
,Size
);
271 Control
[Length
] = '\n';
272 Control
[Length
+1] = '\n';
273 return Section
.Scan(Control
,Length
+2);