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.
 
 
 
 
 
 

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