debrecords.cc 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: debrecords.cc,v 1.10 2001/03/13 06:51:46 jgg Exp $
  4. /* ######################################################################
  5. Debian Package Records - Parser for debian package records
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #include <config.h>
  10. #include <apt-pkg/debrecords.h>
  11. #include <apt-pkg/debindexfile.h>
  12. #include <apt-pkg/strutl.h>
  13. #include <apt-pkg/aptconfiguration.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/cacheiterators.h>
  16. #include <apt-pkg/pkgcache.h>
  17. #include <apt-pkg/tagfile.h>
  18. #include <apt-pkg/error.h>
  19. #include <string.h>
  20. #include <algorithm>
  21. #include <sstream>
  22. #include <string>
  23. #include <vector>
  24. #include <langinfo.h>
  25. #include <apti18n.h>
  26. /*}}}*/
  27. using std::string;
  28. // RecordParser::debRecordParser - Constructor /*{{{*/
  29. debRecordParser::debRecordParser(string FileName,pkgCache &Cache) :
  30. debRecordParserBase(), d(NULL), File(FileName, FileFd::ReadOnly, FileFd::Extension),
  31. Tags(&File, std::max(Cache.Head().MaxVerFileSize, Cache.Head().MaxDescFileSize) + 200)
  32. {
  33. }
  34. /*}}}*/
  35. // RecordParser::Jump - Jump to a specific record /*{{{*/
  36. bool debRecordParser::Jump(pkgCache::VerFileIterator const &Ver)
  37. {
  38. if (Ver.end() == true)
  39. return false;
  40. return Tags.Jump(Section,Ver->Offset);
  41. }
  42. bool debRecordParser::Jump(pkgCache::DescFileIterator const &Desc)
  43. {
  44. if (Desc.end() == true)
  45. return false;
  46. return Tags.Jump(Section,Desc->Offset);
  47. }
  48. /*}}}*/
  49. debRecordParser::~debRecordParser() {}
  50. debRecordParserBase::debRecordParserBase() : Parser(), d(NULL) {}
  51. // RecordParserBase::FileName - Return the archive filename on the site /*{{{*/
  52. string debRecordParserBase::FileName()
  53. {
  54. return Section.FindS("Filename");
  55. }
  56. /*}}}*/
  57. // RecordParserBase::Name - Return the package name /*{{{*/
  58. string debRecordParserBase::Name()
  59. {
  60. string Result = Section.FindS("Package");
  61. // Normalize mixed case package names to lower case, like dpkg does
  62. // See Bug#807012 for details
  63. std::transform(Result.begin(), Result.end(), Result.begin(), tolower_ascii);
  64. return Result;
  65. }
  66. /*}}}*/
  67. // RecordParserBase::Homepage - Return the package homepage /*{{{*/
  68. string debRecordParserBase::Homepage()
  69. {
  70. return Section.FindS("Homepage");
  71. }
  72. /*}}}*/
  73. // RecordParserBase::Hashes - return the available archive hashes /*{{{*/
  74. HashStringList debRecordParserBase::Hashes() const
  75. {
  76. HashStringList hashes;
  77. for (char const * const * type = HashString::SupportedHashes(); *type != NULL; ++type)
  78. {
  79. std::string const hash = Section.FindS(*type);
  80. if (hash.empty() == false)
  81. hashes.push_back(HashString(*type, hash));
  82. }
  83. auto const size = Section.FindULL("Size", 0);
  84. if (size != 0)
  85. hashes.FileSize(size);
  86. return hashes;
  87. }
  88. /*}}}*/
  89. // RecordParserBase::Maintainer - Return the maintainer email /*{{{*/
  90. string debRecordParserBase::Maintainer()
  91. {
  92. return Section.FindS("Maintainer");
  93. }
  94. /*}}}*/
  95. // RecordParserBase::RecordField - Return the value of an arbitrary field /*{{*/
  96. string debRecordParserBase::RecordField(const char *fieldName)
  97. {
  98. return Section.FindS(fieldName);
  99. }
  100. /*}}}*/
  101. // RecordParserBase::ShortDesc - Return a 1 line description /*{{{*/
  102. string debRecordParserBase::ShortDesc(std::string const &lang)
  103. {
  104. string const Res = LongDesc(lang);
  105. if (Res.empty() == true)
  106. return "";
  107. string::size_type const Pos = Res.find('\n');
  108. if (Pos == string::npos)
  109. return Res;
  110. return string(Res,0,Pos);
  111. }
  112. /*}}}*/
  113. // RecordParserBase::LongDesc - Return a longer description /*{{{*/
  114. string debRecordParserBase::LongDesc(std::string const &lang)
  115. {
  116. string orig;
  117. if (lang.empty() == true)
  118. {
  119. std::vector<string> const lang = APT::Configuration::getLanguages();
  120. for (std::vector<string>::const_iterator l = lang.begin();
  121. l != lang.end(); ++l)
  122. {
  123. std::string const tagname = "Description-" + *l;
  124. orig = Section.FindS(tagname.c_str());
  125. if (orig.empty() == false)
  126. break;
  127. else if (*l == "en")
  128. {
  129. orig = Section.FindS("Description");
  130. if (orig.empty() == false)
  131. break;
  132. }
  133. }
  134. if (orig.empty() == true)
  135. orig = Section.FindS("Description");
  136. }
  137. else
  138. {
  139. std::string const tagname = "Description-" + lang;
  140. orig = Section.FindS(tagname.c_str());
  141. if (orig.empty() == true && lang == "en")
  142. orig = Section.FindS("Description");
  143. }
  144. char const * const codeset = nl_langinfo(CODESET);
  145. if (strcmp(codeset,"UTF-8") != 0) {
  146. string dest;
  147. UTF8ToCodeset(codeset, orig, &dest);
  148. return dest;
  149. }
  150. return orig;
  151. }
  152. /*}}}*/
  153. static const char * const SourceVerSeparators = " ()";
  154. // RecordParserBase::SourcePkg - Return the source package name if any /*{{{*/
  155. string debRecordParserBase::SourcePkg()
  156. {
  157. string Res = Section.FindS("Source");
  158. string::size_type Pos = Res.find_first_of(SourceVerSeparators);
  159. if (Pos == string::npos)
  160. return Res;
  161. return string(Res,0,Pos);
  162. }
  163. /*}}}*/
  164. // RecordParserBase::SourceVer - Return the source version number if present /*{{{*/
  165. string debRecordParserBase::SourceVer()
  166. {
  167. string Pkg = Section.FindS("Source");
  168. string::size_type Pos = Pkg.find_first_of(SourceVerSeparators);
  169. if (Pos == string::npos)
  170. return "";
  171. string::size_type VerStart = Pkg.find_first_not_of(SourceVerSeparators, Pos);
  172. if(VerStart == string::npos)
  173. return "";
  174. string::size_type VerEnd = Pkg.find_first_of(SourceVerSeparators, VerStart);
  175. if(VerEnd == string::npos)
  176. // Corresponds to the case of, e.g., "foo (1.2" without a closing
  177. // paren. Be liberal and guess what it means.
  178. return string(Pkg, VerStart);
  179. else
  180. return string(Pkg, VerStart, VerEnd - VerStart);
  181. }
  182. /*}}}*/
  183. // RecordParserBase::GetRec - Return the whole record /*{{{*/
  184. void debRecordParserBase::GetRec(const char *&Start,const char *&Stop)
  185. {
  186. Section.GetSection(Start,Stop);
  187. }
  188. /*}}}*/
  189. debRecordParserBase::~debRecordParserBase() {}
  190. bool debDebFileRecordParser::LoadContent()
  191. {
  192. // load content only once
  193. if (controlContent.empty() == false)
  194. return true;
  195. std::ostringstream content;
  196. if (debDebPkgFileIndex::GetContent(content, debFileName) == false)
  197. return false;
  198. // add two newlines to make sure the scanner finds the section,
  199. // which is usually done by pkgTagFile automatically if needed.
  200. content << "\n\n";
  201. controlContent = content.str();
  202. if (Section.Scan(controlContent.c_str(), controlContent.length()) == false)
  203. return _error->Error(_("Unable to parse package file %s (%d)"), debFileName.c_str(), 3);
  204. return true;
  205. }
  206. bool debDebFileRecordParser::Jump(pkgCache::VerFileIterator const &) { return LoadContent(); }
  207. bool debDebFileRecordParser::Jump(pkgCache::DescFileIterator const &) { return LoadContent(); }
  208. std::string debDebFileRecordParser::FileName() { return debFileName; }
  209. debDebFileRecordParser::debDebFileRecordParser(std::string FileName) : debRecordParserBase(), d(NULL), debFileName(FileName) {}
  210. debDebFileRecordParser::~debDebFileRecordParser() {}