debsrcrecords.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: debsrcrecords.cc,v 1.6 2004/03/17 05:58:54 mdz Exp $
  4. /* ######################################################################
  5. Debian Source Package Records - Parser implementation for Debian style
  6. source indexes
  7. ##################################################################### */
  8. /*}}}*/
  9. // Include Files /*{{{*/
  10. #include <config.h>
  11. #include <apt-pkg/deblistparser.h>
  12. #include <apt-pkg/debsrcrecords.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/strutl.h>
  15. #include <apt-pkg/aptconfiguration.h>
  16. #include <apt-pkg/srcrecords.h>
  17. #include <apt-pkg/tagfile.h>
  18. #include <apt-pkg/hashes.h>
  19. #include <ctype.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <algorithm>
  23. #include <string>
  24. #include <vector>
  25. /*}}}*/
  26. using std::max;
  27. using std::string;
  28. // SrcRecordParser::Binaries - Return the binaries field /*{{{*/
  29. // ---------------------------------------------------------------------
  30. /* This member parses the binaries field into a pair of class arrays and
  31. returns a list of strings representing all of the components of the
  32. binaries field. The returned array need not be freed and will be
  33. reused by the next Binaries function call. This function is commonly
  34. used during scanning to find the right package */
  35. const char **debSrcRecordParser::Binaries()
  36. {
  37. const char *Start, *End;
  38. if (Sect.Find("Binary", Start, End) == false)
  39. return NULL;
  40. for (; isspace(*Start) != 0; ++Start);
  41. if (Start >= End)
  42. return NULL;
  43. StaticBinList.clear();
  44. free(Buffer);
  45. Buffer = strndup(Start, End - Start);
  46. char* bin = Buffer;
  47. do {
  48. char* binStartNext = strchrnul(bin, ',');
  49. char* binEnd = binStartNext - 1;
  50. for (; isspace(*binEnd) != 0; --binEnd)
  51. binEnd = '\0';
  52. StaticBinList.push_back(bin);
  53. if (*binStartNext != ',')
  54. break;
  55. *binStartNext = '\0';
  56. for (bin = binStartNext + 1; isspace(*bin) != 0; ++bin);
  57. } while (*bin != '\0');
  58. StaticBinList.push_back(NULL);
  59. return &StaticBinList[0];
  60. }
  61. /*}}}*/
  62. // SrcRecordParser::BuildDepends - Return the Build-Depends information /*{{{*/
  63. // ---------------------------------------------------------------------
  64. /* This member parses the build-depends information and returns a list of
  65. package/version records representing the build dependency. The returned
  66. array need not be freed and will be reused by the next call to this
  67. function */
  68. bool debSrcRecordParser::BuildDepends(std::vector<pkgSrcRecords::Parser::BuildDepRec> &BuildDeps,
  69. bool const &ArchOnly, bool const &StripMultiArch)
  70. {
  71. unsigned int I;
  72. const char *Start, *Stop;
  73. BuildDepRec rec;
  74. const char *fields[] = {"Build-Depends",
  75. "Build-Depends-Indep",
  76. "Build-Conflicts",
  77. "Build-Conflicts-Indep"};
  78. BuildDeps.clear();
  79. for (I = 0; I < 4; I++)
  80. {
  81. if (ArchOnly && (I == 1 || I == 3))
  82. continue;
  83. if (Sect.Find(fields[I], Start, Stop) == false)
  84. continue;
  85. while (1)
  86. {
  87. Start = debListParser::ParseDepends(Start, Stop,
  88. rec.Package,rec.Version,rec.Op,true,StripMultiArch,true);
  89. if (Start == 0)
  90. return _error->Error("Problem parsing dependency: %s", fields[I]);
  91. rec.Type = I;
  92. if (rec.Package != "")
  93. BuildDeps.push_back(rec);
  94. if (Start == Stop)
  95. break;
  96. }
  97. }
  98. return true;
  99. }
  100. /*}}}*/
  101. // SrcRecordParser::Files - Return a list of files for this source /*{{{*/
  102. // ---------------------------------------------------------------------
  103. /* This parses the list of files and returns it, each file is required to have
  104. a complete source package */
  105. bool debSrcRecordParser::Files(std::vector<pkgSrcRecords::File> &List)
  106. {
  107. List.erase(List.begin(),List.end());
  108. // map from the Hashsum field to the hashsum function,
  109. // unfortunately this is not a 1:1 mapping from
  110. // Hashes::SupporedHashes as e.g. Files is a historic name for the md5
  111. const std::pair<const char*, const char*> SourceHashFields[] = {
  112. std::make_pair( "Checksums-Sha512", "SHA512"),
  113. std::make_pair( "Checksums-Sha256", "SHA256"),
  114. std::make_pair( "Checksums-Sha1", "SHA1"),
  115. std::make_pair( "Files", "MD5Sum"), // historic Name
  116. };
  117. for (unsigned int i=0;
  118. i < sizeof(SourceHashFields)/sizeof(SourceHashFields[0]);
  119. i++)
  120. {
  121. string Files = Sect.FindS(SourceHashFields[i].first);
  122. if (Files.empty() == true)
  123. continue;
  124. // Stash the / terminated directory prefix
  125. string Base = Sect.FindS("Directory");
  126. if (Base.empty() == false && Base[Base.length()-1] != '/')
  127. Base += '/';
  128. std::vector<std::string> const compExts = APT::Configuration::getCompressorExtensions();
  129. // Iterate over the entire list grabbing each triplet
  130. const char *C = Files.c_str();
  131. while (*C != 0)
  132. {
  133. pkgSrcRecords::File F;
  134. string Size;
  135. // Parse each of the elements
  136. std::string RawHash;
  137. if (ParseQuoteWord(C, RawHash) == false ||
  138. ParseQuoteWord(C, Size) == false ||
  139. ParseQuoteWord(C, F.Path) == false)
  140. return _error->Error("Error parsing '%s' record",
  141. SourceHashFields[i].first);
  142. // assign full hash string
  143. F.Hash = HashString(SourceHashFields[i].second, RawHash).toStr();
  144. // API compat hack
  145. if(strcmp(SourceHashFields[i].second, "MD5Sum") == 0)
  146. F.MD5Hash = RawHash;
  147. // Parse the size and append the directory
  148. F.Size = atoi(Size.c_str());
  149. F.Path = Base + F.Path;
  150. // Try to guess what sort of file it is we are getting.
  151. string::size_type Pos = F.Path.length()-1;
  152. while (1)
  153. {
  154. string::size_type Tmp = F.Path.rfind('.',Pos);
  155. if (Tmp == string::npos)
  156. break;
  157. if (F.Type == "tar") {
  158. // source v3 has extension 'debian.tar.*' instead of 'diff.*'
  159. if (string(F.Path, Tmp+1, Pos-Tmp) == "debian")
  160. F.Type = "diff";
  161. break;
  162. }
  163. F.Type = string(F.Path,Tmp+1,Pos-Tmp);
  164. if (std::find(compExts.begin(), compExts.end(), std::string(".").append(F.Type)) != compExts.end() ||
  165. F.Type == "tar")
  166. {
  167. Pos = Tmp-1;
  168. continue;
  169. }
  170. break;
  171. }
  172. List.push_back(F);
  173. }
  174. break;
  175. }
  176. return (List.size() > 0);
  177. }
  178. /*}}}*/
  179. // SrcRecordParser::~SrcRecordParser - Destructor /*{{{*/
  180. // ---------------------------------------------------------------------
  181. /* */
  182. debSrcRecordParser::~debSrcRecordParser()
  183. {
  184. delete[] Buffer;
  185. }
  186. /*}}}*/