]> git.saurik.com Git - apt.git/blame - apt-inst/deb/debfile.cc
* Simplified HttpMethod::Fetch on http.cc removing Tail variable;
[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
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");
49 return;
50 }
b2e465d6
AL
51}
52 /*}}}*/
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. */
57bool debDebFile::CheckMember(const char *Name)
58{
59 if (AR.FindMember(Name) == 0)
b21c0438 60 return false;
b2e465d6
AL
61 return true;
62}
63 /*}}}*/
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! */
71const ARArchive::Member *debDebFile::GotoMember(const char *Name)
72{
73 // Get the archive member and positition the file
74 const ARArchive::Member *Member = AR.FindMember(Name);
75 if (Member == 0)
76 {
b2e465d6
AL
77 return 0;
78 }
79 if (File.Seek(Member->Start) == false)
80 return 0;
81
82 return Member;
83}
84 /*}}}*/
85// DebFile::ExtractControl - Extract Control information /*{{{*/
86// ---------------------------------------------------------------------
87/* Extract the control information into the Database's temporary
88 directory. */
89bool debDebFile::ExtractControl(pkgDataBase &DB)
90{
91 // Get the archive member and positition the file
92 const ARArchive::Member *Member = GotoMember("control.tar.gz");
93 if (Member == 0)
94 return false;
95
96 // Prepare Tar
97 ControlExtract Extract;
b21c0438 98 ExtractTar Tar(File,Member->Size,"gzip");
b2e465d6
AL
99 if (_error->PendingError() == true)
100 return false;
101
102 // Get into the temporary directory
103 string Cwd = SafeGetCWD();
104 string Tmp;
105 if (DB.GetMetaTmp(Tmp) == false)
106 return false;
107 if (chdir(Tmp.c_str()) != 0)
05eb7df0 108 return _error->Errno("chdir",_("Couldn't change to %s"),Tmp.c_str());
b2e465d6
AL
109
110 // Do extraction
111 if (Tar.Go(Extract) == false)
112 return false;
113
114 // Switch out of the tmp directory.
115 if (chdir(Cwd.c_str()) != 0)
116 chdir("/");
117
118 return true;
119}
120 /*}}}*/
121// DebFile::ExtractArchive - Extract the archive data itself /*{{{*/
122// ---------------------------------------------------------------------
123/* Simple wrapper around tar.. */
124bool debDebFile::ExtractArchive(pkgDirStream &Stream)
125{
126 // Get the archive member and positition the file
127 const ARArchive::Member *Member = AR.FindMember("data.tar.gz");
b21c0438
MZ
128 const char *Compressor = "gzip";
129 if (Member == 0) {
130 Member = AR.FindMember("data.tar.bz2");
131 Compressor = "bzip2";
132 }
b2e465d6 133 if (Member == 0)
db0db9fe 134 return _error->Error(_("Internal error, could not locate member"));
b2e465d6
AL
135 if (File.Seek(Member->Start) == false)
136 return false;
137
138 // Prepare Tar
b21c0438 139 ExtractTar Tar(File,Member->Size,Compressor);
b2e465d6
AL
140 if (_error->PendingError() == true)
141 return false;
142 return Tar.Go(Stream);
143}
144 /*}}}*/
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. */
150pkgCache::VerIterator debDebFile::MergeControl(pkgDataBase &DB)
151{
152 // Open the control file
153 string Tmp;
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());
159
160 // Parse it
161 debListParser Parse(&Fd);
162 pkgCache::VerIterator Ver(DB.GetCache());
163 if (DB.GetGenerator().MergeList(Parse,&Ver) == false)
164 return pkgCache::VerIterator(DB.GetCache());
165
166 if (Ver.end() == true)
05eb7df0 167 _error->Error(_("Failed to locate a valid control file"));
b2e465d6
AL
168 return Ver;
169}
170 /*}}}*/
171
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. */
177bool debDebFile::ControlExtract::DoItem(Item &Itm,int &Fd)
178{
179 if (Itm.Type != Item::File)
180 return true;
181
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++)
185 if (*I == '/')
186 *I = '_';
187
188 /* Force the ownership to be root and ensure correct permissions,
189 go-w, the rest are left untouched */
190 Itm.UID = 0;
191 Itm.GID = 0;
192 Itm.Mode &= ~(S_IWGRP | S_IWOTH);
193
194 return pkgDirStream::DoItem(Itm,Fd);
195}
196 /*}}}*/
197
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. */
202bool debDebFile::MemControlExtract::DoItem(Item &Itm,int &Fd)
203{
204 // At the control file, allocate buffer memory.
205 if (Member == Itm.Name)
206 {
207 delete [] Control;
208 Control = new char[Itm.Size+2];
209 IsControl = true;
210 Fd = -2; // Signal to pass to Process
211 Length = Itm.Size;
212 }
213 else
214 IsControl = false;
215
216 return true;
217}
218 /*}}}*/
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. */
223bool debDebFile::MemControlExtract::Process(Item &Itm,const unsigned char *Data,
224 unsigned long Size,unsigned long Pos)
225{
226 memcpy(Control + Pos, Data,Size);
227 return true;
228}
229 /*}}}*/
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. */
234bool debDebFile::MemControlExtract::Read(debDebFile &Deb)
235{
236 // Get the archive member and positition the file
237 const ARArchive::Member *Member = Deb.GotoMember("control.tar.gz");
238 if (Member == 0)
239 return false;
240
241 // Extract it.
b21c0438 242 ExtractTar Tar(Deb.GetFile(),Member->Size,"gzip");
b2e465d6
AL
243 if (Tar.Go(*this) == false)
244 return false;
245
246 if (Control == 0)
247 return true;
248
249 Control[Length] = '\n';
250 Control[Length+1] = '\n';
251 if (Section.Scan(Control,Length+2) == false)
db0db9fe 252 return _error->Error(_("Unparsable control file"));
b2e465d6
AL
253 return true;
254}
255 /*}}}*/
256// MemControlExtract::TakeControl - Parse a memory block /*{{{*/
257// ---------------------------------------------------------------------
258/* The given memory block is loaded into the parser and parsed as a control
259 record. */
260bool debDebFile::MemControlExtract::TakeControl(const void *Data,unsigned long Size)
261{
262 delete [] Control;
263 Control = new char[Size+2];
264 Length = Size;
265 memcpy(Control,Data,Size);
266
267 Control[Length] = '\n';
268 Control[Length+1] = '\n';
269 return Section.Scan(Control,Length+2);
270}
271 /*}}}*/
272