indexfile.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: indexfile.cc,v 1.2.2.1 2003/12/24 23:09:17 mdz Exp $
  4. /* ######################################################################
  5. Index File - Abstraction for an index of archive/souce file.
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include Files /*{{{*/
  9. #include<config.h>
  10. #include <apt-pkg/configuration.h>
  11. #include <apt-pkg/indexfile.h>
  12. #include <apt-pkg/error.h>
  13. #include <apt-pkg/fileutl.h>
  14. #include <apt-pkg/aptconfiguration.h>
  15. #include <apt-pkg/pkgcache.h>
  16. #include <apt-pkg/cacheiterators.h>
  17. #include <apt-pkg/srcrecords.h>
  18. #include <apt-pkg/macros.h>
  19. #include <string>
  20. #include <vector>
  21. #include <clocale>
  22. #include <cstring>
  23. /*}}}*/
  24. // Global list of Item supported
  25. static pkgIndexFile::Type *ItmList[10];
  26. pkgIndexFile::Type **pkgIndexFile::Type::GlobalList = ItmList;
  27. unsigned long pkgIndexFile::Type::GlobalListLen = 0;
  28. // Type::Type - Constructor /*{{{*/
  29. // ---------------------------------------------------------------------
  30. /* */
  31. pkgIndexFile::Type::Type()
  32. {
  33. ItmList[GlobalListLen] = this;
  34. GlobalListLen++;
  35. Label = NULL;
  36. }
  37. /*}}}*/
  38. // Type::GetType - Locate the type by name /*{{{*/
  39. // ---------------------------------------------------------------------
  40. /* */
  41. pkgIndexFile::Type *pkgIndexFile::Type::GetType(const char *Type)
  42. {
  43. for (unsigned I = 0; I != GlobalListLen; I++)
  44. if (strcmp(GlobalList[I]->Label,Type) == 0)
  45. return GlobalList[I];
  46. return 0;
  47. }
  48. /*}}}*/
  49. pkgIndexFile::pkgIndexFile(bool Trusted) : /*{{{*/
  50. d(NULL), Trusted(Trusted)
  51. {
  52. }
  53. /*}}}*/
  54. // IndexFile::ArchiveInfo - Stub /*{{{*/
  55. std::string pkgIndexFile::ArchiveInfo(pkgCache::VerIterator /*Ver*/) const
  56. {
  57. return std::string();
  58. }
  59. /*}}}*/
  60. // IndexFile::FindInCache - Stub /*{{{*/
  61. pkgCache::PkgFileIterator pkgIndexFile::FindInCache(pkgCache &Cache) const
  62. {
  63. return pkgCache::PkgFileIterator(Cache);
  64. }
  65. /*}}}*/
  66. // IndexFile::SourceIndex - Stub /*{{{*/
  67. std::string pkgIndexFile::SourceInfo(pkgSrcRecords::Parser const &/*Record*/,
  68. pkgSrcRecords::File const &/*File*/) const
  69. {
  70. return std::string();
  71. }
  72. /*}}}*/
  73. // IndexFile::TranslationsAvailable - Check if will use Translation /*{{{*/
  74. // ---------------------------------------------------------------------
  75. /* */
  76. bool pkgIndexFile::TranslationsAvailable() {
  77. return (APT::Configuration::getLanguages().empty() != true);
  78. }
  79. /*}}}*/
  80. // IndexFile::CheckLanguageCode - Check the Language Code /*{{{*/
  81. // ---------------------------------------------------------------------
  82. /* No intern need for this method anymore as the check for correctness
  83. is already done in getLanguages(). Note also that this check is
  84. rather bad (doesn't take three character like ast into account).
  85. TODO: Remove method with next API break */
  86. APT_DEPRECATED bool pkgIndexFile::CheckLanguageCode(const char *Lang)
  87. {
  88. if (strlen(Lang) == 2 || (strlen(Lang) == 5 && Lang[2] == '_'))
  89. return true;
  90. if (strcmp(Lang,"C") != 0)
  91. _error->Warning("Wrong language code %s", Lang);
  92. return false;
  93. }
  94. /*}}}*/
  95. // IndexFile::LanguageCode - Return the Language Code /*{{{*/
  96. // ---------------------------------------------------------------------
  97. /* As we have now possibly more than one LanguageCode this method is
  98. supersided by a) private classmembers or b) getLanguages().
  99. TODO: Remove method with next API break */
  100. APT_DEPRECATED std::string pkgIndexFile::LanguageCode() {
  101. if (TranslationsAvailable() == false)
  102. return "";
  103. return APT::Configuration::getLanguages()[0];
  104. }
  105. /*}}}*/
  106. // IndexTarget - Constructor /*{{{*/
  107. IndexTarget::IndexTarget(std::string const &MetaKey, std::string const &ShortDesc,
  108. std::string const &LongDesc, std::string const &URI, bool const IsOptional,
  109. bool const KeepCompressed, std::map<std::string, std::string> const &Options) :
  110. URI(URI), Description(LongDesc), ShortDesc(ShortDesc), MetaKey(MetaKey),
  111. IsOptional(IsOptional), KeepCompressed(KeepCompressed), Options(Options)
  112. {
  113. }
  114. /*}}}*/
  115. std::string IndexTarget::Option(OptionKeys const EnumKey) const /*{{{*/
  116. {
  117. std::string Key;
  118. switch (EnumKey)
  119. {
  120. #define APT_CASE(X) case X: Key = #X; break
  121. APT_CASE(SITE);
  122. APT_CASE(RELEASE);
  123. APT_CASE(COMPONENT);
  124. APT_CASE(LANGUAGE);
  125. APT_CASE(ARCHITECTURE);
  126. APT_CASE(BASE_URI);
  127. APT_CASE(REPO_URI);
  128. APT_CASE(TARGET_OF);
  129. APT_CASE(CREATED_BY);
  130. #undef APT_CASE
  131. case FILENAME: return _config->FindDir("Dir::State::lists") + URItoFileName(URI);
  132. case EXISTING_FILENAME:
  133. std::string const filename = Option(FILENAME);
  134. std::vector<std::string> const types = APT::Configuration::getCompressionTypes();
  135. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  136. {
  137. if (t->empty())
  138. continue;
  139. std::string const file = (*t == "uncompressed") ? filename : (filename + "." + *t);
  140. if (FileExists(file))
  141. return file;
  142. }
  143. return "";
  144. }
  145. std::map<std::string,std::string>::const_iterator const M = Options.find(Key);
  146. if (M == Options.end())
  147. return "";
  148. return M->second;
  149. }
  150. /*}}}*/
  151. std::string IndexTarget::Format(std::string format) const /*{{{*/
  152. {
  153. for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
  154. {
  155. format = SubstVar(format, std::string("$(") + O->first + ")", O->second);
  156. }
  157. format = SubstVar(format, "$(METAKEY)", MetaKey);
  158. format = SubstVar(format, "$(SHORTDESC)", ShortDesc);
  159. format = SubstVar(format, "$(DESCRIPTION)", Description);
  160. format = SubstVar(format, "$(URI)", URI);
  161. format = SubstVar(format, "$(FILENAME)", Option(IndexTarget::FILENAME));
  162. return format;
  163. }
  164. /*}}}*/
  165. pkgIndexTargetFile::pkgIndexTargetFile(IndexTarget const &Target, bool const Trusted) :/*{{{*/
  166. pkgIndexFile(Trusted), d(NULL), Target(Target)
  167. {
  168. }
  169. /*}}}*/
  170. std::string pkgIndexTargetFile::ArchiveURI(std::string File) const/*{{{*/
  171. {
  172. return Target.Option(IndexTarget::REPO_URI) + File;
  173. }
  174. /*}}}*/
  175. std::string pkgIndexTargetFile::Describe(bool Short) const /*{{{*/
  176. {
  177. if (Short)
  178. return Target.Description;
  179. return Target.Description + " (" + IndexFileName() + ")";
  180. }
  181. /*}}}*/
  182. std::string pkgIndexTargetFile::IndexFileName() const /*{{{*/
  183. {
  184. std::string const s = Target.Option(IndexTarget::FILENAME);
  185. if (FileExists(s))
  186. return s;
  187. std::vector<std::string> types = APT::Configuration::getCompressionTypes();
  188. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  189. {
  190. std::string p = s + '.' + *t;
  191. if (FileExists(p))
  192. return p;
  193. }
  194. return s;
  195. }
  196. /*}}}*/
  197. unsigned long pkgIndexTargetFile::Size() const /*{{{*/
  198. {
  199. unsigned long size = 0;
  200. /* we need to ignore errors here; if the lists are absent, just return 0 */
  201. _error->PushToStack();
  202. FileFd f(IndexFileName(), FileFd::ReadOnly, FileFd::Extension);
  203. if (!f.Failed())
  204. size = f.Size();
  205. if (_error->PendingError() == true)
  206. size = 0;
  207. _error->RevertToStack();
  208. return size;
  209. }
  210. /*}}}*/
  211. bool pkgIndexTargetFile::Exists() const /*{{{*/
  212. {
  213. return FileExists(IndexFileName());
  214. }
  215. /*}}}*/
  216. APT_CONST pkgIndexFile::~pkgIndexFile() {}
  217. APT_CONST pkgIndexTargetFile::~pkgIndexTargetFile() {}