debsrcrecords.cc 7.8 KB

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