]>
git.saurik.com Git - apt.git/blob - cmdline/apt-extracttemplates.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-extracttemplates.cc,v 1.15 2003/07/26 00:00:11 mdz Exp $
4 /* ######################################################################
6 APT Extract Templates - Program to extract debconf config and template
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
13 ##################################################################### */
15 // Include Files /*{{{*/
18 #include <apt-pkg/init.h>
19 #include <apt-pkg/cmndline.h>
20 #include <apt-pkg/pkgcache.h>
21 #include <apt-pkg/cacheiterators.h>
22 #include <apt-pkg/configuration.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/debfile.h>
28 #include <apt-pkg/deblistparser.h>
29 #include <apt-pkg/error.h>
30 #include <apt-pkg/strutl.h>
31 #include <apt-pkg/fileutl.h>
32 #include <apt-pkg/pkgsystem.h>
33 #include <apt-pkg/dirstream.h>
34 #include <apt-pkg/mmap.h>
36 #include <apt-private/private-cmndline.h>
44 #include "apt-extracttemplates.h"
51 pkgCache
*DebFile::Cache
= 0;
53 // DebFile::DebFile - Construct the DebFile object /*{{{*/
54 // ---------------------------------------------------------------------
56 DebFile::DebFile(const char *debfile
)
57 : File(debfile
, FileFd::ReadOnly
), Control(NULL
), ControlLen(0),
58 DepOp(0), PreDepOp(0), Config(0), Template(0), Which(None
)
62 // DebFile::~DebFile - Destruct the DebFile object /*{{{*/
63 // ---------------------------------------------------------------------
72 // DebFile::GetInstalledVer - Find out the installed version of a pkg /*{{{*/
73 // ---------------------------------------------------------------------
75 string
DebFile::GetInstalledVer(const string
&package
)
77 pkgCache::PkgIterator Pkg
= Cache
->FindPkg(package
);
78 if (Pkg
.end() == false)
80 pkgCache::VerIterator V
= Pkg
.CurrentVer();
90 // DebFile::Go - Start extracting a debian package /*{{{*/
91 // ---------------------------------------------------------------------
97 return Deb
.ExtractTarMember(*this, "control.tar");
100 // DebFile::DoItem examine element in package and mark /*{{{*/
101 // ---------------------------------------------------------------------
103 bool DebFile::DoItem(Item
&I
, int &Fd
)
105 if (strcmp(I
.Name
, "control") == 0)
108 Control
= new char[I
.Size
+3];
109 Control
[I
.Size
] = '\n';
110 Control
[I
.Size
+ 1] = '\n';
111 Control
[I
.Size
+ 2] = '\0';
113 ControlLen
= I
.Size
+ 3;
114 // make it call the Process method below. this is so evil
117 else if (strcmp(I
.Name
, "config") == 0)
120 Config
= new char[I
.Size
+1];
125 else if (strcmp(I
.Name
, "templates") == 0)
128 Template
= new char[I
.Size
+1];
129 Template
[I
.Size
] = 0;
141 // DebFile::Process examine element in package and copy /*{{{*/
142 // ---------------------------------------------------------------------
144 bool DebFile::Process(Item
&/*I*/, const unsigned char *data
,
145 unsigned long long size
, unsigned long long pos
)
150 memcpy(Control
+ pos
, data
, size
);
153 memcpy(Config
+ pos
, data
, size
);
156 memcpy(Template
+ pos
, data
, size
);
158 default: /* throw it away */ ;
163 // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
164 // ---------------------------------------------------------------------
166 bool DebFile::ParseInfo()
168 if (Control
== NULL
) return false;
170 pkgTagSection Section
;
171 if (Section
.Scan(Control
, ControlLen
) == false)
174 Package
= Section
.FindS("Package");
175 Version
= GetInstalledVer(Package
);
177 const char *Start
, *Stop
;
178 if (Section
.Find("Depends", Start
, Stop
) == true)
184 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
185 if (Start
== 0) return false;
192 if (Start
== Stop
) break;
196 if (Section
.Find("Pre-Depends", Start
, Stop
) == true)
202 Start
= debListParser::ParseDepends(Start
, Stop
, P
, V
, Op
);
203 if (Start
== 0) return false;
210 if (Start
== Stop
) break;
217 // ShowHelp - show a short help text /*{{{*/
218 // ---------------------------------------------------------------------
220 static bool ShowHelp(CommandLine
&)
222 ioprintf(cout
,_("%s %s for %s compiled on %s %s\n"),PACKAGE
,PACKAGE_VERSION
,
223 COMMON_ARCH
,__DATE__
,__TIME__
);
225 if (_config
->FindB("version") == true)
229 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
231 "apt-extracttemplates is a tool to extract config and template info\n"
232 "from debian packages\n"
235 " -h This help text\n"
236 " -t Set the temp dir\n"
237 " -c=? Read this configuration file\n"
238 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
242 // WriteFile - write the contents of the passed string to a file /*{{{*/
243 // ---------------------------------------------------------------------
245 static string
WriteFile(const char *package
, const char *prefix
, const char *data
)
249 std::string tempdir
= GetTempDir();
250 snprintf(fn
, sizeof(fn
), "%s/%s.%s.XXXXXX",
251 _config
->Find("APT::ExtractTemplates::TempDir",
252 tempdir
.c_str()).c_str(),
257 int fd
= mkstemp(fn
);
259 _error
->Errno("ofstream::ofstream",_("Unable to mkstemp %s"),fn
);
262 if (!f
.OpenDescriptor(fd
, FileFd::WriteOnly
, FileFd::None
, true))
264 _error
->Errno("ofstream::ofstream",_("Unable to write to %s"),fn
);
267 f
.Write(data
, strlen(data
));
272 // WriteConfig - write out the config data from a debian package file /*{{{*/
273 // ---------------------------------------------------------------------
275 static void WriteConfig(const DebFile
&file
)
277 string templatefile
= WriteFile(file
.Package
.c_str(), "template", file
.Template
);
278 string configscript
= WriteFile(file
.Package
.c_str(), "config", file
.Config
);
280 if (templatefile
.empty() == true || configscript
.empty() == true)
282 cout
<< file
.Package
<< " " << file
.Version
<< " "
283 << templatefile
<< " " << configscript
<< endl
;
286 // InitCache - initialize the package cache /*{{{*/
287 // ---------------------------------------------------------------------
289 static bool Go(CommandLine
&CmdL
)
291 // Initialize the apt cache
295 pkgCacheGenerator::MakeStatusCache(List
,NULL
,&Map
,true);
298 DebFile::Cache
= new pkgCache(Map
);
299 if (_error
->PendingError() == true)
302 // Find out what version of debconf is currently installed
303 string debconfver
= DebFile::GetInstalledVer("debconf");
304 if (debconfver
.empty() == true)
305 return _error
->Error( _("Cannot get debconf version. Is debconf installed?"));
307 // Process each package passsed in
308 for (unsigned int I
= 0; I
!= CmdL
.FileSize(); I
++)
310 // Will pick up the errors later..
311 DebFile
file(CmdL
.FileList
[I
]);
312 if (file
.Go() == false)
314 _error
->Error("Prior errors apply to %s",CmdL
.FileList
[I
]);
318 // Does the package have templates?
319 if (file
.Template
!= 0 && file
.ParseInfo() == true)
321 // Check to make sure debconf dependencies are
323 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
324 if (file
.DepVer
!= "" &&
325 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
326 file
.DepOp
,file
.DepVer
.c_str()
329 if (file
.PreDepVer
!= "" &&
330 DebFile::Cache
->VS
->CheckDep(debconfver
.c_str(),
331 file
.PreDepOp
,file
.PreDepVer
.c_str()
341 delete DebFile::Cache
;
343 return !_error
->PendingError();
346 int main(int argc
, const char **argv
) /*{{{*/
348 CommandLine::Args Args
[] = {
349 {'h',"help","help",0},
350 {'v',"version","version",0},
351 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg
},
352 {'c',"config-file",0,CommandLine::ConfigFile
},
353 {'o',"option",0,CommandLine::ArbItem
},
356 // Set up gettext support
357 setlocale(LC_ALL
,"");
360 // Parse the command line and initialize the package library
361 CommandLine::Dispatch Cmds
[] = {{NULL
, NULL
}};
363 ParseCommandLine(CmdL
, Cmds
, Args
, &_config
, &_system
, argc
, argv
, ShowHelp
);
367 // Print any errors or warnings found during operation
368 if (_error
->empty() == false)
370 // This goes to stderr..
371 bool Errors
= _error
->PendingError();
372 _error
->DumpErrors();
373 return Errors
== true?100:0;