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