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