reorder includes: add <config.h> if needed and include it at first
[ntk/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
AL
20#include <apt-pkg/pkgcache.h>
21#include <apt-pkg/configuration.h>
22#include <apt-pkg/progress.h>
23#include <apt-pkg/sourcelist.h>
24#include <apt-pkg/pkgcachegen.h>
25#include <apt-pkg/version.h>
234edfd0
AL
26#include <apt-pkg/tagfile.h>
27#include <apt-pkg/extracttar.h>
28#include <apt-pkg/arfile.h>
29#include <apt-pkg/deblistparser.h>
30#include <apt-pkg/error.h>
31#include <apt-pkg/strutl.h>
4decd43c 32#include <apt-pkg/fileutl.h>
ea542140 33
234edfd0
AL
34#include <stdio.h>
35#include <string.h>
36#include <stdlib.h>
37#include <unistd.h>
7e726255 38#include <fstream>
234edfd0 39
233c2b66 40#include <locale.h>
234edfd0
AL
41#include <apti18n.h>
42#include "apt-extracttemplates.h"
7e726255 43 /*}}}*/
234edfd0 44
8f312f45
AL
45using namespace std;
46
4decd43c 47#define TMPDIR "/tmp"
3fc8f685 48
234edfd0
AL
49pkgCache *DebFile::Cache = 0;
50
51// DebFile::DebFile - Construct the DebFile object /*{{{*/
52// ---------------------------------------------------------------------
53/* */
54DebFile::DebFile(const char *debfile)
7e726255
AL
55 : File(debfile, FileFd::ReadOnly), Control(0), DepOp(0),
56 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{
93 ARArchive AR(File);
7e726255
AL
94 if (_error->PendingError() == true)
95 return false;
96
234edfd0
AL
97 const ARArchive::Member *Member = AR.FindMember("control.tar.gz");
98 if (Member == 0)
7e726255 99 return _error->Error(_("%s not a valid DEB package."),File.Name().c_str());
234edfd0
AL
100
101 if (File.Seek(Member->Start) == false)
234edfd0 102 return false;
b21c0438 103 ExtractTar Tar(File, Member->Size,"gzip");
234edfd0
AL
104 return Tar.Go(*this);
105}
106 /*}}}*/
107// DebFile::DoItem examine element in package and mark /*{{{*/
108// ---------------------------------------------------------------------
109/* */
110bool DebFile::DoItem(Item &I, int &Fd)
111{
112 if (strcmp(I.Name, "control") == 0)
113 {
114 delete [] Control;
115 Control = new char[I.Size+1];
116 Control[I.Size] = 0;
117 Which = IsControl;
118 ControlLen = I.Size;
119 // make it call the Process method below. this is so evil
120 Fd = -2;
121 }
122 else if (strcmp(I.Name, "config") == 0)
123 {
124 delete [] Config;
125 Config = new char[I.Size+1];
126 Config[I.Size] = 0;
127 Which = IsConfig;
128 Fd = -2;
129 }
130 else if (strcmp(I.Name, "templates") == 0)
131 {
132 delete [] Template;
133 Template = new char[I.Size+1];
134 Template[I.Size] = 0;
135 Which = IsTemplate;
136 Fd = -2;
137 }
138 else
139 {
7e726255 140 // Ignore it
234edfd0
AL
141 Fd = -1;
142 }
143 return true;
144}
145 /*}}}*/
146// DebFile::Process examine element in package and copy /*{{{*/
147// ---------------------------------------------------------------------
148/* */
149bool DebFile::Process(Item &I, const unsigned char *data,
150 unsigned long size, unsigned long pos)
151{
152 switch (Which)
153 {
154 case IsControl:
155 memcpy(Control + pos, data, size);
156 break;
157 case IsConfig:
158 memcpy(Config + pos, data, size);
159 break;
160 case IsTemplate:
161 memcpy(Template + pos, data, size);
162 break;
163 default: /* throw it away */ ;
164 }
165 return true;
166}
167 /*}}}*/
168// DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
169// ---------------------------------------------------------------------
170/* */
171bool DebFile::ParseInfo()
172{
173 if (Control == NULL) return false;
7e726255 174
234edfd0
AL
175 pkgTagSection Section;
176 Section.Scan(Control, ControlLen);
177
178 Package = Section.FindS("Package");
179 Version = GetInstalledVer(Package);
180
181 const char *Start, *Stop;
182 if (Section.Find("Depends", Start, Stop) == true)
183 {
184 while (1)
185 {
186 string P, V;
187 unsigned int Op;
188 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
189 if (Start == 0) return false;
190 if (P == "debconf")
191 {
192 DepVer = V;
193 DepOp = Op;
194 break;
195 }
196 if (Start == Stop) break;
197 }
198 }
199
200 if (Section.Find("Pre-Depends", Start, Stop) == true)
201 {
202 while (1)
203 {
204 string P, V;
205 unsigned int Op;
206 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
207 if (Start == 0) return false;
208 if (P == "debconf")
209 {
210 PreDepVer = V;
211 PreDepOp = Op;
212 break;
213 }
214 if (Start == Stop) break;
215 }
216 }
217
218 return true;
219}
220 /*}}}*/
221// ShowHelp - show a short help text /*{{{*/
222// ---------------------------------------------------------------------
223/* */
7e726255 224int ShowHelp(void)
234edfd0 225{
5b28c804
OS
226 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
227 COMMON_ARCH,__DATE__,__TIME__);
234edfd0 228
7e726255
AL
229 if (_config->FindB("version") == true)
230 return 0;
5cbf1b49 231
7e726255 232 cout <<
234edfd0
AL
233 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
234 "\n"
235 "apt-extracttemplates is a tool to extract config and template info\n"
7e726255
AL
236 "from debian packages\n"
237 "\n"
238 "Options:\n"
239 " -h This help text\n"
240 " -t Set the temp dir\n"
241 " -c=? Read this configuration file\n"
a2884e32 242 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
7e726255 243 return 0;
234edfd0
AL
244}
245 /*}}}*/
246// WriteFile - write the contents of the passed string to a file /*{{{*/
247// ---------------------------------------------------------------------
248/* */
259ca596 249string WriteFile(const char *package, const char *prefix, const char *data)
3fc8f685
AL
250{
251 char fn[512];
252 static int i;
70e6f24e 253 const char *tempdir = NULL;
8eb5af51
AL
254
255 tempdir = getenv("TMPDIR");
256 if (tempdir == NULL)
257 tempdir = TMPDIR;
258
259 snprintf(fn, sizeof(fn), "%s/%s.%s.%u%d",
260 _config->Find("APT::ExtractTemplates::TempDir", tempdir).c_str(),
261 package, prefix, getpid(), i++);
4decd43c
AL
262 FileFd f;
263 if (data == NULL)
264 data = "";
3fc8f685 265
4decd43c 266 if (!f.Open(fn, FileFd::WriteTemp, 0600))
7e726255
AL
267 {
268 _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
269 return string();
270 }
271
4decd43c
AL
272 f.Write(data, strlen(data));
273 f.Close();
7e726255 274 return fn;
3fc8f685 275}
234edfd0
AL
276 /*}}}*/
277// WriteConfig - write out the config data from a debian package file /*{{{*/
278// ---------------------------------------------------------------------
279/* */
280void WriteConfig(const DebFile &file)
3fc8f685 281{
a5eef4f7
AL
282 string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
283 string configscript = WriteFile(file.Package.c_str(), "config", file.Config);
3fc8f685 284
7e726255 285 if (templatefile.empty() == true || configscript.empty() == true)
3fc8f685 286 return;
234edfd0
AL
287 cout << file.Package << " " << file.Version << " "
288 << templatefile << " " << configscript << endl;
234edfd0
AL
289}
290 /*}}}*/
291// InitCache - initialize the package cache /*{{{*/
292// ---------------------------------------------------------------------
293/* */
7e726255
AL
294bool Go(CommandLine &CmdL)
295{
3fc8f685 296 // Initialize the apt cache
7e726255 297 MMap *Map = 0;
3fc8f685
AL
298 pkgSourceList List;
299 List.ReadMainList();
ea4b220b 300 pkgCacheGenerator::MakeStatusCache(List,NULL,&Map,true);
08945efa
AL
301 if (Map == 0)
302 return false;
7e726255
AL
303 DebFile::Cache = new pkgCache(Map);
304 if (_error->PendingError() == true)
305 return false;
5cbf1b49 306
234edfd0 307 // Find out what version of debconf is currently installed
7e726255
AL
308 string debconfver = DebFile::GetInstalledVer("debconf");
309 if (debconfver.empty() == true)
310 return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
3fc8f685 311
234edfd0 312 // Process each package passsed in
5cbf1b49 313 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
3fc8f685 314 {
7e726255 315 // Will pick up the errors later..
5cbf1b49 316 DebFile file(CmdL.FileList[I]);
7e726255 317 if (file.Go() == false)
08945efa
AL
318 {
319 _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
320 continue;
321 }
322
234edfd0 323 // Does the package have templates?
3fc8f685
AL
324 if (file.Template != 0 && file.ParseInfo() == true)
325 {
234edfd0
AL
326 // Check to make sure debconf dependencies are
327 // satisfied
f034a64a 328 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
234edfd0 329 if (file.DepVer != "" &&
418349f0
AL
330 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
331 file.DepOp,file.DepVer.c_str()
332 ) == false)
3fc8f685 333 continue;
234edfd0 334 if (file.PreDepVer != "" &&
418349f0
AL
335 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
336 file.PreDepOp,file.PreDepVer.c_str()
337 ) == false)
3fc8f685
AL
338 continue;
339
234edfd0 340 WriteConfig(file);
3fc8f685
AL
341 }
342 }
343
344
345 delete Map;
346 delete DebFile::Cache;
7e726255
AL
347
348 return !_error->PendingError();
349}
350 /*}}}*/
92fcbfc1 351int main(int argc, const char **argv) /*{{{*/
7e726255
AL
352{
353 CommandLine::Args Args[] = {
354 {'h',"help","help",0},
355 {'v',"version","version",0},
356 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
357 {'c',"config-file",0,CommandLine::ConfigFile},
358 {'o',"option",0,CommandLine::ArbItem},
359 {0,0,0,0}};
67111687
AL
360
361 // Set up gettext support
362 setlocale(LC_ALL,"");
363 textdomain(PACKAGE);
364
7e726255
AL
365 // Parse the command line and initialize the package library
366 CommandLine CmdL(Args,_config);
367 if (pkgInitConfig(*_config) == false ||
368 CmdL.Parse(argc,argv) == false ||
369 pkgInitSystem(*_config,_system) == false)
370 {
371 _error->DumpErrors();
372 return 100;
373 }
374
375 // See if the help should be shown
376 if (_config->FindB("help") == true ||
377 CmdL.FileSize() == 0)
378 return ShowHelp();
379
380 Go(CmdL);
3fc8f685 381
7e726255
AL
382 // Print any errors or warnings found during operation
383 if (_error->empty() == false)
384 {
385 // This goes to stderr..
386 bool Errors = _error->PendingError();
387 _error->DumpErrors();
388 return Errors == true?100:0;
389 }
390
3fc8f685
AL
391 return 0;
392}
92fcbfc1 393 /*}}}*/