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