]> git.saurik.com Git - apt.git/blame - apt-inst/deb/debfile.cc
apt manpage is built from xml nowadays like the rest
[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>
7296f1e6 24#include <apt-pkg/aptconfiguration.h>
453b82a3
DK
25#include <apt-pkg/arfile.h>
26#include <apt-pkg/dirstream.h>
27#include <apt-pkg/fileutl.h>
28#include <apt-pkg/tagfile.h>
b2e465d6 29
453b82a3
DK
30#include <string.h>
31#include <string>
32#include <vector>
b2e465d6 33#include <sys/stat.h>
453b82a3 34
d77559ac 35#include <apti18n.h>
b2e465d6
AL
36 /*}}}*/
37
38// DebFile::debDebFile - Constructor /*{{{*/
39// ---------------------------------------------------------------------
40/* Open the AR file and check for consistency */
41debDebFile::debDebFile(FileFd &File) : File(File), AR(File)
42{
43 if (_error->PendingError() == true)
44 return;
b21c0438
MZ
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
e7f56293
GJ
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");
b2e465d6 55 return;
b21c0438
MZ
56 }
57
e7f56293
GJ
58 if (!CheckMember("data.tar") &&
59 !CheckMember("data.tar.gz") &&
ac005224 60 !CheckMember("data.tar.bz2") &&
bc33e0f0
DK
61 !CheckMember("data.tar.lzma") &&
62 !CheckMember("data.tar.xz")) {
0e90e042 63 _error->Error(_("This is not a valid DEB archive, missing '%s' member"), "data.tar");
b21c0438
MZ
64 return;
65 }
b2e465d6
AL
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. */
72bool debDebFile::CheckMember(const char *Name)
73{
74 if (AR.FindMember(Name) == 0)
b21c0438 75 return false;
b2e465d6
AL
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! */
86const 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 {
b2e465d6
AL
92 return 0;
93 }
94 if (File.Seek(Member->Start) == false)
95 return 0;
96
97 return Member;
98}
99 /*}}}*/
50b942e9 100// DebFile::ExtractTarMember - Extract the contents of a tar member /*{{{*/
b2e465d6
AL
101// ---------------------------------------------------------------------
102/* Simple wrapper around tar.. */
50b942e9 103bool debDebFile::ExtractTarMember(pkgDirStream &Stream,const char *Name)
b2e465d6 104{
7296f1e6
DK
105 // Get the archive member
106 const ARArchive::Member *Member = NULL;
107 std::string Compressor;
108
7296f1e6
DK
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 {
50b942e9 113 Member = AR.FindMember(std::string(Name).append(c->Extension).c_str());
7296f1e6
DK
114 if (Member == NULL)
115 continue;
116 Compressor = c->Binary;
117 break;
ac005224 118 }
7296f1e6 119
e7f56293
GJ
120 if (Member == NULL)
121 Member = AR.FindMember(std::string(Name).c_str());
122
7296f1e6
DK
123 if (Member == NULL)
124 {
50b942e9 125 std::string ext = std::string(Name) + ".{";
7296f1e6 126 for (std::vector<APT::Configuration::Compressor>::const_iterator c = compressor.begin();
b68ce883
JAK
127 c != compressor.end(); ++c) {
128 if (!c->Extension.empty())
129 ext.append(c->Extension.substr(1));
130 }
7296f1e6
DK
131 ext.append("}");
132 return _error->Error(_("Internal error, could not locate member %s"), ext.c_str());
bc33e0f0 133 }
7296f1e6 134
b2e465d6
AL
135 if (File.Seek(Member->Start) == false)
136 return false;
7296f1e6 137
b2e465d6 138 // Prepare Tar
b21c0438 139 ExtractTar Tar(File,Member->Size,Compressor);
b2e465d6
AL
140 if (_error->PendingError() == true)
141 return false;
142 return Tar.Go(Stream);
143}
144 /*}}}*/
50b942e9
GJ
145// DebFile::ExtractArchive - Extract the archive data itself /*{{{*/
146// ---------------------------------------------------------------------
147/* Simple wrapper around DebFile::ExtractTarMember. */
148bool debDebFile::ExtractArchive(pkgDirStream &Stream)
149{
150 return ExtractTarMember(Stream, "data.tar");
151}
152 /*}}}*/
b2e465d6
AL
153
154// DebFile::ControlExtract::DoItem - Control Tar Extraction /*{{{*/
155// ---------------------------------------------------------------------
156/* This directory stream handler for the control tar handles extracting
157 it into the temporary meta directory. It only extracts files, it does
158 not create directories, links or anything else. */
159bool debDebFile::ControlExtract::DoItem(Item &Itm,int &Fd)
160{
161 if (Itm.Type != Item::File)
162 return true;
163
164 /* Cleanse the file name, prevent people from trying to unpack into
165 absolute paths, .., etc */
166 for (char *I = Itm.Name; *I != 0; I++)
167 if (*I == '/')
168 *I = '_';
169
170 /* Force the ownership to be root and ensure correct permissions,
171 go-w, the rest are left untouched */
172 Itm.UID = 0;
173 Itm.GID = 0;
174 Itm.Mode &= ~(S_IWGRP | S_IWOTH);
175
176 return pkgDirStream::DoItem(Itm,Fd);
177}
178 /*}}}*/
179
180// MemControlExtract::DoItem - Check if it is the control file /*{{{*/
181// ---------------------------------------------------------------------
182/* This sets up to extract the control block member file into a memory
183 block of just the right size. All other files go into the bit bucket. */
184bool debDebFile::MemControlExtract::DoItem(Item &Itm,int &Fd)
185{
186 // At the control file, allocate buffer memory.
187 if (Member == Itm.Name)
188 {
189 delete [] Control;
190 Control = new char[Itm.Size+2];
191 IsControl = true;
192 Fd = -2; // Signal to pass to Process
193 Length = Itm.Size;
194 }
195 else
196 IsControl = false;
197
198 return true;
199}
200 /*}}}*/
201// MemControlExtract::Process - Process extracting the control file /*{{{*/
202// ---------------------------------------------------------------------
203/* Just memcopy the block from the tar extractor and put it in the right
204 place in the pre-allocated memory block. */
65512241 205bool debDebFile::MemControlExtract::Process(Item &/*Itm*/,const unsigned char *Data,
60b64ffc 206#if APT_PKG_ABI >= 413
3621b1c7 207 unsigned long long Size,unsigned long long Pos)
60b64ffc
DK
208#else
209 unsigned long Size,unsigned long Pos)
210#endif
b2e465d6
AL
211{
212 memcpy(Control + Pos, Data,Size);
213 return true;
214}
215 /*}}}*/
216// MemControlExtract::Read - Read the control information from the deb /*{{{*/
217// ---------------------------------------------------------------------
218/* This uses the internal tar extractor to fetch the control file, and then
219 it parses it into a tag section parser. */
220bool debDebFile::MemControlExtract::Read(debDebFile &Deb)
221{
e7f56293 222 if (Deb.ExtractTarMember(*this, "control.tar") == false)
b2e465d6
AL
223 return false;
224
225 if (Control == 0)
226 return true;
227
228 Control[Length] = '\n';
229 Control[Length+1] = '\n';
230 if (Section.Scan(Control,Length+2) == false)
db0db9fe 231 return _error->Error(_("Unparsable control file"));
b2e465d6
AL
232 return true;
233}
234 /*}}}*/
235// MemControlExtract::TakeControl - Parse a memory block /*{{{*/
236// ---------------------------------------------------------------------
237/* The given memory block is loaded into the parser and parsed as a control
238 record. */
60b64ffc 239#if APT_PKG_ABI >= 413
3621b1c7 240bool debDebFile::MemControlExtract::TakeControl(const void *Data,unsigned long long Size)
60b64ffc
DK
241#else
242bool debDebFile::MemControlExtract::TakeControl(const void *Data,unsigned long Size)
243#endif
b2e465d6
AL
244{
245 delete [] Control;
246 Control = new char[Size+2];
247 Length = Size;
248 memcpy(Control,Data,Size);
249
250 Control[Length] = '\n';
251 Control[Length+1] = '\n';
252 return Section.Scan(Control,Length+2);
253}
254 /*}}}*/
255