debsrcrecords.cc 8.2 KB

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