srcrecords.cc 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: srcrecords.cc,v 1.7.2.2 2003/12/26 16:27:34 mdz Exp $
  4. /* ######################################################################
  5. Source Package Records - Allows access to source package records
  6. Parses and allows access to the list of source records and searching by
  7. source name on that list.
  8. ##################################################################### */
  9. /*}}}*/
  10. // Include Files /*{{{*/
  11. #include<config.h>
  12. #include <apt-pkg/srcrecords.h>
  13. #include <apt-pkg/debsrcrecords.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/sourcelist.h>
  16. #include <apt-pkg/metaindex.h>
  17. #include <apt-pkg/indexfile.h>
  18. #include <apt-pkg/macros.h>
  19. #include <string.h>
  20. #include <string>
  21. #include <vector>
  22. #include <apti18n.h>
  23. /*}}}*/
  24. // SrcRecords::pkgSrcRecords - Constructor /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* Open all the source index files */
  27. pkgSrcRecords::pkgSrcRecords(pkgSourceList &List) : d(NULL), Files(0)
  28. {
  29. for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); ++I)
  30. {
  31. std::vector<pkgIndexFile *> *Indexes = (*I)->GetIndexFiles();
  32. for (std::vector<pkgIndexFile *>::const_iterator J = Indexes->begin();
  33. J != Indexes->end(); ++J)
  34. {
  35. _error->PushToStack();
  36. Parser* P = (*J)->CreateSrcParser();
  37. bool const newError = _error->PendingError();
  38. _error->MergeWithStack();
  39. if (newError)
  40. return;
  41. if (P != 0)
  42. Files.push_back(P);
  43. }
  44. }
  45. // Doesn't work without any source index files
  46. if (Files.empty() == true)
  47. {
  48. _error->Error(_("You must put some 'source' URIs"
  49. " in your sources.list"));
  50. return;
  51. }
  52. Restart();
  53. }
  54. /*}}}*/
  55. // SrcRecords::~pkgSrcRecords - Destructor /*{{{*/
  56. // ---------------------------------------------------------------------
  57. /* */
  58. pkgSrcRecords::~pkgSrcRecords()
  59. {
  60. // Blow away all the parser objects
  61. for(std::vector<Parser*>::iterator I = Files.begin(); I != Files.end(); ++I)
  62. delete *I;
  63. }
  64. /*}}}*/
  65. // SrcRecords::Restart - Restart the search /*{{{*/
  66. // ---------------------------------------------------------------------
  67. /* Return all of the parsers to their starting position */
  68. bool pkgSrcRecords::Restart()
  69. {
  70. Current = Files.begin();
  71. for (std::vector<Parser*>::iterator I = Files.begin();
  72. I != Files.end(); ++I)
  73. if ((*I)->Offset() != 0)
  74. (*I)->Restart();
  75. return true;
  76. }
  77. /*}}}*/
  78. // SrcRecords::Step - Step to the next Source Record /*{{{*/
  79. // ---------------------------------------------------------------------
  80. /* Step to the next source package record */
  81. const pkgSrcRecords::Parser* pkgSrcRecords::Step()
  82. {
  83. if (Current == Files.end())
  84. return 0;
  85. // Step to the next record, possibly switching files
  86. while ((*Current)->Step() == false)
  87. {
  88. ++Current;
  89. if (Current == Files.end())
  90. return 0;
  91. }
  92. return *Current;
  93. }
  94. /*}}}*/
  95. // SrcRecords::Find - Find the first source package with the given name /*{{{*/
  96. // ---------------------------------------------------------------------
  97. /* This searches on both source package names and output binary names and
  98. returns the first found. A 'cursor' like system is used to allow this
  99. function to be called multiple times to get successive entries */
  100. pkgSrcRecords::Parser *pkgSrcRecords::Find(const char *Package,bool const &SrcOnly)
  101. {
  102. while (true)
  103. {
  104. if(Step() == 0)
  105. return 0;
  106. // Source name hit
  107. if ((*Current)->Package() == Package)
  108. return *Current;
  109. if (SrcOnly == true)
  110. continue;
  111. // Check for a binary hit
  112. const char **I = (*Current)->Binaries();
  113. for (; I != 0 && *I != 0; ++I)
  114. if (strcmp(Package,*I) == 0)
  115. return *Current;
  116. }
  117. }
  118. /*}}}*/
  119. // Parser::BuildDepType - Convert a build dep to a string /*{{{*/
  120. // ---------------------------------------------------------------------
  121. /* */
  122. const char *pkgSrcRecords::Parser::BuildDepType(unsigned char const &Type)
  123. {
  124. const char *fields[] = {"Build-Depends",
  125. "Build-Depends-Indep",
  126. "Build-Conflicts",
  127. "Build-Conflicts-Indep",
  128. "Build-Depends-Arch",
  129. "Build-Conflicts-Arch"};
  130. if (unlikely(Type >= sizeof(fields)/sizeof(fields[0])))
  131. return "";
  132. return fields[Type];
  133. }
  134. /*}}}*/
  135. bool pkgSrcRecords::Parser::Files2(std::vector<pkgSrcRecords::File2> &F2)/*{{{*/
  136. {
  137. debSrcRecordParser * const deb = dynamic_cast<debSrcRecordParser*>(this);
  138. if (deb != NULL)
  139. return deb->Files2(F2);
  140. std::vector<pkgSrcRecords::File> F;
  141. if (Files(F) == false)
  142. return false;
  143. for (std::vector<pkgSrcRecords::File>::const_iterator f = F.begin(); f != F.end(); ++f)
  144. {
  145. pkgSrcRecords::File2 f2;
  146. #if __GNUC__ >= 4
  147. #pragma GCC diagnostic push
  148. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  149. #endif
  150. f2.MD5Hash = f->MD5Hash;
  151. f2.Size = f->Size;
  152. f2.Hashes.push_back(HashString("MD5Sum", f->MD5Hash));
  153. f2.FileSize = f->Size;
  154. #if __GNUC__ >= 4
  155. #pragma GCC diagnostic pop
  156. #endif
  157. f2.Path = f->Path;
  158. f2.Type = f->Type;
  159. F2.push_back(f2);
  160. }
  161. return true;
  162. }
  163. /*}}}*/
  164. pkgSrcRecords::Parser::Parser(const pkgIndexFile *Index) : d(NULL), iIndex(Index) {}
  165. pkgSrcRecords::Parser::~Parser() {}