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