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