summaryrefslogtreecommitdiff
path: root/cmdline/apt-extracttemplates.cc
blob: e28b42870146cee261dbff00dae705cc47698cfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
// $Id: apt-extracttemplates.cc,v 1.15 2003/07/26 00:00:11 mdz Exp $
/* ######################################################################
   
   APT Extract Templates - Program to extract debconf config and template
                           files

   This is a simple program to extract config and template information 
   from Debian packages. It can be used to speed up the preconfiguration
   process for debconf-enabled packages
   
   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#include<config.h>

#include <apt-pkg/init.h>
#include <apt-pkg/cmndline.h>
#include <apt-pkg/pkgcache.h>
#include <apt-pkg/cacheiterators.h>
#include <apt-pkg/configuration.h>
#include <apt-pkg/sourcelist.h>
#include <apt-pkg/pkgcachegen.h>
#include <apt-pkg/version.h>
#include <apt-pkg/tagfile.h>
#include <apt-pkg/debfile.h>
#include <apt-pkg/deblistparser.h>
#include <apt-pkg/error.h>
#include <apt-pkg/strutl.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/pkgsystem.h>
#include <apt-pkg/dirstream.h>
#include <apt-pkg/mmap.h>

#include <apt-private/private-cmndline.h>
#include <apt-private/private-main.h>

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#include "apt-extracttemplates.h"

#include <apti18n.h>
									/*}}}*/

using namespace std;

pkgCache *DebFile::Cache = 0;

// DebFile::DebFile - Construct the DebFile object			/*{{{*/
// ---------------------------------------------------------------------
/* */
DebFile::DebFile(const char *debfile)
	: File(debfile, FileFd::ReadOnly), Control(NULL), ControlLen(0),
	  DepOp(0), PreDepOp(0), Config(0), Template(0), Which(None)
{
}
									/*}}}*/
// DebFile::~DebFile - Destruct the DebFile object			/*{{{*/
// ---------------------------------------------------------------------
/* */
DebFile::~DebFile()
{
	delete [] Control;
	delete [] Config;
	delete [] Template;
}
									/*}}}*/
// DebFile::GetInstalledVer - Find out the installed version of a pkg	/*{{{*/
// ---------------------------------------------------------------------
/* */
string DebFile::GetInstalledVer(const string &package)
{
	pkgCache::PkgIterator Pkg = Cache->FindPkg(package);
	if (Pkg.end() == false) 
	{
		pkgCache::VerIterator V = Pkg.CurrentVer();
		if (V.end() == false)
		{
			return V.VerStr();
		}
	}

	return string();
}
									/*}}}*/
// DebFile::Go - Start extracting a debian package			/*{{{*/
// ---------------------------------------------------------------------
/* */
bool DebFile::Go()
{
	debDebFile Deb(File);

	return Deb.ExtractTarMember(*this, "control.tar");
}
									/*}}}*/
// DebFile::DoItem examine element in package and mark			/*{{{*/
// ---------------------------------------------------------------------
/* */
bool DebFile::DoItem(Item &I, int &Fd)
{
	if (strcmp(I.Name, "control") == 0)
	{
		delete [] Control;
		Control = new char[I.Size+3];
		Control[I.Size] = '\n';
		Control[I.Size + 1] = '\n';
		Control[I.Size + 2] = '\0';
		Which = IsControl;
		ControlLen = I.Size + 3;
		// make it call the Process method below. this is so evil
		Fd = -2;
	}
	else if (strcmp(I.Name, "config") == 0)
	{
		delete [] Config;
		Config = new char[I.Size+1];
		Config[I.Size] = 0;
		Which = IsConfig;
		Fd = -2; 
	} 
	else if (strcmp(I.Name, "templates") == 0)
	{
		delete [] Template;
		Template = new char[I.Size+1];
		Template[I.Size] = 0;
		Which = IsTemplate;
		Fd = -2;
	} 
	else 
	{
		// Ignore it
		Fd = -1;
	}
	return true;
}
									/*}}}*/
// DebFile::Process examine element in package and copy			/*{{{*/
// ---------------------------------------------------------------------
/* */
bool DebFile::Process(Item &/*I*/, const unsigned char *data,
		unsigned long long size, unsigned long long pos)
{
	switch (Which)
	{
	case IsControl:
		memcpy(Control + pos, data, size);
		break;
	case IsConfig:
		memcpy(Config + pos, data, size);
		break;
	case IsTemplate:
		memcpy(Template + pos, data, size);
		break;
	default: /* throw it away */ ;
	}
	return true;
}
									/*}}}*/
