]> git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
c2b4b1a50e3e82a9a2903b9d3d38529ad8eab7ad
[apt.git] / cmdline / apt-extracttemplates.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: apt-extracttemplates.cc,v 1.3 2001/02/25 05:25:29 tausq 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/debversion.h>
19 #include <apt-pkg/pkgcache.h>
20 #include <apt-pkg/configuration.h>
21 #include <apt-pkg/progress.h>
22 #include <apt-pkg/sourcelist.h>
23 #include <apt-pkg/pkgcachegen.h>
24 #include <apt-pkg/version.h>
25 #include <apt-pkg/tagfile.h>
26 #include <apt-pkg/extracttar.h>
27 #include <apt-pkg/arfile.h>
28 #include <apt-pkg/deblistparser.h>
29 #include <apt-pkg/error.h>
30 #include <apt-pkg/strutl.h>
31
32 #include <stdio.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <wait.h>
37 #include <fstream.h>
38
39 #include <config.h>
40 #include <apti18n.h>
41 #include "apt-extracttemplates.h"
42
43 #define TMPDIR "/var/lib/debconf/"
44
45 pkgCache *DebFile::Cache = 0;
46
47 // DebFile::DebFile - Construct the DebFile object /*{{{*/
48 // ---------------------------------------------------------------------
49 /* */
50 DebFile::DebFile(const char *debfile)
51 : File(debfile, FileFd::ReadOnly), Control(0), DepOp(0), PreDepOp(0),
52 Config(0), Template(0), Which(None)
53 {
54 }
55 /*}}}*/
56 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
57 // ---------------------------------------------------------------------
58 /* */
59 DebFile::~DebFile()
60 {
61 delete [] Control;
62 delete [] Config;
63 delete [] Template;
64 }
65 /*}}}*/
66 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
67 // ---------------------------------------------------------------------
68 /* */
69 char *DebFile::GetInstalledVer(const string &package)
70 {
71 char *ver = 0;
72
73 pkgCache::PkgIterator Pkg = Cache->FindPkg(package);
74 if (Pkg.end() == false)
75 {
76 pkgCache::VerIterator V = Pkg.CurrentVer();
77 if (V.end() == false)
78 {
79 ver = strdup(V.VerStr());
80 }
81 }
82
83 return ver;
84 }
85 /*}}}*/
86 // DebFile::Go - Start extracting a debian package /*{{{*/
87 // ---------------------------------------------------------------------
88 /* */
89 bool DebFile::Go()
90 {
91 ARArchive AR(File);
92 const ARArchive::Member *Member = AR.FindMember("control.tar.gz");
93 if (Member == 0)
94 {
95 fprintf(stderr, _("This is not a valid DEB package.\n"));
96 return false;
97 }
98
99 if (File.Seek(Member->Start) == false)
100 {
101 return false;
102 }
103
104 ExtractTar Tar(File, Member->Size);
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 Fd = -1;
142 }
143 return true;
144 }
145 /*}}}*/
146 // DebFile::Process examine element in package and copy /*{{{*/
147 // ---------------------------------------------------------------------
148 /* */
149 bool DebFile::Process(Item &I, const unsigned char *data,
150 unsigned long size, unsigned long pos)
151 {
152 switch (Which)
153 {
154 case IsControl:
155 memcpy(Control + pos, data, size);
156 break;
157 case IsConfig:
158 memcpy(Config + pos, data, size);
159 break;
160 case IsTemplate:
161 memcpy(Template + pos, data, size);
162 break;
163 default: /* throw it away */ ;
164 }
165 return true;
166 }
167 /*}}}*/
168 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
169 // ---------------------------------------------------------------------
170 /* */
171 bool DebFile::ParseInfo()
172 {
173 if (Control == NULL) return false;
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 void ShowHelp(void)
224 {
225 ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
226 COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
227
228 if (_config->FindB("version") == true) return;
229
230 fprintf(stderr,
231 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
232 "\n"
233 "apt-extracttemplates is a tool to extract config and template info\n"
234 "from debian packages\n"));
235 exit(0);
236 }
237 /*}}}*/
238 // WriteFile - write the contents of the passed string to a file /*{{{*/
239 // ---------------------------------------------------------------------
240 /* */
241 char *WriteFile(const char *prefix, const char *data)
242 {
243 char fn[512];
244 static int i;
245 snprintf(fn, sizeof(fn), "%s%s.%u%d", _config->Find("APT::ExtractTemplates::TempDir", TMPDIR).c_str(), prefix, getpid(), i++);
246
247 ofstream ofs(fn);
248 if (!ofs) return NULL;
249 ofs << (data ? data : "");
250 ofs.close();
251 return strdup(fn);
252 }
253 /*}}}*/
254 // WriteConfig - write out the config data from a debian package file /*{{{*/
255 // ---------------------------------------------------------------------
256 /* */
257 void WriteConfig(const DebFile &file)
258 {
259 char *templatefile = WriteFile("template", file.Template);
260 char *configscript = WriteFile("config", file.Config);
261
262 if (templatefile == 0 || configscript == 0)
263 {
264 fprintf(stderr, _("Cannot write config script or templates\n"));
265 return;
266 }
267 cout << file.Package << " " << file.Version << " "
268 << templatefile << " " << configscript << endl;
269
270 free(templatefile);
271 free(configscript);
272 }
273 /*}}}*/
274 // InitCache - initialize the package cache /*{{{*/
275 // ---------------------------------------------------------------------
276 /* */
277 int InitCache(MMap *&Map, pkgCache *&Cache)
278 {
279 // Initialize the apt cache
280 if (pkgInitConfig(*_config) == false || pkgInitSystem(*_config, _system) == false)
281 {
282 _error->DumpErrors();
283 return -1;
284 }
285 pkgSourceList List;
286 List.ReadMainList();
287 OpProgress Prog;
288 pkgMakeStatusCache(List,Prog,&Map,true);
289 Cache = new pkgCache(Map);
290 return 0;
291 }
292 /*}}}*/
293
294 int main(int argc, const char **argv)
295 {
296 MMap *Map = 0;
297 const char *debconfver = NULL;
298
299 CommandLine::Args Args[] = {
300 {'h',"help","help",0},
301 {'v',"version","version",0},
302 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
303 {'c',"config-file",0,CommandLine::ConfigFile},
304 {'o',"option",0,CommandLine::ArbItem},
305 {0,0,0,0}};
306
307 // Initialize the package cache
308 if (InitCache(Map, DebFile::Cache) < 0 || Map == 0 || DebFile::Cache == 0)
309 {
310 fprintf(stderr, _("Cannot initialize APT cache\n"));
311 return 100;
312 }
313
314 // Parse the command line
315 CommandLine CmdL(Args,_config);
316 if (CmdL.Parse(argc,argv) == false)
317 {
318 fprintf(stderr, _("Cannot parse commandline options\n"));
319 return 100;
320 }
321
322 // See if the help should be shown
323 if (_config->FindB("help") == true || CmdL.FileSize() == 0)
324 {
325 ShowHelp();
326 return 0;
327 }
328
329 // Find out what version of debconf is currently installed
330 if ((debconfver = DebFile::GetInstalledVer("debconf")) == NULL)
331 {
332 fprintf(stderr, _("Cannot get debconf version. Is debconf installed?\n"));
333 return 1;
334 }
335
336 // Process each package passsed in
337 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
338 {
339 DebFile file(CmdL.FileList[I]);
340 if (file.Go() == false)
341 {
342 fprintf(stderr, _("Cannot read %s\n"), CmdL.FileList[I]);
343 continue;
344 }
345 // Does the package have templates?
346 if (file.Template != 0 && file.ParseInfo() == true)
347 {
348 // Check to make sure debconf dependencies are
349 // satisfied
350 if (file.DepVer != "" &&
351 DebFile::Cache->VS->CheckDep(file.DepVer.c_str(),
352 file.DepOp, debconfver) == false)
353 continue;
354 if (file.PreDepVer != "" &&
355 DebFile::Cache->VS->CheckDep(file.PreDepVer.c_str(),
356 file.PreDepOp, debconfver) == false)
357 continue;
358
359 WriteConfig(file);
360 }
361 }
362
363
364 delete Map;
365 delete DebFile::Cache;
366
367 return 0;
368 }