]>
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/configuration.h>
22 #include <apt-pkg/progress.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/extracttar.h>
28 #include <apt-pkg/arfile.h>
29 #include <apt-pkg/deblistparser.h>
30 #include <apt-pkg/error.h>
31 #include <apt-pkg/strutl.h>
32 #include <apt-pkg/fileutl.h>
33 #include <apt-pkg/pkgsystem.h>
43 #include "apt-extracttemplates.h"
52 pkgCache
*DebFile::Cache
= 0;
54 // DebFile::DebFile - Construct the DebFile object /*{{{*/
55 // ---------------------------------------------------------------------
57 DebFile::DebFile(const char *debfile
)
58 : File(debfile
, FileFd::ReadOnly
), Size(0), Control(NULL
), ControlLen(0),
59 DepOp(0), PreDepOp(0), Config(0), Template(0), Which(None
)
63 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
64 // ---------------------------------------------------------------------
73 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
74 // ---------------------------------------------------------------------
76 string
DebFile::GetInstalledVer(const string
&package
)
78 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(package
);
79 if (Pkg
.end() == false)
81 pkgCache::VerIterator V
= Pkg
.CurrentVer();
91 // DebFile::Go - Start extracting a debian package /*{{{*/
92 // ---------------------------------------------------------------------
97 if (_error
->PendingError() == true)
100 const ARArchive::Member
*Member
= AR
.FindMember("control.tar.gz");
102 return _error
->Error(_("%s not a valid DEB package."),File
.Name().c_str());
104 if (File
.Seek(Member
->Start
) == false)
106 ExtractTar
Tar(File
, Member
->Size
,"gzip");
107 return Tar
.Go(*this);
110 // DebFile::DoItem examine element in package and mark /*{{{*/
111 // ---------------------------------------------------------------------
113 bool DebFile::DoItem(Item
&I
, int &Fd
)
115 if (strcmp(I
.Name
, "control") == 0)
118 Control
= new char[I
.Size
+1];
122 // make it call the Process method below. this is so evil
125 else if (strcmp(I
.Name
, "config") == 0)
128 Config
= new char[I
.Size
+1];
133 else if (strcmp(I
.Name
, "templates") == 0)
136 Template
= new char[I
.Size
+1];
137 Template
[I
.Size
] = 0;
149 // DebFile::Process examine element in package and copy /*{{{*/
150 // ---------------------------------------------------------------------
152 bool DebFile::Process(Item
&I
, const unsigned char *data
,
153 unsigned long size
, unsigned long pos
)
158 memcpy(Control
+ pos
, data
, size
);
161 memcpy(Config
+ pos
, data
, size
);
164 memcpy(Template
+ pos
, data
, size
);
166 default: /* throw it away */ ;
171 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
172 // ---------------------------------------------------------------------
174 bool DebFile::ParseInfo()
176 if (Control
== NULL
) return false;
178 pkgTagSection Section
;
179 Section
.Scan(Control
, ControlLen
);
181 Package
= Section
.FindS("Package");
182 Version
= GetInstalledVer(Package
);
184 const char *Start
, *Stop
;
185 if (Section
.Find("Depends", Start
, Stop
) == true)
191 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
192 if (Start
== 0) return false;
199 if (Start
== Stop
) break;
203 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
209 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
210 if (Start
== 0) return false;
217 if (Start
== Stop
) break;
224 // ShowHelp - show a short help text /*{{{*/
225 // ---------------------------------------------------------------------
229 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
230 COMMON_ARCH
,__DATE__
,__TIME__
);
232 if (_config
->FindB("version") == true)
236 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
238 "apt-extracttemplates is a tool to extract config and template info\n"
239 "from debian packages\n"
242 " -h This help text\n"
243 " -t Set the temp dir\n"
244 " -c=? Read this configuration file\n"
245 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
249 // WriteFile - write the contents of the passed string to a file /*{{{*/
250 // ---------------------------------------------------------------------
252 string
WriteFile(const char *package
, const char *prefix
, const char *data
)
256 const char *tempdir
= NULL
;
258 tempdir
= getenv("TMPDIR");
262 snprintf(fn
, sizeof(fn
), "%s/%s.%s.%u%d",
263 _config
->Find("APT::ExtractTemplates::TempDir", tempdir
).c_str(),
264 package
, prefix
, getpid(), i
++);
269 if (!f
.Open(fn
, FileFd::WriteTemp
, 0600))
271 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
275 f
.Write(data
, strlen(data
));
280 // WriteConfig - write out the config data from a debian package file /*{{{*/
281 // ---------------------------------------------------------------------
283 void WriteConfig(const DebFile
&file
)
285 string templatefile
= WriteFile(file
.Package
.c_str(), "template", file
.Template
);
286 string configscript
= WriteFile(file
.Package
.c_str(), "config", file
.Config
);
288 if (templatefile
.empty() == true || configscript
.empty() == true)
290 cout
<< file
.Package
<< " " << file
.Version
<< " "
291 << templatefile
<< " " << configscript
<< endl
;
294 // InitCache - initialize the package cache /*{{{*/
295 // ---------------------------------------------------------------------
297 bool Go(CommandLine
&CmdL
)
299 // Initialize the apt cache
303 pkgCacheGenerator::MakeStatusCache(List
,NULL
,&Map
,true);
306 DebFile::Cache
= new pkgCache(Map
);
307 if (_error
->PendingError() == true)
310 // Find out what version of debconf is currently installed
311 string debconfver
= DebFile::GetInstalledVer("debconf");
312 if (debconfver
.empty() == true)
313 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
315 // Process each package passsed in
316 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
318 // Will pick up the errors later..
319 DebFile
file(CmdL
.FileList
[I
]);
320 if (file
.Go() == false)
322 _error
->Error("Prior errors apply to %s",CmdL
.FileList
[I
]);
326 // Does the package have templates?
327 if (file
.Template
!= 0 && file
.ParseInfo() == true)
329 // Check to make sure debconf dependencies are
331 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
332 if (file
.DepVer
!= "" &&
333 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
334 file
.DepOp
,file
.DepVer
.c_str()
337 if (file
.PreDepVer
!= "" &&
338 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
339 file
.PreDepOp
,file
.PreDepVer
.c_str()
349 delete DebFile::Cache
;
351 return !_error
->PendingError();
354 int main(int argc
, const char **argv
) /*{{{*/
356 CommandLine::Args Args
[] = {
357 {'h',"help","help",0},
358 {'v',"version","version",0},
359 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
360 {'c',"config-file",0,CommandLine::ConfigFile
},
361 {'o',"option",0,CommandLine::ArbItem
},
364 // Set up gettext support
365 setlocale(LC_ALL
,"");
368 // Parse the command line and initialize the package library
369 CommandLine
CmdL(Args
,_config
);
370 if (pkgInitConfig(*_config
) == false ||
371 CmdL
.Parse(argc
,argv
) == false ||
372 pkgInitSystem(*_config
,_system
) == false)
374 _error
->DumpErrors();
378 // See if the help should be shown
379 if (_config
->FindB("help") == true ||
380 CmdL
.FileSize() == 0)
385 // Print any errors or warnings found during operation
386 if (_error
->empty() == false)
388 // This goes to stderr..
389 bool Errors
= _error
->PendingError();
390 _error
->DumpErrors();
391 return Errors
== true?100:0;