]>
git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
9261f68057e9098b399d7c4118b5dddb3ebdbd1e
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-extracttemplates.cc,v 1.12 2002/11/09 22:41:55 doogie 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 *package
, const char *prefix
, const char *data
)
250 snprintf(fn
, sizeof(fn
), "%s/%s.%s.%u%d", _config
->Find("APT::ExtractTemplates::TempDir", TMPDIR
).c_str(), package
, 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(file
.package
, "template", file
.Template
);
272 string configscript
= WriteFile(file
.package
, "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);
293 DebFile::Cache
= new pkgCache(Map
);
294 if (_error
->PendingError() == true)
297 // Find out what version of debconf is currently installed
298 string debconfver
= DebFile::GetInstalledVer("debconf");
299 if (debconfver
.empty() == true)
300 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
302 // Process each package passsed in
303 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
305 // Will pick up the errors later..
306 DebFile
file(CmdL
.FileList
[I
]);
307 if (file
.Go() == false)
309 _error
->Error("Prior errors apply to %s",CmdL
.FileList
[I
]);
313 // Does the package have templates?
314 if (file
.Template
!= 0 && file
.ParseInfo() == true)
316 // Check to make sure debconf dependencies are
318 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
319 if (file
.DepVer
!= "" &&
320 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
321 file
.DepOp
,file
.DepVer
.c_str()
324 if (file
.PreDepVer
!= "" &&
325 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
326 file
.PreDepOp
,file
.PreDepVer
.c_str()
336 delete DebFile::Cache
;
338 return !_error
->PendingError();
342 int main(int argc
, const char **argv
)
344 CommandLine::Args Args
[] = {
345 {'h',"help","help",0},
346 {'v',"version","version",0},
347 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
348 {'c',"config-file",0,CommandLine::ConfigFile
},
349 {'o',"option",0,CommandLine::ArbItem
},
352 // Set up gettext support
353 setlocale(LC_ALL
,"");
356 // Parse the command line and initialize the package library
357 CommandLine
CmdL(Args
,_config
);
358 if (pkgInitConfig(*_config
) == false ||
359 CmdL
.Parse(argc
,argv
) == false ||
360 pkgInitSystem(*_config
,_system
) == false)
362 _error
->DumpErrors();
366 // See if the help should be shown
367 if (_config
->FindB("help") == true ||
368 CmdL
.FileSize() == 0)
373 // Print any errors or warnings found during operation
374 if (_error
->empty() == false)
376 // This goes to stderr..
377 bool Errors
= _error
->PendingError();
378 _error
->DumpErrors();
379 return Errors
== true?100:0;