// DebFile::ParseInfo - Parse control file for dependency info		/*{{{*/
// ---------------------------------------------------------------------
/* */
bool DebFile::ParseInfo()
{
	if (Control == NULL) return false;

	pkgTagSection Section;
	if (Section.Scan(Control, ControlLen) == false)
		return false;

	Package = Section.FindS("Package");
	Version = GetInstalledVer(Package);

	const char *Start, *Stop;
	if (Section.Find("Depends", Start, Stop) == true)
	{
		while (1)
		{
			string P, V;
			unsigned int Op;
			Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
			if (Start == 0) return false;
			if (P == "debconf")
			{
				DepVer = V;
				DepOp = Op;
				break;
			}
			if (Start == Stop) break;
		}
	}
	
	if (Section.Find("Pre-Depends", Start, Stop) == true)
	{
		while (1)
		{
			string P, V;
			unsigned int Op;
			Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
			if (Start == 0) return false;
			if (P == "debconf")
			{
				PreDepVer = V;
				PreDepOp = Op;
				break;
			}
			if (Start == Stop) break;
		}
	}
	
	return true;
}
									/*}}}*/
// ShowHelp - show a short help text					/*{{{*/
// ---------------------------------------------------------------------
/* */
static bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const *)
{
	ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);

	if (_config->FindB("version") == true)
		return true;

	cout <<
		_("Usage: apt-extracttemplates file1 [file2 ...]\n"
		"\n"
		"apt-extracttemplates is a tool to extract config and template info\n"
		"from debian packages\n"
		"\n"
		"Options:\n"
	        "  -h   This help text\n"
		"  -t   Set the temp dir\n"
		"  -c=? Read this configuration file\n"
		"  -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
	return true;
}
									/*}}}*/
// WriteFile - write the contents of the passed string to a file	/*{{{*/
// ---------------------------------------------------------------------
/* */
static string WriteFile(const char *package, const char *prefix, const char *data)
{
	char fn[512];

        std::string tempdir = GetTempDir();
	snprintf(fn, sizeof(fn), "%s/%s.%s.XXXXXX",
                 _config->Find("APT::ExtractTemplates::TempDir", 
                               tempdir.c_str()).c_str(),
                 package, prefix);
	FileFd f;
	if (data == NULL)
		data = "";
        int fd = mkstemp(fn);
        if (fd < 0) {
		_error->Errno("ofstream::ofstream",_("Unable to mkstemp %s"),fn);
                return string();
        }
	if (!f.OpenDescriptor(fd, FileFd::WriteOnly, FileFd::None, true))
	{
		_error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
		return string();
	}
	f.Write(data, strlen(data));
	f.Close();
	return fn;
}
									/*}}}*/
// WriteConfig - write out the config data from a debian package file	/*{{{*/
// ---------------------------------------------------------------------
/* */
static void WriteConfig(const DebFile &file)
{
	string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
	string configscript = WriteFile(file.Package.c_str(), "config", file.Config);

	if (templatefile.empty() == true || configscript.empty() == true)
		return;
	cout << file.Package << " " << file.Version << " " 
	     << templatefile << " " << configscript << endl;
}
									/*}}}*/
// InitCache - initialize the package cache				/*{{{*/
// ---------------------------------------------------------------------
/* */
static bool Go(CommandLine &CmdL)
{	
	// Initialize the apt cache
	MMap *Map = 0;
	pkgSourceList List;
	List.ReadMainList();
	pkgCacheGenerator::MakeStatusCache(List,NULL,&Map,true);
	if (Map == 0)
	   return false;
	DebFile::Cache = new pkgCache(Map);
	if (_error->PendingError() == true)
		return false;

	// Find out what version of debconf is currently installed
	string debconfver = DebFile::GetInstalledVer("debconf");
	if (debconfver.empty() == true)
		return _error->Error( _("Cannot get debconf version. Is debconf installed?"));

	// Process each package passsed in
	for (unsigned int I = 0; I != CmdL.FileSize(); I++)
	{
		// Will pick up the errors later..
		DebFile file(CmdL.FileList[I]);
		if (file.Go() == false)
		{
		        _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
			continue;
		}

		// Does the package have templates?
		if (file.Template != 0 && file.ParseInfo() == true)
		{
			// Check to make sure debconf dependencies are
			// satisfied
			// cout << "Check " << file.DepVer << ',' << debconfver << endl;
			if (file.DepVer != "" &&
			    DebFile::Cache->VS->CheckDep(debconfver.c_str(),
					file.DepOp,file.DepVer.c_str()
							 ) == false)
				continue;
			if (file.PreDepVer != "" &&
			    DebFile::Cache->VS->CheckDep(debconfver.c_str(),
			                file.PreDepOp,file.PreDepVer.c_str()
							 ) == false) 
				continue;

			WriteConfig(file);
		}
	}
	

	delete Map;
	delete DebFile::Cache;
	
	return !_error->PendingError();
}
									/*}}}*/
int main(int argc, const char **argv)					/*{{{*/
{
	InitLocale();

	// Parse the command line and initialize the package library
	CommandLine::DispatchWithHelp Cmds[] = {{nullptr, nullptr, nullptr}};
	CommandLine CmdL;
	ParseCommandLine(CmdL, Cmds, "apt-extracttemplates", &_config, &_system, argc, argv, ShowHelp);

	Go(CmdL);

	return DispatchCommandLine(CmdL, nullptr);
}
									/*}}}*/