]> git.saurik.com Git - apt.git/blame - cmdline/apt-extracttemplates.cc
cvsignore
[apt.git] / cmdline / apt-extracttemplates.cc
CommitLineData
234edfd0
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
5cbf1b49 3// $Id: apt-extracttemplates.cc,v 1.3 2001/02/25 05:25:29 tausq 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 /*{{{*/
3fc8f685 16#include <apt-pkg/init.h>
5cbf1b49 17#include <apt-pkg/cmndline.h>
3fc8f685 18#include <apt-pkg/debversion.h>
3fc8f685
AL
19#include <apt-pkg/pkgcache.h>
20#include <apt-pkg/configuration.h>
21#include <apt-pkg/progress.h>
22#include <apt-pkg/sourcelist.h>
23#include <apt-pkg/pkgcachegen.h>
24#include <apt-pkg/version.h>
234edfd0
AL
25#include <apt-pkg/tagfile.h>
26#include <apt-pkg/extracttar.h>
27#include <apt-pkg/arfile.h>
28#include <apt-pkg/deblistparser.h>
29#include <apt-pkg/error.h>
30#include <apt-pkg/strutl.h>
3fc8f685 31
234edfd0
AL
32#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <unistd.h>
234edfd0
AL
36#include <wait.h>
37#include <fstream.h>
38
39#include <config.h>
40#include <apti18n.h>
41#include "apt-extracttemplates.h"
42
43#define TMPDIR "/var/lib/debconf/"
3fc8f685 44
234edfd0
AL
45pkgCache *DebFile::Cache = 0;
46
47// DebFile::DebFile - Construct the DebFile object /*{{{*/
48// ---------------------------------------------------------------------
49/* */
50DebFile::DebFile(const char *debfile)
51 : File(debfile, FileFd::ReadOnly), Control(0), DepOp(0), PreDepOp(0),
52 Config(0), Template(0), Which(None)
3fc8f685 53{
3fc8f685 54}
234edfd0
AL
55 /*}}}*/
56// DebFile::~DebFile - Destruct the DebFile object /*{{{*/
57// ---------------------------------------------------------------------
58/* */
59DebFile::~DebFile()
60{
61 delete [] Control;
62 delete [] Config;
63 delete [] Template;
64}
65 /*}}}*/
66// DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
67// ---------------------------------------------------------------------
68/* */
69char *DebFile::GetInstalledVer(const string &package)
70{
71 char *ver = 0;
72
73 pkgCache::PkgIterator Pkg = Cache->FindPkg(package);
74 if (Pkg.end() == false)
75 {
76 pkgCache::VerIterator V = Pkg.CurrentVer();
77 if (V.end() == false)
78 {
79 ver = strdup(V.VerStr());
80 }
81 }
3fc8f685 82
234edfd0
AL
83 return ver;
84}
85 /*}}}*/
86// DebFile::Go - Start extracting a debian package /*{{{*/
87// ---------------------------------------------------------------------
88/* */
89bool DebFile::Go()
90{
91 ARArchive AR(File);
92 const ARArchive::Member *Member = AR.FindMember("control.tar.gz");
93 if (Member == 0)
94 {
95 fprintf(stderr, _("This is not a valid DEB package.\n"));
96 return false;
97 }
98
99 if (File.Seek(Member->Start) == false)
100 {
101 return false;
102 }
103
104 ExtractTar Tar(File, Member->Size);
105 return Tar.Go(*this);
106}
107 /*}}}*/
108// DebFile::DoItem examine element in package and mark /*{{{*/
109// ---------------------------------------------------------------------
110/* */
111bool DebFile::DoItem(Item &I, int &Fd)
112{
113 if (strcmp(I.Name, "control") == 0)
114 {
115 delete [] Control;
116 Control = new char[I.Size+1];
117 Control[I.Size] = 0;
118 Which = IsControl;
119 ControlLen = I.Size;
120 // make it call the Process method below. this is so evil
121 Fd = -2;
122 }
123 else if (strcmp(I.Name, "config") == 0)
124 {
125 delete [] Config;
126 Config = new char[I.Size+1];
127 Config[I.Size] = 0;
128 Which = IsConfig;
129 Fd = -2;
130 }
131 else if (strcmp(I.Name, "templates") == 0)
132 {
133 delete [] Template;
134 Template = new char[I.Size+1];
135 Template[I.Size] = 0;
136 Which = IsTemplate;
137 Fd = -2;
138 }
139 else
140 {
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;
174 pkgTagSection Section;
175 Section.Scan(Control, ControlLen);
176
177 Package = Section.FindS("Package");
178 Version = GetInstalledVer(Package);
179
180 const char *Start, *Stop;
181 if (Section.Find("Depends", Start, Stop) == true)
182 {
183 while (1)
184 {
185 string P, V;
186 unsigned int Op;
187 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
188 if (Start == 0) return false;
189 if (P == "debconf")
190 {
191 DepVer = V;
192 DepOp = Op;
193 break;
194 }
195 if (Start == Stop) break;
196 }
197 }
198
199 if (Section.Find("Pre-Depends", Start, Stop) == true)
200 {
201 while (1)
202 {
203 string P, V;
204 unsigned int Op;
205 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
206 if (Start == 0) return false;
207 if (P == "debconf")
208 {
209 PreDepVer = V;
210 PreDepOp = Op;
211 break;
212 }
213 if (Start == Stop) break;
214 }
215 }
216
217 return true;
218}
219 /*}}}*/
220// ShowHelp - show a short help text /*{{{*/
221// ---------------------------------------------------------------------
222/* */
223void ShowHelp(void)
224{
225 ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
226 COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
227
5cbf1b49
AL
228 if (_config->FindB("version") == true) return;
229
234edfd0
AL
230 fprintf(stderr,
231 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
232 "\n"
233 "apt-extracttemplates is a tool to extract config and template info\n"
234 "from debian packages\n"));
235 exit(0);
236}
237 /*}}}*/
238// WriteFile - write the contents of the passed string to a file /*{{{*/
239// ---------------------------------------------------------------------
240/* */
241char *WriteFile(const char *prefix, const char *data)
3fc8f685
AL
242{
243 char fn[512];
244 static int i;
5cbf1b49 245 snprintf(fn, sizeof(fn), "%s%s.%u%d", _config->Find("APT::ExtractTemplates::TempDir", TMPDIR).c_str(), prefix, getpid(), i++);
3fc8f685 246
3fc8f685
AL
247 ofstream ofs(fn);
248 if (!ofs) return NULL;
234edfd0 249 ofs << (data ? data : "");
3fc8f685
AL
250 ofs.close();
251 return strdup(fn);
252}
234edfd0
AL
253 /*}}}*/
254// WriteConfig - write out the config data from a debian package file /*{{{*/
255// ---------------------------------------------------------------------
256/* */
257void WriteConfig(const DebFile &file)
3fc8f685 258{
234edfd0
AL
259 char *templatefile = WriteFile("template", file.Template);
260 char *configscript = WriteFile("config", file.Config);
3fc8f685
AL
261
262 if (templatefile == 0 || configscript == 0)
263 {
234edfd0 264 fprintf(stderr, _("Cannot write config script or templates\n"));
3fc8f685
AL
265 return;
266 }
234edfd0
AL
267 cout << file.Package << " " << file.Version << " "
268 << templatefile << " " << configscript << endl;
3fc8f685 269
234edfd0
AL
270 free(templatefile);
271 free(configscript);
272}
273 /*}}}*/
274// InitCache - initialize the package cache /*{{{*/
275// ---------------------------------------------------------------------
276/* */
277int InitCache(MMap *&Map, pkgCache *&Cache)
3fc8f685
AL
278{
279 // Initialize the apt cache
280 if (pkgInitConfig(*_config) == false || pkgInitSystem(*_config, _system) == false)
281 {
234edfd0
AL
282 _error->DumpErrors();
283 return -1;
3fc8f685
AL
284 }
285 pkgSourceList List;
286 List.ReadMainList();
287 OpProgress Prog;
288 pkgMakeStatusCache(List,Prog,&Map,true);
289 Cache = new pkgCache(Map);
234edfd0 290 return 0;
3fc8f685 291}
234edfd0 292 /*}}}*/
3fc8f685 293
5cbf1b49 294int main(int argc, const char **argv)
3fc8f685 295{
3fc8f685
AL
296 MMap *Map = 0;
297 const char *debconfver = NULL;
298
5cbf1b49
AL
299 CommandLine::Args Args[] = {
300 {'h',"help","help",0},
301 {'v',"version","version",0},
302 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
303 {'c',"config-file",0,CommandLine::ConfigFile},
304 {'o',"option",0,CommandLine::ArbItem},
305 {0,0,0,0}};
306
234edfd0
AL
307 // Initialize the package cache
308 if (InitCache(Map, DebFile::Cache) < 0 || Map == 0 || DebFile::Cache == 0)
3fc8f685 309 {
234edfd0
AL
310 fprintf(stderr, _("Cannot initialize APT cache\n"));
311 return 100;
3fc8f685
AL
312 }
313
5cbf1b49
AL
314 // Parse the command line
315 CommandLine CmdL(Args,_config);
316 if (CmdL.Parse(argc,argv) == false)
317 {
318 fprintf(stderr, _("Cannot parse commandline options\n"));
319 return 100;
320 }
321
322 // See if the help should be shown
323 if (_config->FindB("help") == true || CmdL.FileSize() == 0)
324 {
325 ShowHelp();
326 return 0;
327 }
328
234edfd0
AL
329 // Find out what version of debconf is currently installed
330 if ((debconfver = DebFile::GetInstalledVer("debconf")) == NULL)
3fc8f685 331 {
234edfd0 332 fprintf(stderr, _("Cannot get debconf version. Is debconf installed?\n"));
3fc8f685
AL
333 return 1;
334 }
335
234edfd0 336 // Process each package passsed in
5cbf1b49 337 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
3fc8f685 338 {
5cbf1b49 339 DebFile file(CmdL.FileList[I]);
3fc8f685
AL
340 if (file.Go() == false)
341 {
5cbf1b49 342 fprintf(stderr, _("Cannot read %s\n"), CmdL.FileList[I]);
3fc8f685
AL
343 continue;
344 }
234edfd0 345 // Does the package have templates?
3fc8f685
AL
346 if (file.Template != 0 && file.ParseInfo() == true)
347 {
234edfd0
AL
348 // Check to make sure debconf dependencies are
349 // satisfied
350 if (file.DepVer != "" &&
5cbf1b49
AL
351 DebFile::Cache->VS->CheckDep(file.DepVer.c_str(),
352 file.DepOp, debconfver) == false)
3fc8f685 353 continue;
234edfd0 354 if (file.PreDepVer != "" &&
5cbf1b49
AL
355 DebFile::Cache->VS->CheckDep(file.PreDepVer.c_str(),
356 file.PreDepOp, debconfver) == false)
3fc8f685
AL
357 continue;
358
234edfd0 359 WriteConfig(file);
3fc8f685
AL
360 }
361 }
362
363
364 delete Map;
365 delete DebFile::Cache;
366
367 return 0;
368}