]> git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
Merge branch 'debian/sid' into debian/experimental
[apt.git] / cmdline / apt-extracttemplates.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: apt-extracttemplates.cc,v 1.15 2003/07/26 00:00:11 mdz Exp $
4 /* ######################################################################
5
6 APT Extract Templates - Program to extract debconf config and template
7 files
8
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 /*{{{*/
16 #include<config.h>
17
18 #include <apt-pkg/init.h>
19 #include <apt-pkg/cmndline.h>
20 #include <apt-pkg/pkgcache.h>
21 #include <apt-pkg/configuration.h>
22 #include <apt-pkg/progress.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
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38 #include <locale.h>
39
40 #include <fstream>
41
42 #include "apt-extracttemplates.h"
43
44 #include <apti18n.h>
45 /*}}}*/
46
47 using namespace std;
48
49 pkgCache *DebFile::Cache = 0;
50
51 // DebFile::DebFile - Construct the DebFile object /*{{{*/
52 // ---------------------------------------------------------------------
53 /* */
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)
57 {
58 }
59 /*}}}*/
60 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
61 // ---------------------------------------------------------------------
62 /* */
63 DebFile::~DebFile()
64 {
65 delete [] Control;
66 delete [] Config;
67 delete [] Template;
68 }
69 /*}}}*/
70 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
71 // ---------------------------------------------------------------------
72 /* */
73 string DebFile::GetInstalledVer(const string &package)
74 {
75 pkgCache::PkgIterator Pkg = Cache->FindPkg(package);
76 if (Pkg.end() == false)
77 {
78 pkgCache::VerIterator V = Pkg.CurrentVer();
79 if (V.end() == false)
80 {
81 return V.VerStr();
82 }
83 }
84
85 return string();
86 }
87 /*}}}*/
88 // DebFile::Go - Start extracting a debian package /*{{{*/
89 // ---------------------------------------------------------------------
90 /* */
91 bool DebFile::Go()
92 {
93 debDebFile Deb(File);
94
95 return Deb.ExtractTarMember(*this, "control.tar");
96 }
97 /*}}}*/
98 // DebFile::DoItem examine element in package and mark /*{{{*/
99 // ---------------------------------------------------------------------
100 /* */
101 bool DebFile::DoItem(Item &I, int &Fd)
102 {
103 if (strcmp(I.Name, "control") == 0)
104 {
105 delete [] Control;
106 Control = new char[I.Size+1];
107 Control[I.Size] = 0;
108 Which = IsControl;
109 ControlLen = I.Size;
110 // make it call the Process method below. this is so evil
111 Fd = -2;
112 }
113 else if (strcmp(I.Name, "config") == 0)
114 {
115 delete [] Config;
116 Config = new char[I.Size+1];
117 Config[I.Size] = 0;
118 Which = IsConfig;
119 Fd = -2;
120 }
121 else if (strcmp(I.Name, "templates") == 0)
122 {
123 delete [] Template;
124 Template = new char[I.Size+1];
125 Template[I.Size] = 0;
126 Which = IsTemplate;
127 Fd = -2;
128 }
129 else
130 {
131 // Ignore it
132 Fd = -1;
133 }
134 return true;
135 }
136 /*}}}*/
137 // DebFile::Process examine element in package and copy /*{{{*/
138 // ---------------------------------------------------------------------
139 /* */
140 bool DebFile::Process(Item &I, const unsigned char *data,
141 unsigned long size, unsigned long pos)
142 {
143 switch (Which)
144 {
145 case IsControl:
146 memcpy(Control + pos, data, size);
147 break;
148 case IsConfig:
149 memcpy(Config + pos, data, size);
150 break;
151 case IsTemplate:
152 memcpy(Template + pos, data, size);
153 break;
154 default: /* throw it away */ ;
155 }
156 return true;
157 }
158 /*}}}*/
159 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
160 // ---------------------------------------------------------------------
161 /* */
162 bool DebFile::ParseInfo()
163 {
164 if (Control == NULL) return false;
165
166 pkgTagSection Section;
167 Section.Scan(Control, ControlLen);
168
169 Package = Section.FindS("Package");
170 Version = GetInstalledVer(Package);
171
172 const char *Start, *Stop;
173 if (Section.Find("Depends", Start, Stop) == true)
174 {
175 while (1)
176 {
177 string P, V;
178 unsigned int Op;
179 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
180 if (Start == 0) return false;
181 if (P == "debconf")
182 {
183 DepVer = V;
184 DepOp = Op;
185 break;
186 }
187 if (Start == Stop) break;
188 }
189 }
190
191 if (Section.Find("Pre-Depends", Start, Stop) == true)
192 {
193 while (1)
194 {
195 string P, V;
196 unsigned int Op;
197 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
198 if (Start == 0) return false;
199 if (P == "debconf")
200 {
201 PreDepVer = V;
202 PreDepOp = Op;
203 break;
204 }
205 if (Start == Stop) break;
206 }
207 }
208
209 return true;
210 }
211 /*}}}*/
212 // ShowHelp - show a short help text /*{{{*/
213 // ---------------------------------------------------------------------
214 /* */
215 int ShowHelp(void)
216 {
217 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
218 COMMON_ARCH,__DATE__,__TIME__);
219
220 if (_config->FindB("version") == true)
221 return 0;
222
223 cout <<
224 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
225 "\n"
226 "apt-extracttemplates is a tool to extract config and template info\n"
227 "from debian packages\n"
228 "\n"
229 "Options:\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");
234 return 0;
235 }
236 /*}}}*/
237 // WriteFile - write the contents of the passed string to a file /*{{{*/
238 // ---------------------------------------------------------------------
239 /* */
240 string WriteFile(const char *package, const char *prefix, const char *data)
241 {
242 char fn[512];
243 static int i;
244
245 std::string tempdir = GetTempDir();
246 snprintf(fn, sizeof(fn), "%s/%s.%s.%u%d",
247 _config->Find("APT::ExtractTemplates::TempDir",
248 tempdir.c_str()).c_str(),
249 package, prefix, getpid(), i++);
250 FileFd f;
251 if (data == NULL)
252 data = "";
253
254 if (!f.Open(fn, FileFd::WriteTemp, 0600))
255 {
256 _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
257 return string();
258 }
259
260 f.Write(data, strlen(data));
261 f.Close();
262 return fn;
263 }
264 /*}}}*/
265 // WriteConfig - write out the config data from a debian package file /*{{{*/
266 // ---------------------------------------------------------------------
267 /* */
268 void WriteConfig(const DebFile &file)
269 {
270 string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
271 string configscript = WriteFile(file.Package.c_str(), "config", file.Config);
272
273 if (templatefile.empty() == true || configscript.empty() == true)
274 return;
275 cout << file.Package << " " << file.Version << " "
276 << templatefile << " " << configscript << endl;
277 }
278 /*}}}*/
279 // InitCache - initialize the package cache /*{{{*/
280 // ---------------------------------------------------------------------
281 /* */
282 bool Go(CommandLine &CmdL)
283 {
284 // Initialize the apt cache
285 MMap *Map = 0;
286 pkgSourceList List;
287 List.ReadMainList();
288 pkgCacheGenerator::MakeStatusCache(List,NULL,&Map,true);
289 if (Map == 0)
290 return false;
291 DebFile::Cache = new pkgCache(Map);
292 if (_error->PendingError() == true)
293 return false;
294
295 // Find out what version of debconf is currently installed
296 string debconfver = DebFile::GetInstalledVer("debconf");
297 if (debconfver.empty() == true)
298 return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
299
300 // Process each package passsed in
301 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
302 {
303 // Will pick up the errors later..
304 DebFile file(CmdL.FileList[I]);
305 if (file.Go() == false)
306 {
307 _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
308 continue;
309 }
310
311 // Does the package have templates?
312 if (file.Template != 0 && file.ParseInfo() == true)
313 {
314 // Check to make sure debconf dependencies are
315 // satisfied
316 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
317 if (file.DepVer != "" &&
318 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
319 file.DepOp,file.DepVer.c_str()
320 ) == false)
321 continue;
322 if (file.PreDepVer != "" &&
323 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
324 file.PreDepOp,file.PreDepVer.c_str()
325 ) == false)
326 continue;
327
328 WriteConfig(file);
329 }
330 }
331
332
333 delete Map;
334 delete DebFile::Cache;
335
336 return !_error->PendingError();
337 }
338 /*}}}*/
339 int main(int argc, const char **argv) /*{{{*/
340 {
341 CommandLine::Args Args[] = {
342 {'h',"help","help",0},
343 {'v',"version","version",0},
344 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
345 {'c',"config-file",0,CommandLine::ConfigFile},
346 {'o',"option",0,CommandLine::ArbItem},
347 {0,0,0,0}};
348
349 // Set up gettext support
350 setlocale(LC_ALL,"");
351 textdomain(PACKAGE);
352
353 // Parse the command line and initialize the package library
354 CommandLine CmdL(Args,_config);
355 if (pkgInitConfig(*_config) == false ||
356 CmdL.Parse(argc,argv) == false ||
357 pkgInitSystem(*_config,_system) == false)
358 {
359 _error->DumpErrors();
360 return 100;
361 }
362
363 // See if the help should be shown
364 if (_config->FindB("help") == true ||
365 CmdL.FileSize() == 0)
366 return ShowHelp();
367
368 Go(CmdL);
369
370 // Print any errors or warnings found during operation
371 if (_error->empty() == false)
372 {
373 // This goes to stderr..
374 bool Errors = _error->PendingError();
375 _error->DumpErrors();
376 return Errors == true?100:0;
377 }
378
379 return 0;
380 }
381 /*}}}*/