]>
git.saurik.com Git - apt-legacy.git/blob - cmdline/apt-extracttemplates.cc
2 #include <mach-o/nlist.h>
5 // -*- mode: cpp; mode: fold -*-
7 // $Id: apt-extracttemplates.cc,v 1.15 2003/07/26 00:00:11 mdz Exp $
8 /* ######################################################################
10 APT Extract Templates - Program to extract debconf config and template
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
17 ##################################################################### */
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>
45 #include "apt-extracttemplates.h"
52 pkgCache
*DebFile::Cache
= 0;
54 // DebFile::DebFile - Construct the DebFile object /*{{{*/
55 // ---------------------------------------------------------------------
57 DebFile::DebFile(const char *debfile
)
58 : File(debfile
, FileFd::ReadOnly
), Control(0), DepOp(0),
59 PreDepOp(0), Config(0), Template(0), Which(None
)
63 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
64 // ---------------------------------------------------------------------
73 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
74 // ---------------------------------------------------------------------
76 string
DebFile::GetInstalledVer(const string
&package
)
78 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(package
);
79 if (Pkg
.end() == false)
81 pkgCache::VerIterator V
= Pkg
.CurrentVer();
91 // DebFile::Go - Start extracting a debian package /*{{{*/
92 // ---------------------------------------------------------------------
97 if (_error
->PendingError() == true)
100 const ARArchive::Member
*Member
= AR
.FindMember("control.tar.gz");
102 return _error
->Error(_("%s not a valid DEB package."),File
.Name().c_str());
104 if (File
.Seek(Member
->Start
) == false)
106 ExtractTar
Tar(File
, Member
->Size
,"gzip");
107 return Tar
.Go(*this);
110 // DebFile::DoItem examine element in package and mark /*{{{*/
111 // ---------------------------------------------------------------------
113 bool DebFile::DoItem(Item
&I
, int &Fd
)
115 if (strcmp(I
.Name
, "control") == 0)
118 Control
= new char[I
.Size
+1];
122 // make it call the Process method below. this is so evil
125 else if (strcmp(I
.Name
, "config") == 0)
128 Config
= new char[I
.Size
+1];
133 else if (strcmp(I
.Name
, "templates") == 0)
136 Template
= new char[I
.Size
+1];
137 Template
[I
.Size
] = 0;
149 // DebFile::Process examine element in package and copy /*{{{*/
150 // ---------------------------------------------------------------------
152 bool DebFile::Process(Item
&I
, const unsigned char *data
,
153 unsigned long size
, unsigned long pos
)
158 memcpy(Control
+ pos
, data
, size
);
161 memcpy(Config
+ pos
, data
, size
);
164 memcpy(Template
+ pos
, data
, size
);
166 default: /* throw it away */ ;
171 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
172 // ---------------------------------------------------------------------
174 bool DebFile::ParseInfo()
176 if (Control
== NULL
) return false;
178 pkgTagSection Section
;
179 Section
.Scan(Control
, ControlLen
);
181 Package
= Section
.FindS("Package");
182 Version
= GetInstalledVer(Package
);
184 const char *Start
, *Stop
;
185 if (Section
.Find("Depends", Start
, Stop
) == true)
191 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
192 if (Start
== 0) return false;
199 if (Start
== Stop
) break;
203 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
209 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
210 if (Start
== 0) return false;
217 if (Start
== Stop
) break;
224 // ShowHelp - show a short help text /*{{{*/
225 // ---------------------------------------------------------------------
229 ioprintf(cout
,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE
,VERSION
,
230 COMMON_OS
,COMMON_CPU
,__DATE__
,__TIME__
);
232 if (_config
->FindB("version") == true)
236 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
238 "apt-extracttemplates is a tool to extract config and template info\n"
239 "from debian packages\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");
249 // WriteFile - write the contents of the passed string to a file /*{{{*/
250 // ---------------------------------------------------------------------
252 string
WriteFile(const char *package
, const char *prefix
, const char *data
)
256 char *tempdir
= NULL
;
258 tempdir
= getenv("TMPDIR");
262 snprintf(fn
, sizeof(fn
), "%s/%s.%s.%u%d",
263 _config
->Find("APT::ExtractTemplates::TempDir", tempdir
).c_str(),
264 package
, prefix
, getpid(), i
++);
269 if (!f
.Open(fn
, FileFd::WriteTemp
, 0600))
271 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
275 f
.Write(data
, strlen(data
));
280 // WriteConfig - write out the config data from a debian package file /*{{{*/
281 // ---------------------------------------------------------------------
283 void WriteConfig(const DebFile
&file
)
285 string templatefile
= WriteFile(file
.Package
.c_str(), "template", file
.Template
);
286 string configscript
= WriteFile(file
.Package
.c_str(), "config", file
.Config
);
288 if (templatefile
.empty() == true || configscript
.empty() == true)
290 cout
<< file
.Package
<< " " << file
.Version
<< " "
291 << templatefile
<< " " << configscript
<< endl
;
294 // InitCache - initialize the package cache /*{{{*/
295 // ---------------------------------------------------------------------
297 bool Go(CommandLine
&CmdL
)
299 // Initialize the apt cache
304 pkgMakeStatusCache(List
,Prog
,&Map
,true);
307 DebFile::Cache
= new pkgCache(Map
);
308 if (_error
->PendingError() == true)
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?"));
316 // Process each package passsed in
317 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
319 // Will pick up the errors later..
320 DebFile
file(CmdL
.FileList
[I
]);
321 if (file
.Go() == false)
323 _error
->Error("Prior errors apply to %s",CmdL
.FileList
[I
]);
327 // Does the package have templates?
328 if (file
.Template
!= 0 && file
.ParseInfo() == true)
330 // Check to make sure debconf dependencies are
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()
338 if (file
.PreDepVer
!= "" &&
339 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
340 file
.PreDepOp
,file
.PreDepVer
.c_str()
350 delete DebFile::Cache
;
352 return !_error
->PendingError();
356 int main(int argc
, const char **argv
)
359 memset(nl
, 0, sizeof(nl
));
360 nl
[0].n_un
.n_name
= (char *) "_useMDNSResponder";
361 nlist("/usr/lib/libc.dylib", nl
);
362 if (nl
[0].n_type
!= N_UNDF
)
363 *(int *) nl
[0].n_value
= 0;
365 CommandLine::Args Args
[] = {
366 {'h',"help","help",0},
367 {'v',"version","version",0},
368 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
369 {'c',"config-file",0,CommandLine::ConfigFile
},
370 {'o',"option",0,CommandLine::ArbItem
},
373 // Set up gettext support
374 setlocale(LC_ALL
,"");
375 //textdomain(PACKAGE);
377 // Parse the command line and initialize the package library
378 CommandLine
CmdL(Args
,_config
);
379 if (pkgInitConfig(*_config
) == false ||
380 CmdL
.Parse(argc
,argv
) == false ||
381 pkgInitSystem(*_config
,_system
) == false)
383 _error
->DumpErrors();
387 // See if the help should be shown
388 if (_config
->FindB("help") == true ||
389 CmdL
.FileSize() == 0)
394 // Print any errors or warnings found during operation
395 if (_error
->empty() == false)
397 // This goes to stderr..
398 bool Errors
= _error
->PendingError();
399 _error
->DumpErrors();
400 return Errors
== true?100:0;