debsrcrecords.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. // Stash the / terminated directory prefix
  110. string Base = Sect.FindS("Directory");
  111. if (Base.empty() == false && Base[Base.length()-1] != '/')
  112. Base += '/';
  113. std::vector<std::string> const compExts = APT::Configuration::getCompressorExtensions();
  114. for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
  115. {
  116. // derive field from checksum type
  117. std::string checksumField("Checksums-");
  118. if (strcmp(*type, "MD5Sum") == 0)
  119. checksumField = "Files"; // historic name for MD5 checksums
  120. else
  121. checksumField.append(*type);
  122. string const Files = Sect.FindS(checksumField.c_str());
  123. if (Files.empty() == true)
  124. continue;
  125. // Iterate over the entire list grabbing each triplet
  126. const char *C = Files.c_str();
  127. while (*C != 0)
  128. {
  129. string hash, size, path;
  130. // Parse each of the elements
  131. if (ParseQuoteWord(C, hash) == false ||
  132. ParseQuoteWord(C, size) == false ||
  133. ParseQuoteWord(C, path) == false)
  134. return _error->Error("Error parsing file record in %s of source package %s", checksumField.c_str(), Package().c_str());
  135. HashString const hashString(*type, hash);
  136. if (Base.empty() == false)
  137. path = Base + path;
  138. // look if we have a record for this file already
  139. std::vector<pkgSrcRecords::File>::iterator file = List.begin();
  140. for (; file != List.end(); ++file)
  141. if (file->Path == path)
  142. break;
  143. // we have it already, store the new hash and be done
  144. if (file != List.end())
  145. {
  146. #if __GNUC__ >= 4
  147. // set for compatibility only, so warn users not us
  148. #pragma GCC diagnostic push
  149. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  150. #endif
  151. if (checksumField == "Files")
  152. file->MD5Hash = hash;
  153. #if __GNUC__ >= 4
  154. #pragma GCC diagnostic pop
  155. #endif
  156. // an error here indicates that we have two different hashes for the same file
  157. if (file->Hashes.push_back(hashString) == false)
  158. return _error->Error("Error parsing checksum in %s of source package %s", checksumField.c_str(), Package().c_str());
  159. continue;
  160. }
  161. // we haven't seen this file yet
  162. pkgSrcRecords::File F;
  163. F.Path = path;
  164. F.Size = strtoull(size.c_str(), NULL, 10);
  165. F.Hashes.push_back(hashString);
  166. #if __GNUC__ >= 4
  167. // set for compatibility only, so warn users not us
  168. #pragma GCC diagnostic push
  169. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  170. #endif
  171. if (checksumField == "Files")
  172. F.MD5Hash = hash;
  173. #if __GNUC__ >= 4
  174. #pragma GCC diagnostic pop
  175. #endif
  176. // Try to guess what sort of file it is we are getting.
  177. string::size_type Pos = F.Path.length()-1;
  178. while (1)
  179. {
  180. string::size_type Tmp = F.Path.rfind('.',Pos);
  181. if (Tmp == string::npos)
  182. break;
  183. if (F.Type == "tar") {
  184. // source v3 has extension 'debian.tar.*' instead of 'diff.*'
  185. if (string(F.Path, Tmp+1, Pos-Tmp) == "debian")
  186. F.Type = "diff";
  187. break;
  188. }
  189. F.Type = string(F.Path,Tmp+1,Pos-Tmp);
  190. if (std::find(compExts.begin(), compExts.end(), std::string(".").append(F.Type)) != compExts.end() ||
  191. F.Type == "tar")
  192. {
  193. Pos = Tmp-1;
  194. continue;
  195. }
  196. break;
  197. }
  198. List.push_back(F);
  199. }
  200. }
  201. return true;
  202. }
  203. /*}}}*/
  204. // SrcRecordParser::~SrcRecordParser - Destructor /*{{{*/
  205. // ---------------------------------------------------------------------
  206. /* */
  207. debSrcRecordParser::~debSrcRecordParser()
  208. {
  209. delete[] Buffer;
  210. }
  211. /*}}}*/
  212. debDscRecordParser::debDscRecordParser(std::string const &DscFile, pkgIndexFile const *Index)
  213. : debSrcRecordParser(DscFile, Index)
  214. {
  215. // support clear signed files
  216. if (OpenMaybeClearSignedFile(DscFile, Fd) == false)
  217. {
  218. _error->Error("Failed to open %s", DscFile.c_str());
  219. return;
  220. }
  221. // re-init to ensure the updated Fd is used
  222. Tags.Init(&Fd);
  223. // read the first (and only) record
  224. Step();
  225. }