]> git.saurik.com Git - apt.git/blame - cmdline/apt-extracttemplates.cc
Complete Japanese translation
[apt.git] / cmdline / apt-extracttemplates.cc
CommitLineData
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
48using namespace std;
49
4decd43c 50#define TMPDIR "/tmp"
3fc8f685 51
234edfd0
AL
52pkgCache *DebFile::Cache = 0;
53
54// DebFile::DebFile - Construct the DebFile object /*{{{*/
55// ---------------------------------------------------------------------
56/* */
57DebFile::DebFile(const char *debfile)
dcaa1185
DK
58 : File(debfile, FileFd::ReadOnly), Size(0), Control(NULL), ControlLen(0),
59 DepOp(0), PreDepOp(0), Config(0), Template(0), Which(None)
3fc8f685 60{
3fc8f685 61}
234edfd0
AL
62 /*}}}*/
63// DebFile::~DebFile - Destruct the DebFile object /*{{{*/
64// ---------------------------------------------------------------------
65/* */
66DebFile::~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/* */
7e726255 76string DebFile::GetInstalledVer(const string &package)
234edfd0 77{
234edfd0
AL
78 pkgCache::PkgIterator Pkg = Cache->FindPkg(package);
79 if (Pkg.end() == false)
80 {
81 pkgCache::VerIterator V = Pkg.CurrentVer();
7e726255 82 if (V.end() == false)
234edfd0 83 {
7e726255 84 return V.VerStr();
234edfd0
AL
85 }
86 }
3fc8f685 87
7e726255 88 return string();
234edfd0
AL
89}
90 /*}}}*/
91// DebFile::Go - Start extracting a debian package /*{{{*/
92// ---------------------------------------------------------------------
93/* */
94bool DebFile::Go()
95{
96 ARArchive AR(File);
7e726255
AL
97 if (_error->PendingError() == true)
98 return false;
99
234edfd0
AL
100 const ARArchive::Member *Member = AR.FindMember("control.tar.gz");
101 if (Member == 0)
7e726255 102 return _error->Error(_("%s not a valid DEB package."),File.Name().c_str());
234edfd0
AL
103
104 if (File.Seek(Member->Start) == false)
234edfd0 105 return false;
b21c0438 106 ExtractTar Tar(File, Member->Size,"gzip");
234edfd0
AL
107 return Tar.Go(*this);
108}
109 /*}}}*/
110// DebFile::DoItem examine element in package and mark /*{{{*/
111// ---------------------------------------------------------------------
112/* */
113bool 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 {
7e726255 143 // Ignore it
234edfd0
AL
144 Fd = -1;
145 }
146 return true;
147}
148 /*}}}*/
149// DebFile::Process examine element in package and copy /*{{{*/
150// ---------------------------------------------------------------------
151/* */
152bool 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/* */
174bool DebFile::ParseInfo()
175{
176 if (Control == NULL) return false;
7e726255 177
234edfd0
AL
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/* */
7e726255 227int ShowHelp(void)
234edfd0 228{
9179f697 229 ioprintf(cout,_("%s %s for %s compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
5b28c804 230 COMMON_ARCH,__DATE__,__TIME__);
234edfd0 231
7e726255
AL
232 if (_config->FindB("version") == true)
233 return 0;
5cbf1b49 234
7e726255 235 cout <<
234edfd0
AL
236 _("Usage: apt-extracttemplates file1 [file2 ...]\n"
237 "\n"
238 "apt-extracttemplates is a tool to extract config and template info\n"
7e726255
AL
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"
a2884e32 245 " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n");
7e726255 246 return 0;
234edfd0
AL
247}
248 /*}}}*/
249// WriteFile - write the contents of the passed string to a file /*{{{*/
250// ---------------------------------------------------------------------
251/* */
259ca596 252string WriteFile(const char *package, const char *prefix, const char *data)
3fc8f685
AL
253{
254 char fn[512];
255 static int i;
70e6f24e 256 const char *tempdir = NULL;
8eb5af51
AL
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++);
4decd43c
AL
265 FileFd f;
266 if (data == NULL)
267 data = "";
3fc8f685 268
4decd43c 269 if (!f.Open(fn, FileFd::WriteTemp, 0600))
7e726255
AL
270 {
271 _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
272 return string();
273 }
274
4decd43c
AL
275 f.Write(data, strlen(data));
276 f.Close();
7e726255 277 return fn;
3fc8f685 278}
234edfd0
AL
279 /*}}}*/
280// WriteConfig - write out the config data from a debian package file /*{{{*/
281// ---------------------------------------------------------------------
282/* */
283void WriteConfig(const DebFile &file)
3fc8f685 284{
a5eef4f7
AL
285 string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
286 string configscript = WriteFile(file.Package.c_str(), "config", file.Config);
3fc8f685 287
7e726255 288 if (templatefile.empty() == true || configscript.empty() == true)
3fc8f685 289 return;
234edfd0
AL
290 cout << file.Package << " " << file.Version << " "
291 << templatefile << " " << configscript << endl;
234edfd0
AL
292}
293 /*}}}*/
294// InitCache - initialize the package cache /*{{{*/
295// ---------------------------------------------------------------------
296/* */
7e726255
AL
297bool Go(CommandLine &CmdL)
298{
3fc8f685 299 // Initialize the apt cache
7e726255 300 MMap *Map = 0;
3fc8f685
AL
301 pkgSourceList List;
302 List.ReadMainList();
ea4b220b 303 pkgCacheGenerator::MakeStatusCache(List,NULL,&Map,true);
08945efa
AL
304 if (Map == 0)
305 return false;
7e726255
AL
306 DebFile::Cache = new pkgCache(Map);
307 if (_error->PendingError() == true)
308 return false;
5cbf1b49 309
234edfd0 310 // Find out what version of debconf is currently installed
7e726255
AL
311 string debconfver = DebFile::GetInstalledVer("debconf");
312 if (debconfver.empty() == true)
313 return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
3fc8f685 314
234edfd0 315 // Process each package passsed in
5cbf1b49 316 for (unsigned int I = 0; I != CmdL.FileSize(); I++)
3fc8f685 317 {
7e726255 318 // Will pick up the errors later..
5cbf1b49 319 DebFile file(CmdL.FileList[I]);
7e726255 320 if (file.Go() == false)
08945efa
AL
321 {
322 _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
323 continue;
324 }
325
234edfd0 326 // Does the package have templates?
3fc8f685
AL
327 if (file.Template != 0 && file.ParseInfo() == true)
328 {
234edfd0
AL
329 // Check to make sure debconf dependencies are
330 // satisfied
f034a64a 331 // cout << "Check " << file.DepVer << ',' << debconfver << endl;
234edfd0 332 if (file.DepVer != "" &&
418349f0
AL
333 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
334 file.DepOp,file.DepVer.c_str()
335 ) == false)
3fc8f685 336 continue;
234edfd0 337 if (file.PreDepVer != "" &&
418349f0
AL
338 DebFile::Cache->VS->CheckDep(debconfver.c_str(),
339 file.PreDepOp,file.PreDepVer.c_str()
340 ) == false)
3fc8f685
AL
341 continue;
342
234edfd0 343 WriteConfig(file);
3fc8f685
AL
344 }
345 }
346
347
348 delete Map;
349 delete DebFile::Cache;
7e726255
AL
350
351 return !_error->PendingError();
352}
353 /*}}}*/
92fcbfc1 354int main(int argc, const char **argv) /*{{{*/
7e726255
AL
355{
356 CommandLine::Args Args[] = {
357 {'h',"help","help",0},
358 {'v',"version","version",0},
359 {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
360 {'c',"config-file",0,CommandLine::ConfigFile},
361 {'o',"option",0,CommandLine::ArbItem},
362 {0,0,0,0}};
67111687
AL
363
364 // Set up gettext support
365 setlocale(LC_ALL,"");
366 textdomain(PACKAGE);
367
7e726255
AL
368 // Parse the command line and initialize the package library
369 CommandLine CmdL(Args,_config);
370 if (pkgInitConfig(*_config) == false ||
371 CmdL.Parse(argc,argv) == false ||
372 pkgInitSystem(*_config,_system) == false)
373 {
374 _error->DumpErrors();
375 return 100;
376 }
377
378 // See if the help should be shown
379 if (_config->FindB("help") == true ||
380 CmdL.FileSize() == 0)
381 return ShowHelp();
382
383 Go(CmdL);
3fc8f685 384
7e726255
AL
385 // Print any errors or warnings found during operation
386 if (_error->empty() == false)
387 {
388 // This goes to stderr..
389 bool Errors = _error->PendingError();
390 _error->DumpErrors();
391 return Errors == true?100:0;
392 }
393
3fc8f685
AL
394 return 0;
395}
92fcbfc1 396 /*}}}*/