]>
git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
5d7b76c23e33b2804e9775f36807efc9314f80c8
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>
42 #include "apt-extracttemplates.h"
49 pkgCache
*DebFile::Cache
= 0;
51 // DebFile::DebFile - Construct the DebFile object /*{{{*/
52 // ---------------------------------------------------------------------
54 DebFile::DebFile(const char *debfile
)
55 : File(debfile
, FileFd::ReadOnly
), Control(0), DepOp(0),
56 PreDepOp(0), Config(0), Template(0), Which(None
)
60 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
61 // ---------------------------------------------------------------------
70 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
71 // ---------------------------------------------------------------------
73 string
DebFile::GetInstalledVer(const string
&package
)
75 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(package
);
76 if (Pkg
.end() == false)
78 pkgCache::VerIterator V
= Pkg
.CurrentVer();
88 // DebFile::Go - Start extracting a debian package /*{{{*/
89 // ---------------------------------------------------------------------
94 if (_error
->PendingError() == true)
97 const ARArchive::Member
*Member
= AR
.FindMember("control.tar.gz");
99 return _error
->Error(_("%s not a valid DEB package."),File
.Name().c_str());
101 if (File
.Seek(Member
->Start
) == false)
103 ExtractTar
Tar(File
, Member
->Size
,"gzip");
104 return Tar
.Go(*this);
107 // DebFile::DoItem examine element in package and mark /*{{{*/
108 // ---------------------------------------------------------------------
110 bool DebFile::DoItem(Item
&I
, int &Fd
)
112 if (strcmp(I
.Name
, "control") == 0)
115 Control
= new char[I
.Size
+1];
119 // make it call the Process method below. this is so evil
122 else if (strcmp(I
.Name
, "config") == 0)
125 Config
= new char[I
.Size
+1];
130 else if (strcmp(I
.Name
, "templates") == 0)
133 Template
= new char[I
.Size
+1];
134 Template
[I
.Size
] = 0;
146 // DebFile::Process examine element in package and copy /*{{{*/
147 // ---------------------------------------------------------------------
149 bool DebFile::Process(Item
&I
, const unsigned char *data
,
150 unsigned long size
, unsigned long pos
)
155 memcpy(Control
+ pos
, data
, size
);
158 memcpy(Config
+ pos
, data
, size
);
161 memcpy(Template
+ pos
, data
, size
);
163 default: /* throw it away */ ;
168 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
169 // ---------------------------------------------------------------------
171 bool DebFile::ParseInfo()
173 if (Control
== NULL
) return false;
175 pkgTagSection Section
;
176 Section
.Scan(Control
, ControlLen
);
178 Package
= Section
.FindS("Package");
179 Version
= GetInstalledVer(Package
);
181 const char *Start
, *Stop
;
182 if (Section
.Find("Depends", Start
, Stop
) == true)
188 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
189 if (Start
== 0) return false;
196 if (Start
== Stop
) break;
200 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
206 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
207 if (Start
== 0) return false;
214 if (Start
== Stop
) break;
221 // ShowHelp - show a short help text /*{{{*/
222 // ---------------------------------------------------------------------
226 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,VERSION
,
227 COMMON_ARCH
,__DATE__
,__TIME__
);
229 if (_config
->FindB("version") == true)
233 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
235 "apt-extracttemplates is a tool to extract config and template info\n"
236 "from debian packages\n"
239 " -h This help text\n"
240 " -t Set the temp dir\n"
241 " -c=? Read this configuration file\n"
242 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
246 // WriteFile - write the contents of the passed string to a file /*{{{*/
247 // ---------------------------------------------------------------------
249 string
WriteFile(const char *package
, const char *prefix
, const char *data
)
253 const char *tempdir
= NULL
;
255 tempdir
= getenv("TMPDIR");
259 snprintf(fn
, sizeof(fn
), "%s/%s.%s.%u%d",
260 _config
->Find("APT::ExtractTemplates::TempDir", tempdir
).c_str(),
261 package
, prefix
, getpid(), i
++);
266 if (!f
.Open(fn
, FileFd::WriteTemp
, 0600))
268 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
272 f
.Write(data
, strlen(data
));
277 // WriteConfig - write out the config data from a debian package file /*{{{*/
278 // ---------------------------------------------------------------------
280 void WriteConfig(const DebFile
&file
)
282 string templatefile
= WriteFile(file
.Package
.c_str(), "template", file
.Template
);
283 string configscript
= WriteFile(file
.Package
.c_str(), "config", file
.Config
);
285 if (templatefile
.empty() == true || configscript
.empty() == true)
287 cout
<< file
.Package
<< " " << file
.Version
<< " "
288 << templatefile
<< " " << configscript
<< endl
;
291 // InitCache - initialize the package cache /*{{{*/
292 // ---------------------------------------------------------------------
294 bool Go(CommandLine
&CmdL
)
296 // Initialize the apt cache
300 pkgCacheGenerator::MakeStatusCache(List
,NULL
,&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();
351 int main(int argc
, const char **argv
) /*{{{*/
353 CommandLine::Args Args
[] = {
354 {'h',"help","help",0},
355 {'v',"version","version",0},
356 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
357 {'c',"config-file",0,CommandLine::ConfigFile
},
358 {'o',"option",0,CommandLine::ArbItem
},
361 // Set up gettext support
362 setlocale(LC_ALL
,"");
365 // Parse the command line and initialize the package library
366 CommandLine
CmdL(Args
,_config
);
367 if (pkgInitConfig(*_config
) == false ||
368 CmdL
.Parse(argc
,argv
) == false ||
369 pkgInitSystem(*_config
,_system
) == false)
371 _error
->DumpErrors();
375 // See if the help should be shown
376 if (_config
->FindB("help") == true ||
377 CmdL
.FileSize() == 0)
382 // Print any errors or warnings found during operation
383 if (_error
->empty() == false)
385 // This goes to stderr..
386 bool Errors
= _error
->PendingError();
387 _error
->DumpErrors();
388 return Errors
== true?100:0;