debmetaindex.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // ijones, walters
  2. #include <apt-pkg/debmetaindex.h>
  3. #include <apt-pkg/debindexfile.h>
  4. #include <apt-pkg/strutl.h>
  5. #include <apt-pkg/acquire-item.h>
  6. #include <apt-pkg/configuration.h>
  7. #include <apt-pkg/aptconfiguration.h>
  8. #include <apt-pkg/error.h>
  9. using namespace std;
  10. string debReleaseIndex::Info(const char *Type, const string Section) const
  11. {
  12. string Info = ::URI::SiteOnly(URI) + ' ';
  13. if (Dist[Dist.size() - 1] == '/')
  14. {
  15. if (Dist != "/")
  16. Info += Dist;
  17. }
  18. else
  19. Info += Dist + '/' + Section;
  20. Info += " ";
  21. Info += Type;
  22. return Info;
  23. }
  24. string debReleaseIndex::MetaIndexInfo(const char *Type) const
  25. {
  26. string Info = ::URI::SiteOnly(URI) + ' ';
  27. if (Dist[Dist.size() - 1] == '/')
  28. {
  29. if (Dist != "/")
  30. Info += Dist;
  31. }
  32. else
  33. Info += Dist;
  34. Info += " ";
  35. Info += Type;
  36. return Info;
  37. }
  38. string debReleaseIndex::MetaIndexFile(const char *Type) const
  39. {
  40. return _config->FindDir("Dir::State::lists") +
  41. URItoFileName(MetaIndexURI(Type));
  42. }
  43. string debReleaseIndex::MetaIndexURI(const char *Type) const
  44. {
  45. string Res;
  46. if (Dist == "/")
  47. Res = URI;
  48. else if (Dist[Dist.size()-1] == '/')
  49. Res = URI + Dist;
  50. else
  51. Res = URI + "dists/" + Dist + "/";
  52. Res += Type;
  53. return Res;
  54. }
  55. string debReleaseIndex::IndexURISuffix(const char *Type, const string Section) const
  56. {
  57. string Res ="";
  58. if (Dist[Dist.size() - 1] != '/')
  59. Res += Section + "/binary-" + _config->Find("APT::Architecture") + '/';
  60. return Res + Type;
  61. }
  62. string debReleaseIndex::IndexURI(const char *Type, const string Section) const
  63. {
  64. if (Dist[Dist.size() - 1] == '/')
  65. {
  66. string Res;
  67. if (Dist != "/")
  68. Res = URI + Dist;
  69. else
  70. Res = URI;
  71. return Res + Type;
  72. }
  73. else
  74. return URI + "dists/" + Dist + '/' + IndexURISuffix(Type, Section);
  75. }
  76. string debReleaseIndex::SourceIndexURISuffix(const char *Type, const string Section) const
  77. {
  78. string Res ="";
  79. if (Dist[Dist.size() - 1] != '/')
  80. Res += Section + "/source/";
  81. return Res + Type;
  82. }
  83. string debReleaseIndex::SourceIndexURI(const char *Type, const string Section) const
  84. {
  85. string Res;
  86. if (Dist[Dist.size() - 1] == '/')
  87. {
  88. if (Dist != "/")
  89. Res = URI + Dist;
  90. else
  91. Res = URI;
  92. return Res + Type;
  93. }
  94. else
  95. return URI + "dists/" + Dist + "/" + SourceIndexURISuffix(Type, Section);
  96. }
  97. debReleaseIndex::debReleaseIndex(string URI,string Dist)
  98. {
  99. this->URI = URI;
  100. this->Dist = Dist;
  101. this->Indexes = NULL;
  102. this->Type = "deb";
  103. }
  104. debReleaseIndex::~debReleaseIndex()
  105. {
  106. for (vector<const debSectionEntry *>::const_iterator I = SectionEntries.begin();
  107. I != SectionEntries.end(); I++)
  108. delete *I;
  109. }
  110. vector <struct IndexTarget *>* debReleaseIndex::ComputeIndexTargets() const
  111. {
  112. vector <struct IndexTarget *>* IndexTargets = new vector <IndexTarget *>;
  113. for (vector <const debSectionEntry *>::const_iterator I = SectionEntries.begin();
  114. I != SectionEntries.end();
  115. I++)
  116. {
  117. IndexTarget * Target = new IndexTarget();
  118. Target->ShortDesc = (*I)->IsSrc ? "Sources" : "Packages";
  119. Target->MetaKey
  120. = (*I)->IsSrc ? SourceIndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section)
  121. : IndexURISuffix(Target->ShortDesc.c_str(), (*I)->Section);
  122. Target->URI
  123. = (*I)->IsSrc ? SourceIndexURI(Target->ShortDesc.c_str(), (*I)->Section)
  124. : IndexURI(Target->ShortDesc.c_str(), (*I)->Section);
  125. Target->Description = Info (Target->ShortDesc.c_str(), (*I)->Section);
  126. IndexTargets->push_back (Target);
  127. }
  128. return IndexTargets;
  129. }
  130. /*}}}*/
  131. bool debReleaseIndex::GetIndexes(pkgAcquire *Owner, bool GetAll) const
  132. {
  133. // special case for --print-uris
  134. if (GetAll) {
  135. vector <struct IndexTarget *> *targets = ComputeIndexTargets();
  136. for (vector <struct IndexTarget*>::const_iterator Target = targets->begin(); Target != targets->end(); Target++) {
  137. new pkgAcqIndex(Owner, (*Target)->URI, (*Target)->Description,
  138. (*Target)->ShortDesc, HashString());
  139. }
  140. // this is normally created in pkgAcqMetaSig, but if we run
  141. // in --print-uris mode, we add it here
  142. new pkgAcqMetaIndex(Owner, MetaIndexURI("Release"),
  143. MetaIndexInfo("Release"), "Release",
  144. MetaIndexURI("Release.gpg"),
  145. ComputeIndexTargets(),
  146. new indexRecords (Dist));
  147. }
  148. new pkgAcqMetaSig(Owner, MetaIndexURI("Release.gpg"),
  149. MetaIndexInfo("Release.gpg"), "Release.gpg",
  150. MetaIndexURI("Release"), MetaIndexInfo("Release"), "Release",
  151. ComputeIndexTargets(),
  152. new indexRecords (Dist));
  153. // Queue the translations
  154. std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
  155. for (vector<const debSectionEntry *>::const_iterator I = SectionEntries.begin();
  156. I != SectionEntries.end(); I++) {
  157. if((*I)->IsSrc)
  158. continue;
  159. for (vector<string>::const_iterator l = lang.begin();
  160. l != lang.end(); l++)
  161. {
  162. debTranslationsIndex i = debTranslationsIndex(URI,Dist,(*I)->Section,(*l).c_str());
  163. i.GetIndexes(Owner);
  164. }
  165. }
  166. return true;
  167. }
  168. bool debReleaseIndex::IsTrusted() const
  169. {
  170. string VerifiedSigFile = _config->FindDir("Dir::State::lists") +
  171. URItoFileName(MetaIndexURI("Release")) + ".gpg";
  172. if(_config->FindB("APT::Authentication::TrustCDROM", false))
  173. if(URI.substr(0,strlen("cdrom:")) == "cdrom:")
  174. return true;
  175. if (FileExists(VerifiedSigFile))
  176. return true;
  177. return false;
  178. }
  179. vector <pkgIndexFile *> *debReleaseIndex::GetIndexFiles()
  180. {
  181. if (Indexes != NULL)
  182. return Indexes;
  183. Indexes = new vector <pkgIndexFile*>;
  184. std::vector<std::string> const lang = APT::Configuration::getLanguages(true);
  185. for (vector<const debSectionEntry *>::const_iterator I = SectionEntries.begin();
  186. I != SectionEntries.end(); I++) {
  187. if ((*I)->IsSrc)
  188. Indexes->push_back(new debSourcesIndex (URI, Dist, (*I)->Section, IsTrusted()));
  189. else
  190. {
  191. Indexes->push_back(new debPackagesIndex (URI, Dist, (*I)->Section, IsTrusted()));
  192. for (vector<string>::const_iterator l = lang.begin();
  193. l != lang.end(); l++)
  194. Indexes->push_back(new debTranslationsIndex(URI,Dist,(*I)->Section,(*l).c_str()));
  195. }
  196. }
  197. return Indexes;
  198. }
  199. void debReleaseIndex::PushSectionEntry(const debSectionEntry *Entry)
  200. {
  201. SectionEntries.push_back(Entry);
  202. }
  203. debReleaseIndex::debSectionEntry::debSectionEntry (string Section, bool IsSrc): Section(Section)
  204. {
  205. this->IsSrc = IsSrc;
  206. }
  207. class debSLTypeDebian : public pkgSourceList::Type
  208. {
  209. protected:
  210. bool CreateItemInternal(vector<metaIndex *> &List,string URI,
  211. string Dist,string Section,
  212. bool IsSrc) const
  213. {
  214. for (vector<metaIndex *>::const_iterator I = List.begin();
  215. I != List.end(); I++)
  216. {
  217. // This check insures that there will be only one Release file
  218. // queued for all the Packages files and Sources files it
  219. // corresponds to.
  220. if (strcmp((*I)->GetType(), "deb") == 0)
  221. {
  222. debReleaseIndex *Deb = (debReleaseIndex *) (*I);
  223. // This check insures that there will be only one Release file
  224. // queued for all the Packages files and Sources files it
  225. // corresponds to.
  226. if (Deb->GetURI() == URI && Deb->GetDist() == Dist)
  227. {
  228. Deb->PushSectionEntry(new debReleaseIndex::debSectionEntry(Section, IsSrc));
  229. return true;
  230. }
  231. }
  232. }
  233. // No currently created Release file indexes this entry, so we create a new one.
  234. // XXX determine whether this release is trusted or not
  235. debReleaseIndex *Deb = new debReleaseIndex(URI,Dist);
  236. Deb->PushSectionEntry (new debReleaseIndex::debSectionEntry(Section, IsSrc));
  237. List.push_back(Deb);
  238. return true;
  239. }
  240. };
  241. class debSLTypeDeb : public debSLTypeDebian
  242. {
  243. public:
  244. bool CreateItem(vector<metaIndex *> &List,string URI,
  245. string Dist,string Section) const
  246. {
  247. return CreateItemInternal(List, URI, Dist, Section, false);
  248. }
  249. debSLTypeDeb()
  250. {
  251. Name = "deb";
  252. Label = "Standard Debian binary tree";
  253. }
  254. };
  255. class debSLTypeDebSrc : public debSLTypeDebian
  256. {
  257. public:
  258. bool CreateItem(vector<metaIndex *> &List,string URI,
  259. string Dist,string Section) const
  260. {
  261. return CreateItemInternal(List, URI, Dist, Section, true);
  262. }
  263. debSLTypeDebSrc()
  264. {
  265. Name = "deb-src";
  266. Label = "Standard Debian source tree";
  267. }
  268. };
  269. debSLTypeDeb _apt_DebType;
  270. debSLTypeDebSrc _apt_DebSrcType;