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