]>
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/cacheiterators.h>
22 #include <apt-pkg/configuration.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/debfile.h>
28 #include <apt-pkg/deblistparser.h>
29 #include <apt-pkg/error.h>
30 #include <apt-pkg/strutl.h>
31 #include <apt-pkg/fileutl.h>
32 #include <apt-pkg/pkgsystem.h>
33 #include <apt-pkg/dirstream.h>
34 #include <apt-pkg/mmap.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
), Size(0), Control(NULL
), ControlLen(0),
56 DepOp(0), 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 // ---------------------------------------------------------------------
95 return Deb
.ExtractTarMember(*this, "control.tar");
98 // DebFile::DoItem examine element in package and mark /*{{{*/
99 // ---------------------------------------------------------------------
101 bool DebFile::DoItem(Item
&I
, int &Fd
)
103 if (strcmp(I
.Name
, "control") == 0)
106 Control
= new char[I
.Size
+1];
110 // make it call the Process method below. this is so evil
113 else if (strcmp(I
.Name
, "config") == 0)
116 Config
= new char[I
.Size
+1];
121 else if (strcmp(I
.Name
, "templates") == 0)
124 Template
= new char[I
.Size
+1];
125 Template
[I
.Size
] = 0;
137 // DebFile::Process examine element in package and copy /*{{{*/
138 // ---------------------------------------------------------------------
140 bool DebFile::Process(Item
&/*I*/, const unsigned char *data
,
141 unsigned long size
, unsigned long pos
)
146 memcpy(Control
+ pos
, data
, size
);
149 memcpy(Config
+ pos
, data
, size
);
152 memcpy(Template
+ pos
, data
, size
);
154 default: /* throw it away */ ;
159 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
160 // ---------------------------------------------------------------------
162 bool DebFile::ParseInfo()
164 if (Control
== NULL
) return false;
166 pkgTagSection Section
;
167 Section
.Scan(Control
, ControlLen
);
169 Package
= Section
.FindS("Package");
170 Version
= GetInstalledVer(Package
);
172 const char *Start
, *Stop
;
173 if (Section
.Find("Depends", Start
, Stop
) == true)
179 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
180 if (Start
== 0) return false;
187 if (Start
== Stop
) break;
191 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
197 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
198 if (Start
== 0) return false;
205 if (Start
== Stop
) break;
212 // ShowHelp - show a short help text /*{{{*/
213 // ---------------------------------------------------------------------
215 static int ShowHelp(void)
217 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
218 COMMON_ARCH
,__DATE__
,__TIME__
);
220 if (_config
->FindB("version") == true)
224 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
226 "apt-extracttemplates is a tool to extract config and template info\n"
227 "from debian packages\n"
230 " -h This help text\n"
231 " -t Set the temp dir\n"
232 " -c=? Read this configuration file\n"
233 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
237 // WriteFile - write the contents of the passed string to a file /*{{{*/
238 // ---------------------------------------------------------------------
240 static string
WriteFile(const char *package
, const char *prefix
, const char *data
)
244 std::string tempdir
= GetTempDir();
245 snprintf(fn
, sizeof(fn
), "%s/%s.%s.XXXXXX",
246 _config
->Find("APT::ExtractTemplates::TempDir",
247 tempdir
.c_str()).c_str(),
252 int fd
= mkstemp(fn
);
254 _error
->Errno("ofstream::ofstream",_("Unable to mkstemp %s"),fn
);
257 if (!f
.OpenDescriptor(fd
, FileFd::WriteOnly
, FileFd::None
, true))
259 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
262 f
.Write(data
, strlen(data
));
267 // WriteConfig - write out the config data from a debian package file /*{{{*/
268 // ---------------------------------------------------------------------
270 static void WriteConfig(const DebFile
&file
)
272 string templatefile
= WriteFile(file
.Package
.c_str(), "template", file
.Template
);
273 string configscript
= WriteFile(file
.Package
.c_str(), "config", file
.Config
);
275 if (templatefile
.empty() == true || configscript
.empty() == true)
277 cout
<< file
.Package
<< " " << file
.Version
<< " "
278 << templatefile
<< " " << configscript
<< endl
;
281 // InitCache - initialize the package cache /*{{{*/
282 // ---------------------------------------------------------------------
284 static bool Go(CommandLine
&CmdL
)
286 // Initialize the apt cache
290 pkgCacheGenerator::MakeStatusCache(List
,NULL
,&Map
,true);
293 DebFile::Cache
= new pkgCache(Map
);
294 if (_error
->PendingError() == true)
297 // Find out what version of debconf is currently installed
298 string debconfver
= DebFile::GetInstalledVer("debconf");
299 if (debconfver
.empty() == true)
300 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
302 // Process each package passsed in
303 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
305 // Will pick up the errors later..
306 DebFile
file(CmdL
.FileList
[I
]);
307 if (file
.Go() == false)
309 _error
->Error("Prior errors apply to %s",CmdL
.FileList
[I
]);
313 // Does the package have templates?
314 if (file
.Template
!= 0 && file
.ParseInfo() == true)
316 // Check to make sure debconf dependencies are
318 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
319 if (file
.DepVer
!= "" &&
320 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
321 file
.DepOp
,file
.DepVer
.c_str()
324 if (file
.PreDepVer
!= "" &&
325 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
326 file
.PreDepOp
,file
.PreDepVer
.c_str()
336 delete DebFile::Cache
;
338 return !_error
->PendingError();
341 int main(int argc
, const char **argv
) /*{{{*/
343 CommandLine::Args Args
[] = {
344 {'h',"help","help",0},
345 {'v',"version","version",0},
346 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
347 {'c',"config-file",0,CommandLine::ConfigFile
},
348 {'o',"option",0,CommandLine::ArbItem
},
351 // Set up gettext support
352 setlocale(LC_ALL
,"");
355 // Parse the command line and initialize the package library
356 CommandLine
CmdL(Args
,_config
);
357 if (pkgInitConfig(*_config
) == false ||
358 CmdL
.Parse(argc
,argv
) == false ||
359 pkgInitSystem(*_config
,_system
) == false)
361 _error
->DumpErrors();
365 // See if the help should be shown
366 if (_config
->FindB("help") == true ||
367 CmdL
.FileSize() == 0)
372 // Print any errors or warnings found during operation
373 if (_error
->empty() == false)
375 // This goes to stderr..
376 bool Errors
= _error
->PendingError();
377 _error
->DumpErrors();
378 return Errors
== true?100:0;