debindexfile.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: debindexfile.cc,v 1.5.2.3 2004/01/04 19:11:00 mdz Exp $
  4. /* ######################################################################
  5. Debian Specific sources.list types and the three sorts of Debian
  6. index files.
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <config.h>
  11. #include <apt-pkg/debindexfile.h>
  12. #include <apt-pkg/debsrcrecords.h>
  13. #include <apt-pkg/deblistparser.h>
  14. #include <apt-pkg/debrecords.h>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/error.h>
  17. #include <apt-pkg/fileutl.h>
  18. #include <apt-pkg/indexfile.h>
  19. #include <apt-pkg/pkgcache.h>
  20. #include <apt-pkg/cacheiterators.h>
  21. #include <apt-pkg/pkgrecords.h>
  22. #include <apt-pkg/srcrecords.h>
  23. #include <stdio.h>
  24. #include <iostream>
  25. #include <string>
  26. #include <sstream>
  27. #include <sys/stat.h>
  28. /*}}}*/
  29. // Sources Index /*{{{*/
  30. debSourcesIndex::debSourcesIndex(IndexTarget const &Target,bool const Trusted) :
  31. pkgDebianIndexTargetFile(Target, Trusted), d(NULL)
  32. {
  33. }
  34. std::string debSourcesIndex::SourceInfo(pkgSrcRecords::Parser const &Record,
  35. pkgSrcRecords::File const &File) const
  36. {
  37. // The result looks like: http://foo/debian/ stable/main src 1.1.1 (dsc)
  38. std::string Res = Target.Description;
  39. Res.erase(Target.Description.rfind(' '));
  40. Res += " ";
  41. Res += Record.Package();
  42. Res += " ";
  43. Res += Record.Version();
  44. if (File.Type.empty() == false)
  45. Res += " (" + File.Type + ")";
  46. return Res;
  47. }
  48. pkgSrcRecords::Parser *debSourcesIndex::CreateSrcParser() const
  49. {
  50. std::string const SourcesURI = IndexFileName();
  51. if (FileExists(SourcesURI))
  52. return new debSrcRecordParser(SourcesURI, this);
  53. return NULL;
  54. }
  55. bool debSourcesIndex::OpenListFile(FileFd &, std::string const &)
  56. {
  57. return true;
  58. }
  59. pkgCacheListParser * debSourcesIndex::CreateListParser(FileFd &)
  60. {
  61. return NULL;
  62. }
  63. uint8_t debSourcesIndex::GetIndexFlags() const
  64. {
  65. return 0;
  66. }
  67. /*}}}*/
  68. // Packages Index /*{{{*/
  69. debPackagesIndex::debPackagesIndex(IndexTarget const &Target, bool const Trusted) :
  70. pkgDebianIndexTargetFile(Target, Trusted), d(NULL)
  71. {
  72. }
  73. std::string debPackagesIndex::ArchiveInfo(pkgCache::VerIterator const &Ver) const
  74. {
  75. std::string Res = Target.Description;
  76. Res.erase(Target.Description.rfind(' '));
  77. Res += " ";
  78. Res += Ver.ParentPkg().Name();
  79. Res += " ";
  80. std::string const Dist = Target.Option(IndexTarget::RELEASE);
  81. if (Dist.empty() == false && Dist[Dist.size() - 1] != '/')
  82. Res.append(Ver.Arch()).append(" ");
  83. Res += Ver.VerStr();
  84. return Res;
  85. }
  86. uint8_t debPackagesIndex::GetIndexFlags() const
  87. {
  88. return 0;
  89. }
  90. /*}}}*/
  91. // Translation-* Index /*{{{*/
  92. debTranslationsIndex::debTranslationsIndex(IndexTarget const &Target) :
  93. pkgDebianIndexTargetFile(Target, true), d(NULL)
  94. {}
  95. bool debTranslationsIndex::HasPackages() const
  96. {
  97. return Exists();
  98. }
  99. bool debTranslationsIndex::OpenListFile(FileFd &Pkg, std::string const &FileName)
  100. {
  101. if (FileExists(FileName))
  102. return pkgDebianIndexTargetFile::OpenListFile(Pkg, FileName);
  103. return true;
  104. }
  105. uint8_t debTranslationsIndex::GetIndexFlags() const
  106. {
  107. return pkgCache::Flag::NotSource | pkgCache::Flag::NoPackages;
  108. }
  109. std::string debTranslationsIndex::GetArchitecture() const
  110. {
  111. return std::string();
  112. }
  113. pkgCacheListParser * debTranslationsIndex::CreateListParser(FileFd &Pkg)
  114. {
  115. if (Pkg.IsOpen() == false)
  116. return NULL;
  117. _error->PushToStack();
  118. pkgCacheListParser * const Parser = new debTranslationsParser(&Pkg);
  119. bool const newError = _error->PendingError();
  120. _error->MergeWithStack();
  121. return newError ? NULL : Parser;
  122. }
  123. /*}}}*/
  124. // dpkg/status Index /*{{{*/
  125. debStatusIndex::debStatusIndex(std::string const &File) : pkgDebianIndexRealFile(File, true), d(NULL)
  126. {
  127. }
  128. std::string debStatusIndex::GetArchitecture() const
  129. {
  130. return std::string();
  131. }
  132. std::string debStatusIndex::GetComponent() const
  133. {
  134. return "now";
  135. }
  136. uint8_t debStatusIndex::GetIndexFlags() const
  137. {
  138. return pkgCache::Flag::NotSource;
  139. }
  140. pkgCacheListParser * debStatusIndex::CreateListParser(FileFd &Pkg)
  141. {
  142. if (Pkg.IsOpen() == false)
  143. return NULL;
  144. _error->PushToStack();
  145. pkgCacheListParser * const Parser = new debStatusListParser(&Pkg);
  146. bool const newError = _error->PendingError();
  147. _error->MergeWithStack();
  148. return newError ? NULL : Parser;
  149. }
  150. /*}}}*/
  151. // DebPkgFile Index - a single .deb file as an index /*{{{*/
  152. debDebPkgFileIndex::debDebPkgFileIndex(std::string const &DebFile)
  153. : pkgDebianIndexRealFile(DebFile, true), d(NULL), DebFile(DebFile)
  154. {
  155. }
  156. bool debDebPkgFileIndex::GetContent(std::ostream &content, std::string const &debfile)
  157. {
  158. struct stat Buf;
  159. if (stat(debfile.c_str(), &Buf) != 0)
  160. return false;
  161. // get the control data out of the deb file via dpkg-deb -I
  162. std::string dpkg = _config->Find("Dir::Bin::dpkg","dpkg-deb");
  163. std::vector<const char *> Args;
  164. Args.push_back(dpkg.c_str());
  165. Args.push_back("-I");
  166. Args.push_back(debfile.c_str());
  167. Args.push_back("control");
  168. Args.push_back(NULL);
  169. FileFd PipeFd;
  170. pid_t Child;
  171. if(Popen((const char**)&Args[0], PipeFd, Child, FileFd::ReadOnly) == false)
  172. return _error->Error("Popen failed");
  173. content << "Filename: " << debfile << "\n";
  174. content << "Size: " << Buf.st_size << "\n";
  175. bool first_line_seen = false;
  176. char buffer[1024];
  177. do {
  178. unsigned long long actual = 0;
  179. if (PipeFd.Read(buffer, sizeof(buffer)-1, &actual) == false)
  180. return _error->Errno("read", "Failed to read dpkg pipe");
  181. if (actual == 0)
  182. break;
  183. buffer[actual] = '\0';
  184. char const * b = buffer;
  185. if (first_line_seen == false)
  186. {
  187. for (; *b != '\0' && (*b == '\n' || *b == '\r'); ++b)
  188. /* skip over leading newlines */;
  189. if (*b == '\0')
  190. continue;
  191. first_line_seen = true;
  192. }
  193. content << b;
  194. } while(true);
  195. ExecWait(Child, "Popen");
  196. return true;
  197. }
  198. bool debDebPkgFileIndex::OpenListFile(FileFd &Pkg, std::string const &FileName)
  199. {
  200. // write the control data to a tempfile
  201. if (GetTempFile("deb-file-" + flNotDir(FileName), true, &Pkg) == NULL)
  202. return false;
  203. std::ostringstream content;
  204. if (GetContent(content, FileName) == false)
  205. return false;
  206. std::string const contentstr = content.str();
  207. if (contentstr.empty())
  208. return true;
  209. if (Pkg.Write(contentstr.c_str(), contentstr.length()) == false || Pkg.Seek(0) == false)
  210. return false;
  211. return true;
  212. }
  213. pkgCacheListParser * debDebPkgFileIndex::CreateListParser(FileFd &Pkg)
  214. {
  215. if (Pkg.IsOpen() == false)
  216. return NULL;
  217. _error->PushToStack();
  218. pkgCacheListParser * const Parser = new debDebFileParser(&Pkg, DebFile);
  219. bool const newError = _error->PendingError();
  220. _error->MergeWithStack();
  221. return newError ? NULL : Parser;
  222. }
  223. uint8_t debDebPkgFileIndex::GetIndexFlags() const
  224. {
  225. return pkgCache::Flag::LocalSource;
  226. }
  227. std::string debDebPkgFileIndex::GetArchitecture() const
  228. {
  229. return std::string();
  230. }
  231. std::string debDebPkgFileIndex::GetComponent() const
  232. {
  233. return "local-deb";
  234. }
  235. pkgCache::PkgFileIterator debDebPkgFileIndex::FindInCache(pkgCache &Cache) const
  236. {
  237. std::string const FileName = IndexFileName();
  238. pkgCache::PkgFileIterator File = Cache.FileBegin();
  239. for (; File.end() == false; ++File)
  240. {
  241. if (File.FileName() == NULL || FileName != File.FileName())
  242. continue;
  243. // we can't do size checks here as file size != content size
  244. return File;
  245. }
  246. return File;
  247. }
  248. std::string debDebPkgFileIndex::ArchiveInfo_impl(pkgCache::VerIterator const &Ver) const
  249. {
  250. std::string Res = IndexFileName() + " ";
  251. Res.append(Ver.ParentPkg().Name()).append(" ");
  252. Res.append(Ver.Arch()).append(" ");
  253. Res.append(Ver.VerStr());
  254. return Res;
  255. }
  256. /*}}}*/
  257. // DscFile Index - a single .dsc file as an index /*{{{*/
  258. debDscFileIndex::debDscFileIndex(std::string const &DscFile)
  259. : pkgDebianIndexRealFile(DscFile, true), d(NULL)
  260. {
  261. }
  262. pkgSrcRecords::Parser *debDscFileIndex::CreateSrcParser() const
  263. {
  264. if (Exists() == false)
  265. return NULL;
  266. return new debDscRecordParser(File, this);
  267. }
  268. std::string debDscFileIndex::GetComponent() const
  269. {
  270. return "local-dsc";
  271. }
  272. std::string debDscFileIndex::GetArchitecture() const
  273. {
  274. return "source";
  275. }
  276. uint8_t debDscFileIndex::GetIndexFlags() const
  277. {
  278. return pkgCache::Flag::LocalSource;
  279. }
  280. /*}}}*/
  281. // ControlFile Index - a directory with a debian/control file /*{{{*/
  282. std::string debDebianSourceDirIndex::GetComponent() const
  283. {
  284. return "local-control";
  285. }
  286. /*}}}*/
  287. // Index File types for Debian /*{{{*/
  288. class APT_HIDDEN debIFTypeSrc : public pkgIndexFile::Type
  289. {
  290. public:
  291. debIFTypeSrc() {Label = "Debian Source Index";};
  292. };
  293. class APT_HIDDEN debIFTypePkg : public pkgIndexFile::Type
  294. {
  295. public:
  296. virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
  297. {
  298. return new debRecordParser(File.FileName(),*File.Cache());
  299. };
  300. debIFTypePkg() {Label = "Debian Package Index";};
  301. };
  302. class APT_HIDDEN debIFTypeTrans : public debIFTypePkg
  303. {
  304. public:
  305. debIFTypeTrans() {Label = "Debian Translation Index";};
  306. };
  307. class APT_HIDDEN debIFTypeStatus : public pkgIndexFile::Type
  308. {
  309. public:
  310. virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
  311. {
  312. return new debRecordParser(File.FileName(),*File.Cache());
  313. };
  314. debIFTypeStatus() {Label = "Debian dpkg status file";};
  315. };
  316. class APT_HIDDEN debIFTypeDebPkgFile : public pkgIndexFile::Type
  317. {
  318. public:
  319. virtual pkgRecords::Parser *CreatePkgParser(pkgCache::PkgFileIterator const &File) const APT_OVERRIDE
  320. {
  321. return new debDebFileRecordParser(File.FileName());
  322. };
  323. debIFTypeDebPkgFile() {Label = "Debian deb file";};
  324. };
  325. class APT_HIDDEN debIFTypeDscFile : public pkgIndexFile::Type
  326. {
  327. public:
  328. virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string const &DscFile) const APT_OVERRIDE
  329. {
  330. return new debDscRecordParser(DscFile, NULL);
  331. };
  332. debIFTypeDscFile() {Label = "Debian dsc file";};
  333. };
  334. class APT_HIDDEN debIFTypeDebianSourceDir : public pkgIndexFile::Type
  335. {
  336. public:
  337. virtual pkgSrcRecords::Parser *CreateSrcPkgParser(std::string const &SourceDir) const APT_OVERRIDE
  338. {
  339. return new debDscRecordParser(SourceDir + std::string("/debian/control"), NULL);
  340. };
  341. debIFTypeDebianSourceDir() {Label = "Debian control file";};
  342. };
  343. APT_HIDDEN debIFTypeSrc _apt_Src;
  344. APT_HIDDEN debIFTypePkg _apt_Pkg;
  345. APT_HIDDEN debIFTypeTrans _apt_Trans;
  346. APT_HIDDEN debIFTypeStatus _apt_Status;
  347. APT_HIDDEN debIFTypeDebPkgFile _apt_DebPkgFile;
  348. // file based pseudo indexes
  349. APT_HIDDEN debIFTypeDscFile _apt_DscFile;
  350. APT_HIDDEN debIFTypeDebianSourceDir _apt_DebianSourceDir;
  351. const pkgIndexFile::Type *debSourcesIndex::GetType() const
  352. {
  353. return &_apt_Src;
  354. }
  355. const pkgIndexFile::Type *debPackagesIndex::GetType() const
  356. {
  357. return &_apt_Pkg;
  358. }
  359. const pkgIndexFile::Type *debTranslationsIndex::GetType() const
  360. {
  361. return &_apt_Trans;
  362. }
  363. const pkgIndexFile::Type *debStatusIndex::GetType() const
  364. {
  365. return &_apt_Status;
  366. }
  367. const pkgIndexFile::Type *debDebPkgFileIndex::GetType() const
  368. {
  369. return &_apt_DebPkgFile;
  370. }
  371. const pkgIndexFile::Type *debDscFileIndex::GetType() const
  372. {
  373. return &_apt_DscFile;
  374. }
  375. const pkgIndexFile::Type *debDebianSourceDirIndex::GetType() const
  376. {
  377. return &_apt_DebianSourceDir;
  378. }
  379. /*}}}*/
  380. debStatusIndex::~debStatusIndex() {}
  381. debPackagesIndex::~debPackagesIndex() {}
  382. debTranslationsIndex::~debTranslationsIndex() {}
  383. debSourcesIndex::~debSourcesIndex() {}
  384. debDebPkgFileIndex::~debDebPkgFileIndex() {}
  385. debDscFileIndex::~debDscFileIndex() {}