]> git.saurik.com Git - apt-legacy.git/blob - cmdline/apt-extracttemplates.cc.orig
Fixed Dl-Limit (long standing APT bug that they won't acknowledge), specified error...
[apt-legacy.git] / cmdline / apt-extracttemplates.cc.orig
1 extern "C" {
2 #include <mach-o/nlist.h>
3 }
4
5 // -*- mode: cpp; mode: fold -*-
6 // Description /*{{{*/
7 // $Id: apt-extracttemplates.cc,v 1.15 2003/07/26 00:00:11 mdz Exp $
8 /* ######################################################################
9
10 APT Extract Templates - Program to extract debconf config and template
11 files
12
13 This is a simple program to extract config and template information
14 from Debian packages. It can be used to speed up the preconfiguration
15 process for debconf-enabled packages
16
17 ##################################################################### */
18 /*}}}*/
19 // Include Files /*{{{*/
20 #include <apt-pkg/init.h>
21 #include <apt-pkg/cmndline.h>
22 #include <apt-pkg/pkgcache.h>
23 #include <apt-pkg/configuration.h>
24 #include <apt-pkg/progress.h>
25 #include <apt-pkg/sourcelist.h>
26 #include <apt-pkg/pkgcachegen.h>
27 #include <apt-pkg/version.h>
28 #include <apt-pkg/tagfile.h>
29 #include <apt-pkg/extracttar.h>
30 #include <apt-pkg/arfile.h>
31 #include <apt-pkg/deblistparser.h>
32 #include <apt-pkg/error.h>
33 #include <apt-pkg/strutl.h>
34 #include <apt-pkg/fileutl.h>
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <fstream>
41
42 #include <locale.h>
43 #include <config.h>
44 #include <apti18n.h>
45 #include "apt-extracttemplates.h"
46 /*}}}*/
47
48 using namespace std;
49
50 #define TMPDIR "/tmp"
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(0), DepOp(0),
59 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 ARArchive AR(File);
97 if (_error->PendingError() == true)
98 return false;
99
100 const ARArchive::Member *Member = AR.FindMember("control.tar.gz");
101 if (Member == 0)
102 return _error->Error(_("%s not a valid DEB package."),File.Name().c_str());
103
104 if (File.Seek(Member->Start) == false)
105 return false;
106 ExtractTar Tar(File, Member->Size,"gzip");
107 return Tar.Go(*this);
108 }
109 /*}}}*/
110 // DebFile::DoItem examine element in package and mark /*{{{*/
111 // ---------------------------------------------------------------------
112 /* */
113 bool DebFile::DoItem(Item &I, int &Fd)
114 {
115 if (strcmp(I.Name, "control") == 0)
116 {
117 delete [] Control;
118 Control = new char[I.Size+1];
119 Control[I.Size] = 0;
120 Which = IsControl;
121 ControlLen = I.Size;
122 // make it call the Process method below. this is so evil
123 Fd = -2;
124 }
125 else if (strcmp(I.Name, "config") == 0)
126 {
127 delete [] Config;
128 Config = new char[I.Size+1];
129 Config[I.Size] = 0;
130 Which = IsConfig;
131 Fd = -2;
132 }
133 else if (strcmp(I.Name, "templates") == 0)
134 {
135 delete [] Template;
136 Template = new char[I.Size+1];
137 Template[I.Size] = 0;
138 Which = IsTemplate;
139 Fd = -2;
140 }
141 else
142 {
143 // Ignore it
144 Fd = -1;
145 }
146 return true;
147 }
148 /*}}}*/
149 // DebFile::Process examine element in package and copy /*{{{*/
150 // ---------------------------------------------------------------------
151 /* */
152 bool DebFile::Process(Item &I, const unsigned char *data,
153 unsigned long size, unsigned long pos)
154 {
155 switch (Which)
156 {
157 case IsControl:
158 memcpy(Control + pos, data, size);
159 break;
160 case IsConfig:
161 memcpy(Config + pos, data, size);
162 break;
163 case IsTemplate:
164 memcpy(Template + pos, data, size);
165 break;
166 default: /* throw it away */ ;
167 }
168 return true;
169 }
170 /*}}}*/
171 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
172 // ---------------------------------------------------------------------
173 /* */
174 bool DebFile::ParseInfo()
175 {
176 if (Control == NULL) return false;
177
178 pkgTagSection Section;
179 Section.Scan(Control, ControlLen);
180
181 Package = Section.FindS("Package");
182 Version = GetInstalledVer(Package);
183
184 const char *Start, *Stop;
185 if (Section.Find("Depends", Start, Stop) == true)
186 {
187 while (1)
188 {
189 string P, V;
190 unsigned int Op;
191 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
192 if (Start == 0) return false;
193 if (P == "debconf")
194 {
195 DepVer = V;
196 DepOp = Op;
197 break;
198 }
199 if (Start == Stop) break;
200 }
201 }
202
203 if (Section.Find("Pre-Depends", Start, Stop) == true)
204 {
205 while (1)
206 {
207 string P, V;
208 unsigned int Op;
209 Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
210 if (Start == 0) return false;
211 if (P == "debconf")
212 {
213 PreDepVer = V;
214 PreDepOp = Op;
215 break;
216 }
217 if (Start == Stop) break;
218 }
219 }
220
221 return true;
222 }
223 /*}}}*/
224 // ShowHelp - show a short help text /*{{{*/
225 // ---------------------------------------------------------------------
226 /* */
227 int ShowHelp(void)
228 {
229 ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
230 COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
231
232 if (_config->FindB("version") == true)
233 return 0;
234
235 cout <<
236 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
237 "\n"
238 "apt-extracttemplates is a tool to extract config and template info\n"
239 "from debian packages\n"
240 "\n"
241 "Options:\n"
242 " -h This help text\n"
243 " -t Set the temp dir\n"
244 " -c=? Read this configuration file\n"
245 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
246 return 0;
247 }
248 /*}}}*/
249 // WriteFile - write the contents of the passed string to a file /*{{{*/
250 // ---------------------------------------------------------------------
251 /* */
252 string WriteFile(const char *package, const char *prefix, const char *data)
253 {
254 char fn[512];
255 static int i;
256 char *tempdir = NULL;
257
258 tempdir = getenv("TMPDIR");
259 if (tempdir == NULL)
260 tempdir = TMPDIR;
261
262 snprintf(fn, sizeof(fn), "%s/%s.%s.%u%d",
263 _config->Find("APT::ExtractTemplates::TempDir", tempdir).c_str(),
264 package, prefix, getpid(), i++);
265 FileFd f;
266 if (data == NULL)
267 data = "";
268
269 if (!f.Open(fn, FileFd::WriteTemp, 0600))
270 {
271 _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
272 return string();
273 }
274
275 f.Write(data, strlen(data));
276 f.Close();
277 return fn;
278 }
279 /*}}}*/
280 // WriteConfig - write out the config data from a debian package file /*{{{*/
281 // ---------------------------------------------------------------------
282 /* */
283 void WriteConfig(const DebFile &file)
284 {
285 string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
286 string configscript = WriteFile(file.Package.c_str(), "config", file.Config);
287
288 if (templatefile.empty() == true || configscript.empty() == true)
289 return;
290 cout << file.Package << " " << file.Version << " "
291 << templatefile << " " << configscript << endl;
292 }
293 /*}}}*/
294 // InitCache - initialize the package cache /*{{{*/
295 // ---------------------------------------------------------------------
296 /* */
297 bool Go(CommandLine &CmdL)
298 {
299 // Initialize the apt cache
300 MMap *Map = 0;
301 pkgSourceList List;
302 List.ReadMainList();
303 OpProgress Prog;
304 pkgMakeStatusCache(List,Prog,&Map,true);
305 if (Map == 0)
306 return false;
307 DebFile::Cache = new pkgCache(Map);
308 if (_error->PendingError() == true)
309 return false;
310
311 // Find out what version of debconf is currently installed
312 string debconfver = DebFile::GetInstalledVer("debconf");
313 if (debconfver.empty() == true)
314 return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
315
316 // Process each package passsed in
317 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
318 {
319 // Will pick up the errors later..
320 DebFile file(CmdL.FileList[I]);
321 if (file.Go() == false)
322 {
323 _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
324 continue;
325 }
326
327 // Does the package have templates?
328 if (file.Template != 0 && file.ParseInfo() == true)
329 {
330 // Check to make sure debconf dependencies are
331 // satisfied
332 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
333 if (file.DepVer != "" &&
334 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
335 file.DepOp,file.DepVer.c_str()
336 ) == false)
337 continue;
338 if (file.PreDepVer != "" &&
339 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
340 file.PreDepOp,file.PreDepVer.c_str()
341 ) == false)
342 continue;
343
344 WriteConfig(file);
345 }
346 }
347
348
349 delete Map;
350 delete DebFile::Cache;
351
352 return !_error->PendingError();
353 }
354 /*}}}*/
355
356 int main(int argc, const char **argv)
357 {
358 #if !defined(__ENVIRONMENT_ASPEN_VERSION_MIN_REQUIRED__) || __ENVIRONMENT_ASPEN_VERSION_MIN_REQUIRED__ < 10200
359 struct nlist nl[2];
360 memset(nl, 0, sizeof(nl));
361 nl[0].n_un.n_name = (char *) "_useMDNSResponder";
362 nlist("/usr/lib/libc.dylib", nl);
363 if (nl[0].n_type != N_UNDF)
364 *(int *) nl[0].n_value = 0;
365 #endif
366
367 CommandLine::Args Args[] = {
368 {'h',"help","help",0},
369 {'v',"version","version",0},
370 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
371 {'c',"config-file",0,CommandLine::ConfigFile},
372 {'o',"option",0,CommandLine::ArbItem},
373 {0,0,0,0}};
374
375 // Set up gettext support
376 setlocale(LC_ALL,"");
377 //textdomain(PACKAGE);
378
379 // Parse the command line and initialize the package library
380 CommandLine CmdL(Args,_config);
381 if (pkgInitConfig(*_config) == false ||
382 CmdL.Parse(argc,argv) == false ||
383 pkgInitSystem(*_config,_system) == false)
384 {
385 _error->DumpErrors();
386 return 100;
387 }
388
389 // See if the help should be shown
390 if (_config->FindB("help") == true ||
391 CmdL.FileSize() == 0)
392 return ShowHelp();
393
394 Go(CmdL);
395
396 // Print any errors or warnings found during operation
397 if (_error->empty() == false)
398 {
399 // This goes to stderr..
400 bool Errors = _error->PendingError();
401 _error->DumpErrors();
402 return Errors == true?100:0;
403 }
404
405 return 0;
406 }