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