srcrecords.cc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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), Current(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. Parser* P = (*J)->CreateSrcParser();
  36. if (_error->PendingError() == true)
  37. return;
  38. if (P != 0)
  39. Files.push_back(P);
  40. }
  41. }
  42. // Doesn't work without any source index files
  43. if (Files.empty() == true)
  44. {
  45. _error->Error(_("You must put some 'source' URIs"
  46. " in your sources.list"));
  47. return;
  48. }
  49. Restart();
  50. }
  51. /*}}}*/
  52. // SrcRecords::~pkgSrcRecords - Destructor /*{{{*/
  53. // ---------------------------------------------------------------------
  54. /* */
  55. pkgSrcRecords::~pkgSrcRecords()
  56. {
  57. // Blow away all the parser objects
  58. for(std::vector<Parser*>::iterator I = Files.begin(); I != Files.end(); ++I)
  59. delete *I;
  60. }
  61. /*}}}*/
  62. // SrcRecords::Restart - Restart the search /*{{{*/
  63. // ---------------------------------------------------------------------
  64. /* Return all of the parsers to their starting position */
  65. bool pkgSrcRecords::Restart()
  66. {
  67. Current = Files.begin();
  68. for (std::vector<Parser*>::iterator I = Files.begin();
  69. I != Files.end(); ++I)
  70. if ((*I)->Offset() != 0)
  71. (*I)->Restart();
  72. return true;
  73. }
  74. /*}}}*/
  75. // SrcRecords::Step - Step to the next Source Record /*{{{*/
  76. // ---------------------------------------------------------------------
  77. /* Step to the next source package record */
  78. const pkgSrcRecords::Parser* pkgSrcRecords::Step()
  79. {
  80. if (Current == Files.end())
  81. return 0;
  82. // Step to the next record, possibly switching files
  83. while ((*Current)->Step() == false)
  84. {
  85. if (_error->PendingError() == true)
  86. return 0;
  87. ++Current;
  88. if (Current == Files.end())
  89. return 0;
  90. }
  91. return *Current;
  92. }
  93. /*}}}*/
  94. // SrcRecords::Find - Find the first source package with the given name /*{{{*/
  95. // ---------------------------------------------------------------------
  96. /* This searches on both source package names and output binary names and
  97. returns the first found. A 'cursor' like system is used to allow this
  98. function to be called multiple times to get successive entries */
  99. pkgSrcRecords::Parser *pkgSrcRecords::Find(const char *Package,bool const &SrcOnly)
  100. {
  101. while (true)
  102. {
  103. if(Step() == 0)
  104. return 0;
  105. // IO error somehow
  106. if (_error->PendingError() == true)
  107. return 0;
  108. // Source name hit
  109. if ((*Current)->Package() == Package)
  110. return *Current;
  111. if (SrcOnly == true)
  112. continue;
  113. // Check for a binary hit
  114. const char **I = (*Current)->Binaries();
  115. for (; I != 0 && *I != 0; ++I)
  116. if (strcmp(Package,*I) == 0)
  117. return *Current;
  118. }
  119. }
  120. /*}}}*/
  121. // Parser::BuildDepType - Convert a build dep to a string /*{{{*/
  122. // ---------------------------------------------------------------------
  123. /* */
  124. const char *pkgSrcRecords::Parser::BuildDepType(unsigned char const &Type)
  125. {
  126. const char *fields[] = {"Build-Depends",
  127. "Build-Depends-Indep",
  128. "Build-Conflicts",
  129. "Build-Conflicts-Indep"};
  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. /*}}}*/