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