debsrcrecords.cc 9.1 KB

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