apt-extracttemplates.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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/configuration.h>
  18. #include <apt-pkg/progress.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/extracttar.h>
  24. #include <apt-pkg/arfile.h>
  25. #include <apt-pkg/deblistparser.h>
  26. #include <apt-pkg/error.h>
  27. #include <apt-pkg/strutl.h>
  28. #include <apt-pkg/fileutl.h>
  29. #include <apt-pkg/pkgsystem.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <locale.h>
  35. #include <fstream>
  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. 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,"gzip");
  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 compiled on %s %s\n"),PACKAGE,PACKAGE_VERSION,
  209. COMMON_ARCH,__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 arbitrary 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. std::string tempdir = GetTempDir();
  234. snprintf(fn, sizeof(fn), "%s/%s.%s.%u%d",
  235. _config->Find("APT::ExtractTemplates::TempDir",
  236. tempdir.c_str()).c_str(),
  237. package, prefix, getpid(), i++);
  238. FileFd f;
  239. if (data == NULL)
  240. data = "";
  241. if (!f.Open(fn, FileFd::WriteTemp, 0600))
  242. {
  243. _error->Errno("ofstream::ofstream",_("Unable to write to %s"),fn);
  244. return string();
  245. }
  246. f.Write(data, strlen(data));
  247. f.Close();
  248. return fn;
  249. }
  250. /*}}}*/
  251. // WriteConfig - write out the config data from a debian package file /*{{{*/
  252. // ---------------------------------------------------------------------
  253. /* */
  254. void WriteConfig(const DebFile &file)
  255. {
  256. string templatefile = WriteFile(file.Package.c_str(), "template", file.Template);
  257. string configscript = WriteFile(file.Package.c_str(), "config", file.Config);
  258. if (templatefile.empty() == true || configscript.empty() == true)
  259. return;
  260. cout << file.Package << " " << file.Version << " "
  261. << templatefile << " " << configscript << endl;
  262. }
  263. /*}}}*/
  264. // InitCache - initialize the package cache /*{{{*/
  265. // ---------------------------------------------------------------------
  266. /* */
  267. bool Go(CommandLine &CmdL)
  268. {
  269. // Initialize the apt cache
  270. MMap *Map = 0;
  271. pkgSourceList List;
  272. List.ReadMainList();
  273. pkgCacheGenerator::MakeStatusCache(List,NULL,&Map,true);
  274. if (Map == 0)
  275. return false;
  276. DebFile::Cache = new pkgCache(Map);
  277. if (_error->PendingError() == true)
  278. return false;
  279. // Find out what version of debconf is currently installed
  280. string debconfver = DebFile::GetInstalledVer("debconf");
  281. if (debconfver.empty() == true)
  282. return _error->Error( _("Cannot get debconf version. Is debconf installed?"));
  283. // Process each package passsed in
  284. for (unsigned int I = 0; I != CmdL.FileSize(); I++)
  285. {
  286. // Will pick up the errors later..
  287. DebFile file(CmdL.FileList[I]);
  288. if (file.Go() == false)
  289. {
  290. _error->Error("Prior errors apply to %s",CmdL.FileList[I]);
  291. continue;
  292. }
  293. // Does the package have templates?
  294. if (file.Template != 0 && file.ParseInfo() == true)
  295. {
  296. // Check to make sure debconf dependencies are
  297. // satisfied
  298. // cout << "Check " << file.DepVer << ',' << debconfver << endl;
  299. if (file.DepVer != "" &&
  300. DebFile::Cache->VS->CheckDep(debconfver.c_str(),
  301. file.DepOp,file.DepVer.c_str()
  302. ) == false)
  303. continue;
  304. if (file.PreDepVer != "" &&
  305. DebFile::Cache->VS->CheckDep(debconfver.c_str(),
  306. file.PreDepOp,file.PreDepVer.c_str()
  307. ) == false)
  308. continue;
  309. WriteConfig(file);
  310. }
  311. }
  312. delete Map;
  313. delete DebFile::Cache;
  314. return !_error->PendingError();
  315. }
  316. /*}}}*/
  317. int main(int argc, const char **argv) /*{{{*/
  318. {
  319. CommandLine::Args Args[] = {
  320. {'h',"help","help",0},
  321. {'v',"version","version",0},
  322. {'t',"tempdir","APT::ExtractTemplates::TempDir",CommandLine::HasArg},
  323. {'c',"config-file",0,CommandLine::ConfigFile},
  324. {'o',"option",0,CommandLine::ArbItem},
  325. {0,0,0,0}};
  326. // Set up gettext support
  327. setlocale(LC_ALL,"");
  328. textdomain(PACKAGE);
  329. // Parse the command line and initialize the package library
  330. CommandLine CmdL(Args,_config);
  331. if (pkgInitConfig(*_config) == false ||
  332. CmdL.Parse(argc,argv) == false ||
  333. pkgInitSystem(*_config,_system) == false)
  334. {
  335. _error->DumpErrors();
  336. return 100;
  337. }
  338. // See if the help should be shown
  339. if (_config->FindB("help") == true ||
  340. CmdL.FileSize() == 0)
  341. return ShowHelp();
  342. Go(CmdL);
  343. // Print any errors or warnings found during operation
  344. if (_error->empty() == false)
  345. {
  346. // This goes to stderr..
  347. bool Errors = _error->PendingError();
  348. _error->DumpErrors();
  349. return Errors == true?100:0;
  350. }
  351. return 0;
  352. }
  353. /*}}}*/