]>
git.saurik.com Git - apt-legacy.git/blob - cmdline/apt-extracttemplates.cc
a931b415fc1d275c871c31a5609469552da14d39
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 %s compiled on %s %s\n"),PACKAGE
,VERSION
,
226 COMMON_OS
,COMMON_CPU
,__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 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
300 pkgMakeStatusCache(List
,Prog
,&Map
,true);
303 DebFile::Cache
= new pkgCache(Map
);
304 if (_error
->PendingError() == true)
307 // Find out what version of debconf is currently installed
308 string debconfver
= DebFile::GetInstalledVer("debconf");
309 if (debconfver
.empty() == true)
310 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
312 // Process each package passsed in
313 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
315 // Will pick up the errors later..
316 DebFile
file(CmdL
.FileList
[I
]);
317 if (file
.Go() == false)
319 _error
->Error("Prior errors apply to %s",CmdL
.FileList
[I
]);
323 // Does the package have templates?
324 if (file
.Template
!= 0 && file
.ParseInfo() == true)
326 // Check to make sure debconf dependencies are
328 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
329 if (file
.DepVer
!= "" &&
330 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
331 file
.DepOp
,file
.DepVer
.c_str()
334 if (file
.PreDepVer
!= "" &&
335 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
336 file
.PreDepOp
,file
.PreDepVer
.c_str()
346 delete DebFile::Cache
;
348 return !_error
->PendingError();
352 int main(int argc
, const char **argv
)
354 CommandLine::Args Args
[] = {
355 {'h',"help","help",0},
356 {'v',"version","version",0},
357 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
358 {'c',"config-file",0,CommandLine::ConfigFile
},
359 {'o',"option",0,CommandLine::ArbItem
},
362 // Set up gettext support
363 setlocale(LC_ALL
,"");
364 //textdomain(PACKAGE);
366 // Parse the command line and initialize the package library
367 CommandLine
CmdL(Args
,_config
);
368 if (pkgInitConfig(*_config
) == false ||
369 CmdL
.Parse(argc
,argv
) == false ||
370 pkgInitSystem(*_config
,_system
) == false)
372 _error
->DumpErrors();
376 // See if the help should be shown
377 if (_config
->FindB("help") == true ||
378 CmdL
.FileSize() == 0)
383 // Print any errors or warnings found during operation
384 if (_error
->empty() == false)
386 // This goes to stderr..
387 bool Errors
= _error
->PendingError();
388 _error
->DumpErrors();
389 return Errors
== true?100:0;