debsrcrecords.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. ;
  59. } while (*bin != '\0');
  60. StaticBinList.push_back(NULL);
  61. return &StaticBinList[0];
  62. }
  63. /*}}}*/
  64. // SrcRecordParser::BuildDepends - Return the Build-Depends information /*{{{*/
  65. // ---------------------------------------------------------------------
  66. /* This member parses the build-depends information and returns a list of
  67. package/version records representing the build dependency. The returned
  68. array need not be freed and will be reused by the next call to this
  69. function */
  70. bool debSrcRecordParser::BuildDepends(std::vector<pkgSrcRecords::Parser::BuildDepRec> &BuildDeps,
  71. bool const &ArchOnly, bool const &StripMultiArch)
  72. {
  73. unsigned int I;
  74. const char *Start, *Stop;
  75. BuildDepRec rec;
  76. const char *fields[] = {"Build-Depends",
  77. "Build-Depends-Indep",
  78. "Build-Conflicts",
  79. "Build-Conflicts-Indep"};
  80. BuildDeps.clear();
  81. for (I = 0; I < 4; I++)
  82. {
  83. if (ArchOnly && (I == 1 || I == 3))
  84. continue;
  85. if (Sect.Find(fields[I], Start, Stop) == false)
  86. continue;
  87. while (1)
  88. {
  89. Start = debListParser::ParseDepends(Start, Stop,
  90. rec.Package,rec.Version,rec.Op,true,StripMultiArch,true);
  91. if (Start == 0)
  92. return _error->Error("Problem parsing dependency: %s", fields[I]);
  93. rec.Type = I;
  94. if (rec.Package != "")
  95. BuildDeps.push_back(rec);
  96. if (Start == Stop)
  97. break;
  98. }
  99. }
  100. return true;
  101. }
  102. /*}}}*/
  103. // SrcRecordParser::Files - Return a list of files for this source /*{{{*/
  104. // ---------------------------------------------------------------------
  105. /* This parses the list of files and returns it, each file is required to have
  106. a complete source package */
  107. bool debSrcRecordParser::Files(std::vector<pkgSrcRecords::File> &List)
  108. {
  109. List.erase(List.begin(),List.end());
  110. // Stash the / terminated directory prefix
  111. string Base = Sect.FindS("Directory");
  112. if (Base.empty() == false && Base[Base.length()-1] != '/')
  113. Base += '/';
  114. std::vector<std::string> const compExts = APT::Configuration::getCompressorExtensions();
  115. for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
  116. {
  117. // derive field from checksum type
  118. std::string checksumField("Checksums-");
  119. if (strcmp(*type, "MD5Sum") == 0)
  120. checksumField = "Files"; // historic name for MD5 checksums
  121. else
  122. checksumField.append(*type);
  123. string const Files = Sect.FindS(checksumField.c_str());
  124. if (Files.empty() == true)
  125. continue;
  126. // Iterate over the entire list grabbing each triplet
  127. const char *C = Files.c_str();
  128. while (*C != 0)
  129. {
  130. string hash, size, path;
  131. // Parse each of the elements
  132. if (ParseQuoteWord(C, hash) == false ||
  133. ParseQuoteWord(C, size) == false ||
  134. ParseQuoteWord(C, path) == false)
  135. return _error->Error("Error parsing file record in %s of source package %s", checksumField.c_str(), Package().c_str());
  136. HashString const hashString(*type, hash);
  137. if (Base.empty() == false)
  138. path = Base + path;
  139. // look if we have a record for this file already
  140. std::vector<pkgSrcRecords::File>::iterator file = List.begin();
  141. for (; file != List.end(); ++file)
  142. if (file->Path == path)
  143. break;
  144. // we have it already, store the new hash and be done
  145. if (file != List.end())
  146. {
  147. #if __GNUC__ >= 4
  148. // set for compatibility only, so warn users not us
  149. #pragma GCC diagnostic push
  150. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  151. #endif
  152. if (checksumField == "Files")
  153. file->MD5Hash = hash;
  154. #if __GNUC__ >= 4
  155. #pragma GCC diagnostic pop
  156. #endif
  157. // an error here indicates that we have two different hashes for the same file
  158. if (file->Hashes.push_back(hashString) == false)
  159. return _error->Error("Error parsing checksum in %s of source package %s", checksumField.c_str(), Package().c_str());
  160. continue;
  161. }
  162. // we haven't seen this file yet
  163. pkgSrcRecords::File F;
  164. F.Path = path;
  165. F.Size = strtoull(size.c_str(), NULL, 10);
  166. F.Hashes.push_back(hashString);
  167. #if __GNUC__ >= 4
  168. // set for compatibility only, so warn users not us
  169. #pragma GCC diagnostic push
  170. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  171. #endif
  172. if (checksumField == "Files")
  173. F.MD5Hash = hash;
  174. #if __GNUC__ >= 4
  175. #pragma GCC diagnostic pop
  176. #endif
  177. // Try to guess what sort of file it is we are getting.
  178. string::size_type Pos = F.Path.length()-1;
  179. while (1)
  180. {
  181. string::size_type Tmp = F.Path.rfind('.',Pos);
  182. if (Tmp == string::npos)
  183. break;
  184. if (F.Type == "tar") {
  185. // source v3 has extension 'debian.tar.*' instead of 'diff.*'
  186. if (string(F.Path, Tmp+1, Pos-Tmp) == "debian")
  187. F.Type = "diff";
  188. break;
  189. }
  190. F.Type = string(F.Path,Tmp+1,Pos-Tmp);
  191. if (std::find(compExts.begin(), compExts.end(), std::string(".").append(F.Type)) != compExts.end() ||
  192. F.Type == "tar")
  193. {
  194. Pos = Tmp-1;
  195. continue;
  196. }
  197. break;
  198. }
  199. List.push_back(F);
  200. }
  201. }
  202. return true;
  203. }
  204. /*}}}*/
  205. // SrcRecordParser::~SrcRecordParser - Destructor /*{{{*/
  206. // ---------------------------------------------------------------------
  207. /* */
  208. debSrcRecordParser::~debSrcRecordParser()
  209. {
  210. // was allocated via strndup()
  211. free(Buffer);
  212. }
  213. /*}}}*/
  214. debDscRecordParser::debDscRecordParser(std::string const &DscFile, pkgIndexFile const *Index)
  215. : debSrcRecordParser(DscFile, Index)
  216. {
  217. // support clear signed files
  218. if (OpenMaybeClearSignedFile(DscFile, Fd) == false)
  219. {
  220. _error->Error("Failed to open %s", DscFile.c_str());
  221. return;
  222. }
  223. // re-init to ensure the updated Fd is used
  224. Tags.Init(&Fd);
  225. // read the first (and only) record
  226. Step();
  227. }