]>
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"
50 pkgCache
*DebFile::Cache
= 0;
52 // DebFile::DebFile - Construct the DebFile object /*{{{*/
53 // ---------------------------------------------------------------------
55 DebFile::DebFile(const char *debfile
)
56 : File(debfile
, FileFd::ReadOnly
), Size(0), Control(NULL
), ControlLen(0),
57 DepOp(0), PreDepOp(0), Config(0), Template(0), Which(None
)
61 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
62 // ---------------------------------------------------------------------
71 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
72 // ---------------------------------------------------------------------
74 string
DebFile::GetInstalledVer(const string
&package
)
76 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(package
);
77 if (Pkg
.end() == false)
79 pkgCache::VerIterator V
= Pkg
.CurrentVer();
89 // DebFile::Go - Start extracting a debian package /*{{{*/
90 // ---------------------------------------------------------------------
95 if (_error
->PendingError() == true)
98 const ARArchive::Member
*Member
= AR
.FindMember("control.tar.gz");
100 return _error
->Error(_("%s not a valid DEB package."),File
.Name().c_str());
102 if (File
.Seek(Member
->Start
) == false)
104 ExtractTar
Tar(File
, Member
->Size
,"gzip");
105 return Tar
.Go(*this);
108 // DebFile::DoItem examine element in package and mark /*{{{*/
109 // ---------------------------------------------------------------------
111 bool DebFile::DoItem(Item
&I
, int &Fd
)
113 if (strcmp(I
.Name
, "control") == 0)
116 Control
= new char[I
.Size
+1];
120 // make it call the Process method below. this is so evil
123 else if (strcmp(I
.Name
, "config") == 0)
126 Config
= new char[I
.Size
+1];
131 else if (strcmp(I
.Name
, "templates") == 0)
134 Template
= new char[I
.Size
+1];
135 Template
[I
.Size
] = 0;
147 // DebFile::Process examine element in package and copy /*{{{*/
148 // ---------------------------------------------------------------------
150 bool DebFile::Process(Item
&I
, const unsigned char *data
,
151 unsigned long size
, unsigned long pos
)
156 memcpy(Control
+ pos
, data
, size
);
159 memcpy(Config
+ pos
, data
, size
);
162 memcpy(Template
+ pos
, data
, size
);
164 default: /* throw it away */ ;
169 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
170 // ---------------------------------------------------------------------
172 bool DebFile::ParseInfo()
174 if (Control
== NULL
) return false;
176 pkgTagSection Section
;
177 Section
.Scan(Control
, ControlLen
);
179 Package
= Section
.FindS("Package");
180 Version
= GetInstalledVer(Package
);
182 const char *Start
, *Stop
;
183 if (Section
.Find("Depends", Start
, Stop
) == true)
189 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
190 if (Start
== 0) return false;
197 if (Start
== Stop
) break;
201 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
207 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
208 if (Start
== 0) return false;
215 if (Start
== Stop
) break;
222 // ShowHelp - show a short help text /*{{{*/
223 // ---------------------------------------------------------------------
227 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
228 COMMON_ARCH
,__DATE__
,__TIME__
);
230 if (_config
->FindB("version") == true)
234 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
236 "apt-extracttemplates is a tool to extract config and template info\n"
237 "from debian packages\n"
240 " -h This help text\n"
241 " -t Set the temp dir\n"
242 " -c=? Read this configuration file\n"
243 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
247 // WriteFile - write the contents of the passed string to a file /*{{{*/
248 // ---------------------------------------------------------------------
250 string
WriteFile(const char *package
, const char *prefix
, const char *data
)
255 std::string tempdir
= GetTempDir();
256 snprintf(fn
, sizeof(fn
), "%s/%s.%s.%u%d",
257 _config
->Find("APT::ExtractTemplates::TempDir",
258 tempdir
.c_str()).c_str(),
259 package
, prefix
, getpid(), i
++);
264 if (!f
.Open(fn
, FileFd::WriteTemp
, 0600))
266 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
270 f
.Write(data
, strlen(data
));
275 // WriteConfig - write out the config data from a debian package file /*{{{*/
276 // ---------------------------------------------------------------------
278 void WriteConfig(const DebFile
&file
)
280 string templatefile
= WriteFile(file
.Package
.c_str(), "template", file
.Template
);
281 string configscript
= WriteFile(file
.Package
.c_str(), "config", file
.Config
);
283 if (templatefile
.empty() == true || configscript
.empty() == true)
285 cout
<< file
.Package
<< " " << file
.Version
<< " "
286 << templatefile
<< " " << configscript
<< endl
;
289 // InitCache - initialize the package cache /*{{{*/
290 // ---------------------------------------------------------------------
292 bool Go(CommandLine
&CmdL
)
294 // Initialize the apt cache
298 pkgCacheGenerator::MakeStatusCache(List
,NULL
,&Map
,true);
301 DebFile::Cache
= new pkgCache(Map
);
302 if (_error
->PendingError() == true)
305 // Find out what version of debconf is currently installed
306 string debconfver
= DebFile::GetInstalledVer("debconf");
307 if (debconfver
.empty() == true)
308 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
310 // Process each package passsed in
311 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
313 // Will pick up the errors later..
314 DebFile
file(CmdL
.FileList
[I
]);
315 if (file
.Go() == false)
317 _error
->Error("Prior errors apply to %s",CmdL
.FileList
[I
]);
321 // Does the package have templates?
322 if (file
.Template
!= 0 && file
.ParseInfo() == true)
324 // Check to make sure debconf dependencies are
326 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
327 if (file
.DepVer
!= "" &&
328 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
329 file
.DepOp
,file
.DepVer
.c_str()
332 if (file
.PreDepVer
!= "" &&
333 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
334 file
.PreDepOp
,file
.PreDepVer
.c_str()
344 delete DebFile::Cache
;
346 return !_error
->PendingError();
349 int main(int argc
, const char **argv
) /*{{{*/
351 CommandLine::Args Args
[] = {
352 {'h',"help","help",0},
353 {'v',"version","version",0},
354 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
355 {'c',"config-file",0,CommandLine::ConfigFile
},
356 {'o',"option",0,CommandLine::ArbItem
},
359 // Set up gettext support
360 setlocale(LC_ALL
,"");
363 // Parse the command line and initialize the package library
364 CommandLine
CmdL(Args
,_config
);
365 if (pkgInitConfig(*_config
) == false ||
366 CmdL
.Parse(argc
,argv
) == false ||
367 pkgInitSystem(*_config
,_system
) == false)
369 _error
->DumpErrors();
373 // See if the help should be shown
374 if (_config
->FindB("help") == true ||
375 CmdL
.FileSize() == 0)
380 // Print any errors or warnings found during operation
381 if (_error
->empty() == false)
383 // This goes to stderr..
384 bool Errors
= _error
->PendingError();
385 _error
->DumpErrors();
386 return Errors
== true?100:0;