]>
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>
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
), Size(0), Control(NULL
), ControlLen(0),
55 DepOp(0), 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 // ---------------------------------------------------------------------
94 return Deb
.ExtractTarMember(*this, "control.tar");
97 // DebFile::DoItem examine element in package and mark /*{{{*/
98 // ---------------------------------------------------------------------
100 bool DebFile::DoItem(Item
&I
, int &Fd
)
102 if (strcmp(I
.Name
, "control") == 0)
105 Control
= new char[I
.Size
+1];
109 // make it call the Process method below. this is so evil
112 else if (strcmp(I
.Name
, "config") == 0)
115 Config
= new char[I
.Size
+1];
120 else if (strcmp(I
.Name
, "templates") == 0)
123 Template
= new char[I
.Size
+1];
124 Template
[I
.Size
] = 0;
136 // DebFile::Process examine element in package and copy /*{{{*/
137 // ---------------------------------------------------------------------
139 bool DebFile::Process(Item
&/*I*/, const unsigned char *data
,
140 unsigned long size
, unsigned long pos
)
145 memcpy(Control
+ pos
, data
, size
);
148 memcpy(Config
+ pos
, data
, size
);
151 memcpy(Template
+ pos
, data
, size
);
153 default: /* throw it away */ ;
158 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
159 // ---------------------------------------------------------------------
161 bool DebFile::ParseInfo()
163 if (Control
== NULL
) return false;
165 pkgTagSection Section
;
166 Section
.Scan(Control
, ControlLen
);
168 Package
= Section
.FindS("Package");
169 Version
= GetInstalledVer(Package
);
171 const char *Start
, *Stop
;
172 if (Section
.Find("Depends", Start
, Stop
) == true)
178 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
179 if (Start
== 0) return false;
186 if (Start
== Stop
) break;
190 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
196 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
197 if (Start
== 0) return false;
204 if (Start
== Stop
) break;
211 // ShowHelp - show a short help text /*{{{*/
212 // ---------------------------------------------------------------------
214 static int ShowHelp(void)
216 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
217 COMMON_ARCH
,__DATE__
,__TIME__
);
219 if (_config
->FindB("version") == true)
223 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
225 "apt-extracttemplates is a tool to extract config and template info\n"
226 "from debian packages\n"
229 " -h This help text\n"
230 " -t Set the temp dir\n"
231 " -c=? Read this configuration file\n"
232 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
236 // WriteFile - write the contents of the passed string to a file /*{{{*/
237 // ---------------------------------------------------------------------
239 static string
WriteFile(const char *package
, const char *prefix
, const char *data
)
244 std::string tempdir
= GetTempDir();
245 snprintf(fn
, sizeof(fn
), "%s/%s.%s.%u%d",
246 _config
->Find("APT::ExtractTemplates::TempDir",
247 tempdir
.c_str()).c_str(),
248 package
, prefix
, getpid(), i
++);
253 if (!f
.Open(fn
, FileFd::WriteTemp
, 0600))
255 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
259 f
.Write(data
, strlen(data
));
264 // WriteConfig - write out the config data from a debian package file /*{{{*/
265 // ---------------------------------------------------------------------
267 static void WriteConfig(const DebFile
&file
)
269 string templatefile
= WriteFile(file
.Package
.c_str(), "template", file
.Template
);
270 string configscript
= WriteFile(file
.Package
.c_str(), "config", file
.Config
);
272 if (templatefile
.empty() == true || configscript
.empty() == true)
274 cout
<< file
.Package
<< " " << file
.Version
<< " "
275 << templatefile
<< " " << configscript
<< endl
;
278 // InitCache - initialize the package cache /*{{{*/
279 // ---------------------------------------------------------------------
281 static bool Go(CommandLine
&CmdL
)
283 // Initialize the apt cache
287 pkgCacheGenerator::MakeStatusCache(List
,NULL
,&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)
306 _error
->Error("Prior errors apply to %s",CmdL
.FileList
[I
]);
310 // Does the package have templates?
311 if (file
.Template
!= 0 && file
.ParseInfo() == true)
313 // Check to make sure debconf dependencies are
315 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
316 if (file
.DepVer
!= "" &&
317 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
318 file
.DepOp
,file
.DepVer
.c_str()
321 if (file
.PreDepVer
!= "" &&
322 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
323 file
.PreDepOp
,file
.PreDepVer
.c_str()
333 delete DebFile::Cache
;
335 return !_error
->PendingError();
338 int main(int argc
, const char **argv
) /*{{{*/
340 CommandLine::Args Args
[] = {
341 {'h',"help","help",0},
342 {'v',"version","version",0},
343 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
344 {'c',"config-file",0,CommandLine::ConfigFile
},
345 {'o',"option",0,CommandLine::ArbItem
},
348 // Set up gettext support
349 setlocale(LC_ALL
,"");
352 // Parse the command line and initialize the package library
353 CommandLine
CmdL(Args
,_config
);
354 if (pkgInitConfig(*_config
) == false ||
355 CmdL
.Parse(argc
,argv
) == false ||
356 pkgInitSystem(*_config
,_system
) == false)
358 _error
->DumpErrors();
362 // See if the help should be shown
363 if (_config
->FindB("help") == true ||
364 CmdL
.FileSize() == 0)
369 // Print any errors or warnings found during operation
370 if (_error
->empty() == false)
372 // This goes to stderr..
373 bool Errors
= _error
->PendingError();
374 _error
->DumpErrors();
375 return Errors
== true?100:0;