]> git.saurik.com Git - apt.git/blame - cmdline/apt-extracttemplates.cc
* methods/copy.cc:
[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 /*{{{*/
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;
b21c0438 102 ExtractTar Tar(File, Member->Size,"gzip");
234edfd0
AL
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 224{
5b28c804
OS
225 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION,
226 COMMON_ARCH,__DATE__,__TIME__);
234edfd0 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"
a2884e32 241 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
7e726255 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;
8eb5af51
AL
252 char *tempdir = NULL;
253
254 tempdir = getenv("TMPDIR");
255 if (tempdir == NULL)
256 tempdir = TMPDIR;
257
258 snprintf(fn, sizeof(fn), "%s/%s.%s.%u%d",
259 _config->Find("APT::ExtractTemplates::TempDir", tempdir).c_str(),
260 package, prefix, getpid(), i++);
4decd43c
AL
261 FileFd f;
262 if (data == NULL)
263 data = "";
3fc8f685 264
4decd43c 265 if (!f.Open(fn, FileFd::WriteTemp, 0600))
7e726255
AL
266 {
267 _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
268 return string();
269 }
270
4decd43c
AL
271 f.Write(data, strlen(data));
272 f.Close();
7e726255 273 return fn;
3fc8f685 274}
234edfd0
AL
275 /*}}}*/
276// WriteConfig - write out the config data from a debian package file /*{{{*/
277// ---------------------------------------------------------------------
278/* */
279void WriteConfig(const DebFile &file)
3fc8f685 280{
a5eef4f7
AL
281 string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
282 string configscript = WriteFile(file.Package.c_str(), "config", file.Config);
3fc8f685 283
7e726255 284 if (templatefile.empty() == true || configscript.empty() == true)
3fc8f685 285 return;
234edfd0
AL
286 cout << file.Package << " " << file.Version << " "
287 << templatefile << " " << configscript << endl;
234edfd0
AL
288}
289 /*}}}*/
290// InitCache - initialize the package cache /*{{{*/
291// ---------------------------------------------------------------------
292/* */
7e726255
AL
293bool Go(CommandLine &CmdL)
294{
3fc8f685 295 // Initialize the apt cache
7e726255 296 MMap *Map = 0;
3fc8f685
AL
297 pkgSourceList List;
298 List.ReadMainList();
299 OpProgress Prog;
300 pkgMakeStatusCache(List,Prog,&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 /*}}}*/
351
352int main(int argc, const char **argv)
353{
354 CommandLine::Args Args[] = {
355 {'h',"help","help",0},
356 {'v',"version","version",0},
357 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
358 {'c',"config-file",0,CommandLine::ConfigFile},
359 {'o',"option",0,CommandLine::ArbItem},
360 {0,0,0,0}};
67111687
AL
361
362 // Set up gettext support
363 setlocale(LC_ALL,"");
364 textdomain(PACKAGE);
365
7e726255
AL
366 // Parse the command line and initialize the package library
367 CommandLine CmdL(Args,_config);
368 if (pkgInitConfig(*_config) == false ||
369 CmdL.Parse(argc,argv) == false ||
370 pkgInitSystem(*_config,_system) == false)
371 {
372 _error->DumpErrors();
373 return 100;
374 }
375
376 // See if the help should be shown
377 if (_config->FindB("help") == true ||
378 CmdL.FileSize() == 0)
379 return ShowHelp();
380
381 Go(CmdL);
3fc8f685 382
7e726255
AL
383 // Print any errors or warnings found during operation
384 if (_error->empty() == false)
385 {
386 // This goes to stderr..
387 bool Errors = _error->PendingError();
388 _error->DumpErrors();
389 return Errors == true?100:0;
390 }
391
3fc8f685
AL
392 return 0;
393}