debsrcrecords.cc 8.3 KB

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