]> git.saurik.com Git - apt.git/blame - cmdline/apt-extracttemplates.cc
deal with --version more centrally
[apt.git] / cmdline / apt-extracttemplates.cc
CommitLineData
234edfd0
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
8eb5af51 3// $Id: apt-extracttemplates.cc,v 1.15 2003/07/26 00:00:11 mdz Exp $
234edfd0
AL
4/* ######################################################################
5
6 APT Extract Templates - Program to extract debconf config and template
7 files
3fc8f685 8
234edfd0
AL
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
12
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
ea542140
DK
16#include<config.h>
17
3fc8f685 18#include <apt-pkg/init.h>
5cbf1b49 19#include <apt-pkg/cmndline.h>
3fc8f685 20#include <apt-pkg/pkgcache.h>
453b82a3 21#include <apt-pkg/cacheiterators.h>
3fc8f685 22#include <apt-pkg/configuration.h>
3fc8f685
AL
23#include <apt-pkg/sourcelist.h>
24#include <apt-pkg/pkgcachegen.h>
25#include <apt-pkg/version.h>
234edfd0 26#include <apt-pkg/tagfile.h>
e7f56293 27#include <apt-pkg/debfile.h>
234edfd0
AL
28#include <apt-pkg/deblistparser.h>
29#include <apt-pkg/error.h>
30#include <apt-pkg/strutl.h>
4decd43c 31#include <apt-pkg/fileutl.h>
472ff00e 32#include <apt-pkg/pkgsystem.h>
453b82a3
DK
33#include <apt-pkg/dirstream.h>
34#include <apt-pkg/mmap.h>
ea542140 35
ad7e0941 36#include <apt-private/private-cmndline.h>
e7e10e47 37#include <apt-private/private-main.h>
ad7e0941 38
453b82a3 39#include <iostream>
234edfd0
AL
40#include <stdio.h>
41#include <string.h>
234edfd0 42#include <unistd.h>
8d50b63f 43#include <stdlib.h>
234edfd0 44
234edfd0 45#include "apt-extracttemplates.h"
a00a9b44
DK
46
47#include <apti18n.h>
7e726255 48 /*}}}*/
234edfd0 49
8f312f45
AL
50using namespace std;
51
234edfd0
AL
52pkgCache *DebFile::Cache = 0;
53
54// DebFile::DebFile - Construct the DebFile object /*{{{*/
55// ---------------------------------------------------------------------
56/* */
57DebFile::DebFile(const char *debfile)
387fb4ae 58 : File(debfile, FileFd::ReadOnly), Control(NULL), ControlLen(0),
dcaa1185 59 DepOp(0), PreDepOp(0), Config(0), Template(0), Which(None)
3fc8f685 60{
3fc8f685 61}
234edfd0
AL
62 /*}}}*/
63// DebFile::~DebFile - Destruct the DebFile object /*{{{*/
64// ---------------------------------------------------------------------
65/* */
66DebFile::~DebFile()
67{
68 delete [] Control;
69 delete [] Config;
70 delete [] Template;
71}
72 /*}}}*/
73// DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
74// ---------------------------------------------------------------------
75/* */
7e726255 76string DebFile::GetInstalledVer(const string &package)
234edfd0 77{
234edfd0
AL
78 pkgCache::PkgIterator Pkg = Cache->FindPkg(package);
79 if (Pkg.end() == false)
80 {
81 pkgCache::VerIterator V = Pkg.CurrentVer();
7e726255 82 if (V.end() == false)
234edfd0 83 {
7e726255 84 return V.VerStr();
234edfd0
AL
85 }
86 }
3fc8f685 87
7e726255 88 return string();
234edfd0
AL
89}
90 /*}}}*/
91// DebFile::Go - Start extracting a debian package /*{{{*/
92// ---------------------------------------------------------------------
93/* */
94bool DebFile::Go()
95{
e7f56293
GJ
96 debDebFile Deb(File);
97
98 return Deb.ExtractTarMember(*this, "control.tar");
234edfd0
AL
99}
100 /*}}}*/
101// DebFile::DoItem examine element in package and mark /*{{{*/
102// ---------------------------------------------------------------------
103/* */
104bool DebFile::DoItem(Item &I, int &Fd)
105{
106 if (strcmp(I.Name, "control") == 0)
107 {
108 delete [] Control;
8710a36a
DK
109 Control = new char[I.Size+3];
110 Control[I.Size] = '\n';
111 Control[I.Size + 1] = '\n';
112 Control[I.Size + 2] = '\0';
234edfd0 113 Which = IsControl;
8710a36a 114 ControlLen = I.Size + 3;
234edfd0
AL
115 // make it call the Process method below. this is so evil
116 Fd = -2;
117 }
118 else if (strcmp(I.Name, "config") == 0)
119 {
120 delete [] Config;
121 Config = new char[I.Size+1];
122 Config[I.Size] = 0;
123 Which = IsConfig;
124 Fd = -2;
125 }
126 else if (strcmp(I.Name, "templates") == 0)
127 {
128 delete [] Template;
129 Template = new char[I.Size+1];
130 Template[I.Size] = 0;
131 Which = IsTemplate;
132 Fd = -2;
133 }
134 else
135 {
7e726255 136 // Ignore it
234edfd0
AL
137 Fd = -1;
138 }
139 return true;
140}
141 /*}}}*/
142// DebFile::Process examine element in package and copy /*{{{*/
143// ---------------------------------------------------------------------
144/* */
65512241 145bool DebFile::Process(Item &/*I*/, const unsigned char *data,
3621b1c7 146 unsigned long long size, unsigned long long pos)
234edfd0
AL
147{
148 switch (Which)
149 {
150 case IsControl:
151 memcpy(Control + pos, data, size);
152 break;
153 case IsConfig:
154 memcpy(Config + pos, data, size);
155 break;
156 case IsTemplate:
157 memcpy(Template + pos, data, size);
158 break;
159 default: /* throw it away */ ;
160 }
161 return true;
162}
163 /*}}}*/
164// DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
165// ---------------------------------------------------------------------
166/* */
167bool DebFile::ParseInfo()
168{
169 if (Control == NULL) return false;
8710a36a 170
234edfd0 171 pkgTagSection Section;
8710a36a
DK
172 if (Section.Scan(Control, ControlLen) == false)
173 return false;
234edfd0
AL
174
175 Package = Section.FindS("Package");
176 Version = GetInstalledVer(Package);
177
178 const char *Start, *Stop;
179 if (Section.Find("Depends", Start, Stop) == true)
180 {
181 while (1)
182 {
183 string P, V;
184 unsigned int Op;
185 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
186 if (Start == 0) return false;
187 if (P == "debconf")
188 {
189 DepVer = V;
190 DepOp = Op;
191 break;
192 }
193 if (Start == Stop) break;
194 }
195 }
196
197 if (Section.Find("Pre-Depends", Start, Stop) == true)
198 {
199 while (1)
200 {
201 string P, V;
202 unsigned int Op;
203 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
204 if (Start == 0) return false;
205 if (P == "debconf")
206 {
207 PreDepVer = V;
208 PreDepOp = Op;
209 break;
210 }
211 if (Start == Stop) break;
212 }
213 }
214
215 return true;
216}
217 /*}}}*/
6079b276 218bool ShowHelp(CommandLine &, aptDispatchWithHelp const *) /*{{{*/
234edfd0 219{
ad7e0941 220 cout <<
234edfd0
AL
221 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
222 "\n"
223 "apt-extracttemplates is a tool to extract config and template info\n"
7e726255
AL
224 "from debian packages\n"
225 "\n"
226 "Options:\n"
227 " -h This help text\n"
228 " -t Set the temp dir\n"
229 " -c=? Read this configuration file\n"
a2884e32 230 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
ad7e0941 231 return true;
234edfd0
AL
232}
233 /*}}}*/
234// WriteFile - write the contents of the passed string to a file /*{{{*/
235// ---------------------------------------------------------------------
236/* */
c3ccac92 237static string WriteFile(const char *package, const char *prefix, const char *data)
3fc8f685
AL
238{
239 char fn[512];
8eb5af51 240
68e01721 241 std::string tempdir = GetTempDir();
8d50b63f 242 snprintf(fn, sizeof(fn), "%s/%s.%s.XXXXXX",
68e01721
MV
243 _config->Find("APT::ExtractTemplates::TempDir",
244 tempdir.c_str()).c_str(),
8d50b63f 245 package, prefix);
4decd43c
AL
246 FileFd f;
247 if (data == NULL)
248 data = "";
8d50b63f
MV
249 int fd = mkstemp(fn);
250 if (fd < 0) {
251 _error->Errno("ofstream::ofstream",_("Unable to mkstemp %s"),fn);
252 return string();
253 }
254 if (!f.OpenDescriptor(fd, FileFd::WriteOnly, FileFd::None, true))
7e726255
AL
255 {
256 _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
257 return string();
258 }
4decd43c
AL
259 f.Write(data, strlen(data));
260 f.Close();
7e726255 261 return fn;
3fc8f685 262}
234edfd0
AL
263 /*}}}*/
264// WriteConfig - write out the config data from a debian package file /*{{{*/
265// ---------------------------------------------------------------------
266/* */
c3ccac92 267static void WriteConfig(const DebFile &file)
3fc8f685 268{
a5eef4f7
AL
269 string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
270 string configscript = WriteFile(file.Package.c_str(), "config", file.Config);
3fc8f685 271
7e726255 272 if (templatefile.empty() == true || configscript.empty() == true)
3fc8f685 273 return;
234edfd0
AL
274 cout << file.Package << " " << file.Version << " "
275 << templatefile << " " << configscript << endl;
234edfd0
AL
276}
277 /*}}}*/
278// InitCache - initialize the package cache /*{{{*/
279// ---------------------------------------------------------------------
280/* */
c3ccac92 281static bool Go(CommandLine &CmdL)
7e726255 282{
3fc8f685 283 // Initialize the apt cache
7e726255 284 MMap *Map = 0;
3fc8f685
AL
285 pkgSourceList List;
286 List.ReadMainList();
ea4b220b 287 pkgCacheGenerator::MakeStatusCache(List,NULL,&Map,true);
08945efa
AL
288 if (Map == 0)
289 return false;
7e726255
AL
290 DebFile::Cache = new pkgCache(Map);
291 if (_error->PendingError() == true)
292 return false;
5cbf1b49 293
234edfd0 294 // Find out what version of debconf is currently installed
7e726255
AL
295 string debconfver = DebFile::GetInstalledVer("debconf");
296 if (debconfver.empty() == true)
297 return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
3fc8f685 298
234edfd0 299 // Process each package passsed in
5cbf1b49 300 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
3fc8f685 301 {
7e726255 302 // Will pick up the errors later..
5cbf1b49 303 DebFile file(CmdL.FileList[I]);
7e726255 304 if (file.Go() == false)
08945efa
AL
305 {
306 _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
307 continue;
308 }
309
234edfd0 310 // Does the package have templates?
3fc8f685
AL
311 if (file.Template != 0 && file.ParseInfo() == true)
312 {
234edfd0
AL
313 // Check to make sure debconf dependencies are
314 // satisfied
f034a64a 315 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
234edfd0 316 if (file.DepVer != "" &&
418349f0
AL
317 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
318 file.DepOp,file.DepVer.c_str()
319 ) == false)
3fc8f685 320 continue;
234edfd0 321 if (file.PreDepVer != "" &&
418349f0
AL
322 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
323 file.PreDepOp,file.PreDepVer.c_str()
324 ) == false)
3fc8f685
AL
325 continue;
326
234edfd0 327 WriteConfig(file);
3fc8f685
AL
328 }
329 }
330
331
332 delete Map;
333 delete DebFile::Cache;
7e726255
AL
334
335 return !_error->PendingError();
336}
337 /*}}}*/
6079b276 338std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
011188e3
DK
339{
340 return {
341 {nullptr, nullptr, nullptr}
342 };
343}
344 /*}}}*/
92fcbfc1 345int main(int argc, const char **argv) /*{{{*/
7e726255 346{
e7e10e47 347 InitLocale();
67111687 348
ad7e0941 349 CommandLine CmdL;
011188e3 350 auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_EXTRACTTEMPLATES, &_config, &_system, argc, argv);
ad7e0941 351
7e726255 352 Go(CmdL);
3fc8f685 353
011188e3 354 return DispatchCommandLine(CmdL, {});
3fc8f685 355}
92fcbfc1 356 /*}}}*/