]> git.saurik.com Git - apt.git/blob - apt-inst/deb/debfile.cc
Release 0.9.16.1
[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/aptconfiguration.h>
25 #include <apt-pkg/arfile.h>
26 #include <apt-pkg/dirstream.h>
27 #include <apt-pkg/fileutl.h>
28 #include <apt-pkg/tagfile.h>
29
30 #include <string.h>
31 #include <string>
32 #include <vector>
33 #include <sys/stat.h>
34
35 #include <apti18n.h>
36 /*}}}*/
37
38 // DebFile::debDebFile - Constructor /*{{{*/
39 // ---------------------------------------------------------------------
40 /* Open the AR file and check for consistency */
41 debDebFile::debDebFile(FileFd &File) : File(File), AR(File)
42 {
43 if (_error->PendingError() == true)
44 return;
45
46 if (!CheckMember("debian-binary")) {
47 _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "debian-binary");
48 return;
49 }
50
51 if (!CheckMember("control.tar") &&
52 !CheckMember("control.tar.gz") &&
53 !CheckMember("control.tar.xz")) {
54 _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "control.tar");
55 return;
56 }
57
58 if (!CheckMember("data.tar") &&
59 !CheckMember("data.tar.gz") &&
60 !CheckMember("data.tar.bz2") &&
61 !CheckMember("data.tar.lzma") &&
62 !CheckMember("data.tar.xz")) {
63 _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "data.tar");
64 return;
65 }
66 }
67 /*}}}*/
68 // DebFile::CheckMember - Check if a named member is in the archive /*{{{*/
69 // ---------------------------------------------------------------------
70 /* This is used to check for a correct deb and to give nicer error messages
71 for people playing around. */
72 bool debDebFile::CheckMember(const char *Name)
73 {
74 if (AR.FindMember(Name) == 0)
75 return false;
76 return true;
77 }
78 /*}}}*/
79 // DebFile::GotoMember - Jump to a Member /*{{{*/
80 // ---------------------------------------------------------------------
81 /* Jump in the file to the start of a named member and return the information
82 about that member. The caller can then read from the file up to the
83 returned size. Note, since this relies on the file position this is
84 a destructive operation, it also changes the last returned Member
85 structure - so don't nest them! */
86 const ARArchive::Member *debDebFile::GotoMember(const char *Name)
87 {
88 // Get the archive member and positition the file
89 const ARArchive::Member *Member = AR.FindMember(Name);
90 if (Member == 0)
91 {
92 return 0;
93 }
94 if (File.Seek(Member->Start) == false)
95 return 0;
96
97 return Member;
98 }
99 /*}}}*/
100 // DebFile::ExtractTarMember - Extract the contents of a tar member /*{{{*/
101 // ---------------------------------------------------------------------
102 /* Simple wrapper around tar.. */
103 bool debDebFile::ExtractTarMember(pkgDirStream &Stream,const char *Name)
104 {
105 // Get the archive member
106 const ARArchive::Member *Member = NULL;
107 std::string Compressor;
108
109 std::vector<APT::Configuration::Compressor> compressor = APT::Configuration::getCompressors();
110 for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
111 c != compressor.end(); ++c)
112 {
113 Member = AR.FindMember(std::string(Name).append(c->Extension).c_str());
114 if (Member == NULL)
115 continue;
116 Compressor = c->Binary;
117 break;
118 }
119
120 if (Member == NULL)
121 Member = AR.FindMember(std::string(Name).c_str());
122
123 if (Member == NULL)
124 {
125 std::string ext = std::string(Name) + ".{";
126 for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
127 c != compressor.end(); ++c)
128 ext.append(c->Extension.substr(1));
129 ext.append("}");
130 return _error->Error(_("Internal error, could not locate member %s"), ext.c_str());
131 }
132
133 if (File.Seek(Member->Start) == false)
134 return false;
135
136 // Prepare Tar
137 ExtractTar Tar(File,Member->Size,Compressor);
138 if (_error->PendingError() == true)
139 return false;
140 return Tar.Go(Stream);
141 }
142 /*}}}*/
143 // DebFile::ExtractArchive - Extract the archive data itself /*{{{*/
144 // ---------------------------------------------------------------------
145 /* Simple wrapper around DebFile::ExtractTarMember. */
146 bool debDebFile::ExtractArchive(pkgDirStream &Stream)
147 {
148 return ExtractTarMember(Stream, "data.tar");
149 }
150 /*}}}*/
151
152 // DebFile::ControlExtract::DoItem - Control Tar Extraction /*{{{*/
153 // ---------------------------------------------------------------------
154 /* This directory stream handler for the control tar handles extracting
155 it into the temporary meta directory. It only extracts files, it does
156 not create directories, links or anything else. */
157 bool debDebFile::ControlExtract::DoItem(Item &Itm,int &Fd)
158 {
159 if (Itm.Type != Item::File)
160 return true;
161
162 /* Cleanse the file name, prevent people from trying to unpack into
163 absolute paths, .., etc */
164 for (char *I = Itm.Name; *I != 0; I++)
165 if (*I == '/')
166 *I = '_';
167
168 /* Force the ownership to be root and ensure correct permissions,
169 go-w, the rest are left untouched */
170 Itm.UID = 0;
171 Itm.GID = 0;
172 Itm.Mode &= ~(S_IWGRP | S_IWOTH);
173
174 return pkgDirStream::DoItem(Itm,Fd);
175 }
176 /*}}}*/
177
178 // MemControlExtract::DoItem - Check if it is the control file /*{{{*/
179 // ---------------------------------------------------------------------
180 /* This sets up to extract the control block member file into a memory
181 block of just the right size. All other files go into the bit bucket. */
182 bool debDebFile::MemControlExtract::DoItem(Item &Itm,int &Fd)
183 {
184 // At the control file, allocate buffer memory.
185 if (Member == Itm.Name)
186 {
187 delete [] Control;
188 Control = new char[Itm.Size+2];
189 IsControl = true;
190 Fd = -2; // Signal to pass to Process
191 Length = Itm.Size;
192 }
193 else
194 IsControl = false;
195
196 return true;
197 }
198 /*}}}*/
199 // MemControlExtract::Process - Process extracting the control file /*{{{*/
200 // ---------------------------------------------------------------------
201 /* Just memcopy the block from the tar extractor and put it in the right
202 place in the pre-allocated memory block. */
203 bool debDebFile::MemControlExtract::Process(Item &/*Itm*/,const unsigned char *Data,
204 unsigned long Size,unsigned long Pos)
205 {
206 memcpy(Control + Pos, Data,Size);
207 return true;
208 }
209 /*}}}*/
210 // MemControlExtract::Read - Read the control information from the deb /*{{{*/
211 // ---------------------------------------------------------------------
212 /* This uses the internal tar extractor to fetch the control file, and then
213 it parses it into a tag section parser. */
214 bool debDebFile::MemControlExtract::Read(debDebFile &Deb)
215 {
216 if (Deb.ExtractTarMember(*this, "control.tar") == false)
217 return false;
218
219 if (Control == 0)
220 return true;
221
222 Control[Length] = '\n';
223 Control[Length+1] = '\n';
224 if (Section.Scan(Control,Length+2) == false)
225 return _error->Error(_("Unparsable control file"));
226 return true;
227 }
228 /*}}}*/
229 // MemControlExtract::TakeControl - Parse a memory block /*{{{*/
230 // ---------------------------------------------------------------------
231 /* The given memory block is loaded into the parser and parsed as a control
232 record. */
233 bool debDebFile::MemControlExtract::TakeControl(const void *Data,unsigned long Size)
234 {
235 delete [] Control;
236 Control = new char[Size+2];
237 Length = Size;
238 memcpy(Control,Data,Size);
239
240 Control[Length] = '\n';
241 Control[Length+1] = '\n';
242 return Section.Scan(Control,Length+2);
243 }
244 /*}}}*/
245