apt-extracttemplates.cc 9.5 KB

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