]> git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
1b16542fe2762c24da884cfbbb17c10839c17240
[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 #include <apt-private/private-main.h>
38
39 #include <iostream>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44
45 #include "apt-extracttemplates.h"
46
47 #include <apti18n.h>
48 /*}}}*/
49
50 using namespace std;
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), 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 debDebFile Deb(File);
97
98 return Deb.ExtractTarMember(*this, "control.tar");
99 }
100 /*}}}*/
101 // DebFile::DoItem examine element in package and mark /*{{{*/
102 // ---------------------------------------------------------------------
103 /* */
104 bool DebFile::DoItem(Item &I, int &Fd)
105 {
106 if (strcmp(I.Name, "control") == 0)
107 {
108 delete [] Control;
109 Control = new char[I.Size+3];
110 Control[I.Size] = '\n';
111 Control[I.Size + 1] = '\n';
112 Control[I.Size + 2] = '\0';
113 Which = IsControl;
114 ControlLen = I.Size + 3;
115 // make it call the Process method below. this is so evil
116 Fd = -2;
117 }
118 else if (strcmp(I.Name, "config") == 0)
119 {
120 delete [] Config;
121 Config = new char[I.Size+1];
122 Config[I.Size] = 0;
123 Which = IsConfig;
124 Fd = -2;
125 }
126 else if (strcmp(I.Name, "templates") == 0)
127 {
128 delete [] Template;
129 Template = new char[I.Size+1];
130 Template[I.Size] = 0;
131 Which = IsTemplate;
132 Fd = -2;
133 }
134 else
135 {
136 // Ignore it
137 Fd = -1;
138 }
139 return true;
140 }
141 /*}}}*/
142 // DebFile::Process examine element in package and copy /*{{{*/
143 // ---------------------------------------------------------------------
144 /* */
145 bool DebFile::Process(Item &/*I*/, const unsigned char *data,
146 unsigned long long size, unsigned long long pos)
147 {
148 switch (Which)
149 {
150 case IsControl:
151 memcpy(Control + pos, data, size);
152 break;
153 case IsConfig:
154 memcpy(Config + pos, data, size);
155 break;
156 case IsTemplate:
157 memcpy(Template + pos, data, size);
158 break;
159 default: /* throw it away */ ;
160 }
161 return true;
162 }
163 /*}}}*/
164 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
165 // ---------------------------------------------------------------------
166 /* */
167 bool DebFile::ParseInfo()
168 {
169 if (Control == NULL) return false;
170
171 pkgTagSection Section;
172 if (Section.Scan(Control, ControlLen) == false)
173 return false;
174
175 Package = Section.FindS("Package");
176 Version = GetInstalledVer(Package);
177
178 const char *Start, *Stop;
179 if (Section.Find("Depends", Start, Stop) == true)
180 {
181 while (1)
182 {
183 string P, V;
184 unsigned int Op;
185 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
186 if (Start == 0) return false;
187 if (P == "debconf")
188 {
189 DepVer = V;
190 DepOp = Op;
191 break;
192 }
193 if (Start == Stop) break;
194 }
195 }
196
197 if (Section.Find("Pre-Depends", Start, Stop) == true)
198 {
199 while (1)
200 {
201 string P, V;
202 unsigned int Op;
203 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
204 if (Start == 0) return false;
205 if (P == "debconf")
206 {
207 PreDepVer = V;
208 PreDepOp = Op;
209 break;
210 }
211 if (Start == Stop) break;
212 }
213 }
214
215 return true;
216 }
217 /*}}}*/
218 bool ShowHelp(CommandLine &, aptDispatchWithHelp const *) /*{{{*/
219 {
220 ioprintf(std::cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
221
222 if (_config->FindB("version") == true)
223 return true;
224
225 cout <<
226 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
227 "\n"
228 "apt-extracttemplates is a tool to extract config and template info\n"
229 "from debian packages\n"
230 "\n"
231 "Options:\n"
232 " -h This help text\n"
233 " -t Set the temp dir\n"
234 " -c=? Read this configuration file\n"
235 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
236 return true;
237 }
238 /*}}}*/
239 // WriteFile - write the contents of the passed string to a file /*{{{*/
240 // ---------------------------------------------------------------------
241 /* */
242 static string WriteFile(const char *package, const char *prefix, const char *data)
243 {
244 char fn[512];
245
246 std::string tempdir = GetTempDir();
247 snprintf(fn, sizeof(fn), "%s/%s.%s.XXXXXX",
248 _config->Find("APT::ExtractTemplates::TempDir",
249 tempdir.c_str()).c_str(),
250 package, prefix);
251 FileFd f;
252 if (data == NULL)
253 data = "";
254 int fd = mkstemp(fn);
255 if (fd < 0) {
256 _error->Errno("ofstream::ofstream",_("Unable to mkstemp %s"),fn);
257 return string();
258 }
259 if (!f.OpenDescriptor(fd, FileFd::WriteOnly, FileFd::None, true))
260 {
261 _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
262 return string();
263 }
264 f.Write(data, strlen(data));
265 f.Close();
266 return fn;
267 }
268 /*}}}*/
269 // WriteConfig - write out the config data from a debian package file /*{{{*/
270 // ---------------------------------------------------------------------
271 /* */
272 static void WriteConfig(const DebFile &file)
273 {
274 string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
275 string configscript = WriteFile(file.Package.c_str(), "config", file.Config);
276
277 if (templatefile.empty() == true || configscript.empty() == true)
278 return;
279 cout << file.Package << " " << file.Version << " "
280 << templatefile << " " << configscript << endl;
281 }
282 /*}}}*/
283 // InitCache - initialize the package cache /*{{{*/
284 // ---------------------------------------------------------------------
285 /* */
286 static bool Go(CommandLine &CmdL)
287 {
288 // Initialize the apt cache
289 MMap *Map = 0;
290 pkgSourceList List;
291 List.ReadMainList();
292 pkgCacheGenerator::MakeStatusCache(List,NULL,&Map,true);
293 if (Map == 0)
294 return false;
295 DebFile::Cache = new pkgCache(Map);
296 if (_error->PendingError() == true)
297 return false;
298
299 // Find out what version of debconf is currently installed
300 string debconfver = DebFile::GetInstalledVer("debconf");
301 if (debconfver.empty() == true)
302 return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
303
304 // Process each package passsed in
305 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
306 {
307 // Will pick up the errors later..
308 DebFile file(CmdL.FileList[I]);
309 if (file.Go() == false)
310 {
311 _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
312 continue;
313 }
314
315 // Does the package have templates?
316 if (file.Template != 0 && file.ParseInfo() == true)
317 {
318 // Check to make sure debconf dependencies are
319 // satisfied
320 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
321 if (file.DepVer != "" &&
322 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
323 file.DepOp,file.DepVer.c_str()
324 ) == false)
325 continue;
326 if (file.PreDepVer != "" &&
327 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
328 file.PreDepOp,file.PreDepVer.c_str()
329 ) == false)
330 continue;
331
332 WriteConfig(file);
333 }
334 }
335
336
337 delete Map;
338 delete DebFile::Cache;
339
340 return !_error->PendingError();
341 }
342 /*}}}*/
343 std::vector<aptDispatchWithHelp> GetCommands() /*{{{*/
344 {
345 return {
346 {nullptr, nullptr, nullptr}
347 };
348 }
349 /*}}}*/
350 int main(int argc, const char **argv) /*{{{*/
351 {
352 InitLocale();
353
354 CommandLine CmdL;
355 auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_EXTRACTTEMPLATES, &_config, &_system, argc, argv);
356
357 Go(CmdL);
358
359 return DispatchCommandLine(CmdL, {});
360 }
361 /*}}}*/