]>
git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-extracttemplates.cc,v 1.14 2003/01/11 07:18: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>
41 #include "apt-extracttemplates.h"
48 pkgCache
*DebFile::Cache
= 0;
50 // DebFile::DebFile - Construct the DebFile object /*{{{*/
51 // ---------------------------------------------------------------------
53 DebFile::DebFile(const char *debfile
)
54 : File(debfile
, FileFd::ReadOnly
), Control(0), DepOp(0),
55 PreDepOp(0), Config(0), Template(0), Which(None
)
59 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
60 // ---------------------------------------------------------------------
69 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
70 // ---------------------------------------------------------------------
72 string
DebFile::GetInstalledVer(const string
&package
)
74 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(package
);
75 if (Pkg
.end() == false)
77 pkgCache::VerIterator V
= Pkg
.CurrentVer();
87 // DebFile::Go - Start extracting a debian package /*{{{*/
88 // ---------------------------------------------------------------------
93 if (_error
->PendingError() == true)
96 const ARArchive::Member
*Member
= AR
.FindMember("control.tar.gz");
98 return _error
->Error(_("%s not a valid DEB package."),File
.Name().c_str());
100 if (File
.Seek(Member
->Start
) == false)
102 ExtractTar
Tar(File
, Member
->Size
);
103 return Tar
.Go(*this);
106 // DebFile::DoItem examine element in package and mark /*{{{*/
107 // ---------------------------------------------------------------------
109 bool DebFile::DoItem(Item
&I
, int &Fd
)
111 if (strcmp(I
.Name
, "control") == 0)
114 Control
= new char[I
.Size
+1];
118 // make it call the Process method below. this is so evil
121 else if (strcmp(I
.Name
, "config") == 0)
124 Config
= new char[I
.Size
+1];
129 else if (strcmp(I
.Name
, "templates") == 0)
132 Template
= new char[I
.Size
+1];
133 Template
[I
.Size
] = 0;
145 // DebFile::Process examine element in package and copy /*{{{*/
146 // ---------------------------------------------------------------------
148 bool DebFile::Process(Item
&I
, const unsigned char *data
,
149 unsigned long size
, unsigned long pos
)
154 memcpy(Control
+ pos
, data
, size
);
157 memcpy(Config
+ pos
, data
, size
);
160 memcpy(Template
+ pos
, data
, size
);
162 default: /* throw it away */ ;
167 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
168 // ---------------------------------------------------------------------
170 bool DebFile::ParseInfo()
172 if (Control
== NULL
) return false;
174 pkgTagSection Section
;
175 Section
.Scan(Control
, ControlLen
);
177 Package
= Section
.FindS("Package");
178 Version
= GetInstalledVer(Package
);
180 const char *Start
, *Stop
;
181 if (Section
.Find("Depends", Start
, Stop
) == true)
187 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
188 if (Start
== 0) return false;
195 if (Start
== Stop
) break;
199 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
205 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
206 if (Start
== 0) return false;
213 if (Start
== Stop
) break;
220 // ShowHelp - show a short help text /*{{{*/
221 // ---------------------------------------------------------------------
225 ioprintf(cout
,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE
,VERSION
,
226 COMMON_OS
,COMMON_CPU
,__DATE__
,__TIME__
);
228 if (_config
->FindB("version") == true)
232 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
234 "apt-extracttemplates is a tool to extract config and template info\n"
235 "from debian packages\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");
245 // WriteFile - write the contents of the passed string to a file /*{{{*/
246 // ---------------------------------------------------------------------
248 string
WriteFile(const char *package
, const char *prefix
, const char *data
)
252 snprintf(fn
, sizeof(fn
), "%s/%s.%s.%u%d", _config
->Find("APT::ExtractTemplates::TempDir", TMPDIR
).c_str(), package
, prefix
, getpid(), i
++);
257 if (!f
.Open(fn
, FileFd::WriteTemp
, 0600))
259 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
263 f
.Write(data
, strlen(data
));
268 // WriteConfig - write out the config data from a debian package file /*{{{*/
269 // ---------------------------------------------------------------------
271 void WriteConfig(const DebFile
&file
)
273 string templatefile
= WriteFile(file
.Package
.c_str(), "template", file
.Template
);
274 string configscript
= WriteFile(file
.Package
.c_str(), "config", file
.Config
);
276 if (templatefile
.empty() == true || configscript
.empty() == true)
278 cout
<< file
.Package
<< " " << file
.Version
<< " "
279 << templatefile
<< " " << configscript
<< endl
;
282 // InitCache - initialize the package cache /*{{{*/
283 // ---------------------------------------------------------------------
285 bool Go(CommandLine
&CmdL
)
287 // Initialize the apt cache
292 pkgMakeStatusCache(List
,Prog
,&Map
,true);
295 DebFile::Cache
= new pkgCache(Map
);
296 if (_error
->PendingError() == true)
299 // Find out what version of debconf is currently installed
300 string debconfver
= DebFile::GetInstalledVer("debconf");
301 if (debconfver
.empty() == true)
302 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
304 // Process each package passsed in
305 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
307 // Will pick up the errors later..
308 DebFile
file(CmdL
.FileList
[I
]);
309 if (file
.Go() == false)
311 _error
->Error("Prior errors apply to %s",CmdL
.FileList
[I
]);
315 // Does the package have templates?
316 if (file
.Template
!= 0 && file
.ParseInfo() == true)
318 // Check to make sure debconf dependencies are
320 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
321 if (file
.DepVer
!= "" &&
322 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
323 file
.DepOp
,file
.DepVer
.c_str()
326 if (file
.PreDepVer
!= "" &&
327 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
328 file
.PreDepOp
,file
.PreDepVer
.c_str()
338 delete DebFile::Cache
;
340 return !_error
->PendingError();
344 int main(int argc
, const char **argv
)
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
},
354 // Set up gettext support
355 setlocale(LC_ALL
,"");
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)
364 _error
->DumpErrors();
368 // See if the help should be shown
369 if (_config
->FindB("help") == true ||
370 CmdL
.FileSize() == 0)
375 // Print any errors or warnings found during operation
376 if (_error
->empty() == false)
378 // This goes to stderr..
379 bool Errors
= _error
->PendingError();
380 _error
->DumpErrors();
381 return Errors
== true?100:0;