apt-extracttemplates.cc 9.9 KB

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