]>
git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-extracttemplates.cc,v 1.10 2002/02/25 03:52:58 tausq Exp $
4 /* ######################################################################
6 APT Extract Templates - Program to extract debconf config and template
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
13 ##################################################################### */
15 // Include Files /*{{{*/
16 #include <apt-pkg/init.h>
17 #include <apt-pkg/cmndline.h>
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>
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>
30 #include <apt-pkg/fileutl.h>
41 #include "apt-extracttemplates.h"
46 pkgCache
*DebFile::Cache
= 0;
48 // DebFile::DebFile - Construct the DebFile object /*{{{*/
49 // ---------------------------------------------------------------------
51 DebFile::DebFile(const char *debfile
)
52 : File(debfile
, FileFd::ReadOnly
), Control(0), DepOp(0),
53 PreDepOp(0), Config(0), Template(0), Which(None
)
57 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
58 // ---------------------------------------------------------------------
67 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
68 // ---------------------------------------------------------------------
70 string
DebFile::GetInstalledVer(const string
&package
)
72 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(package
);
73 if (Pkg
.end() == false)
75 pkgCache::VerIterator V
= Pkg
.CurrentVer();
85 // DebFile::Go - Start extracting a debian package /*{{{*/
86 // ---------------------------------------------------------------------
91 if (_error
->PendingError() == true)
94 const ARArchive::Member
*Member
= AR
.FindMember("control.tar.gz");
96 return _error
->Error(_("%s not a valid DEB package."),File
.Name().c_str());
98 if (File
.Seek(Member
->Start
) == false)
100 ExtractTar
Tar(File
, Member
->Size
);
101 return Tar
.Go(*this);
104 // DebFile::DoItem examine element in package and mark /*{{{*/
105 // ---------------------------------------------------------------------
107 bool DebFile::DoItem(Item
&I
, int &Fd
)
109 if (strcmp(I
.Name
, "control") == 0)
112 Control
= new char[I
.Size
+1];
116 // make it call the Process method below. this is so evil
119 else if (strcmp(I
.Name
, "config") == 0)
122 Config
= new char[I
.Size
+1];
127 else if (strcmp(I
.Name
, "templates") == 0)
130 Template
= new char[I
.Size
+1];
131 Template
[I
.Size
] = 0;
143 // DebFile::Process examine element in package and copy /*{{{*/
144 // ---------------------------------------------------------------------
146 bool DebFile::Process(Item
&I
, const unsigned char *data
,
147 unsigned long size
, unsigned long pos
)
152 memcpy(Control
+ pos
, data
, size
);
155 memcpy(Config
+ pos
, data
, size
);
158 memcpy(Template
+ pos
, data
, size
);
160 default: /* throw it away */ ;
165 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
166 // ---------------------------------------------------------------------
168 bool DebFile::ParseInfo()
170 if (Control
== NULL
) return false;
172 pkgTagSection Section
;
173 Section
.Scan(Control
, ControlLen
);
175 Package
= Section
.FindS("Package");
176 Version
= GetInstalledVer(Package
);
178 const char *Start
, *Stop
;
179 if (Section
.Find("Depends", Start
, Stop
) == true)
185 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
186 if (Start
== 0) return false;
193 if (Start
== Stop
) break;
197 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
203 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
204 if (Start
== 0) return false;
211 if (Start
== Stop
) break;
218 // ShowHelp - show a short help text /*{{{*/
219 // ---------------------------------------------------------------------
223 ioprintf(cout
,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE
,VERSION
,
224 COMMON_OS
,COMMON_CPU
,__DATE__
,__TIME__
);
226 if (_config
->FindB("version") == true)
230 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
232 "apt-extracttemplates is a tool to extract config and template info\n"
233 "from debian packages\n"
236 " -h This help text\n"
237 " -t Set the temp dir\n"
238 " -c=? Read this configuration file\n"
239 " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n");
243 // WriteFile - write the contents of the passed string to a file /*{{{*/
244 // ---------------------------------------------------------------------
246 string
WriteFile(const char *prefix
, const char *data
)
250 snprintf(fn
, sizeof(fn
), "%s/%s.%u%d", _config
->Find("APT::ExtractTemplates::TempDir", TMPDIR
).c_str(), prefix
, getpid(), i
++);
255 if (!f
.Open(fn
, FileFd::WriteTemp
, 0600))
257 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
261 f
.Write(data
, strlen(data
));
266 // WriteConfig - write out the config data from a debian package file /*{{{*/
267 // ---------------------------------------------------------------------
269 void WriteConfig(const DebFile
&file
)
271 string templatefile
= WriteFile("template", file
.Template
);
272 string configscript
= WriteFile("config", file
.Config
);
274 if (templatefile
.empty() == true || configscript
.empty() == true)
276 cout
<< file
.Package
<< " " << file
.Version
<< " "
277 << templatefile
<< " " << configscript
<< endl
;
280 // InitCache - initialize the package cache /*{{{*/
281 // ---------------------------------------------------------------------
283 bool Go(CommandLine
&CmdL
)
285 // Initialize the apt cache
290 pkgMakeStatusCache(List
,Prog
,&Map
,true);
291 DebFile::Cache
= new pkgCache(Map
);
292 if (_error
->PendingError() == true)
295 // Find out what version of debconf is currently installed
296 string debconfver
= DebFile::GetInstalledVer("debconf");
297 if (debconfver
.empty() == true)
298 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
300 // Process each package passsed in
301 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
303 // Will pick up the errors later..
304 DebFile
file(CmdL
.FileList
[I
]);
305 if (file
.Go() == false)
308 // Does the package have templates?
309 if (file
.Template
!= 0 && file
.ParseInfo() == true)
311 // Check to make sure debconf dependencies are
313 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
314 if (file
.DepVer
!= "" &&
315 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
316 file
.DepOp
,file
.DepVer
.c_str()
319 if (file
.PreDepVer
!= "" &&
320 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
321 file
.PreDepOp
,file
.PreDepVer
.c_str()
331 delete DebFile::Cache
;
333 return !_error
->PendingError();
337 int main(int argc
, const char **argv
)
339 CommandLine::Args Args
[] = {
340 {'h',"help","help",0},
341 {'v',"version","version",0},
342 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
343 {'c',"config-file",0,CommandLine::ConfigFile
},
344 {'o',"option",0,CommandLine::ArbItem
},
347 // Set up gettext support
348 setlocale(LC_ALL
,"");
351 // Parse the command line and initialize the package library
352 CommandLine
CmdL(Args
,_config
);
353 if (pkgInitConfig(*_config
) == false ||
354 CmdL
.Parse(argc
,argv
) == false ||
355 pkgInitSystem(*_config
,_system
) == false)
357 _error
->DumpErrors();
361 // See if the help should be shown
362 if (_config
->FindB("help") == true ||
363 CmdL
.FileSize() == 0)
368 // Print any errors or warnings found during operation
369 if (_error
->empty() == false)
371 // This goes to stderr..
372 bool Errors
= _error
->PendingError();
373 _error
->DumpErrors();
374 return Errors
== true?100:0;