apt-extracttemplates.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #define _GNU_SOURCE
  6. #include <getopt.h>
  7. #include <wait.h>
  8. #include <fstream.h>
  9. #include <apt-pkg/init.h>
  10. #if APT_PKG_MAJOR >= 3
  11. #define APT_COMPATIBILITY 986
  12. #include <apt-pkg/debversion.h>
  13. #endif
  14. #include <apt-pkg/pkgcache.h>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/progress.h>
  17. #include <apt-pkg/sourcelist.h>
  18. #include <apt-pkg/pkgcachegen.h>
  19. #include <apt-pkg/version.h>
  20. #include "debfile.h"
  21. #define TMPDIR "/var/lib/debconf/"
  22. #define STR(x) (x ? x : "")
  23. //#define TMPDIR "tmp/"
  24. void help(void)
  25. {
  26. fprintf(stderr, "apt-extracttemplates deb [deb]\n");
  27. exit(0);
  28. }
  29. char *writefile(const char *prefix, const char *data)
  30. {
  31. char fn[512];
  32. static int i;
  33. snprintf(fn, sizeof(fn), "%s%s.%u%d", TMPDIR, prefix, getpid(), i++);
  34. if (data == NULL) data = "";
  35. ofstream ofs(fn);
  36. if (!ofs) return NULL;
  37. ofs << data;
  38. ofs.close();
  39. return strdup(fn);
  40. }
  41. void writeconfig(const DebFile &file)
  42. {
  43. char *templatefile = writefile("template", file.Template);
  44. char *configscript = writefile("config", file.Config);
  45. if (templatefile == 0 || configscript == 0)
  46. {
  47. fprintf(stderr, "Cannot write config script or templates\n");
  48. return;
  49. }
  50. printf("%s %s %s %s\n",
  51. STR(file.Package), // Package
  52. STR(file.Version), // Version
  53. templatefile, // Template
  54. configscript // Config
  55. );
  56. }
  57. void init(MMap *&Map, pkgCache *&Cache)
  58. {
  59. // Initialize the apt cache
  60. if (pkgInitConfig(*_config) == false || pkgInitSystem(*_config, _system) == false)
  61. {
  62. fprintf(stderr, "Cannot initialize apt cache\n");
  63. return;
  64. }
  65. pkgSourceList List;
  66. List.ReadMainList();
  67. OpProgress Prog;
  68. pkgMakeStatusCache(List,Prog,&Map,true);
  69. Cache = new pkgCache(Map);
  70. }
  71. int main(int argc, char **argv, char **env)
  72. {
  73. int idx = 0;
  74. char **debs = 0;
  75. int numdebs = 0;
  76. MMap *Map = 0;
  77. const char *debconfver = NULL;
  78. init(Map, DebFile::Cache);
  79. if (Map == 0 || DebFile::Cache == 0)
  80. {
  81. fprintf(stderr, "Cannot initialize APT cache\n");
  82. return 1;
  83. }
  84. debconfver = DebFile::GetInstalledVer("debconf");
  85. if (debconfver == NULL)
  86. {
  87. fprintf(stderr, "Cannot get debconf version. Is debconf installed?\n");
  88. return 1;
  89. }
  90. numdebs = argc - 1;
  91. debs = new char *[numdebs];
  92. memcpy(debs, &argv[1], sizeof(char *) * numdebs);
  93. if (numdebs < 1)
  94. {
  95. fprintf(stderr, "apt-extracttemplates foo.deb [...]\n");
  96. return 0;
  97. }
  98. for (idx = 0; idx < numdebs; idx++)
  99. {
  100. DebFile file(debs[idx]);
  101. if (file.Go() == false)
  102. {
  103. fprintf(stderr, "Cannot read %s\n", debs[idx]);
  104. continue;
  105. }
  106. if (file.Template != 0 && file.ParseInfo() == true)
  107. {
  108. if (file.DepVer != 0 && *file.DepVer != 0 &&
  109. pkgCheckDep(file.DepVer,
  110. debconfver, file.DepOp) == false)
  111. continue;
  112. if (file.PreDepVer != 0 && *file.PreDepVer != 0 &&
  113. pkgCheckDep(file.PreDepVer,
  114. debconfver, file.PreDepOp) == false)
  115. continue;
  116. writeconfig(file);
  117. }
  118. }
  119. delete Map;
  120. delete DebFile::Cache;
  121. return 0;
  122. }