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