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