]> git.saurik.com Git - apt.git/blob - apt-inst/deb/debfile.cc
enable APT in unpack/configure ordering to handle loops as well
[apt.git] / apt-inst / deb / debfile.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: debfile.cc,v 1.3.2.1 2004/01/16 18:58:50 mdz Exp $
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 /*{{{*/
19 #include<config.h>
20
21 #include <apt-pkg/debfile.h>
22 #include <apt-pkg/extracttar.h>
23 #include <apt-pkg/error.h>
24 #include <apt-pkg/deblistparser.h>
25 #include <apt-pkg/aptconfiguration.h>
26
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <apti18n.h>
30 /*}}}*/
31
32 // DebFile::debDebFile - Constructor /*{{{*/
33 // ---------------------------------------------------------------------
34 /* Open the AR file and check for consistency */
35 debDebFile::debDebFile(FileFd &File) : File(File), AR(File)
36 {
37 if (_error->PendingError() == true)
38 return;
39
40 if (!CheckMember("debian-binary")) {
41 _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "debian-binary");
42 return;
43 }
44
45 if (!CheckMember("control.tar.gz")) {
46 _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "control.tar.gz");
47 return;
48 }
49
50 if (!CheckMember("data.tar.gz") &&
51 !CheckMember("data.tar.bz2") &&
52 !CheckMember("data.tar.lzma") &&
53 !CheckMember("data.tar.xz")) {
54 // FIXME: add data.tar.xz here - adding it now would require a Translation round for a very small gain
55 _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");
56 return;
57 }
58 }
59 /*}}}*/
60 // DebFile::CheckMember - Check if a named member is in the archive /*{{{*/
61 // ---------------------------------------------------------------------
62 /* This is used to check for a correct deb and to give nicer error messages
63 for people playing around. */
64 bool debDebFile::CheckMember(const char *Name)
65 {
66 if (AR.FindMember(Name) == 0)
67 return false;
68 return true;
69 }
70 /*}}}*/
71 // DebFile::GotoMember - Jump to a Member /*{{{*/
72 // ---------------------------------------------------------------------
73 /* Jump in the file to the start of a named member and return the information
74 about that member. The caller can then read from the file up to the
75 returned size. Note, since this relies on the file position this is
76 a destructive operation, it also changes the last returned Member
77 structure - so don't nest them! */
78 const ARArchive::Member *debDebFile::GotoMember(const char *Name)
79 {
80 // Get the archive member and positition the file
81 const ARArchive::Member *Member = AR.FindMember(Name);
82 if (Member == 0)
83 {
84 return 0;
85 }
86 if (File.Seek(Member->Start) == false)
87 return 0;
88
89 return Member;
90 }
91 /*}}}*/
92 // DebFile::ExtractControl - Extract Control information /*{{{*/
93 // ---------------------------------------------------------------------
94 /* Extract the control information into the Database's temporary
95 directory. */
96 bool debDebFile::ExtractControl(pkgDataBase &DB)
97 {
98 // Get the archive member and positition the file
99 const ARArchive::Member *Member = GotoMember("control.tar.gz");
100 if (Member == 0)
101 return false;
102
103 // Prepare Tar
104 ControlExtract Extract;
105 ExtractTar Tar(File,Member->Size,"gzip");
106 if (_error->PendingError() == true)
107 return false;
108
109 // Get into the temporary directory
110 string Cwd = SafeGetCWD();
111 string Tmp;
112 if (DB.GetMetaTmp(Tmp) == false)
113 return false;
114 if (chdir(Tmp.c_str()) != 0)
115 return _error->Errno("chdir",_("Couldn't change to %s"),Tmp.c_str());
116
117 // Do extraction
118 if (Tar.Go(Extract) == false)
119 return false;
120
121 // Switch out of the tmp directory.
122 if (chdir(Cwd.c_str()) != 0)
123 chdir("/");
124
125 return true;
126 }
127 /*}}}*/
128 // DebFile::ExtractArchive - Extract the archive data itself /*{{{*/
129 // ---------------------------------------------------------------------
130 /* Simple wrapper around tar.. */
131 bool debDebFile::ExtractArchive(pkgDirStream &Stream)
132 {
133 // Get the archive member
134 const ARArchive::Member *Member = NULL;
135 std::string Compressor;
136
137 std::string const data = "data.tar";
138 std::vector<APT::Configuration::Compressor> compressor = APT::Configuration::getCompressors();
139 for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
140 c != compressor.end(); ++c)
141 {
142 Member = AR.FindMember(std::string(data).append(c->Extension).c_str());
143 if (Member == NULL)
144 continue;
145 Compressor = c->Binary;
146 break;
147 }
148
149 if (Member == NULL)
150 {
151 std::string ext = "data.tar.{";
152 for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
153 c != compressor.end(); ++c)
154 ext.append(c->Extension.substr(1));
155 ext.append("}");
156 return _error->Error(_("Internal error, could not locate member %s"), ext.c_str());
157 }
158
159 if (File.Seek(Member->Start) == false)
160 return false;
161
162 // Prepare Tar
163 ExtractTar Tar(File,Member->Size,Compressor);
164 if (_error->PendingError() == true)
165 return false;
166 return Tar.Go(Stream);
167 }
168 /*}}}*/
169 // DebFile::MergeControl - Merge the control information /*{{{*/
170 // ---------------------------------------------------------------------
171 /* This reads the extracted control file into the cache and returns the
172 version that was parsed. All this really does is select the correct
173 parser and correct file to parse. */
174 pkgCache::VerIterator debDebFile::MergeControl(pkgDataBase &DB)
175 {
176 // Open the control file
177 string Tmp;
178 if (DB.GetMetaTmp(Tmp) == false)
179 return pkgCache::VerIterator(DB.GetCache());
180 FileFd Fd(Tmp + "control",FileFd::ReadOnly);
181 if (_error->PendingError() == true)
182 return pkgCache::VerIterator(DB.GetCache());
183
184 // Parse it
185 debListParser Parse(&Fd);
186 pkgCache::VerIterator Ver(DB.GetCache());
187 if (DB.GetGenerator().MergeList(Parse,&Ver) == false)
188 return pkgCache::VerIterator(DB.GetCache());
189
190 if (Ver.end() == true)
191 _error->Error(_("Failed to locate a valid control file"));
192 return Ver;
193 }
194 /*}}}*/
195
196 // DebFile::ControlExtract::DoItem - Control Tar Extraction /*{{{*/
197 // ---------------------------------------------------------------------
198 /* This directory stream handler for the control tar handles extracting
199 it into the temporary meta directory. It only extracts files, it does
200 not create directories, links or anything else. */
201 bool debDebFile::ControlExtract::DoItem(Item &Itm,int &Fd)
202 {
203 if (Itm.Type != Item::File)
204 return true;
205
206 /* Cleanse the file name, prevent people from trying to unpack into
207 absolute paths, .., etc */
208 for (char *I = Itm.Name; *I != 0; I++)
209 if (*I == '/')
210 *I = '_';
211
212 /* Force the ownership to be root and ensure correct permissions,
213 go-w, the rest are left untouched */
214 Itm.UID = 0;
215 Itm.GID = 0;
216 Itm.Mode &= ~(S_IWGRP | S_IWOTH);
217
218 return pkgDirStream::DoItem(Itm,Fd);
219 }
220 /*}}}*/
221
222 // MemControlExtract::DoItem - Check if it is the control file /*{{{*/
223 // ---------------------------------------------------------------------
224 /* This sets up to extract the control block member file into a memory
225 block of just the right size. All other files go into the bit bucket. */
226 bool debDebFile::MemControlExtract::DoItem(Item &Itm,int &Fd)
227 {
228 // At the control file, allocate buffer memory.
229 if (Member == Itm.Name)
230 {
231 delete [] Control;
232 Control = new char[Itm.Size+2];
233 IsControl = true;
234 Fd = -2; // Signal to pass to Process
235 Length = Itm.Size;
236 }
237 else
238 IsControl = false;
239
240 return true;
241 }
242 /*}}}*/
243 // MemControlExtract::Process - Process extracting the control file /*{{{*/
244 // ---------------------------------------------------------------------
245 /* Just memcopy the block from the tar extractor and put it in the right
246 place in the pre-allocated memory block. */
247 bool debDebFile::MemControlExtract::Process(Item &Itm,const unsigned char *Data,
248 unsigned long Size,unsigned long Pos)
249 {
250 memcpy(Control + Pos, Data,Size);
251 return true;
252 }
253 /*}}}*/
254 // MemControlExtract::Read - Read the control information from the deb /*{{{*/
255 // ---------------------------------------------------------------------
256 /* This uses the internal tar extractor to fetch the control file, and then
257 it parses it into a tag section parser. */
258 bool debDebFile::MemControlExtract::Read(debDebFile &Deb)
259 {
260 // Get the archive member and positition the file
261 const ARArchive::Member *Member = Deb.GotoMember("control.tar.gz");
262 if (Member == 0)
263 return false;
264
265 // Extract it.
266 ExtractTar Tar(Deb.GetFile(),Member->Size,"gzip");
267 if (Tar.Go(*this) == false)
268 return false;
269
270 if (Control == 0)
271 return true;
272
273 Control[Length] = '\n';
274 Control[Length+1] = '\n';
275 if (Section.Scan(Control,Length+2) == false)
276 return _error->Error(_("Unparsable control file"));
277 return true;
278 }
279 /*}}}*/
280 // MemControlExtract::TakeControl - Parse a memory block /*{{{*/
281 // ---------------------------------------------------------------------
282 /* The given memory block is loaded into the parser and parsed as a control
283 record. */
284 bool debDebFile::MemControlExtract::TakeControl(const void *Data,unsigned long Size)
285 {
286 delete [] Control;
287 Control = new char[Size+2];
288 Length = Size;
289 memcpy(Control,Data,Size);
290
291 Control[Length] = '\n';
292 Control[Length+1] = '\n';
293 return Section.Scan(Control,Length+2);
294 }
295 /*}}}*/
296