]>
git.saurik.com Git - apt.git/blob - apt-inst/deb/debfile.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: debfile.cc,v 1.2 2001/02/20 07:03:17 jgg 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>
32 // DebFile::debDebFile - Constructor /*{{{*/
33 // ---------------------------------------------------------------------
34 /* Open the AR file and check for consistency */
35 debDebFile::debDebFile(FileFd
&File
) : File(File
), AR(File
)
37 if (_error
->PendingError() == true)
40 // Check the members for validity
41 if (CheckMember("debian-binary") == false ||
42 CheckMember("control.tar.gz") == false ||
43 CheckMember("data.tar.gz") == false)
47 // DebFile::CheckMember - Check if a named member is in the archive /*{{{*/
48 // ---------------------------------------------------------------------
49 /* This is used to check for a correct deb and to give nicer error messages
50 for people playing around. */
51 bool debDebFile::CheckMember(const char *Name
)
53 if (AR
.FindMember(Name
) == 0)
54 return _error
->Error("This is not a valid DEB archive, missing '%s' member",Name
);
58 // DebFile::GotoMember - Jump to a Member /*{{{*/
59 // ---------------------------------------------------------------------
60 /* Jump in the file to the start of a named member and return the information
61 about that member. The caller can then read from the file up to the
62 returned size. Note, since this relies on the file position this is
63 a destructive operation, it also changes the last returned Member
64 structure - so don't nest them! */
65 const ARArchive::Member
*debDebFile::GotoMember(const char *Name
)
67 // Get the archive member and positition the file
68 const ARArchive::Member
*Member
= AR
.FindMember(Name
);
71 _error
->Error("Internal Error, could not locate member %s",Name
);
74 if (File
.Seek(Member
->Start
) == false)
80 // DebFile::ExtractControl - Extract Control information /*{{{*/
81 // ---------------------------------------------------------------------
82 /* Extract the control information into the Database's temporary
84 bool debDebFile::ExtractControl(pkgDataBase
&DB
)
86 // Get the archive member and positition the file
87 const ARArchive::Member
*Member
= GotoMember("control.tar.gz");
92 ControlExtract Extract
;
93 ExtractTar
Tar(File
,Member
->Size
);
94 if (_error
->PendingError() == true)
97 // Get into the temporary directory
98 string Cwd
= SafeGetCWD();
100 if (DB
.GetMetaTmp(Tmp
) == false)
102 if (chdir(Tmp
.c_str()) != 0)
103 return _error
->Errno("chdir","Couldn't change to %s",Tmp
.c_str());
106 if (Tar
.Go(Extract
) == false)
109 // Switch out of the tmp directory.
110 if (chdir(Cwd
.c_str()) != 0)
116 // DebFile::ExtractArchive - Extract the archive data itself /*{{{*/
117 // ---------------------------------------------------------------------
118 /* Simple wrapper around tar.. */
119 bool debDebFile::ExtractArchive(pkgDirStream
&Stream
)
121 // Get the archive member and positition the file
122 const ARArchive::Member
*Member
= AR
.FindMember("data.tar.gz");
124 return _error
->Error("Internal Error, could not locate member");
125 if (File
.Seek(Member
->Start
) == false)
129 ExtractTar
Tar(File
,Member
->Size
);
130 if (_error
->PendingError() == true)
132 return Tar
.Go(Stream
);
135 // DebFile::MergeControl - Merge the control information /*{{{*/
136 // ---------------------------------------------------------------------
137 /* This reads the extracted control file into the cache and returns the
138 version that was parsed. All this really does is select the correct
139 parser and correct file to parse. */
140 pkgCache::VerIterator
debDebFile::MergeControl(pkgDataBase
&DB
)
142 // Open the control file
144 if (DB
.GetMetaTmp(Tmp
) == false)
145 return pkgCache::VerIterator(DB
.GetCache());
146 FileFd
Fd(Tmp
+ "control",FileFd::ReadOnly
);
147 if (_error
->PendingError() == true)
148 return pkgCache::VerIterator(DB
.GetCache());
151 debListParser
Parse(&Fd
);
152 pkgCache::VerIterator
Ver(DB
.GetCache());
153 if (DB
.GetGenerator().MergeList(Parse
,&Ver
) == false)
154 return pkgCache::VerIterator(DB
.GetCache());
156 if (Ver
.end() == true)
157 _error
->Error("Failed to locate a valid control file");
162 // DebFile::ControlExtract::DoItem - Control Tar Extraction /*{{{*/
163 // ---------------------------------------------------------------------
164 /* This directory stream handler for the control tar handles extracting
165 it into the temporary meta directory. It only extracts files, it does
166 not create directories, links or anything else. */
167 bool debDebFile::ControlExtract::DoItem(Item
&Itm
,int &Fd
)
169 if (Itm
.Type
!= Item::File
)
172 /* Cleanse the file name, prevent people from trying to unpack into
173 absolute paths, .., etc */
174 for (char *I
= Itm
.Name
; *I
!= 0; I
++)
178 /* Force the ownership to be root and ensure correct permissions,
179 go-w, the rest are left untouched */
182 Itm
.Mode
&= ~(S_IWGRP
| S_IWOTH
);
184 return pkgDirStream::DoItem(Itm
,Fd
);
188 // MemControlExtract::DoItem - Check if it is the control file /*{{{*/
189 // ---------------------------------------------------------------------
190 /* This sets up to extract the control block member file into a memory
191 block of just the right size. All other files go into the bit bucket. */
192 bool debDebFile::MemControlExtract::DoItem(Item
&Itm
,int &Fd
)
194 // At the control file, allocate buffer memory.
195 if (Member
== Itm
.Name
)
198 Control
= new char[Itm
.Size
+2];
200 Fd
= -2; // Signal to pass to Process
209 // MemControlExtract::Process - Process extracting the control file /*{{{*/
210 // ---------------------------------------------------------------------
211 /* Just memcopy the block from the tar extractor and put it in the right
212 place in the pre-allocated memory block. */
213 bool debDebFile::MemControlExtract::Process(Item
&Itm
,const unsigned char *Data
,
214 unsigned long Size
,unsigned long Pos
)
216 memcpy(Control
+ Pos
, Data
,Size
);
220 // MemControlExtract::Read - Read the control information from the deb /*{{{*/
221 // ---------------------------------------------------------------------
222 /* This uses the internal tar extractor to fetch the control file, and then
223 it parses it into a tag section parser. */
224 bool debDebFile::MemControlExtract::Read(debDebFile
&Deb
)
226 // Get the archive member and positition the file
227 const ARArchive::Member
*Member
= Deb
.GotoMember("control.tar.gz");
232 ExtractTar
Tar(Deb
.GetFile(),Member
->Size
);
233 if (Tar
.Go(*this) == false)
239 Control
[Length
] = '\n';
240 Control
[Length
+1] = '\n';
241 if (Section
.Scan(Control
,Length
+2) == false)
242 return _error
->Error("Unparsible control file");
246 // MemControlExtract::TakeControl - Parse a memory block /*{{{*/
247 // ---------------------------------------------------------------------
248 /* The given memory block is loaded into the parser and parsed as a control
250 bool debDebFile::MemControlExtract::TakeControl(const void *Data
,unsigned long Size
)
253 Control
= new char[Size
+2];
255 memcpy(Control
,Data
,Size
);
257 Control
[Length
] = '\n';
258 Control
[Length
+1] = '\n';
259 return Section
.Scan(Control
,Length
+2);