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