You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

363 lines
9.2 KiB

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