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