]>
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/configuration.h> | |
22 | #include <apt-pkg/progress.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/extracttar.h> | |
28 | #include <apt-pkg/arfile.h> | |
29 | #include <apt-pkg/deblistparser.h> | |
30 | #include <apt-pkg/error.h> | |
31 | #include <apt-pkg/strutl.h> | |
32 | #include <apt-pkg/fileutl.h> | |
33 | ||
34 | #include <stdio.h> | |
35 | #include <string.h> | |
36 | #include <stdlib.h> | |
37 | #include <unistd.h> | |
38 | #include <fstream> | |
39 | ||
40 | #include <locale.h> | |
41 | #include <apti18n.h> | |
42 | #include "apt-extracttemplates.h" | |
43 | /*}}}*/ | |
44 | ||
45 | using namespace std; | |
46 | ||
47 | #define TMPDIR "/tmp" | |
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(0), DepOp(0), | |
56 | 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 | ARArchive AR(File); | |
94 | if (_error->PendingError() == true) | |
95 | return false; | |
96 | ||
97 | const ARArchive::Member *Member = AR.FindMember("control.tar.gz"); | |
98 | if (Member == 0) | |
99 | return _error->Error(_("%s not a valid DEB package."),File.Name().c_str()); | |
100 | ||
101 | if (File.Seek(Member->Start) == false) | |
102 | return false; | |
103 | ExtractTar Tar(File, Member->Size,"gzip"); | |
104 | return Tar.Go(*this); | |
105 | } | |
106 | /*}}}*/ | |
107 | // DebFile::DoItem examine element in package and mark /*{{{*/ | |
108 | // --------------------------------------------------------------------- | |
109 | /* */ | |
110 | bool DebFile::DoItem(Item &I, int &Fd) | |
111 | { | |
112 | if (strcmp(I.Name, "control") == 0) | |
113 | { | |
114 | delete [] Control; | |
115 | Control = new char[I.Size+1]; | |
116 | Control[I.Size] = 0; | |
117 | Which = IsControl; | |
118 | ControlLen = I.Size; | |
119 | // make it call the Process method below. this is so evil | |
120 | Fd = -2; | |
121 | } | |
122 | else if (strcmp(I.Name, "config") == 0) | |
123 | { | |
124 | delete [] Config; | |
125 | Config = new char[I.Size+1]; | |
126 | Config[I.Size] = 0; | |
127 | Which = IsConfig; | |
128 | Fd = -2; | |
129 | } | |
130 | else if (strcmp(I.Name, "templates") == 0) | |
131 | { | |
132 | delete [] Template; | |
133 | Template = new char[I.Size+1]; | |
134 | Template[I.Size] = 0; | |
135 | Which = IsTemplate; | |
136 | Fd = -2; | |
137 | } | |
138 | else | |
139 | { | |
140 | // Ignore it | |
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 | ||
175 | pkgTagSection Section; | |
176 | Section.Scan(Control, ControlLen); | |
177 | ||
178 | Package = Section.FindS("Package"); | |
179 | Version = GetInstalledVer(Package); | |
180 | ||
181 | const char *Start, *Stop; | |
182 | if (Section.Find("Depends", Start, Stop) == true) | |
183 | { | |
184 | while (1) | |
185 | { | |
186 | string P, V; | |
187 | unsigned int Op; | |
188 | Start = debListParser::ParseDepends(Start, Stop, P, V, Op); | |
189 | if (Start == 0) return false; | |
190 | if (P == "debconf") | |
191 | { | |
192 | DepVer = V; | |
193 | DepOp = Op; | |
194 | break; | |
195 | } | |
196 | if (Start == Stop) break; | |
197 | } | |
198 | } | |
199 | ||
200 | if (Section.Find("Pre-Depends", Start, Stop) == true) | |
201 | { | |
202 | while (1) | |
203 | { | |
204 | string P, V; | |
205 | unsigned int Op; | |
206 | Start = debListParser::ParseDepends(Start, Stop, P, V, Op); | |
207 | if (Start == 0) return false; | |
208 | if (P == "debconf") | |
209 | { | |
210 | PreDepVer = V; | |
211 | PreDepOp = Op; | |
212 | break; | |
213 | } | |
214 | if (Start == Stop) break; | |
215 | } | |
216 | } | |
217 | ||
218 | return true; | |
219 | } | |
220 | /*}}}*/ | |
221 | // ShowHelp - show a short help text /*{{{*/ | |
222 | // --------------------------------------------------------------------- | |
223 | /* */ | |
224 | int ShowHelp(void) | |
225 | { | |
226 | ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,VERSION, | |
227 | COMMON_ARCH,__DATE__,__TIME__); | |
228 | ||
229 | if (_config->FindB("version") == true) | |
230 | return 0; | |
231 | ||
232 | cout << | |
233 | _("Usage: apt-extracttemplates file1 [file2 ...]\n" | |
234 | "\n" | |
235 | "apt-extracttemplates is a tool to extract config and template info\n" | |
236 | "from debian packages\n" | |
237 | "\n" | |
238 | "Options:\n" | |
239 | " -h This help text\n" | |
240 | " -t Set the temp dir\n" | |
241 | " -c=? Read this configuration file\n" | |
242 | " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"); | |
243 | return 0; | |
244 | } | |
245 | /*}}}*/ | |
246 | // WriteFile - write the contents of the passed string to a file /*{{{*/ | |
247 | // --------------------------------------------------------------------- | |
248 | /* */ | |
249 | string WriteFile(const char *package, const char *prefix, const char *data) | |
250 | { | |
251 | char fn[512]; | |
252 | static int i; | |
253 | const char *tempdir = NULL; | |
254 | ||
255 | tempdir = getenv("TMPDIR"); | |
256 | if (tempdir == NULL) | |
257 | tempdir = TMPDIR; | |
258 | ||
259 | snprintf(fn, sizeof(fn), "%s/%s.%s.%u%d", | |
260 | _config->Find("APT::ExtractTemplates::TempDir", tempdir).c_str(), | |
261 | package, prefix, getpid(), i++); | |
262 | FileFd f; | |
263 | if (data == NULL) | |
264 | data = ""; | |
265 | ||
266 | if (!f.Open(fn, FileFd::WriteTemp, 0600)) | |
267 | { | |
268 | _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn); | |
269 | return string(); | |
270 | } | |
271 | ||
272 | f.Write(data, strlen(data)); | |
273 | f.Close(); | |
274 | return fn; | |
275 | } | |
276 | /*}}}*/ | |
277 | // WriteConfig - write out the config data from a debian package file /*{{{*/ | |
278 | // --------------------------------------------------------------------- | |
279 | /* */ | |
280 | void WriteConfig(const DebFile &file) | |
281 | { | |
282 | string templatefile = WriteFile(file.Package.c_str(), "template", file.Template); | |
283 | string configscript = WriteFile(file.Package.c_str(), "config", file.Config); | |
284 | ||
285 | if (templatefile.empty() == true || configscript.empty() == true) | |
286 | return; | |
287 | cout << file.Package << " " << file.Version << " " | |
288 | << templatefile << " " << configscript << endl; | |
289 | } | |
290 | /*}}}*/ | |
291 | // InitCache - initialize the package cache /*{{{*/ | |
292 | // --------------------------------------------------------------------- | |
293 | /* */ | |
294 | bool Go(CommandLine &CmdL) | |
295 | { | |
296 | // Initialize the apt cache | |
297 | MMap *Map = 0; | |
298 | pkgSourceList List; | |
299 | List.ReadMainList(); | |
300 | pkgCacheGenerator::MakeStatusCache(List,NULL,&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 | int main(int argc, const char **argv) /*{{{*/ | |
352 | { | |
353 | CommandLine::Args Args[] = { | |
354 | {'h',"help","help",0}, | |
355 | {'v',"version","version",0}, | |
356 | {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg}, | |
357 | {'c',"config-file",0,CommandLine::ConfigFile}, | |
358 | {'o',"option",0,CommandLine::ArbItem}, | |
359 | {0,0,0,0}}; | |
360 | ||
361 | // Set up gettext support | |
362 | setlocale(LC_ALL,""); | |
363 | textdomain(PACKAGE); | |
364 | ||
365 | // Parse the command line and initialize the package library | |
366 | CommandLine CmdL(Args,_config); | |
367 | if (pkgInitConfig(*_config) == false || | |
368 | CmdL.Parse(argc,argv) == false || | |
369 | pkgInitSystem(*_config,_system) == false) | |
370 | { | |
371 | _error->DumpErrors(); | |
372 | return 100; | |
373 | } | |
374 | ||
375 | // See if the help should be shown | |
376 | if (_config->FindB("help") == true || | |
377 | CmdL.FileSize() == 0) | |
378 | return ShowHelp(); | |
379 | ||
380 | Go(CmdL); | |
381 | ||
382 | // Print any errors or warnings found during operation | |
383 | if (_error->empty() == false) | |
384 | { | |
385 | // This goes to stderr.. | |
386 | bool Errors = _error->PendingError(); | |
387 | _error->DumpErrors(); | |
388 | return Errors == true?100:0; | |
389 | } | |
390 | ||
391 | return 0; | |
392 | } | |
393 | /*}}}*/ |