debmetaindex.cc 7.9 KB

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