]>
git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-extracttemplates.cc,v 1.2 2001/02/25 04:53:59 tausq 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 #if APT_PKG_MAJOR >= 3
18 #define APT_COMPATIBILITY 986
19 #include <apt-pkg/debversion.h>
21 #include <apt-pkg/pkgcache.h>
22 #include <apt-pkg/configuration.h>
23 #include <apt-pkg/progress.h>
24 #include <apt-pkg/sourcelist.h>
25 #include <apt-pkg/pkgcachegen.h>
26 #include <apt-pkg/version.h>
27 #include <apt-pkg/tagfile.h>
28 #include <apt-pkg/extracttar.h>
29 #include <apt-pkg/arfile.h>
30 #include <apt-pkg/deblistparser.h>
31 #include <apt-pkg/error.h>
32 #include <apt-pkg/strutl.h>
45 #include "apt-extracttemplates.h"
47 #define TMPDIR "/var/lib/debconf/"
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), PreDepOp(0),
56 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 char *DebFile::GetInstalledVer(const string
&package
)
77 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(package
);
78 if (Pkg
.end() == false)
80 pkgCache::VerIterator V
= Pkg
.CurrentVer();
83 ver
= strdup(V
.VerStr());
90 // DebFile::Go - Start extracting a debian package /*{{{*/
91 // ---------------------------------------------------------------------
96 const ARArchive::Member
*Member
= AR
.FindMember("control.tar.gz");
99 fprintf(stderr
, _("This is not a valid DEB package.\n"));
103 if (File
.Seek(Member
->Start
) == false)
108 ExtractTar
Tar(File
, Member
->Size
);
109 return Tar
.Go(*this);
112 // DebFile::DoItem examine element in package and mark /*{{{*/
113 // ---------------------------------------------------------------------
115 bool DebFile::DoItem(Item
&I
, int &Fd
)
117 if (strcmp(I
.Name
, "control") == 0)
120 Control
= new char[I
.Size
+1];
124 // make it call the Process method below. this is so evil
127 else if (strcmp(I
.Name
, "config") == 0)
130 Config
= new char[I
.Size
+1];
135 else if (strcmp(I
.Name
, "templates") == 0)
138 Template
= new char[I
.Size
+1];
139 Template
[I
.Size
] = 0;
150 // DebFile::Process examine element in package and copy /*{{{*/
151 // ---------------------------------------------------------------------
153 bool DebFile::Process(Item
&I
, const unsigned char *data
,
154 unsigned long size
, unsigned long pos
)
159 memcpy(Control
+ pos
, data
, size
);
162 memcpy(Config
+ pos
, data
, size
);
165 memcpy(Template
+ pos
, data
, size
);
167 default: /* throw it away */ ;
172 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
173 // ---------------------------------------------------------------------
175 bool DebFile::ParseInfo()
177 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 %s compiled on %s %s\n"),PACKAGE
,VERSION
,
230 COMMON_OS
,COMMON_CPU
,__DATE__
,__TIME__
);
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"));
240 // WriteFile - write the contents of the passed string to a file /*{{{*/
241 // ---------------------------------------------------------------------
243 char *WriteFile(const char *prefix
, const char *data
)
247 snprintf(fn
, sizeof(fn
), "%s%s.%u%d", TMPDIR
, prefix
, getpid(), i
++);
250 if (!ofs
) return NULL
;
251 ofs
<< (data
? data
: "");
256 // WriteConfig - write out the config data from a debian package file /*{{{*/
257 // ---------------------------------------------------------------------
259 void WriteConfig(const DebFile
&file
)
261 char *templatefile
= WriteFile("template", file
.Template
);
262 char *configscript
= WriteFile("config", file
.Config
);
264 if (templatefile
== 0 || configscript
== 0)
266 fprintf(stderr
, _("Cannot write config script or templates\n"));
269 cout
<< file
.Package
<< " " << file
.Version
<< " "
270 << templatefile
<< " " << configscript
<< endl
;
276 // InitCache - initialize the package cache /*{{{*/
277 // ---------------------------------------------------------------------
279 int InitCache(MMap
*&Map
, pkgCache
*&Cache
)
281 // Initialize the apt cache
282 if (pkgInitConfig(*_config
) == false || pkgInitSystem(*_config
, _system
) == false)
284 _error
->DumpErrors();
290 pkgMakeStatusCache(List
,Prog
,&Map
,true);
291 Cache
= new pkgCache(Map
);
296 int main(int argc
, char **argv
, char **env
)
302 const char *debconfver
= NULL
;
304 // Initialize the package cache
305 if (InitCache(Map
, DebFile::Cache
) < 0 || Map
== 0 || DebFile::Cache
== 0)
307 fprintf(stderr
, _("Cannot initialize APT cache\n"));
311 // Find out what version of debconf is currently installed
312 if ((debconfver
= DebFile::GetInstalledVer("debconf")) == NULL
)
314 fprintf(stderr
, _("Cannot get debconf version. Is debconf installed?\n"));
318 // Process each package passsed in
320 debs
= new char *[numdebs
];
321 memcpy(debs
, &argv
[1], sizeof(char *) * numdebs
);
323 if (numdebs
< 1) ShowHelp();
325 for (idx
= 0; idx
< numdebs
; idx
++)
327 DebFile
file(debs
[idx
]);
328 if (file
.Go() == false)
330 fprintf(stderr
, _("Cannot read %s\n"), debs
[idx
]);
333 // Does the package have templates?
334 if (file
.Template
!= 0 && file
.ParseInfo() == true)
336 // Check to make sure debconf dependencies are
338 if (file
.DepVer
!= "" &&
339 pkgCheckDep(file
.DepVer
.c_str(),
340 debconfver
, file
.DepOp
) == false)
342 if (file
.PreDepVer
!= "" &&
343 pkgCheckDep(file
.PreDepVer
.c_str(),
344 debconfver
, file
.PreDepOp
) == false)
353 delete DebFile::Cache
;