You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

317 lines
9.5 KiB

  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. // 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. // We parsed a package that was ignored (wrong architecture restriction
  118. // or something).
  119. if (rec.Package == "") {
  120. // If we are in an OR group, we need to set the "Or" flag of the
  121. // previous entry to our value.
  122. if (BuildDeps.size() > 0 && (BuildDeps[BuildDeps.size() - 1].Op & pkgCache::Dep::Or) == pkgCache::Dep::Or) {
  123. BuildDeps[BuildDeps.size() - 1].Op &= ~pkgCache::Dep::Or;
  124. BuildDeps[BuildDeps.size() - 1].Op |= (rec.Op & pkgCache::Dep::Or);
  125. }
  126. } else {
  127. BuildDeps.push_back(rec);
  128. }
  129. if (Start == Stop)
  130. break;
  131. }
  132. }
  133. return true;
  134. }
  135. /*}}}*/
  136. // SrcRecordParser::Files - Return a list of files for this source /*{{{*/
  137. // ---------------------------------------------------------------------
  138. /* This parses the list of files and returns it, each file is required to have
  139. a complete source package */
  140. bool debSrcRecordParser::Files(std::vector<pkgSrcRecords::File> &F)
  141. {
  142. std::vector<pkgSrcRecords::File2> F2;
  143. if (Files2(F2) == false)
  144. return false;
  145. for (std::vector<pkgSrcRecords::File2>::const_iterator f2 = F2.begin(); f2 != F2.end(); ++f2)
  146. {
  147. pkgSrcRecords::File2 f;
  148. #if __GNUC__ >= 4
  149. #pragma GCC diagnostic push
  150. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  151. #endif
  152. f.MD5Hash = f2->MD5Hash;
  153. f.Size = f2->Size;
  154. #if __GNUC__ >= 4
  155. #pragma GCC diagnostic pop
  156. #endif
  157. f.Path = f2->Path;
  158. f.Type = f2->Type;
  159. F.push_back(f);
  160. }
  161. return true;
  162. }
  163. bool debSrcRecordParser::Files2(std::vector<pkgSrcRecords::File2> &List)
  164. {
  165. List.clear();
  166. // Stash the / terminated directory prefix
  167. string Base = Sect.FindS("Directory");
  168. if (Base.empty() == false && Base[Base.length()-1] != '/')
  169. Base += '/';
  170. std::vector<std::string> const compExts = APT::Configuration::getCompressorExtensions();
  171. for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
  172. {
  173. // derive field from checksum type
  174. std::string checksumField("Checksums-");
  175. if (strcmp(*type, "MD5Sum") == 0)
  176. checksumField = "Files"; // historic name for MD5 checksums
  177. else
  178. checksumField.append(*type);
  179. string const Files = Sect.FindS(checksumField.c_str());
  180. if (Files.empty() == true)
  181. continue;
  182. // Iterate over the entire list grabbing each triplet
  183. const char *C = Files.c_str();
  184. while (*C != 0)
  185. {
  186. string hash, size, path;
  187. // Parse each of the elements
  188. if (ParseQuoteWord(C, hash) == false ||
  189. ParseQuoteWord(C, size) == false ||
  190. ParseQuoteWord(C, path) == false)
  191. return _error->Error("Error parsing file record in %s of source package %s", checksumField.c_str(), Package().c_str());
  192. if (iIndex == nullptr && checksumField == "Files")
  193. {
  194. // the Files field has a different format than the rest in deb-changes files
  195. std::string ignore;
  196. if (ParseQuoteWord(C, ignore) == false ||
  197. ParseQuoteWord(C, path) == false)
  198. return _error->Error("Error parsing file record in %s of source package %s", checksumField.c_str(), Package().c_str());
  199. }
  200. HashString const hashString(*type, hash);
  201. if (Base.empty() == false)
  202. path = Base + path;
  203. // look if we have a record for this file already
  204. std::vector<pkgSrcRecords::File2>::iterator file = List.begin();
  205. for (; file != List.end(); ++file)
  206. if (file->Path == path)
  207. break;
  208. // we have it already, store the new hash and be done
  209. if (file != List.end())
  210. {
  211. if (checksumField == "Files")
  212. APT_IGNORE_DEPRECATED(file->MD5Hash = hash;)
  213. // an error here indicates that we have two different hashes for the same file
  214. if (file->Hashes.push_back(hashString) == false)
  215. return _error->Error("Error parsing checksum in %s of source package %s", checksumField.c_str(), Package().c_str());
  216. continue;
  217. }
  218. // we haven't seen this file yet
  219. pkgSrcRecords::File2 F;
  220. F.Path = path;
  221. F.FileSize = strtoull(size.c_str(), NULL, 10);
  222. F.Hashes.push_back(hashString);
  223. F.Hashes.FileSize(F.FileSize);
  224. APT_IGNORE_DEPRECATED_PUSH
  225. F.Size = F.FileSize;
  226. if (checksumField == "Files")
  227. F.MD5Hash = hash;
  228. APT_IGNORE_DEPRECATED_POP
  229. // Try to guess what sort of file it is we are getting.
  230. string::size_type Pos = F.Path.length()-1;
  231. while (1)
  232. {
  233. string::size_type Tmp = F.Path.rfind('.',Pos);
  234. if (Tmp == string::npos)
  235. break;
  236. if (F.Type == "tar") {
  237. // source v3 has extension 'debian.tar.*' instead of 'diff.*'
  238. if (string(F.Path, Tmp+1, Pos-Tmp) == "debian")
  239. F.Type = "diff";
  240. break;
  241. }
  242. F.Type = string(F.Path,Tmp+1,Pos-Tmp);
  243. if (std::find(compExts.begin(), compExts.end(), std::string(".").append(F.Type)) != compExts.end() ||
  244. F.Type == "tar")
  245. {
  246. Pos = Tmp-1;
  247. continue;
  248. }
  249. break;
  250. }
  251. List.push_back(F);
  252. }
  253. }
  254. return true;
  255. }
  256. /*}}}*/
  257. // SrcRecordParser::~SrcRecordParser - Destructor /*{{{*/
  258. // ---------------------------------------------------------------------
  259. /* */
  260. debSrcRecordParser::~debSrcRecordParser()
  261. {
  262. // was allocated via strndup()
  263. free(Buffer);
  264. }
  265. /*}}}*/
  266. debDscRecordParser::debDscRecordParser(std::string const &DscFile, pkgIndexFile const *Index)
  267. : debSrcRecordParser("", Index)
  268. {
  269. // support clear signed files
  270. if (OpenMaybeClearSignedFile(DscFile, Fd) == false)
  271. {
  272. _error->Error("Failed to open %s", DscFile.c_str());
  273. return;
  274. }
  275. // re-init to ensure the updated Fd is used
  276. Tags.Init(&Fd, pkgTagFile::SUPPORT_COMMENTS);
  277. // read the first (and only) record
  278. Step();
  279. }