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.
 
 
 
 
 
 

202 lines
6.0 KiB

  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. 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. std::map<std::string, std::string> const &Options) :
  110. URI(URI), Description(LongDesc), ShortDesc(ShortDesc), MetaKey(MetaKey), IsOptional(IsOptional), Options(Options)
  111. {
  112. }
  113. /*}}}*/
  114. std::string IndexTarget::Option(OptionKeys const EnumKey) const /*{{{*/
  115. {
  116. std::string Key;
  117. switch (EnumKey)
  118. {
  119. #define APT_CASE(X) case X: Key = #X; break
  120. APT_CASE(SITE);
  121. APT_CASE(RELEASE);
  122. APT_CASE(COMPONENT);
  123. APT_CASE(LANGUAGE);
  124. APT_CASE(ARCHITECTURE);
  125. APT_CASE(BASE_URI);
  126. APT_CASE(REPO_URI);
  127. APT_CASE(CREATED_BY);
  128. #undef APT_CASE
  129. }
  130. std::map<std::string,std::string>::const_iterator const M = Options.find(Key);
  131. if (M == Options.end())
  132. return "";
  133. return M->second;
  134. }
  135. /*}}}*/
  136. pkgIndexTargetFile::pkgIndexTargetFile(IndexTarget const &Target, bool const Trusted) :/*{{{*/
  137. pkgIndexFile(Trusted), Target(Target)
  138. {
  139. }
  140. /*}}}*/
  141. std::string pkgIndexTargetFile::ArchiveURI(std::string File) const/*{{{*/
  142. {
  143. return Target.Option(IndexTarget::REPO_URI) + File;
  144. }
  145. /*}}}*/
  146. std::string pkgIndexTargetFile::Describe(bool Short) const /*{{{*/
  147. {
  148. if (Short)
  149. return Target.Description;
  150. return Target.Description + " (" + IndexFileName() + ")";
  151. }
  152. /*}}}*/
  153. std::string pkgIndexTargetFile::IndexFileName() const /*{{{*/
  154. {
  155. std::string const s =_config->FindDir("Dir::State::lists") + URItoFileName(Target.URI);
  156. if (FileExists(s))
  157. return s;
  158. std::vector<std::string> types = APT::Configuration::getCompressionTypes();
  159. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  160. {
  161. std::string p = s + '.' + *t;
  162. if (FileExists(p))
  163. return p;
  164. }
  165. return s;
  166. }
  167. /*}}}*/
  168. unsigned long pkgIndexTargetFile::Size() const /*{{{*/
  169. {
  170. unsigned long size = 0;
  171. /* we need to ignore errors here; if the lists are absent, just return 0 */
  172. _error->PushToStack();
  173. FileFd f(IndexFileName(), FileFd::ReadOnly, FileFd::Extension);
  174. if (!f.Failed())
  175. size = f.Size();
  176. if (_error->PendingError() == true)
  177. size = 0;
  178. _error->RevertToStack();
  179. return size;
  180. }
  181. /*}}}*/
  182. bool pkgIndexTargetFile::Exists() const /*{{{*/
  183. {
  184. return FileExists(IndexFileName());
  185. }
  186. /*}}}*/