]>
git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-extracttemplates.cc,v 1.15 2003/07/26 00:00:11 mdz 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 /*{{{*/
18 #include <apt-pkg/init.h>
19 #include <apt-pkg/cmndline.h>
20 #include <apt-pkg/pkgcache.h>
21 #include <apt-pkg/cacheiterators.h>
22 #include <apt-pkg/configuration.h>
23 #include <apt-pkg/sourcelist.h>
24 #include <apt-pkg/pkgcachegen.h>
25 #include <apt-pkg/version.h>
26 #include <apt-pkg/tagfile.h>
27 #include <apt-pkg/debfile.h>
28 #include <apt-pkg/deblistparser.h>
29 #include <apt-pkg/error.h>
30 #include <apt-pkg/strutl.h>
31 #include <apt-pkg/fileutl.h>
32 #include <apt-pkg/pkgsystem.h>
33 #include <apt-pkg/dirstream.h>
34 #include <apt-pkg/mmap.h>
42 #include "apt-extracttemplates.h"
49 pkgCache
*DebFile::Cache
= 0;
51 // DebFile::DebFile - Construct the DebFile object /*{{{*/
52 // ---------------------------------------------------------------------
54 DebFile::DebFile(const char *debfile
)
55 : File(debfile
, FileFd::ReadOnly
), Control(NULL
), ControlLen(0),
56 DepOp(0), PreDepOp(0), Config(0), Template(0), Which(None
)
60 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
61 // ---------------------------------------------------------------------
70 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
71 // ---------------------------------------------------------------------
73 string
DebFile::GetInstalledVer(const string
&package
)
75 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(package
);
76 if (Pkg
.end() == false)
78 pkgCache::VerIterator V
= Pkg
.CurrentVer();
88 // DebFile::Go - Start extracting a debian package /*{{{*/
89 // ---------------------------------------------------------------------
95 return Deb
.ExtractTarMember(*this, "control.tar");
98 // DebFile::DoItem examine element in package and mark /*{{{*/
99 // ---------------------------------------------------------------------
101 bool DebFile::DoItem(Item
&I
, int &Fd
)
103 if (strcmp(I
.Name
, "control") == 0)
106 Control
= new char[I
.Size
+3];
107 Control
[I
.Size
] = '\n';
108 Control
[I
.Size
+ 1] = '\n';
109 Control
[I
.Size
+ 2] = '\0';
111 ControlLen
= I
.Size
+ 3;
112 // make it call the Process method below. this is so evil
115 else if (strcmp(I
.Name
, "config") == 0)
118 Config
= new char[I
.Size
+1];
123 else if (strcmp(I
.Name
, "templates") == 0)
126 Template
= new char[I
.Size
+1];
127 Template
[I
.Size
] = 0;
139 // DebFile::Process examine element in package and copy /*{{{*/
140 // ---------------------------------------------------------------------
142 bool DebFile::Process(Item
&/*I*/, const unsigned char *data
,
143 unsigned long long size
, unsigned long long pos
)
148 memcpy(Control
+ pos
, data
, size
);
151 memcpy(Config
+ pos
, data
, size
);
154 memcpy(Template
+ pos
, data
, size
);
156 default: /* throw it away */ ;
161 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
162 // ---------------------------------------------------------------------
164 bool DebFile::ParseInfo()
166 if (Control
== NULL
) return false;
168 pkgTagSection Section
;
169 if (Section
.Scan(Control
, ControlLen
) == false)
172 Package
= Section
.FindS("Package");
173 Version
= GetInstalledVer(Package
);
175 const char *Start
, *Stop
;
176 if (Section
.Find("Depends", Start
, Stop
) == true)
182 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
183 if (Start
== 0) return false;
190 if (Start
== Stop
) break;
194 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
200 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
201 if (Start
== 0) return false;
208 if (Start
== Stop
) break;
215 // ShowHelp - show a short help text /*{{{*/
216 // ---------------------------------------------------------------------
218 static int ShowHelp(void)
220 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
221 COMMON_ARCH
,__DATE__
,__TIME__
);
223 if (_config
->FindB("version") == true)
227 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
229 "apt-extracttemplates is a tool to extract config and template info\n"
230 "from debian packages\n"
233 " -h This help text\n"
234 " -t Set the temp dir\n"
235 " -c=? Read this configuration file\n"
236 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
240 // WriteFile - write the contents of the passed string to a file /*{{{*/
241 // ---------------------------------------------------------------------
243 static string
WriteFile(const char *package
, const char *prefix
, const char *data
)
247 std::string tempdir
= GetTempDir();
248 snprintf(fn
, sizeof(fn
), "%s/%s.%s.XXXXXX",
249 _config
->Find("APT::ExtractTemplates::TempDir",
250 tempdir
.c_str()).c_str(),
255 int fd
= mkstemp(fn
);
257 _error
->Errno("ofstream::ofstream",_("Unable to mkstemp %s"),fn
);
260 if (!f
.OpenDescriptor(fd
, FileFd::WriteOnly
, FileFd::None
, true))
262 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
265 f
.Write(data
, strlen(data
));
270 // WriteConfig - write out the config data from a debian package file /*{{{*/
271 // ---------------------------------------------------------------------
273 static void WriteConfig(const DebFile
&file
)
275 string templatefile
= WriteFile(file
.Package
.c_str(), "template", file
.Template
);
276 string configscript
= WriteFile(file
.Package
.c_str(), "config", file
.Config
);
278 if (templatefile
.empty() == true || configscript
.empty() == true)
280 cout
<< file
.Package
<< " " << file
.Version
<< " "
281 << templatefile
<< " " << configscript
<< endl
;
284 // InitCache - initialize the package cache /*{{{*/
285 // ---------------------------------------------------------------------
287 static bool Go(CommandLine
&CmdL
)
289 // Initialize the apt cache
293 pkgCacheGenerator::MakeStatusCache(List
,NULL
,&Map
,true);
296 DebFile::Cache
= new pkgCache(Map
);
297 if (_error
->PendingError() == true)
300 // Find out what version of debconf is currently installed
301 string debconfver
= DebFile::GetInstalledVer("debconf");
302 if (debconfver
.empty() == true)
303 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
305 // Process each package passsed in
306 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
308 // Will pick up the errors later..
309 DebFile
file(CmdL
.FileList
[I
]);
310 if (file
.Go() == false)
312 _error
->Error("Prior errors apply to %s",CmdL
.FileList
[I
]);
316 // Does the package have templates?
317 if (file
.Template
!= 0 && file
.ParseInfo() == true)
319 // Check to make sure debconf dependencies are
321 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
322 if (file
.DepVer
!= "" &&
323 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
324 file
.DepOp
,file
.DepVer
.c_str()
327 if (file
.PreDepVer
!= "" &&
328 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
329 file
.PreDepOp
,file
.PreDepVer
.c_str()
339 delete DebFile::Cache
;
341 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;