]>
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") && !CheckMember("data.tar.bz2")) {
48 _error
->Error(_("This is not a valid DEB archive, it has no '%s' or '%s' member"), "data.tar.gz", "data.tar.bz2");
53 // DebFile::CheckMember - Check if a named member is in the archive /*{{{*/
54 // ---------------------------------------------------------------------
55 /* This is used to check for a correct deb and to give nicer error messages
56 for people playing around. */
57 bool debDebFile::CheckMember(const char *Name
)
59 if (AR
.FindMember(Name
) == 0)
64 // DebFile::GotoMember - Jump to a Member /*{{{*/
65 // ---------------------------------------------------------------------
66 /* Jump in the file to the start of a named member and return the information
67 about that member. The caller can then read from the file up to the
68 returned size. Note, since this relies on the file position this is
69 a destructive operation, it also changes the last returned Member
70 structure - so don't nest them! */
71 const ARArchive::Member
*debDebFile::GotoMember(const char *Name
)
73 // Get the archive member and positition the file
74 const ARArchive::Member
*Member
= AR
.FindMember(Name
);
79 if (File
.Seek(Member
->Start
) == false)
85 // DebFile::ExtractControl - Extract Control information /*{{{*/
86 // ---------------------------------------------------------------------
87 /* Extract the control information into the Database's temporary
89 bool debDebFile::ExtractControl(pkgDataBase
&DB
)
91 // Get the archive member and positition the file
92 const ARArchive::Member
*Member
= GotoMember("control.tar.gz");
97 ControlExtract Extract
;
98 ExtractTar
Tar(File
,Member
->Size
,"gzip");
99 if (_error
->PendingError() == true)
102 // Get into the temporary directory
103 string Cwd
= SafeGetCWD();
105 if (DB
.GetMetaTmp(Tmp
) == false)
107 if (chdir(Tmp
.c_str()) != 0)
108 return _error
->Errno("chdir",_("Couldn't change to %s"),Tmp
.c_str());
111 if (Tar
.Go(Extract
) == false)
114 // Switch out of the tmp directory.
115 if (chdir(Cwd
.c_str()) != 0)
121 // DebFile::ExtractArchive - Extract the archive data itself /*{{{*/
122 // ---------------------------------------------------------------------
123 /* Simple wrapper around tar.. */
124 bool debDebFile::ExtractArchive(pkgDirStream
&Stream
)
126 // Get the archive member and positition the file
127 const ARArchive::Member
*Member
= AR
.FindMember("data.tar.gz");
128 const char *Compressor
= "gzip";
130 Member
= AR
.FindMember("data.tar.bz2");
131 Compressor
= "bzip2";
134 return _error
->Error(_("Internal error, could not locate member"));
135 if (File
.Seek(Member
->Start
) == false)
139 ExtractTar
Tar(File
,Member
->Size
,Compressor
);
140 if (_error
->PendingError() == true)
142 return Tar
.Go(Stream
);
145 // DebFile::MergeControl - Merge the control information /*{{{*/
146 // ---------------------------------------------------------------------
147 /* This reads the extracted control file into the cache and returns the
148 version that was parsed. All this really does is select the correct
149 parser and correct file to parse. */
150 pkgCache::VerIterator
debDebFile::MergeControl(pkgDataBase
&DB
)
152 // Open the control file
154 if (DB
.GetMetaTmp(Tmp
) == false)
155 return pkgCache::VerIterator(DB
.GetCache());
156 FileFd
Fd(Tmp
+ "control",FileFd::ReadOnly
);
157 if (_error
->PendingError() == true)
158 return pkgCache::VerIterator(DB
.GetCache());
161 debListParser
Parse(&Fd
);
162 pkgCache::VerIterator
Ver(DB
.GetCache());
163 if (DB
.GetGenerator().MergeList(Parse
,&Ver
) == false)
164 return pkgCache::VerIterator(DB
.GetCache());
166 if (Ver
.end() == true)
167 _error
->Error(_("Failed to locate a valid control file"));
172 // DebFile::ControlExtract::DoItem - Control Tar Extraction /*{{{*/
173 // ---------------------------------------------------------------------
174 /* This directory stream handler for the control tar handles extracting
175 it into the temporary meta directory. It only extracts files, it does
176 not create directories, links or anything else. */
177 bool debDebFile::ControlExtract::DoItem(Item
&Itm
,int &Fd
)
179 if (Itm
.Type
!= Item::File
)
182 /* Cleanse the file name, prevent people from trying to unpack into
183 absolute paths, .., etc */
184 for (char *I
= Itm
.Name
; *I
!= 0; I
++)
188 /* Force the ownership to be root and ensure correct permissions,
189 go-w, the rest are left untouched */
192 Itm
.Mode
&= ~(S_IWGRP
| S_IWOTH
);
194 return pkgDirStream::DoItem(Itm
,Fd
);
198 // MemControlExtract::DoItem - Check if it is the control file /*{{{*/
199 // ---------------------------------------------------------------------
200 /* This sets up to extract the control block member file into a memory
201 block of just the right size. All other files go into the bit bucket. */
202 bool debDebFile::MemControlExtract::DoItem(Item
&Itm
,int &Fd
)
204 // At the control file, allocate buffer memory.
205 if (Member
== Itm
.Name
)
208 Control
= new char[Itm
.Size
+2];
210 Fd
= -2; // Signal to pass to Process
219 // MemControlExtract::Process - Process extracting the control file /*{{{*/
220 // ---------------------------------------------------------------------
221 /* Just memcopy the block from the tar extractor and put it in the right
222 place in the pre-allocated memory block. */
223 bool debDebFile::MemControlExtract::Process(Item
&Itm
,const unsigned char *Data
,
224 unsigned long Size
,unsigned long Pos
)
226 memcpy(Control
+ Pos
, Data
,Size
);
230 // MemControlExtract::Read - Read the control information from the deb /*{{{*/
231 // ---------------------------------------------------------------------
232 /* This uses the internal tar extractor to fetch the control file, and then
233 it parses it into a tag section parser. */
234 bool debDebFile::MemControlExtract::Read(debDebFile
&Deb
)
236 // Get the archive member and positition the file
237 const ARArchive::Member
*Member
= Deb
.GotoMember("control.tar.gz");
242 ExtractTar
Tar(Deb
.GetFile(),Member
->Size
,"gzip");
243 if (Tar
.Go(*this) == false)
249 Control
[Length
] = '\n';
250 Control
[Length
+1] = '\n';
251 if (Section
.Scan(Control
,Length
+2) == false)
252 return _error
->Error(_("Unparsable control file"));
256 // MemControlExtract::TakeControl - Parse a memory block /*{{{*/
257 // ---------------------------------------------------------------------
258 /* The given memory block is loaded into the parser and parsed as a control
260 bool debDebFile::MemControlExtract::TakeControl(const void *Data
,unsigned long Size
)
263 Control
= new char[Size
+2];
265 memcpy(Control
,Data
,Size
);
267 Control
[Length
] = '\n';
268 Control
[Length
+1] = '\n';
269 return Section
.Scan(Control
,Length
+2);