debsrcrecords.cc 8.9 KB

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