debsrcrecords.cc 7.3 KB

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