]> git.saurik.com Git - apt.git/blame - cmdline/apt-extracttemplates.cc
debian/apt.postinst: chown _apt:root /etc/apt/auth.conf
[apt.git] / cmdline / apt-extracttemplates.cc
CommitLineData
234edfd0
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
8eb5af51 3// $Id: apt-extracttemplates.cc,v 1.15 2003/07/26 00:00:11 mdz Exp $
234edfd0
AL
4/* ######################################################################
5
6 APT Extract Templates - Program to extract debconf config and template
7 files
3fc8f685 8
234edfd0
AL
9 This is a simple program to extract config and template information
10 from Debian packages. It can be used to speed up the preconfiguration
11 process for debconf-enabled packages
12
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
ea542140
DK
16#include<config.h>
17
3fc8f685 18#include <apt-pkg/init.h>
5cbf1b49 19#include <apt-pkg/cmndline.h>
3fc8f685 20#include <apt-pkg/pkgcache.h>
453b82a3 21#include <apt-pkg/cacheiterators.h>
3fc8f685 22#include <apt-pkg/configuration.h>
3fc8f685
AL
23#include <apt-pkg/sourcelist.h>
24#include <apt-pkg/pkgcachegen.h>
25#include <apt-pkg/version.h>
234edfd0 26#include <apt-pkg/tagfile.h>
e7f56293 27#include <apt-pkg/debfile.h>
234edfd0
AL
28#include <apt-pkg/deblistparser.h>
29#include <apt-pkg/error.h>
30#include <apt-pkg/strutl.h>
4decd43c 31#include <apt-pkg/fileutl.h>
472ff00e 32#include <apt-pkg/pkgsystem.h>
453b82a3
DK
33#include <apt-pkg/dirstream.h>
34#include <apt-pkg/mmap.h>
ea542140 35
453b82a3 36#include <iostream>
234edfd0
AL
37#include <stdio.h>
38#include <string.h>
234edfd0 39#include <unistd.h>
8d50b63f 40#include <stdlib.h>
234edfd0 41
234edfd0 42#include "apt-extracttemplates.h"
a00a9b44
DK
43
44#include <apti18n.h>
7e726255 45 /*}}}*/
234edfd0 46
8f312f45
AL
47using namespace std;
48
234edfd0
AL
49pkgCache *DebFile::Cache = 0;
50
51// DebFile::DebFile - Construct the DebFile object /*{{{*/
52// ---------------------------------------------------------------------
53/* */
54DebFile::DebFile(const char *debfile)
387fb4ae 55 : File(debfile, FileFd::ReadOnly), Control(NULL), ControlLen(0),
dcaa1185 56 DepOp(0), PreDepOp(0), Config(0), Template(0), Which(None)
3fc8f685 57{
3fc8f685 58}
234edfd0
AL
59 /*}}}*/
60// DebFile::~DebFile - Destruct the DebFile object /*{{{*/
61// ---------------------------------------------------------------------
62/* */
63DebFile::~DebFile()
64{
65 delete [] Control;
66 delete [] Config;
67 delete [] Template;
68}
69 /*}}}*/
70// DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
71// ---------------------------------------------------------------------
72/* */
7e726255 73string DebFile::GetInstalledVer(const string &package)
234edfd0 74{
234edfd0
AL
75 pkgCache::PkgIterator Pkg = Cache->FindPkg(package);
76 if (Pkg.end() == false)
77 {
78 pkgCache::VerIterator V = Pkg.CurrentVer();
7e726255 79 if (V.end() == false)
234edfd0 80 {
7e726255 81 return V.VerStr();
234edfd0
AL
82 }
83 }
3fc8f685 84
7e726255 85 return string();
234edfd0
AL
86}
87 /*}}}*/
88// DebFile::Go - Start extracting a debian package /*{{{*/
89// ---------------------------------------------------------------------
90/* */
91bool DebFile::Go()
92{
e7f56293
GJ
93 debDebFile Deb(File);
94
95 return Deb.ExtractTarMember(*this, "control.tar");
234edfd0
AL
96}
97 /*}}}*/
98// DebFile::DoItem examine element in package and mark /*{{{*/
99// ---------------------------------------------------------------------
100/* */
101bool DebFile::DoItem(Item &I, int &Fd)
102{
103 if (strcmp(I.Name, "control") == 0)
104 {
105 delete [] Control;
8710a36a
DK
106 Control = new char[I.Size+3];
107 Control[I.Size] = '\n';
108 Control[I.Size + 1] = '\n';
109 Control[I.Size + 2] = '\0';
234edfd0 110 Which = IsControl;
8710a36a 111 ControlLen = I.Size + 3;
234edfd0
AL
112 // make it call the Process method below. this is so evil
113 Fd = -2;
114 }
115 else if (strcmp(I.Name, "config") == 0)
116 {
117 delete [] Config;
118 Config = new char[I.Size+1];
119 Config[I.Size] = 0;
120 Which = IsConfig;
121 Fd = -2;
122 }
123 else if (strcmp(I.Name, "templates") == 0)
124 {
125 delete [] Template;
126 Template = new char[I.Size+1];
127 Template[I.Size] = 0;
128 Which = IsTemplate;
129 Fd = -2;
130 }
131 else
132 {
7e726255 133 // Ignore it
234edfd0
AL
134 Fd = -1;
135 }
136 return true;
137}
138 /*}}}*/
139// DebFile::Process examine element in package and copy /*{{{*/
140// ---------------------------------------------------------------------
141/* */
65512241 142bool DebFile::Process(Item &/*I*/, const unsigned char *data,
3621b1c7 143 unsigned long long size, unsigned long long pos)
234edfd0
AL
144{
145 switch (Which)
146 {
147 case IsControl:
148 memcpy(Control + pos, data, size);
149 break;
150 case IsConfig:
151 memcpy(Config + pos, data, size);
152 break;
153 case IsTemplate:
154 memcpy(Template + pos, data, size);
155 break;
156 default: /* throw it away */ ;
157 }
158 return true;
159}
160 /*}}}*/
161// DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
162// ---------------------------------------------------------------------
163/* */
164bool DebFile::ParseInfo()
165{
166 if (Control == NULL) return false;
8710a36a 167
234edfd0 168 pkgTagSection Section;
8710a36a
DK
169 if (Section.Scan(Control, ControlLen) == false)
170 return false;
234edfd0
AL
171
172 Package = Section.FindS("Package");
173 Version = GetInstalledVer(Package);
174
175 const char *Start, *Stop;
176 if (Section.Find("Depends", Start, Stop) == true)
177 {
178 while (1)
179 {
180 string P, V;
181 unsigned int Op;
182 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
183 if (Start == 0) return false;
184 if (P == "debconf")
185 {
186 DepVer = V;
187 DepOp = Op;
188 break;
189 }
190 if (Start == Stop) break;
191 }
192 }
193
194 if (Section.Find("Pre-Depends", Start, Stop) == true)
195 {
196 while (1)
197 {
198 string P, V;
199 unsigned int Op;
200 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
201 if (Start == 0) return false;
202 if (P == "debconf")
203 {
204 PreDepVer = V;
205 PreDepOp = Op;
206 break;
207 }
208 if (Start == Stop) break;
209 }
210 }
211
212 return true;
213}
214 /*}}}*/
215// ShowHelp - show a short help text /*{{{*/
216// ---------------------------------------------------------------------
217/* */
c3ccac92 218static int ShowHelp(void)
234edfd0 219{
9179f697 220 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
5b28c804 221 COMMON_ARCH,__DATE__,__TIME__);
234edfd0 222
7e726255
AL
223 if (_config->FindB("version") == true)
224 return 0;
5cbf1b49 225
7e726255 226 cout <<
234edfd0
AL
227 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
228 "\n"
229 "apt-extracttemplates is a tool to extract config and template info\n"
7e726255
AL
230 "from debian packages\n"
231 "\n"
232 "Options:\n"
233 " -h This help text\n"
234 " -t Set the temp dir\n"
235 " -c=? Read this configuration file\n"
a2884e32 236 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
7e726255 237 return 0;
234edfd0
AL
238}
239 /*}}}*/
240// WriteFile - write the contents of the passed string to a file /*{{{*/
241// ---------------------------------------------------------------------
242/* */
c3ccac92 243static string WriteFile(const char *package, const char *prefix, const char *data)
3fc8f685
AL
244{
245 char fn[512];
8eb5af51 246
68e01721 247 std::string tempdir = GetTempDir();
8d50b63f 248 snprintf(fn, sizeof(fn), "%s/%s.%s.XXXXXX",
68e01721
MV
249 _config->Find("APT::ExtractTemplates::TempDir",
250 tempdir.c_str()).c_str(),
8d50b63f 251 package, prefix);
4decd43c
AL
252 FileFd f;
253 if (data == NULL)
254 data = "";
8d50b63f
MV
255 int fd = mkstemp(fn);
256 if (fd < 0) {
257 _error->Errno("ofstream::ofstream",_("Unable to mkstemp %s"),fn);
258 return string();
259 }
260 if (!f.OpenDescriptor(fd, FileFd::WriteOnly, FileFd::None, true))
7e726255
AL
261 {
262 _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
263 return string();
264 }
4decd43c
AL
265 f.Write(data, strlen(data));
266 f.Close();
7e726255 267 return fn;
3fc8f685 268}
234edfd0
AL
269 /*}}}*/
270// WriteConfig - write out the config data from a debian package file /*{{{*/
271// ---------------------------------------------------------------------
272/* */
c3ccac92 273static void WriteConfig(const DebFile &file)
3fc8f685 274{
a5eef4f7
AL
275 string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
276 string configscript = WriteFile(file.Package.c_str(), "config", file.Config);
3fc8f685 277
7e726255 278 if (templatefile.empty() == true || configscript.empty() == true)
3fc8f685 279 return;
234edfd0
AL
280 cout << file.Package << " " << file.Version << " "
281 << templatefile << " " << configscript << endl;
234edfd0
AL
282}
283 /*}}}*/
284// InitCache - initialize the package cache /*{{{*/
285// ---------------------------------------------------------------------
286/* */
c3ccac92 287static bool Go(CommandLine &CmdL)
7e726255 288{
3fc8f685 289 // Initialize the apt cache
7e726255 290 MMap *Map = 0;
3fc8f685
AL
291 pkgSourceList List;
292 List.ReadMainList();
ea4b220b 293 pkgCacheGenerator::MakeStatusCache(List,NULL,&Map,true);
08945efa
AL
294 if (Map == 0)
295 return false;
7e726255
AL
296 DebFile::Cache = new pkgCache(Map);
297 if (_error->PendingError() == true)
298 return false;
5cbf1b49 299
234edfd0 300 // Find out what version of debconf is currently installed
7e726255
AL
301 string debconfver = DebFile::GetInstalledVer("debconf");
302 if (debconfver.empty() == true)
303 return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
3fc8f685 304
234edfd0 305 // Process each package passsed in
5cbf1b49 306 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
3fc8f685 307 {
7e726255 308 // Will pick up the errors later..
5cbf1b49 309 DebFile file(CmdL.FileList[I]);
7e726255 310 if (file.Go() == false)
08945efa
AL
311 {
312 _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
313 continue;
314 }
315
234edfd0 316 // Does the package have templates?
3fc8f685
AL
317 if (file.Template != 0 && file.ParseInfo() == true)
318 {
234edfd0
AL
319 // Check to make sure debconf dependencies are
320 // satisfied
f034a64a 321 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
234edfd0 322 if (file.DepVer != "" &&
418349f0
AL
323 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
324 file.DepOp,file.DepVer.c_str()
325 ) == false)
3fc8f685 326 continue;
234edfd0 327 if (file.PreDepVer != "" &&
418349f0
AL
328 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
329 file.PreDepOp,file.PreDepVer.c_str()
330 ) == false)
3fc8f685
AL
331 continue;
332
234edfd0 333 WriteConfig(file);
3fc8f685
AL
334 }
335 }
336
337
338 delete Map;
339 delete DebFile::Cache;
7e726255
AL
340
341 return !_error->PendingError();
342}
343 /*}}}*/
92fcbfc1 344int main(int argc, const char **argv) /*{{{*/
7e726255
AL
345{
346 CommandLine::Args Args[] = {
347 {'h',"help","help",0},
348 {'v',"version","version",0},
349 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
350 {'c',"config-file",0,CommandLine::ConfigFile},
351 {'o',"option",0,CommandLine::ArbItem},
352 {0,0,0,0}};
67111687
AL
353
354 // Set up gettext support
355 setlocale(LC_ALL,"");
356 textdomain(PACKAGE);
357
7e726255
AL
358 // Parse the command line and initialize the package library
359 CommandLine CmdL(Args,_config);
360 if (pkgInitConfig(*_config) == false ||
361 CmdL.Parse(argc,argv) == false ||
362 pkgInitSystem(*_config,_system) == false)
363 {
364 _error->DumpErrors();
365 return 100;
366 }
367
368 // See if the help should be shown
369 if (_config->FindB("help") == true ||
370 CmdL.FileSize() == 0)
371 return ShowHelp();
372
373 Go(CmdL);
3fc8f685 374
7e726255
AL
375 // Print any errors or warnings found during operation
376 if (_error->empty() == false)
377 {
378 // This goes to stderr..
379 bool Errors = _error->PendingError();
380 _error->DumpErrors();
381 return Errors == true?100:0;
382 }
383
3fc8f685
AL
384 return 0;
385}
92fcbfc1 386 /*}}}*/