]>
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 /*{{{*/
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
,"gzip");
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 compiled on %s %s\n"),PACKAGE
,VERSION
,
226 COMMON_ARCH
,__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 arbitrary 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 const char *tempdir
= NULL
;
254 tempdir
= getenv("TMPDIR");
258 snprintf(fn
, sizeof(fn
), "%s/%s.%s.%u%d",
259 _config
->Find("APT::ExtractTemplates::TempDir", tempdir
).c_str(),
260 package
, prefix
, getpid(), i
++);
265 if (!f
.Open(fn
, FileFd::WriteTemp
, 0600))
267 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
271 f
.Write(data
, strlen(data
));
276 // WriteConfig - write out the config data from a debian package file /*{{{*/
277 // ---------------------------------------------------------------------
279 void WriteConfig(const DebFile
&file
)
281 string templatefile
= WriteFile(file
.Package
.c_str(), "template", file
.Template
);
282 string configscript
= WriteFile(file
.Package
.c_str(), "config", file
.Config
);
284 if (templatefile
.empty() == true || configscript
.empty() == true)
286 cout
<< file
.Package
<< " " << file
.Version
<< " "
287 << templatefile
<< " " << configscript
<< endl
;
290 // InitCache - initialize the package cache /*{{{*/
291 // ---------------------------------------------------------------------
293 bool Go(CommandLine
&CmdL
)
295 // Initialize the apt cache
299 pkgCacheGenerator::MakeStatusCache(List
,NULL
,&Map
,true);
302 DebFile::Cache
= new pkgCache(Map
);
303 if (_error
->PendingError() == true)
306 // Find out what version of debconf is currently installed
307 string debconfver
= DebFile::GetInstalledVer("debconf");
308 if (debconfver
.empty() == true)
309 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
311 // Process each package passsed in
312 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
314 // Will pick up the errors later..
315 DebFile
file(CmdL
.FileList
[I
]);
316 if (file
.Go() == false)
318 _error
->Error("Prior errors apply to %s",CmdL
.FileList
[I
]);
322 // Does the package have templates?
323 if (file
.Template
!= 0 && file
.ParseInfo() == true)
325 // Check to make sure debconf dependencies are
327 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
328 if (file
.DepVer
!= "" &&
329 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
330 file
.DepOp
,file
.DepVer
.c_str()
333 if (file
.PreDepVer
!= "" &&
334 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
335 file
.PreDepOp
,file
.PreDepVer
.c_str()
345 delete DebFile::Cache
;
347 return !_error
->PendingError();
350 int main(int argc
, const char **argv
) /*{{{*/
352 CommandLine::Args Args
[] = {
353 {'h',"help","help",0},
354 {'v',"version","version",0},
355 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
356 {'c',"config-file",0,CommandLine::ConfigFile
},
357 {'o',"option",0,CommandLine::ArbItem
},
360 // Set up gettext support
361 setlocale(LC_ALL
,"");
364 // Parse the command line and initialize the package library
365 CommandLine
CmdL(Args
,_config
);
366 if (pkgInitConfig(*_config
) == false ||
367 CmdL
.Parse(argc
,argv
) == false ||
368 pkgInitSystem(*_config
,_system
) == false)
370 _error
->DumpErrors();
374 // See if the help should be shown
375 if (_config
->FindB("help") == true ||
376 CmdL
.FileSize() == 0)
381 // Print any errors or warnings found during operation
382 if (_error
->empty() == false)
384 // This goes to stderr..
385 bool Errors
= _error
->PendingError();
386 _error
->DumpErrors();
387 return Errors
== true?100:0;