]>
git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
9422a946a747c1adc5b8159f634756e123ffce5a
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-extracttemplates.cc,v 1.8 2002/01/09 04:59:44 jgg 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>
40 #include "apt-extracttemplates.h"
45 pkgCache
*DebFile::Cache
= 0;
47 // DebFile::DebFile - Construct the DebFile object /*{{{*/
48 // ---------------------------------------------------------------------
50 DebFile::DebFile(const char *debfile
)
51 : File(debfile
, FileFd::ReadOnly
), Control(0), DepOp(0),
52 PreDepOp(0), Config(0), Template(0), Which(None
)
56 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
57 // ---------------------------------------------------------------------
66 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
67 // ---------------------------------------------------------------------
69 string
DebFile::GetInstalledVer(const string
&package
)
71 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(package
);
72 if (Pkg
.end() == false)
74 pkgCache::VerIterator V
= Pkg
.CurrentVer();
84 // DebFile::Go - Start extracting a debian package /*{{{*/
85 // ---------------------------------------------------------------------
90 if (_error
->PendingError() == true)
93 const ARArchive::Member
*Member
= AR
.FindMember("control.tar.gz");
95 return _error
->Error(_("%s not a valid DEB package."),File
.Name().c_str());
97 if (File
.Seek(Member
->Start
) == false)
99 ExtractTar
Tar(File
, Member
->Size
);
100 return Tar
.Go(*this);
103 // DebFile::DoItem examine element in package and mark /*{{{*/
104 // ---------------------------------------------------------------------
106 bool DebFile::DoItem(Item
&I
, int &Fd
)
108 if (strcmp(I
.Name
, "control") == 0)
111 Control
= new char[I
.Size
+1];
115 // make it call the Process method below. this is so evil
118 else if (strcmp(I
.Name
, "config") == 0)
121 Config
= new char[I
.Size
+1];
126 else if (strcmp(I
.Name
, "templates") == 0)
129 Template
= new char[I
.Size
+1];
130 Template
[I
.Size
] = 0;
142 // DebFile::Process examine element in package and copy /*{{{*/
143 // ---------------------------------------------------------------------
145 bool DebFile::Process(Item
&I
, const unsigned char *data
,
146 unsigned long size
, unsigned long pos
)
151 memcpy(Control
+ pos
, data
, size
);
154 memcpy(Config
+ pos
, data
, size
);
157 memcpy(Template
+ pos
, data
, size
);
159 default: /* throw it away */ ;
164 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
165 // ---------------------------------------------------------------------
167 bool DebFile::ParseInfo()
169 if (Control
== NULL
) return false;
171 pkgTagSection Section
;
172 Section
.Scan(Control
, ControlLen
);
174 Package
= Section
.FindS("Package");
175 Version
= GetInstalledVer(Package
);
177 const char *Start
, *Stop
;
178 if (Section
.Find("Depends", Start
, Stop
) == true)
184 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
185 if (Start
== 0) return false;
192 if (Start
== Stop
) break;
196 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
202 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
203 if (Start
== 0) return false;
210 if (Start
== Stop
) break;
217 // ShowHelp - show a short help text /*{{{*/
218 // ---------------------------------------------------------------------
222 ioprintf(cout
,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE
,VERSION
,
223 COMMON_OS
,COMMON_CPU
,__DATE__
,__TIME__
);
225 if (_config
->FindB("version") == true)
229 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
231 "apt-extracttemplates is a tool to extract config and template info\n"
232 "from debian packages\n"
235 " -h This help text\n"
236 " -t Set the temp dir\n"
237 " -c=? Read this configuration file\n"
238 " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n");
242 // WriteFile - write the contents of the passed string to a file /*{{{*/
243 // ---------------------------------------------------------------------
245 string
WriteFile(const char *prefix
, const char *data
)
249 snprintf(fn
, sizeof(fn
), "%s/%s.%u%d", _config
->Find("APT::ExtractTemplates::TempDir", TMPDIR
).c_str(), prefix
, getpid(), i
++);
254 if (!f
.Open(fn
, FileFd::WriteTemp
, 0600))
256 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
260 f
.Write(data
, strlen(data
));
265 // WriteConfig - write out the config data from a debian package file /*{{{*/
266 // ---------------------------------------------------------------------
268 void WriteConfig(const DebFile
&file
)
270 string templatefile
= WriteFile("template", file
.Template
);
271 string configscript
= WriteFile("config", file
.Config
);
273 if (templatefile
.empty() == true || configscript
.empty() == true)
275 cout
<< file
.Package
<< " " << file
.Version
<< " "
276 << templatefile
<< " " << configscript
<< endl
;
279 // InitCache - initialize the package cache /*{{{*/
280 // ---------------------------------------------------------------------
282 bool Go(CommandLine
&CmdL
)
284 // Initialize the apt cache
289 pkgMakeStatusCache(List
,Prog
,&Map
,true);
290 DebFile::Cache
= new pkgCache(Map
);
291 if (_error
->PendingError() == true)
294 // Find out what version of debconf is currently installed
295 string debconfver
= DebFile::GetInstalledVer("debconf");
296 if (debconfver
.empty() == true)
297 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
299 // Process each package passsed in
300 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
302 // Will pick up the errors later..
303 DebFile
file(CmdL
.FileList
[I
]);
304 if (file
.Go() == false)
307 // Does the package have templates?
308 if (file
.Template
!= 0 && file
.ParseInfo() == true)
310 // Check to make sure debconf dependencies are
312 cout
<< "Check " << file
.DepVer
<< ',' << debconfver
<< endl
;
313 if (file
.DepVer
!= "" &&
314 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
315 file
.DepOp
,file
.DepVer
.c_str()
318 if (file
.PreDepVer
!= "" &&
319 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
320 file
.PreDepOp
,file
.PreDepVer
.c_str()
330 delete DebFile::Cache
;
332 return !_error
->PendingError();
336 int main(int argc
, const char **argv
)
338 CommandLine::Args Args
[] = {
339 {'h',"help","help",0},
340 {'v',"version","version",0},
341 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
342 {'c',"config-file",0,CommandLine::ConfigFile
},
343 {'o',"option",0,CommandLine::ArbItem
},
346 // Set up gettext support
347 setlocale(LC_ALL
,"");
350 // Parse the command line and initialize the package library
351 CommandLine
CmdL(Args
,_config
);
352 if (pkgInitConfig(*_config
) == false ||
353 CmdL
.Parse(argc
,argv
) == false ||
354 pkgInitSystem(*_config
,_system
) == false)
356 _error
->DumpErrors();
360 // See if the help should be shown
361 if (_config
->FindB("help") == true ||
362 CmdL
.FileSize() == 0)
367 // Print any errors or warnings found during operation
368 if (_error
->empty() == false)
370 // This goes to stderr..
371 bool Errors
= _error
->PendingError();
372 _error
->DumpErrors();
373 return Errors
== true?100:0;