]> git.saurik.com Git - apt.git/blame - apt-inst/deb/debfile.cc
* ftparchive/multicompress.cc, apt-inst/deb/debfile.cc:
[apt.git] / apt-inst / deb / debfile.cc
CommitLineData
b2e465d6
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
7db98ffc 3// $Id: debfile.cc,v 1.3.2.1 2004/01/16 18:58:50 mdz Exp $
b2e465d6
AL
4/* ######################################################################
5
6 Debian Archive File (.deb)
7
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.
12
13 It also uses the deb package list parser to parse the control file
14 into the cache.
15
16 ##################################################################### */
17 /*}}}*/
18// Include Files /*{{{*/
b2e465d6
AL
19#include <apt-pkg/debfile.h>
20#include <apt-pkg/extracttar.h>
21#include <apt-pkg/error.h>
22#include <apt-pkg/deblistparser.h>
23
24#include <sys/stat.h>
25#include <unistd.h>
d77559ac 26#include <apti18n.h>
b2e465d6
AL
27 /*}}}*/
28
29// DebFile::debDebFile - Constructor /*{{{*/
30// ---------------------------------------------------------------------
31/* Open the AR file and check for consistency */
32debDebFile::debDebFile(FileFd &File) : File(File), AR(File)
33{
34 if (_error->PendingError() == true)
35 return;
b21c0438
MZ
36
37 if (!CheckMember("debian-binary")) {
38 _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "debian-binary");
39 return;
40 }
41
42 if (!CheckMember("control.tar.gz")) {
43 _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "control.tar.gz");
b2e465d6 44 return;
b21c0438
MZ
45 }
46
ac005224
OS
47 if (!CheckMember("data.tar.gz") &&
48 !CheckMember("data.tar.bz2") &&
bc33e0f0
DK
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
ac005224 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");
b21c0438
MZ
53 return;
54 }
b2e465d6
AL
55}
56 /*}}}*/
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. */
61bool debDebFile::CheckMember(const char *Name)
62{
63 if (AR.FindMember(Name) == 0)
b21c0438 64 return false;
b2e465d6
AL
65 return true;
66}
67 /*}}}*/
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! */
75const ARArchive::Member *debDebFile::GotoMember(const char *Name)
76{
77 // Get the archive member and positition the file
78 const ARArchive::Member *Member = AR.FindMember(Name);
79 if (Member == 0)
80 {
b2e465d6
AL
81 return 0;
82 }
83 if (File.Seek(Member->Start) == false)
84 return 0;
85
86 return Member;
87}
88 /*}}}*/
89// DebFile::ExtractControl - Extract Control information /*{{{*/
90// ---------------------------------------------------------------------
91/* Extract the control information into the Database's temporary
92 directory. */
93bool debDebFile::ExtractControl(pkgDataBase &DB)
94{
95 // Get the archive member and positition the file
96 const ARArchive::Member *Member = GotoMember("control.tar.gz");
97 if (Member == 0)
98 return false;
99
100 // Prepare Tar
101 ControlExtract Extract;
b21c0438 102 ExtractTar Tar(File,Member->Size,"gzip");
b2e465d6
AL
103 if (_error->PendingError() == true)
104 return false;
105
106 // Get into the temporary directory
107 string Cwd = SafeGetCWD();
108 string Tmp;
109 if (DB.GetMetaTmp(Tmp) == false)
110 return false;
111 if (chdir(Tmp.c_str()) != 0)
05eb7df0 112 return _error->Errno("chdir",_("Couldn't change to %s"),Tmp.c_str());
b2e465d6
AL
113
114 // Do extraction
115 if (Tar.Go(Extract) == false)
116 return false;
117
118 // Switch out of the tmp directory.
119 if (chdir(Cwd.c_str()) != 0)
120 chdir("/");
121
122 return true;
123}
124 /*}}}*/
125// DebFile::ExtractArchive - Extract the archive data itself /*{{{*/
126// ---------------------------------------------------------------------
127/* Simple wrapper around tar.. */
128bool debDebFile::ExtractArchive(pkgDirStream &Stream)
129{
130 // Get the archive member and positition the file
131 const ARArchive::Member *Member = AR.FindMember("data.tar.gz");
b21c0438
MZ
132 const char *Compressor = "gzip";
133 if (Member == 0) {
134 Member = AR.FindMember("data.tar.bz2");
135 Compressor = "bzip2";
136 }
ac005224
OS
137 if (Member == 0) {
138 Member = AR.FindMember("data.tar.lzma");
139 Compressor = "lzma";
140 }
bc33e0f0
DK
141 if (Member == 0) {
142 Member = AR.FindMember("data.tar.xz");
143 Compressor = "xz";
144 }
b2e465d6 145 if (Member == 0)
db0db9fe 146 return _error->Error(_("Internal error, could not locate member"));
b2e465d6
AL
147 if (File.Seek(Member->Start) == false)
148 return false;
149
150 // Prepare Tar
b21c0438 151 ExtractTar Tar(File,Member->Size,Compressor);
b2e465d6
AL
152 if (_error->PendingError() == true)
153 return false;
154 return Tar.Go(Stream);
155}
156 /*}}}*/
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. */
162pkgCache::VerIterator debDebFile::MergeControl(pkgDataBase &DB)
163{
164 // Open the control file
165 string Tmp;
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());
171
172 // Parse it
173 debListParser Parse(&Fd);
174 pkgCache::VerIterator Ver(DB.GetCache());
175 if (DB.GetGenerator().MergeList(Parse,&Ver) == false)
176 return pkgCache::VerIterator(DB.GetCache());
177
178 if (Ver.end() == true)
05eb7df0 179 _error->Error(_("Failed to locate a valid control file"));
b2e465d6
AL
180 return Ver;
181}
182 /*}}}*/
183
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. */
189bool debDebFile::ControlExtract::DoItem(Item &Itm,int &Fd)
190{
191 if (Itm.Type != Item::File)
192 return true;
193
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++)
197 if (*I == '/')
198 *I = '_';
199
200 /* Force the ownership to be root and ensure correct permissions,
201 go-w, the rest are left untouched */
202 Itm.UID = 0;
203 Itm.GID = 0;
204 Itm.Mode &= ~(S_IWGRP | S_IWOTH);
205
206 return pkgDirStream::DoItem(Itm,Fd);
207}
208 /*}}}*/
209
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. */
214bool debDebFile::MemControlExtract::DoItem(Item &Itm,int &Fd)
215{
216 // At the control file, allocate buffer memory.
217 if (Member == Itm.Name)
218 {
219 delete [] Control;
220 Control = new char[Itm.Size+2];
221 IsControl = true;
222 Fd = -2; // Signal to pass to Process
223 Length = Itm.Size;
224 }
225 else
226 IsControl = false;
227
228 return true;
229}
230 /*}}}*/
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. */
235bool debDebFile::MemControlExtract::Process(Item &Itm,const unsigned char *Data,
236 unsigned long Size,unsigned long Pos)
237{
238 memcpy(Control + Pos, Data,Size);
239 return true;
240}
241 /*}}}*/
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. */
246bool debDebFile::MemControlExtract::Read(debDebFile &Deb)
247{
248 // Get the archive member and positition the file
249 const ARArchive::Member *Member = Deb.GotoMember("control.tar.gz");
250 if (Member == 0)
251 return false;
252
253 // Extract it.
b21c0438 254 ExtractTar Tar(Deb.GetFile(),Member->Size,"gzip");
b2e465d6
AL
255 if (Tar.Go(*this) == false)
256 return false;
257
258 if (Control == 0)
259 return true;
260
261 Control[Length] = '\n';
262 Control[Length+1] = '\n';
263 if (Section.Scan(Control,Length+2) == false)
db0db9fe 264 return _error->Error(_("Unparsable control file"));
b2e465d6
AL
265 return true;
266}
267 /*}}}*/
268// MemControlExtract::TakeControl - Parse a memory block /*{{{*/
269// ---------------------------------------------------------------------
270/* The given memory block is loaded into the parser and parsed as a control
271 record. */
272bool debDebFile::MemControlExtract::TakeControl(const void *Data,unsigned long Size)
273{
274 delete [] Control;
275 Control = new char[Size+2];
276 Length = Size;
277 memcpy(Control,Data,Size);
278
279 Control[Length] = '\n';
280 Control[Length+1] = '\n';
281 return Section.Scan(Control,Length+2);
282}
283 /*}}}*/
284