]> git.saurik.com Git - apt.git/blob - apt-inst/deb/debfile.cc
* add --dsc-only option, thanks to K. Richard Pixley
[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 #ifdef __GNUG__
20 #pragma implementation "apt-pkg/debfile.h"
21 #endif
22
23 #include <apt-pkg/debfile.h>
24 #include <apt-pkg/extracttar.h>
25 #include <apt-pkg/error.h>
26 #include <apt-pkg/deblistparser.h>
27
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <apti18n.h>
31 /*}}}*/
32
33 // DebFile::debDebFile - Constructor /*{{{*/
34 // ---------------------------------------------------------------------
35 /* Open the AR file and check for consistency */
36 debDebFile::debDebFile(FileFd &File) : File(File), AR(File)
37 {
38 if (_error->PendingError() == true)
39 return;
40
41 if (!CheckMember("debian-binary")) {
42 _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "debian-binary");
43 return;
44 }
45
46 if (!CheckMember("control.tar.gz")) {
47 _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "control.tar.gz");
48 return;
49 }
50
51 if (!CheckMember("data.tar.gz") && !CheckMember("data.tar.bz2")) {
52 _error->Error(_("This is not a valid DEB archive, it has no '%s' or '%s' member"), "data.tar.gz", "data.tar.bz2");
53 return;
54 }
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. */
61 bool debDebFile::CheckMember(const char *Name)
62 {
63 if (AR.FindMember(Name) == 0)
64 return false;
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! */
75 const 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 {
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. */
93 bool 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;
102 ExtractTar Tar(File,Member->Size,"gzip");
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)
112 return _error->Errno("chdir",_("Couldn't change to %s"),Tmp.c_str());
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.. */
128 bool debDebFile::ExtractArchive(pkgDirStream &Stream)
129 {
130 // Get the archive member and positition the file
131 const ARArchive::Member *Member = AR.FindMember("data.tar.gz");
132 const char *Compressor = "gzip";
133 if (Member == 0) {
134 Member = AR.FindMember("data.tar.bz2");
135 Compressor = "bzip2";
136 }
137 if (Member == 0)
138 return _error->Error(_("Internal error, could not locate member"));
139 if (File.Seek(Member->Start) == false)
140 return false;
141
142 // Prepare Tar
143 ExtractTar Tar(File,Member->Size,Compressor);
144 if (_error->PendingError() == true)
145 return false;
146 return Tar.Go(Stream);
147 }
148 /*}}}*/
149 // DebFile::MergeControl - Merge the control information /*{{{*/
150 // ---------------------------------------------------------------------
151 /* This reads the extracted control file into the cache and returns the
152 version that was parsed. All this really does is select the correct
153 parser and correct file to parse. */
154 pkgCache::VerIterator debDebFile::MergeControl(pkgDataBase &DB)
155 {
156 // Open the control file
157 string Tmp;
158 if (DB.GetMetaTmp(Tmp) == false)
159 return pkgCache::VerIterator(DB.GetCache());
160 FileFd Fd(Tmp + "control",FileFd::ReadOnly);
161 if (_error->PendingError() == true)
162 return pkgCache::VerIterator(DB.GetCache());
163
164 // Parse it
165 debListParser Parse(&Fd);
166 pkgCache::VerIterator Ver(DB.GetCache());
167 if (DB.GetGenerator().MergeList(Parse,&Ver) == false)
168 return pkgCache::VerIterator(DB.GetCache());
169
170 if (Ver.end() == true)
171 _error->Error(_("Failed to locate a valid control file"));
172 return Ver;
173 }
174 /*}}}*/
175
176 // DebFile::ControlExtract::DoItem - Control Tar Extraction /*{{{*/
177 // ---------------------------------------------------------------------
178 /* This directory stream handler for the control tar handles extracting
179 it into the temporary meta directory. It only extracts files, it does
180 not create directories, links or anything else. */
181 bool debDebFile::ControlExtract::DoItem(Item &Itm,int &Fd)
182 {
183 if (Itm.Type != Item::File)
184 return true;
185
186 /* Cleanse the file name, prevent people from trying to unpack into
187 absolute paths, .., etc */
188 for (char *I = Itm.Name; *I != 0; I++)
189 if (*I == '/')
190 *I = '_';
191
192 /* Force the ownership to be root and ensure correct permissions,
193 go-w, the rest are left untouched */
194 Itm.UID = 0;
195 Itm.GID = 0;
196 Itm.Mode &= ~(S_IWGRP | S_IWOTH);
197
198 return pkgDirStream::DoItem(Itm,Fd);
199 }
200 /*}}}*/
201
202 // MemControlExtract::DoItem - Check if it is the control file /*{{{*/
203 // ---------------------------------------------------------------------
204 /* This sets up to extract the control block member file into a memory
205 block of just the right size. All other files go into the bit bucket. */
206 bool debDebFile::MemControlExtract::DoItem(Item &Itm,int &Fd)
207 {
208 // At the control file, allocate buffer memory.
209 if (Member == Itm.Name)
210 {
211 delete [] Control;
212 Control = new char[Itm.Size+2];
213 IsControl = true;
214 Fd = -2; // Signal to pass to Process
215 Length = Itm.Size;
216 }
217 else
218 IsControl = false;
219
220 return true;
221 }
222 /*}}}*/
223 // MemControlExtract::Process - Process extracting the control file /*{{{*/
224 // ---------------------------------------------------------------------
225 /* Just memcopy the block from the tar extractor and put it in the right
226 place in the pre-allocated memory block. */
227 bool debDebFile::MemControlExtract::Process(Item &Itm,const unsigned char *Data,
228 unsigned long Size,unsigned long Pos)
229 {
230 memcpy(Control + Pos, Data,Size);
231 return true;
232 }
233 /*}}}*/
234 // MemControlExtract::Read - Read the control information from the deb /*{{{*/
235 // ---------------------------------------------------------------------
236 /* This uses the internal tar extractor to fetch the control file, and then
237 it parses it into a tag section parser. */
238 bool debDebFile::MemControlExtract::Read(debDebFile &Deb)
239 {
240 // Get the archive member and positition the file
241 const ARArchive::Member *Member = Deb.GotoMember("control.tar.gz");
242 if (Member == 0)
243 return false;
244
245 // Extract it.
246 ExtractTar Tar(Deb.GetFile(),Member->Size,"gzip");
247 if (Tar.Go(*this) == false)
248 return false;
249
250 if (Control == 0)
251 return true;
252
253 Control[Length] = '\n';
254 Control[Length+1] = '\n';
255 if (Section.Scan(Control,Length+2) == false)
256 return _error->Error(_("Unparsable control file"));
257 return true;
258 }
259 /*}}}*/
260 // MemControlExtract::TakeControl - Parse a memory block /*{{{*/
261 // ---------------------------------------------------------------------
262 /* The given memory block is loaded into the parser and parsed as a control
263 record. */
264 bool debDebFile::MemControlExtract::TakeControl(const void *Data,unsigned long Size)
265 {
266 delete [] Control;
267 Control = new char[Size+2];
268 Length = Size;
269 memcpy(Control,Data,Size);
270
271 Control[Length] = '\n';
272 Control[Length+1] = '\n';
273 return Section.Scan(Control,Length+2);
274 }
275 /*}}}*/
276