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