apt-extracttemplates.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-extracttemplates.cc,v 1.14 2003/01/11 07:18:44 jgg 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 <apt-pkg/init.h>
  14. #include <apt-pkg/cmndline.h>
  15. #include <apt-pkg/pkgcache.h>
  16. #include <apt-pkg/configuration.h>
  17. #include <apt-pkg/progress.h>
  18. #include <apt-pkg/sourcelist.h>
  19. #include <apt-pkg/pkgcachegen.h>
  20. #include <apt-pkg/version.h>
  21. #include <apt-pkg/tagfile.h>
  22. #include <apt-pkg/extracttar.h>
  23. #include <apt-pkg/arfile.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 <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. #include <fstream>
  33. #include <locale.h>
  34. #include <config.h>
  35. #include <apti18n.h>
  36. #include "apt-extracttemplates.h"
  37. /*}}}*/
  38. using namespace std;
  39. #define TMPDIR "/tmp"
  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), Control(0), DepOp(0),
  46. 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. ARArchive AR(File);
  83. if (_error->PendingError() == true)
  84. return false;
  85. const ARArchive::Member *Member = AR.FindMember("control.tar.gz");
  86. if (Member == 0)
  87. return _error->Error(_("%s not a valid DEB package."),File.Name().c_str());
  88. if (File.Seek(Member->Start) == false)
  89. return false;
  90. ExtractTar Tar(File, Member->Size);
  91. return Tar.Go(*this);
  92. }
  93. /*}}}*/
  94. // DebFile::DoItem examine element in package and mark /*{{{*/
  95. // ---------------------------------------------------------------------
  96. /* */
  97. bool DebFile::DoItem(Item &I, int &Fd)
  98. {
  99. if (strcmp(I.Name, "control") == 0)
  100. {
  101. delete [] Control;
  102. Control = new char[I.Size+1];
  103. Control[I.Size] = 0;
  104. Which = IsControl;
  105. ControlLen = I.Size;
  106. // make it call the Process method below. this is so evil
  107. Fd = -2;
  108. }
  109. else if (strcmp(I.Name, "config") == 0)
  110. {
  111. delete [] Config;
  112. Config = new char[I.Size+1];
  113. Config[I.Size] = 0;
  114. Which = IsConfig;
  115. Fd = -2;
  116. }
  117. else if (strcmp(I.Name, "templates") == 0)
  118. {
  119. delete [] Template;
  120. Template = new char[I.Size+1];
  121. Template[I.Size] = 0;
  122. Which = IsTemplate;
  123. Fd = -2;
  124. }
  125. else
  126. {
  127. // Ignore it
  128. Fd = -1;
  129. }
  130. return true;
  131. }
  132. /*}}}*/
  133. // DebFile::Process examine element in package and copy /*{{{*/
  134. // ---------------------------------------------------------------------
  135. /* */
  136. bool DebFile::Process(Item &I, const unsigned char *data,
  137. unsigned long size, unsigned long pos)
  138. {
  139. switch (Which)
  140. {
  141. case IsControl:
  142. memcpy(Control + pos, data, size);
  143. break;
  144. case IsConfig:
  145. memcpy(Config + pos, data, size);
  146. break;
  147. case IsTemplate:
  148. memcpy(Template + pos, data, size);
  149. break;
  150. default: /* throw it away */ ;
  151. }
  152. return true;
  153. }
  154. /*}}}*/
  155. // DebFile::ParseInfo - Parse control file for dependency info /*{{{*/
  156. // ---------------------------------------------------------------------
  157. /* */
  158. bool DebFile::ParseInfo()
  159. {
  160. if (Control == NULL) return false;
  161. pkgTagSection Section;
  162. Section.Scan(Control, ControlLen);
  163. Package = Section.FindS("Package");
  164. Version = GetInstalledVer(Package);
  165. const char *Start, *Stop;
  166. if (Section.Find("Depends", Start, Stop) == true)
  167. {
  168. while (1)
  169. {
  170. string P, V;
  171. unsigned int Op;
  172. Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
  173. if (Start == 0) return false;
  174. if (P == "debconf")
  175. {
  176. DepVer = V;
  177. DepOp = Op;
  178. break;
  179. }
  180. if (Start == Stop) break;
  181. }
  182. }
  183. if (Section.Find("Pre-Depends", Start, Stop) == true)
  184. {
  185. while (1)
  186. {
  187. string P, V;
  188. unsigned int Op;
  189. Start = debListParser::ParseDepends(Start, Stop, P, V, Op);
  190. if (Start == 0) return false;
  191. if (P == "debconf")
  192. {
  193. PreDepVer = V;
  194. PreDepOp = Op;
  195. break;
  196. }
  197. if (Start == Stop) break;
  198. }
  199. }
  200. return true;
  201. }
  202. /*}}}*/
  203. // ShowHelp - show a short help text /*{{{*/
  204. // ---------------------------------------------------------------------
  205. /* */
  206. int ShowHelp(void)
  207. {
  208. ioprintf(cout,_("%s %s for %s %s compiled on %s %s\n"),PACKAGE,VERSION,
  209. COMMON_OS,COMMON_CPU,__DATE__,__TIME__);
  210. if (_config->FindB("version") == true)
  211. return 0;
  212. cout <<
  213. _("Usage: apt-extracttemplates file1 [file2 ...]\n"
  214. "\n"
  215. "apt-extracttemplates is a tool to extract config and template info\n"
  216. "from debian packages\n"
  217. "\n"
  218. "Options:\n"
  219. " -h This help text\n"
  220. " -t Set the temp dir\n"
  221. " -c=? Read this configuration file\n"
  222. " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp\n");
  223. return 0;
  224. }
  225. /*}}}*/
  226. // WriteFile - write the contents of the passed string to a file /*{{{*/
  227. // ---------------------------------------------------------------------
  228. /* */
  229. string WriteFile(const char *package, const char *prefix, const char *data)
  230. {
  231. char fn[512];
  232. static int i;
  233. snprintf(fn, sizeof(fn), "%s/%s.%s.%u%d", _config->Find("APT::ExtractTemplates::TempDir", TMPDIR).c_str(), package, prefix, getpid(), i++);
  234. FileFd f;
  235. if (data == NULL)
  236. data = "";
  237. if (!f.Open(fn, FileFd::WriteTemp, 0600))
  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. 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. bool Go(CommandLine &CmdL)
  264. {
  265. // Initialize the apt cache
  266. MMap *Map = 0;
  267. pkgSourceList List;
  268. List.ReadMainList();
  269. OpProgress Prog;
  270. pkgMakeStatusCache(List,Prog,&Map,true);
  271. if (Map == 0)
  272. return false;
  273. DebFile::Cache = new pkgCache(Map);
  274. if (_error->PendingError() == true)
  275. return false;
  276. // Find out what version of debconf is currently installed
  277. string debconfver = DebFile::GetInstalledVer("debconf");
  278. if (debconfver.empty() == true)
  279. return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
  280. // Process each package passsed in
  281. for (unsigned int I = 0; I != CmdL.FileSize(); I++)
  282. {
  283. // Will pick up the errors later..
  284. DebFile file(CmdL.FileList[I]);
  285. if (file.Go() == false)
  286. {
  287. _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
  288. continue;
  289. }
  290. // Does the package have templates?
  291. if (file.Template != 0 && file.ParseInfo() == true)
  292. {
  293. // Check to make sure debconf dependencies are
  294. // satisfied
  295. // cout << "Check " << file.DepVer << ',' << debconfver << endl;
  296. if (file.DepVer != "" &&
  297. DebFile::Cache->VS->CheckDep(debconfver.c_str(),
  298. file.DepOp,file.DepVer.c_str()
  299. ) == false)
  300. continue;
  301. if (file.PreDepVer != "" &&
  302. DebFile::Cache->VS->CheckDep(debconfver.c_str(),
  303. file.PreDepOp,file.PreDepVer.c_str()
  304. ) == false)
  305. continue;
  306. WriteConfig(file);
  307. }
  308. }
  309. delete Map;
  310. delete DebFile::Cache;
  311. return !_error->PendingError();
  312. }
  313. /*}}}*/
  314. int main(int argc, const char **argv)
  315. {
  316. CommandLine::Args Args[] = {
  317. {'h',"help","help",0},
  318. {'v',"version","version",0},
  319. {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
  320. {'c',"config-file",0,CommandLine::ConfigFile},
  321. {'o',"option",0,CommandLine::ArbItem},
  322. {0,0,0,0}};
  323. // Set up gettext support
  324. setlocale(LC_ALL,"");
  325. textdomain(PACKAGE);
  326. // Parse the command line and initialize the package library
  327. CommandLine CmdL(Args,_config);
  328. if (pkgInitConfig(*_config) == false ||
  329. CmdL.Parse(argc,argv) == false ||
  330. pkgInitSystem(*_config,_system) == false)
  331. {
  332. _error->DumpErrors();
  333. return 100;
  334. }
  335. // See if the help should be shown
  336. if (_config->FindB("help") == true ||
  337. CmdL.FileSize() == 0)
  338. return ShowHelp();
  339. Go(CmdL);
  340. // Print any errors or warnings found during operation
  341. if (_error->empty() == false)
  342. {
  343. // This goes to stderr..
  344. bool Errors = _error->PendingError();
  345. _error->DumpErrors();
  346. return Errors == true?100:0;
  347. }
  348. return 0;
  349. }