apt-extracttemplates.cc 9.7 KB

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