]> git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
tests: use SHA1 checksum only by default in tests
[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/cacheiterators.h>
22 #include <apt-pkg/configuration.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/debfile.h>
28 #include <apt-pkg/deblistparser.h>
29 #include <apt-pkg/error.h>
30 #include <apt-pkg/strutl.h>
31 #include <apt-pkg/fileutl.h>
32 #include <apt-pkg/pkgsystem.h>
33 #include <apt-pkg/dirstream.h>
34 #include <apt-pkg/mmap.h>
35
36 #include <apt-private/private-cmndline.h>
37
38 #include <iostream>
39 #include <stdio.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <stdlib.h>
43
44 #include "apt-extracttemplates.h"
45
46 #include <apti18n.h>
47 /*}}}*/
48
49 using namespace std;
50
51 pkgCache *DebFile::Cache = 0;
52
53 // DebFile::DebFile - Construct the DebFile object /*{{{*/
54 // ---------------------------------------------------------------------
55 /* */
56 DebFile::DebFile(const char *debfile)
57 : File(debfile, FileFd::ReadOnly), Control(NULL), ControlLen(0),
58 DepOp(0), PreDepOp(0), Config(0), Template(0), Which(None)
59 {
60 }
61 /*}}}*/
62 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
63 // ---------------------------------------------------------------------
64 /* */
65 DebFile::~DebFile()
66 {
67 delete [] Control;
68 delete [] Config;
69 delete [] Template;
70 }
71 /*}}}*/
72 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
73 // ---------------------------------------------------------------------
74 /* */
75 string DebFile::GetInstalledVer(const string &package)
76 {
77 pkgCache::PkgIterator Pkg = Cache->FindPkg(package);
78 if (Pkg.end() == false)
79 {
80 pkgCache::VerIterator V = Pkg.CurrentVer();
81 if (V.end() == false)
82 {
83 return V.VerStr();
84 }
85 }
86
87 return string();
88 }
89 /*}}}*/
90 // DebFile::Go - Start extracting a debian package /*{{{*/
91 // ---------------------------------------------------------------------
92 /* */
93 bool DebFile::Go()
94 {
95 debDebFile Deb(File);
96
97 return Deb.ExtractTarMember(*this, "control.tar");
98 }
99 /*}}}*/
100 // DebFile::DoItem examine element in package and mark /*{{{*/
101 // ---------------------------------------------------------------------
102 /* */
103 bool DebFile::DoItem(Item &I, int &Fd)
104 {
105 if (strcmp(I.Name, "control") == 0)
106 {
107 delete [] Control;
108 Control = new char[I.Size+3];
109 Control[I.Size] = '\n';
110 Control[I.Size + 1] = '\n';
111 Control[I.Size + 2] = '\0';
112 Which = IsControl;
113 ControlLen = I.Size + 3;
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 long size, unsigned long 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 if (Section.Scan(Control, ControlLen) == false)
172 return false;
173
174 Package = Section.FindS("Package");
175 Version = GetInstalledVer(Package);
176
177 const char *Start, *Stop;
178 if (Section.Find("Depends", Start, Stop) == true)
179 {
180 while (1)
181 {
182 string P, V;
183 unsigned int Op;
184 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
185 if (Start == 0) return false;
186 if (P == "debconf")
187 {
188 DepVer = V;
189 DepOp = Op;
190 break;
191 }
192 if (Start == Stop) break;
193 }
194 }
195
196 if (Section.Find("Pre-Depends", Start, Stop) == true)
197 {
198 while (1)
199 {
200 string P, V;
201 unsigned int Op;
202 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
203 if (Start == 0) return false;
204 if (P == "debconf")
205 {
206 PreDepVer = V;
207 PreDepOp = Op;
208 break;
209 }
210 if (Start == Stop) break;
211 }
212 }
213
214 return true;
215 }
216 /*}}}*/
217 // ShowHelp - show a short help text /*{{{*/
218 // ---------------------------------------------------------------------
219 /* */
220 static bool ShowHelp(CommandLine &)
221 {
222 ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
223
224 if (_config->FindB("version") == true)
225 return true;
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 arbitrary configuration option, eg -o dir::cache=/tmp\n");
238 return true;
239 }
240 /*}}}*/
241 // WriteFile - write the contents of the passed string to a file /*{{{*/
242 // ---------------------------------------------------------------------
243 /* */
244 static string WriteFile(const char *package, const char *prefix, const char *data)
245 {
246 char fn[512];
247
248 std::string tempdir = GetTempDir();
249 snprintf(fn, sizeof(fn), "%s/%s.%s.XXXXXX",
250 _config->Find("APT::ExtractTemplates::TempDir",
251 tempdir.c_str()).c_str(),
252 package, prefix);
253 FileFd f;
254 if (data == NULL)
255 data = "";
256 int fd = mkstemp(fn);
257 if (fd < 0) {
258 _error->Errno("ofstream::ofstream",_("Unable to mkstemp %s"),fn);
259 return string();
260 }
261 if (!f.OpenDescriptor(fd, FileFd::WriteOnly, FileFd::None, true))
262 {
263 _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
264 return string();
265 }
266 f.Write(data, strlen(data));
267 f.Close();
268 return fn;
269 }
270 /*}}}*/
271 // WriteConfig - write out the config data from a debian package file /*{{{*/
272 // ---------------------------------------------------------------------
273 /* */
274 static void WriteConfig(const DebFile &file)
275 {
276 string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
277 string configscript = WriteFile(file.Package.c_str(), "config", file.Config);
278
279 if (templatefile.empty() == true || configscript.empty() == true)
280 return;
281 cout << file.Package << " " << file.Version << " "
282 << templatefile << " " << configscript << endl;
283 }
284 /*}}}*/
285 // InitCache - initialize the package cache /*{{{*/
286 // ---------------------------------------------------------------------
287 /* */
288 static bool Go(CommandLine &CmdL)
289 {
290 // Initialize the apt cache
291 MMap *Map = 0;
292 pkgSourceList List;
293 List.ReadMainList();
294 pkgCacheGenerator::MakeStatusCache(List,NULL,&Map,true);
295 if (Map == 0)
296 return false;
297 DebFile::Cache = new pkgCache(Map);
298 if (_error->PendingError() == true)
299 return false;
300
301 // Find out what version of debconf is currently installed
302 string debconfver = DebFile::GetInstalledVer("debconf");
303 if (debconfver.empty() == true)
304 return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
305
306 // Process each package passsed in
307 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
308 {
309 // Will pick up the errors later..
310 DebFile file(CmdL.FileList[I]);
311 if (file.Go() == false)
312 {
313 _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
314 continue;
315 }
316
317 // Does the package have templates?
318 if (file.Template != 0 && file.ParseInfo() == true)
319 {
320 // Check to make sure debconf dependencies are
321 // satisfied
322 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
323 if (file.DepVer != "" &&
324 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
325 file.DepOp,file.DepVer.c_str()
326 ) == false)
327 continue;
328 if (file.PreDepVer != "" &&
329 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
330 file.PreDepOp,file.PreDepVer.c_str()
331 ) == false)
332 continue;
333
334 WriteConfig(file);
335 }
336 }
337
338
339 delete Map;
340 delete DebFile::Cache;
341
342 return !_error->PendingError();
343 }
344 /*}}}*/
345 int main(int argc, const char **argv) /*{{{*/
346 {
347 CommandLine::Args Args[] = {
348 {'h',"help","help",0},
349 {'v',"version","version",0},
350 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
351 {'c',"config-file",0,CommandLine::ConfigFile},
352 {'o',"option",0,CommandLine::ArbItem},
353 {0,0,0,0}};
354
355 // Set up gettext support
356 setlocale(LC_ALL,"");
357 textdomain(PACKAGE);
358
359 // Parse the command line and initialize the package library
360 CommandLine::Dispatch Cmds[] = {{NULL, NULL}};
361 CommandLine CmdL;
362 ParseCommandLine(CmdL, Cmds, Args, &_config, &_system, argc, argv, ShowHelp);
363
364 Go(CmdL);
365
366 // Print any errors or warnings found during operation
367 if (_error->empty() == false)
368 {
369 // This goes to stderr..
370 bool Errors = _error->PendingError();
371 _error->DumpErrors();
372 return Errors == true?100:0;
373 }
374
375 return 0;
376 }
377 /*}}}*/