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