]>
git.saurik.com Git - apt.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 /*{{{*/
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 _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");
55 // DebFile::CheckMember - Check if a named member is in the archive /*{{{*/
56 // ---------------------------------------------------------------------
57 /* This is used to check for a correct deb and to give nicer error messages
58 for people playing around. */
59 bool debDebFile::CheckMember(const char *Name
)
61 if (AR
.FindMember(Name
) == 0)
66 // DebFile::GotoMember - Jump to a Member /*{{{*/
67 // ---------------------------------------------------------------------
68 /* Jump in the file to the start of a named member and return the information
69 about that member. The caller can then read from the file up to the
70 returned size. Note, since this relies on the file position this is
71 a destructive operation, it also changes the last returned Member
72 structure - so don't nest them! */
73 const ARArchive::Member
*debDebFile::GotoMember(const char *Name
)
75 // Get the archive member and positition the file
76 const ARArchive::Member
*Member
= AR
.FindMember(Name
);
81 if (File
.Seek(Member
->Start
) == false)
87 // DebFile::ExtractControl - Extract Control information /*{{{*/
88 // ---------------------------------------------------------------------
89 /* Extract the control information into the Database's temporary
91 bool debDebFile::ExtractControl(pkgDataBase
&DB
)
93 // Get the archive member and positition the file
94 const ARArchive::Member
*Member
= GotoMember("control.tar.gz");
99 ControlExtract Extract
;
100 ExtractTar
Tar(File
,Member
->Size
,"gzip");
101 if (_error
->PendingError() == true)
104 // Get into the temporary directory
105 string Cwd
= SafeGetCWD();
107 if (DB
.GetMetaTmp(Tmp
) == false)
109 if (chdir(Tmp
.c_str()) != 0)
110 return _error
->Errno("chdir",_("Couldn't change to %s"),Tmp
.c_str());
113 if (Tar
.Go(Extract
) == false)
116 // Switch out of the tmp directory.
117 if (chdir(Cwd
.c_str()) != 0)
123 // DebFile::ExtractArchive - Extract the archive data itself /*{{{*/
124 // ---------------------------------------------------------------------
125 /* Simple wrapper around tar.. */
126 bool debDebFile::ExtractArchive(pkgDirStream
&Stream
)
128 // Get the archive member and positition the file
129 const ARArchive::Member
*Member
= AR
.FindMember("data.tar.gz");
130 const char *Compressor
= "gzip";
132 Member
= AR
.FindMember("data.tar.bz2");
133 Compressor
= "bzip2";
136 Member
= AR
.FindMember("data.tar.lzma");
140 return _error
->Error(_("Internal error, could not locate member"));
141 if (File
.Seek(Member
->Start
) == false)
145 ExtractTar
Tar(File
,Member
->Size
,Compressor
);
146 if (_error
->PendingError() == true)
148 return Tar
.Go(Stream
);
151 // DebFile::MergeControl - Merge the control information /*{{{*/
152 // ---------------------------------------------------------------------
153 /* This reads the extracted control file into the cache and returns the
154 version that was parsed. All this really does is select the correct
155 parser and correct file to parse. */
156 pkgCache::VerIterator
debDebFile::MergeControl(pkgDataBase
&DB
)
158 // Open the control file
160 if (DB
.GetMetaTmp(Tmp
) == false)
161 return pkgCache::VerIterator(DB
.GetCache());
162 FileFd
Fd(Tmp
+ "control",FileFd::ReadOnly
);
163 if (_error
->PendingError() == true)
164 return pkgCache::VerIterator(DB
.GetCache());
167 debListParser
Parse(&Fd
);
168 pkgCache::VerIterator
Ver(DB
.GetCache());
169 if (DB
.GetGenerator().MergeList(Parse
,&Ver
) == false)
170 return pkgCache::VerIterator(DB
.GetCache());
172 if (Ver
.end() == true)
173 _error
->Error(_("Failed to locate a valid control file"));
178 // DebFile::ControlExtract::DoItem - Control Tar Extraction /*{{{*/
179 // ---------------------------------------------------------------------
180 /* This directory stream handler for the control tar handles extracting
181 it into the temporary meta directory. It only extracts files, it does
182 not create directories, links or anything else. */
183 bool debDebFile::ControlExtract::DoItem(Item
&Itm
,int &Fd
)
185 if (Itm
.Type
!= Item::File
)
188 /* Cleanse the file name, prevent people from trying to unpack into
189 absolute paths, .., etc */
190 for (char *I
= Itm
.Name
; *I
!= 0; I
++)
194 /* Force the ownership to be root and ensure correct permissions,
195 go-w, the rest are left untouched */
198 Itm
.Mode
&= ~(S_IWGRP
| S_IWOTH
);
200 return pkgDirStream::DoItem(Itm
,Fd
);
204 // MemControlExtract::DoItem - Check if it is the control file /*{{{*/
205 // ---------------------------------------------------------------------
206 /* This sets up to extract the control block member file into a memory
207 block of just the right size. All other files go into the bit bucket. */
208 bool debDebFile::MemControlExtract::DoItem(Item
&Itm
,int &Fd
)
210 // At the control file, allocate buffer memory.
211 if (Member
== Itm
.Name
)
214 Control
= new char[Itm
.Size
+2];
216 Fd
= -2; // Signal to pass to Process
225 // MemControlExtract::Process - Process extracting the control file /*{{{*/
226 // ---------------------------------------------------------------------
227 /* Just memcopy the block from the tar extractor and put it in the right
228 place in the pre-allocated memory block. */
229 bool debDebFile::MemControlExtract::Process(Item
&Itm
,const unsigned char *Data
,
230 unsigned long Size
,unsigned long Pos
)
232 memcpy(Control
+ Pos
, Data
,Size
);
236 // MemControlExtract::Read - Read the control information from the deb /*{{{*/
237 // ---------------------------------------------------------------------
238 /* This uses the internal tar extractor to fetch the control file, and then
239 it parses it into a tag section parser. */
240 bool debDebFile::MemControlExtract::Read(debDebFile
&Deb
)
242 // Get the archive member and positition the file
243 const ARArchive::Member
*Member
= Deb
.GotoMember("control.tar.gz");
248 ExtractTar
Tar(Deb
.GetFile(),Member
->Size
,"gzip");
249 if (Tar
.Go(*this) == false)
255 Control
[Length
] = '\n';
256 Control
[Length
+1] = '\n';
257 if (Section
.Scan(Control
,Length
+2) == false)
258 return _error
->Error(_("Unparsable control file"));
262 // MemControlExtract::TakeControl - Parse a memory block /*{{{*/
263 // ---------------------------------------------------------------------
264 /* The given memory block is loaded into the parser and parsed as a control
266 bool debDebFile::MemControlExtract::TakeControl(const void *Data
,unsigned long Size
)
269 Control
= new char[Size
+2];
271 memcpy(Control
,Data
,Size
);
273 Control
[Length
] = '\n';
274 Control
[Length
+1] = '\n';
275 return Section
.Scan(Control
,Length
+2);