debsrcrecords.cc 7.9 KB

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