]>
git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-extracttemplates.cc,v 1.4 2001/02/27 04:26:03 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>
39 #include "apt-extracttemplates.h"
42 #define TMPDIR "/var/lib/debconf/"
44 pkgCache
*DebFile::Cache
= 0;
46 // DebFile::DebFile - Construct the DebFile object /*{{{*/
47 // ---------------------------------------------------------------------
49 DebFile::DebFile(const char *debfile
)
50 : File(debfile
, FileFd::ReadOnly
), Control(0), DepOp(0),
51 PreDepOp(0), Config(0), Template(0), Which(None
)
55 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
56 // ---------------------------------------------------------------------
65 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
66 // ---------------------------------------------------------------------
68 string
DebFile::GetInstalledVer(const string
&package
)
70 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(package
);
71 if (Pkg
.end() == false)
73 pkgCache::VerIterator V
= Pkg
.CurrentVer();
83 // DebFile::Go - Start extracting a debian package /*{{{*/
84 // ---------------------------------------------------------------------
89 if (_error
->PendingError() == true)
92 const ARArchive::Member
*Member
= AR
.FindMember("control.tar.gz");
94 return _error
->Error(_("%s not a valid DEB package."),File
.Name().c_str());
96 if (File
.Seek(Member
->Start
) == false)
98 ExtractTar
Tar(File
, Member
->Size
);
102 // DebFile::DoItem examine element in package and mark /*{{{*/
103 // ---------------------------------------------------------------------
105 bool DebFile::DoItem(Item
&I
, int &Fd
)
107 if (strcmp(I
.Name
, "control") == 0)
110 Control
= new char[I
.Size
+1];
114 // make it call the Process method below. this is so evil
117 else if (strcmp(I
.Name
, "config") == 0)
120 Config
= new char[I
.Size
+1];
125 else if (strcmp(I
.Name
, "templates") == 0)
128 Template
= new char[I
.Size
+1];
129 Template
[I
.Size
] = 0;
141 // DebFile::Process examine element in package and copy /*{{{*/
142 // ---------------------------------------------------------------------
144 bool DebFile::Process(Item
&I
, const unsigned char *data
,
145 unsigned long size
, unsigned long pos
)
150 memcpy(Control
+ pos
, data
, size
);
153 memcpy(Config
+ pos
, data
, size
);
156 memcpy(Template
+ pos
, data
, size
);
158 default: /* throw it away */ ;
163 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
164 // ---------------------------------------------------------------------
166 bool DebFile::ParseInfo()
168 if (Control
== NULL
) return false;
170 pkgTagSection Section
;
171 Section
.Scan(Control
, ControlLen
);
173 Package
= Section
.FindS("Package");
174 Version
= GetInstalledVer(Package
);
176 const char *Start
, *Stop
;
177 if (Section
.Find("Depends", Start
, Stop
) == true)
183 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
184 if (Start
== 0) return false;
191 if (Start
== Stop
) break;
195 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
201 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
202 if (Start
== 0) return false;
209 if (Start
== Stop
) break;
216 // ShowHelp - show a short help text /*{{{*/
217 // ---------------------------------------------------------------------
221 ioprintf(cout
,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE
,VERSION
,
222 COMMON_OS
,COMMON_CPU
,__DATE__
,__TIME__
);
224 if (_config
->FindB("version") == true)
228 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
230 "apt-extracttemplates is a tool to extract config and template info\n"
231 "from debian packages\n"
234 " -h This help text\n"
235 " -t Set the temp dir\n"
236 " -c=? Read this configuration file\n"
237 " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n");
241 // WriteFile - write the contents of the passed string to a file /*{{{*/
242 // ---------------------------------------------------------------------
244 string
WriteFile(const char *prefix
, const char *data
)
248 snprintf(fn
, sizeof(fn
), "%s%s.%u%d", _config
->Find("APT::ExtractTemplates::TempDir", TMPDIR
).c_str(), prefix
, getpid(), i
++);
253 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
257 ofs
<< (data
? data
: "");
262 // WriteConfig - write out the config data from a debian package file /*{{{*/
263 // ---------------------------------------------------------------------
265 void WriteConfig(const DebFile
&file
)
267 string templatefile
= WriteFile("template", file
.Template
);
268 string configscript
= WriteFile("config", file
.Config
);
270 if (templatefile
.empty() == true || configscript
.empty() == true)
272 cout
<< file
.Package
<< " " << file
.Version
<< " "
273 << templatefile
<< " " << configscript
<< endl
;
276 // InitCache - initialize the package cache /*{{{*/
277 // ---------------------------------------------------------------------
279 bool Go(CommandLine
&CmdL
)
281 // Initialize the apt cache
286 pkgMakeStatusCache(List
,Prog
,&Map
,true);
287 DebFile::Cache
= new pkgCache(Map
);
288 if (_error
->PendingError() == true)
291 // Find out what version of debconf is currently installed
292 string debconfver
= DebFile::GetInstalledVer("debconf");
293 if (debconfver
.empty() == true)
294 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
296 // Process each package passsed in
297 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
299 // Will pick up the errors later..
300 DebFile
file(CmdL
.FileList
[I
]);
301 if (file
.Go() == false)
304 // Does the package have templates?
305 if (file
.Template
!= 0 && file
.ParseInfo() == true)
307 // Check to make sure debconf dependencies are
309 if (file
.DepVer
!= "" &&
310 DebFile::Cache
->VS
->CheckDep(file
.DepVer
.c_str(),
311 file
.DepOp
, debconfver
.c_str()) == false)
313 if (file
.PreDepVer
!= "" &&
314 DebFile::Cache
->VS
->CheckDep(file
.PreDepVer
.c_str(),
315 file
.PreDepOp
, debconfver
.c_str()) == false)
324 delete DebFile::Cache
;
326 return !_error
->PendingError();
330 int main(int argc
, const char **argv
)
332 CommandLine::Args Args
[] = {
333 {'h',"help","help",0},
334 {'v',"version","version",0},
335 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
336 {'c',"config-file",0,CommandLine::ConfigFile
},
337 {'o',"option",0,CommandLine::ArbItem
},
340 // Parse the command line and initialize the package library
341 CommandLine
CmdL(Args
,_config
);
342 if (pkgInitConfig(*_config
) == false ||
343 CmdL
.Parse(argc
,argv
) == false ||
344 pkgInitSystem(*_config
,_system
) == false)
346 _error
->DumpErrors();
350 // See if the help should be shown
351 if (_config
->FindB("help") == true ||
352 CmdL
.FileSize() == 0)
357 // Print any errors or warnings found during operation
358 if (_error
->empty() == false)
360 // This goes to stderr..
361 bool Errors
= _error
->PendingError();
362 _error
->DumpErrors();
363 return Errors
== true?100:0;