]>
git.saurik.com Git - apt.git/blob - apt-inst/deb/debfile.cc
2b1fdedc8070e4be7538e6caffd2d500e4a5e113
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 /*{{{*/
19 #include <apt-pkg/debfile.h>
20 #include <apt-pkg/extracttar.h>
21 #include <apt-pkg/error.h>
22 #include <apt-pkg/deblistparser.h>
29 // DebFile::debDebFile - Constructor /*{{{*/
30 // ---------------------------------------------------------------------
31 /* Open the AR file and check for consistency */
32 debDebFile::debDebFile(FileFd
&File
) : File(File
), AR(File
)
34 if (_error
->PendingError() == true)
37 if (!CheckMember("debian-binary")) {
38 _error
->Error(_("This is not a valid DEB archive, missing '%s' member"), "debian-binary");
42 if (!CheckMember("control.tar.gz")) {
43 _error
->Error(_("This is not a valid DEB archive, missing '%s' member"), "control.tar.gz");
47 if (!CheckMember("data.tar.gz") &&
48 !CheckMember("data.tar.bz2") &&
49 !CheckMember("data.tar.lzma") &&
50 !CheckMember("data.tar.xz")) {
51 // FIXME: add data.tar.xz here - adding it now would require a Translation round for a very small gain
52 _error
->Error(_("This is not a valid DEB archive, it has no '%s', '%s' or '%s' member"), "data.tar.gz", "data.tar.bz2", "data.tar.lzma");
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 Member
= AR
.FindMember("data.tar.lzma");
142 Member
= AR
.FindMember("data.tar.xz");
146 return _error
->Error(_("Internal error, could not locate member %s"), "data.tar.{gz,bz2,lzma,xz}");
147 if (File
.Seek(Member
->Start
) == false)
151 ExtractTar
Tar(File
,Member
->Size
,Compressor
);
152 if (_error
->PendingError() == true)
154 return Tar
.Go(Stream
);
157 // DebFile::MergeControl - Merge the control information /*{{{*/
158 // ---------------------------------------------------------------------
159 /* This reads the extracted control file into the cache and returns the
160 version that was parsed. All this really does is select the correct
161 parser and correct file to parse. */
162 pkgCache::VerIterator
debDebFile::MergeControl(pkgDataBase
&DB
)
164 // Open the control file
166 if (DB
.GetMetaTmp(Tmp
) == false)
167 return pkgCache::VerIterator(DB
.GetCache());
168 FileFd
Fd(Tmp
+ "control",FileFd::ReadOnly
);
169 if (_error
->PendingError() == true)
170 return pkgCache::VerIterator(DB
.GetCache());
173 debListParser
Parse(&Fd
);
174 pkgCache::VerIterator
Ver(DB
.GetCache());
175 if (DB
.GetGenerator().MergeList(Parse
,&Ver
) == false)
176 return pkgCache::VerIterator(DB
.GetCache());
178 if (Ver
.end() == true)
179 _error
->Error(_("Failed to locate a valid control file"));
184 // DebFile::ControlExtract::DoItem - Control Tar Extraction /*{{{*/
185 // ---------------------------------------------------------------------
186 /* This directory stream handler for the control tar handles extracting
187 it into the temporary meta directory. It only extracts files, it does
188 not create directories, links or anything else. */
189 bool debDebFile::ControlExtract::DoItem(Item
&Itm
,int &Fd
)
191 if (Itm
.Type
!= Item::File
)
194 /* Cleanse the file name, prevent people from trying to unpack into
195 absolute paths, .., etc */
196 for (char *I
= Itm
.Name
; *I
!= 0; I
++)
200 /* Force the ownership to be root and ensure correct permissions,
201 go-w, the rest are left untouched */
204 Itm
.Mode
&= ~(S_IWGRP
| S_IWOTH
);
206 return pkgDirStream::DoItem(Itm
,Fd
);
210 // MemControlExtract::DoItem - Check if it is the control file /*{{{*/
211 // ---------------------------------------------------------------------
212 /* This sets up to extract the control block member file into a memory
213 block of just the right size. All other files go into the bit bucket. */
214 bool debDebFile::MemControlExtract::DoItem(Item
&Itm
,int &Fd
)
216 // At the control file, allocate buffer memory.
217 if (Member
== Itm
.Name
)
220 Control
= new char[Itm
.Size
+2];
222 Fd
= -2; // Signal to pass to Process
231 // MemControlExtract::Process - Process extracting the control file /*{{{*/
232 // ---------------------------------------------------------------------
233 /* Just memcopy the block from the tar extractor and put it in the right
234 place in the pre-allocated memory block. */
235 bool debDebFile::MemControlExtract::Process(Item
&Itm
,const unsigned char *Data
,
236 unsigned long Size
,unsigned long Pos
)
238 memcpy(Control
+ Pos
, Data
,Size
);
242 // MemControlExtract::Read - Read the control information from the deb /*{{{*/
243 // ---------------------------------------------------------------------
244 /* This uses the internal tar extractor to fetch the control file, and then
245 it parses it into a tag section parser. */
246 bool debDebFile::MemControlExtract::Read(debDebFile
&Deb
)
248 // Get the archive member and positition the file
249 const ARArchive::Member
*Member
= Deb
.GotoMember("control.tar.gz");
254 ExtractTar
Tar(Deb
.GetFile(),Member
->Size
,"gzip");
255 if (Tar
.Go(*this) == false)
261 Control
[Length
] = '\n';
262 Control
[Length
+1] = '\n';
263 if (Section
.Scan(Control
,Length
+2) == false)
264 return _error
->Error(_("Unparsable control file"));
268 // MemControlExtract::TakeControl - Parse a memory block /*{{{*/
269 // ---------------------------------------------------------------------
270 /* The given memory block is loaded into the parser and parsed as a control
272 bool debDebFile::MemControlExtract::TakeControl(const void *Data
,unsigned long Size
)
275 Control
= new char[Size
+2];
277 memcpy(Control
,Data
,Size
);
279 Control
[Length
] = '\n';
280 Control
[Length
+1] = '\n';
281 return Section
.Scan(Control
,Length
+2);