]> git.saurik.com Git - apt.git/blame - cmdline/apt-extracttemplates.cc
Latest version from unstable (amd64, etc.)
[apt.git] / cmdline / apt-extracttemplates.cc
CommitLineData
234edfd0
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
8f312f45 3// $Id: apt-extracttemplates.cc,v 1.14 2003/01/11 07:18:44 jgg 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
AL
18#include <apt-pkg/pkgcache.h>
19#include <apt-pkg/configuration.h>
20#include <apt-pkg/progress.h>
21#include <apt-pkg/sourcelist.h>
22#include <apt-pkg/pkgcachegen.h>
23#include <apt-pkg/version.h>
234edfd0
AL
24#include <apt-pkg/tagfile.h>
25#include <apt-pkg/extracttar.h>
26#include <apt-pkg/arfile.h>
27#include <apt-pkg/deblistparser.h>
28#include <apt-pkg/error.h>
29#include <apt-pkg/strutl.h>
4decd43c 30#include <apt-pkg/fileutl.h>
3fc8f685 31
234edfd0
AL
32#include <stdio.h>
33#include <string.h>
34#include <stdlib.h>
35#include <unistd.h>
7e726255 36#include <fstream>
234edfd0 37
233c2b66 38#include <locale.h>
234edfd0
AL
39#include <config.h>
40#include <apti18n.h>
41#include "apt-extracttemplates.h"
7e726255 42 /*}}}*/
234edfd0 43
8f312f45
AL
44using namespace std;
45
4decd43c 46#define TMPDIR "/tmp"
3fc8f685 47
234edfd0
AL
48pkgCache *DebFile::Cache = 0;
49
50// DebFile::DebFile - Construct the DebFile object /*{{{*/
51// ---------------------------------------------------------------------
52/* */
53DebFile::DebFile(const char *debfile)
7e726255
AL
54 : File(debfile, FileFd::ReadOnly), Control(0), DepOp(0),
55 PreDepOp(0), Config(0), Template(0), Which(None)
3fc8f685 56{
3fc8f685 57}
234edfd0
AL
58 /*}}}*/
59// DebFile::~DebFile - Destruct the DebFile object /*{{{*/
60// ---------------------------------------------------------------------
61/* */
62DebFile::~DebFile()
63{
64 delete [] Control;
65 delete [] Config;
66 delete [] Template;
67}
68 /*}}}*/
69// DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
70// ---------------------------------------------------------------------
71/* */
7e726255 72string DebFile::GetInstalledVer(const string &package)
234edfd0 73{
234edfd0
AL
74 pkgCache::PkgIterator Pkg = Cache->FindPkg(package);
75 if (Pkg.end() == false)
76 {
77 pkgCache::VerIterator V = Pkg.CurrentVer();
7e726255 78 if (V.end() == false)
234edfd0 79 {
7e726255 80 return V.VerStr();
234edfd0
AL
81 }
82 }
3fc8f685 83
7e726255 84 return string();
234edfd0
AL
85}
86 /*}}}*/
87// DebFile::Go - Start extracting a debian package /*{{{*/
88// ---------------------------------------------------------------------
89/* */
90bool DebFile::Go()
91{
92 ARArchive AR(File);
7e726255
AL
93 if (_error->PendingError() == true)
94 return false;
95
234edfd0
AL
96 const ARArchive::Member *Member = AR.FindMember("control.tar.gz");
97 if (Member == 0)
7e726255 98 return _error->Error(_("%s not a valid DEB package."),File.Name().c_str());
234edfd0
AL
99
100 if (File.Seek(Member->Start) == false)
234edfd0 101 return false;
234edfd0
AL
102 ExtractTar Tar(File, Member->Size);
103 return Tar.Go(*this);
104}
105 /*}}}*/
106// DebFile::DoItem examine element in package and mark /*{{{*/
107// ---------------------------------------------------------------------
108/* */
109bool DebFile::DoItem(Item &I, int &Fd)
110{
111 if (strcmp(I.Name, "control") == 0)
112 {
113 delete [] Control;
114 Control = new char[I.Size+1];
115 Control[I.Size] = 0;
116 Which = IsControl;
117 ControlLen = I.Size;
118 // make it call the Process method below. this is so evil
119 Fd = -2;
120 }
121 else if (strcmp(I.Name, "config") == 0)
122 {
123 delete [] Config;
124 Config = new char[I.Size+1];
125 Config[I.Size] = 0;
126 Which = IsConfig;
127 Fd = -2;
128 }
129 else if (strcmp(I.Name, "templates") == 0)
130 {
131 delete [] Template;
132 Template = new char[I.Size+1];
133 Template[I.Size] = 0;
134 Which = IsTemplate;
135 Fd = -2;
136 }
137 else
138 {
7e726255 139 // Ignore it
234edfd0
AL
140 Fd = -1;
141 }
142 return true;
143}
144 /*}}}*/
145// DebFile::Process examine element in package and copy /*{{{*/
146// ---------------------------------------------------------------------
147/* */
148bool DebFile::Process(Item &I, const unsigned char *data,
149 unsigned long size, unsigned long pos)
150{
151 switch (Which)
152 {
153 case IsControl:
154 memcpy(Control + pos, data, size);
155 break;
156 case IsConfig:
157 memcpy(Config + pos, data, size);
158 break;
159 case IsTemplate:
160 memcpy(Template + pos, data, size);
161 break;
162 default: /* throw it away */ ;
163 }
164 return true;
165}
166 /*}}}*/
167// DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
168// ---------------------------------------------------------------------
169/* */
170bool DebFile::ParseInfo()
171{
172 if (Control == NULL) return false;
7e726255 173
234edfd0
AL
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/* */
7e726255 223int ShowHelp(void)
234edfd0
AL
224{
225 ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
226 COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
227
7e726255
AL
228 if (_config->FindB("version") == true)
229 return 0;
5cbf1b49 230
7e726255 231 cout <<
234edfd0
AL
232 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
233 "\n"
234 "apt-extracttemplates is a tool to extract config and template info\n"
7e726255
AL
235 "from debian packages\n"
236 "\n"
237 "Options:\n"
238 " -h This help text\n"
239 " -t Set the temp dir\n"
240 " -c=? Read this configuration file\n"
241 " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n");
242 return 0;
234edfd0
AL
243}
244 /*}}}*/
245// WriteFile - write the contents of the passed string to a file /*{{{*/
246// ---------------------------------------------------------------------
247/* */
259ca596 248string WriteFile(const char *package, const char *prefix, const char *data)
3fc8f685
AL
249{
250 char fn[512];
251 static int i;
259ca596 252 snprintf(fn, sizeof(fn), "%s/%s.%s.%u%d", _config->Find("APT::ExtractTemplates::TempDir", TMPDIR).c_str(), package, prefix, getpid(), i++);
4decd43c
AL
253 FileFd f;
254 if (data == NULL)
255 data = "";
3fc8f685 256
4decd43c 257 if (!f.Open(fn, FileFd::WriteTemp, 0600))
7e726255
AL
258 {
259 _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
260 return string();
261 }
262
4decd43c
AL
263 f.Write(data, strlen(data));
264 f.Close();
7e726255 265 return fn;
3fc8f685 266}
234edfd0
AL
267 /*}}}*/
268// WriteConfig - write out the config data from a debian package file /*{{{*/
269// ---------------------------------------------------------------------
270/* */
271void WriteConfig(const DebFile &file)
3fc8f685 272{
a5eef4f7
AL
273 string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
274 string configscript = WriteFile(file.Package.c_str(), "config", file.Config);
3fc8f685 275
7e726255 276 if (templatefile.empty() == true || configscript.empty() == true)
3fc8f685 277 return;
234edfd0
AL
278 cout << file.Package << " " << file.Version << " "
279 << templatefile << " " << configscript << endl;
234edfd0
AL
280}
281 /*}}}*/
282// InitCache - initialize the package cache /*{{{*/
283// ---------------------------------------------------------------------
284/* */
7e726255
AL
285bool Go(CommandLine &CmdL)
286{
3fc8f685 287 // Initialize the apt cache
7e726255 288 MMap *Map = 0;
3fc8f685
AL
289 pkgSourceList List;
290 List.ReadMainList();
291 OpProgress Prog;
292 pkgMakeStatusCache(List,Prog,&Map,true);
08945efa
AL
293 if (Map == 0)
294 return false;
7e726255
AL
295 DebFile::Cache = new pkgCache(Map);
296 if (_error->PendingError() == true)
297 return false;
5cbf1b49 298
234edfd0 299 // Find out what version of debconf is currently installed
7e726255
AL
300 string debconfver = DebFile::GetInstalledVer("debconf");
301 if (debconfver.empty() == true)
302 return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
3fc8f685 303
234edfd0 304 // Process each package passsed in
5cbf1b49 305 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
3fc8f685 306 {
7e726255 307 // Will pick up the errors later..
5cbf1b49 308 DebFile file(CmdL.FileList[I]);
7e726255 309 if (file.Go() == false)
08945efa
AL
310 {
311 _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
312 continue;
313 }
314
234edfd0 315 // Does the package have templates?
3fc8f685
AL
316 if (file.Template != 0 && file.ParseInfo() == true)
317 {
234edfd0
AL
318 // Check to make sure debconf dependencies are
319 // satisfied
f034a64a 320 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
234edfd0 321 if (file.DepVer != "" &&
418349f0
AL
322 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
323 file.DepOp,file.DepVer.c_str()
324 ) == false)
3fc8f685 325 continue;
234edfd0 326 if (file.PreDepVer != "" &&
418349f0
AL
327 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
328 file.PreDepOp,file.PreDepVer.c_str()
329 ) == false)
3fc8f685
AL
330 continue;
331
234edfd0 332 WriteConfig(file);
3fc8f685
AL
333 }
334 }
335
336
337 delete Map;
338 delete DebFile::Cache;
7e726255
AL
339
340 return !_error->PendingError();
341}
342 /*}}}*/
343
344int main(int argc, const char **argv)
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}