]>
git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
c5c37d122e506a2f825456f4a259e749ba80fca4
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>
36 #include <apt-private/private-cmndline.h>
37 #include <apt-private/private-main.h>
45 #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
), 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 // ---------------------------------------------------------------------
98 return Deb
.ExtractTarMember(*this, "control.tar");
101 // DebFile::DoItem examine element in package and mark /*{{{*/
102 // ---------------------------------------------------------------------
104 bool DebFile::DoItem(Item
&I
, int &Fd
)
106 if (strcmp(I
.Name
, "control") == 0)
109 Control
= new char[I
.Size
+3];
110 Control
[I
.Size
] = '\n';
111 Control
[I
.Size
+ 1] = '\n';
112 Control
[I
.Size
+ 2] = '\0';
114 ControlLen
= I
.Size
+ 3;
115 // make it call the Process method below. this is so evil
118 else if (strcmp(I
.Name
, "config") == 0)
121 Config
= new char[I
.Size
+1];
126 else if (strcmp(I
.Name
, "templates") == 0)
129 Template
= new char[I
.Size
+1];
130 Template
[I
.Size
] = 0;
142 // DebFile::Process examine element in package and copy /*{{{*/
143 // ---------------------------------------------------------------------
145 bool DebFile::Process(Item
&/*I*/, const unsigned char *data
,
146 unsigned long long size
, unsigned long long pos
)
151 memcpy(Control
+ pos
, data
, size
);
154 memcpy(Config
+ pos
, data
, size
);
157 memcpy(Template
+ pos
, data
, size
);
159 default: /* throw it away */ ;
164 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
165 // ---------------------------------------------------------------------
167 bool DebFile::ParseInfo()
169 if (Control
== NULL
) return false;
171 pkgTagSection Section
;
172 if (Section
.Scan(Control
, ControlLen
) == false)
175 Package
= Section
.FindS("Package");
176 Version
= GetInstalledVer(Package
);
178 const char *Start
, *Stop
;
179 if (Section
.Find("Depends", Start
, Stop
) == true)
185 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
186 if (Start
== 0) return false;
193 if (Start
== Stop
) break;
197 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
203 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
204 if (Start
== 0) return false;
211 if (Start
== Stop
) break;
218 // ShowHelp - show a short help text /*{{{*/
219 bool ShowHelp(CommandLine
&, CommandLine::DispatchWithHelp
const *)
221 ioprintf(std::cout
, "%s %s (%s)\n", PACKAGE
, PACKAGE_VERSION
, COMMON_ARCH
);
223 if (_config
->FindB("version") == true)
227 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
229 "apt-extracttemplates is a tool to extract config and template info\n"
230 "from debian packages\n"
233 " -h This help text\n"
234 " -t Set the temp dir\n"
235 " -c=? Read this configuration file\n"
236 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
240 // WriteFile - write the contents of the passed string to a file /*{{{*/
241 // ---------------------------------------------------------------------
243 static string
WriteFile(const char *package
, const char *prefix
, const char *data
)
247 std::string tempdir
= GetTempDir();
248 snprintf(fn
, sizeof(fn
), "%s/%s.%s.XXXXXX",
249 _config
->Find("APT::ExtractTemplates::TempDir",
250 tempdir
.c_str()).c_str(),
255 int fd
= mkstemp(fn
);
257 _error
->Errno("ofstream::ofstream",_("Unable to mkstemp %s"),fn
);
260 if (!f
.OpenDescriptor(fd
, FileFd::WriteOnly
, FileFd::None
, true))
262 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
265 f
.Write(data
, strlen(data
));
270 // WriteConfig - write out the config data from a debian package file /*{{{*/
271 // ---------------------------------------------------------------------
273 static void WriteConfig(const DebFile
&file
)
275 string templatefile
= WriteFile(file
.Package
.c_str(), "template", file
.Template
);
276 string configscript
= WriteFile(file
.Package
.c_str(), "config", file
.Config
);
278 if (templatefile
.empty() == true || configscript
.empty() == true)
280 cout
<< file
.Package
<< " " << file
.Version
<< " "
281 << templatefile
<< " " << configscript
<< endl
;
284 // InitCache - initialize the package cache /*{{{*/
285 // ---------------------------------------------------------------------
287 static bool Go(CommandLine
&CmdL
)
289 // Initialize the apt cache
293 pkgCacheGenerator::MakeStatusCache(List
,NULL
,&Map
,true);
296 DebFile::Cache
= new pkgCache(Map
);
297 if (_error
->PendingError() == true)
300 // Find out what version of debconf is currently installed
301 string debconfver
= DebFile::GetInstalledVer("debconf");
302 if (debconfver
.empty() == true)
303 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
305 // Process each package passsed in
306 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
308 // Will pick up the errors later..
309 DebFile
file(CmdL
.FileList
[I
]);
310 if (file
.Go() == false)
312 _error
->Error("Prior errors apply to %s",CmdL
.FileList
[I
]);
316 // Does the package have templates?
317 if (file
.Template
!= 0 && file
.ParseInfo() == true)
319 // Check to make sure debconf dependencies are
321 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
322 if (file
.DepVer
!= "" &&
323 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
324 file
.DepOp
,file
.DepVer
.c_str()
327 if (file
.PreDepVer
!= "" &&
328 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
329 file
.PreDepOp
,file
.PreDepVer
.c_str()
339 delete DebFile::Cache
;
341 return !_error
->PendingError();
344 std::vector
<CommandLine::DispatchWithHelp
> GetCommands() /*{{{*/
347 {nullptr, nullptr, nullptr}
351 int main(int argc
, const char **argv
) /*{{{*/
356 auto const Cmds
= ParseCommandLine(CmdL
, APT_CMD::APT_EXTRACTTEMPLATES
, &_config
, &_system
, argc
, argv
);
360 return DispatchCommandLine(CmdL
, {});