3 #include <apt-pkg/error.h>
4 #include <apt-pkg/debmetaindex.h>
5 #include <apt-pkg/debindexfile.h>
6 #include <apt-pkg/strutl.h>
7 #include <apt-pkg/fileutl.h>
8 #include <apt-pkg/acquire-item.h>
9 #include <apt-pkg/configuration.h>
10 #include <apt-pkg/aptconfiguration.h>
11 #include <apt-pkg/indexrecords.h>
12 #include <apt-pkg/sourcelist.h>
13 #include <apt-pkg/hashes.h>
14 #include <apt-pkg/metaindex.h>
15 #include <apt-pkg/pkgcachegen.h>
16 #include <apt-pkg/tagfile.h>
17 #include <apt-pkg/gpgv.h>
18 #include <apt-pkg/macros.h>
27 #include <sys/types.h>
34 string
debReleaseIndex::MetaIndexInfo(const char *Type
) const
36 string Info
= ::URI::ArchiveOnly(URI
) + ' ';
37 if (Dist
[Dist
.size() - 1] == '/')
48 std::string
debReleaseIndex::Describe() const
50 return MetaIndexInfo("Release");
53 string
debReleaseIndex::MetaIndexFile(const char *Type
) const
55 return _config
->FindDir("Dir::State::lists") +
56 URItoFileName(MetaIndexURI(Type
));
59 string
debReleaseIndex::MetaIndexURI(const char *Type
) const
65 else if (Dist
[Dist
.size()-1] == '/')
68 Res
= URI
+ "dists/" + Dist
+ "/";
74 std::string
debReleaseIndex::LocalFileName() const
76 // see if we have a InRelease file
77 std::string PathInRelease
= MetaIndexFile("InRelease");
78 if (FileExists(PathInRelease
))
81 // and if not return the normal one
82 if (FileExists(PathInRelease
))
83 return MetaIndexFile("Release");
88 debReleaseIndex::debReleaseIndex(string
const &URI
, string
const &Dist
) :
89 metaIndex(URI
, Dist
, "deb"), Trusted(CHECK_TRUST
)
92 debReleaseIndex::debReleaseIndex(string
const &URI
, string
const &Dist
, bool const Trusted
) :
93 metaIndex(URI
, Dist
, "deb") {
97 debReleaseIndex::~debReleaseIndex() {
98 for (map
<string
, vector
<debSectionEntry
const*> >::const_iterator A
= ArchEntries
.begin();
99 A
!= ArchEntries
.end(); ++A
)
100 for (vector
<const debSectionEntry
*>::const_iterator S
= A
->second
.begin();
101 S
!= A
->second
.end(); ++S
)
105 template<typename CallC
>
106 void foreachTarget(std::string
const URI
, std::string
const Dist
,
107 std::map
<std::string
, std::vector
<debReleaseIndex::debSectionEntry
const *> > const &ArchEntries
,
110 bool const flatArchive
= (Dist
[Dist
.length() - 1] == '/');
111 std::string baseURI
= URI
;
118 baseURI
+= "dists/" + Dist
+ "/";
119 std::string
const Release
= (Dist
== "/") ? "" : Dist
;
120 std::string
const Site
= ::URI::ArchiveOnly(URI
);
121 std::vector
<std::string
> lang
= APT::Configuration::getLanguages(true);
123 lang
.push_back("none");
124 map
<string
, vector
<debReleaseIndex::debSectionEntry
const*> >::const_iterator
const src
= ArchEntries
.find("source");
125 if (src
!= ArchEntries
.end())
127 std::vector
<std::string
> const targets
= _config
->FindVector("APT::Acquire::Targets::deb-src", "", true);
128 for (std::vector
<std::string
>::const_iterator T
= targets
.begin(); T
!= targets
.end(); ++T
)
130 #define APT_T_CONFIG(X) _config->Find(std::string("APT::Acquire::Targets::deb-src::") + *T + "::" + (X))
131 std::string
const MetaKey
= APT_T_CONFIG(flatArchive
? "flatMetaKey" : "MetaKey");
132 std::string
const ShortDesc
= APT_T_CONFIG("ShortDescription");
133 std::string
const LongDesc
= APT_T_CONFIG(flatArchive
? "flatDescription" : "Description");
134 bool const IsOptional
= _config
->FindB(std::string("APT::Acquire::Targets::deb-src::") + *T
+ "::Optional", true);
139 vector
<debReleaseIndex::debSectionEntry
const*> const SectionEntries
= src
->second
;
140 for (vector
<debReleaseIndex::debSectionEntry
const*>::const_iterator I
= SectionEntries
.begin();
141 I
!= SectionEntries
.end(); ++I
)
143 for (vector
<std::string
>::const_iterator l
= lang
.begin(); l
!= lang
.end(); ++l
)
145 if (*l
== "none" && MetaKey
.find("$(LANGUAGE)") != std::string::npos
)
148 std::map
<std::string
, std::string
> Options
;
149 Options
.insert(std::make_pair("SITE", Site
));
150 Options
.insert(std::make_pair("RELEASE", Release
));
151 if (MetaKey
.find("$(COMPONENT)") != std::string::npos
)
152 Options
.insert(std::make_pair("COMPONENT", (*I
)->Section
));
153 if (MetaKey
.find("$(LANGUAGE)") != std::string::npos
)
154 Options
.insert(std::make_pair("LANGUAGE", *l
));
155 Options
.insert(std::make_pair("ARCHITECTURE", "source"));
156 Options
.insert(std::make_pair("BASE_URI", baseURI
));
157 Options
.insert(std::make_pair("REPO_URI", URI
));
158 Options
.insert(std::make_pair("TARGET_OF", "deb-src"));
159 Options
.insert(std::make_pair("CREATED_BY", *T
));
160 Call(MetaKey
, ShortDesc
, LongDesc
, IsOptional
, Options
);
162 if (MetaKey
.find("$(LANGUAGE)") == std::string::npos
)
166 if (MetaKey
.find("$(COMPONENT)") == std::string::npos
)
172 std::vector
<std::string
> const targets
= _config
->FindVector("APT::Acquire::Targets::deb", "", true);
173 for (std::vector
<std::string
>::const_iterator T
= targets
.begin(); T
!= targets
.end(); ++T
)
175 #define APT_T_CONFIG(X) _config->Find(std::string("APT::Acquire::Targets::deb::") + *T + "::" + (X))
176 std::string
const MetaKey
= APT_T_CONFIG(flatArchive
? "flatMetaKey" : "MetaKey");
177 std::string
const ShortDesc
= APT_T_CONFIG("ShortDescription");
178 std::string
const LongDesc
= APT_T_CONFIG(flatArchive
? "flatDescription" : "Description");
179 bool const IsOptional
= _config
->FindB(std::string("APT::Acquire::Targets::deb::") + *T
+ "::Optional", true);
184 for (map
<string
, vector
<debReleaseIndex::debSectionEntry
const*> >::const_iterator a
= ArchEntries
.begin();
185 a
!= ArchEntries
.end(); ++a
)
187 if (a
->first
== "source")
190 for (vector
<const debReleaseIndex::debSectionEntry
*>::const_iterator I
= a
->second
.begin();
191 I
!= a
->second
.end(); ++I
) {
193 for (vector
<std::string
>::const_iterator l
= lang
.begin(); l
!= lang
.end(); ++l
)
195 if (*l
== "none" && MetaKey
.find("$(LANGUAGE)") != std::string::npos
)
198 std::map
<std::string
, std::string
> Options
;
199 Options
.insert(std::make_pair("SITE", Site
));
200 Options
.insert(std::make_pair("RELEASE", Release
));
201 if (MetaKey
.find("$(COMPONENT)") != std::string::npos
)
202 Options
.insert(std::make_pair("COMPONENT", (*I
)->Section
));
203 if (MetaKey
.find("$(LANGUAGE)") != std::string::npos
)
204 Options
.insert(std::make_pair("LANGUAGE", *l
));
205 if (MetaKey
.find("$(ARCHITECTURE)") != std::string::npos
)
206 Options
.insert(std::make_pair("ARCHITECTURE", a
->first
));
207 Options
.insert(std::make_pair("BASE_URI", baseURI
));
208 Options
.insert(std::make_pair("REPO_URI", URI
));
209 Options
.insert(std::make_pair("TARGET_OF", "deb"));
210 Options
.insert(std::make_pair("CREATED_BY", *T
));
211 Call(MetaKey
, ShortDesc
, LongDesc
, IsOptional
, Options
);
213 if (MetaKey
.find("$(LANGUAGE)") == std::string::npos
)
217 if (MetaKey
.find("$(COMPONENT)") == std::string::npos
)
221 if (MetaKey
.find("$(ARCHITECTURE)") == std::string::npos
)
228 struct ComputeIndexTargetsClass
230 vector
<IndexTarget
> IndexTargets
;
232 void operator()(std::string MetaKey
, std::string ShortDesc
, std::string LongDesc
,
233 bool const IsOptional
, std::map
<std::string
, std::string
> Options
)
235 for (std::map
<std::string
, std::string
>::const_iterator O
= Options
.begin(); O
!= Options
.end(); ++O
)
237 MetaKey
= SubstVar(MetaKey
, std::string("$(") + O
->first
+ ")", O
->second
);
238 ShortDesc
= SubstVar(ShortDesc
, std::string("$(") + O
->first
+ ")", O
->second
);
239 LongDesc
= SubstVar(LongDesc
, std::string("$(") + O
->first
+ ")", O
->second
);
245 Options
.find("BASE_URI")->second
+ MetaKey
,
249 IndexTargets
.push_back(Target
);
253 std::vector
<IndexTarget
> debReleaseIndex::GetIndexTargets() const
255 ComputeIndexTargetsClass comp
;
256 foreachTarget(URI
, Dist
, ArchEntries
, comp
);
257 return comp
.IndexTargets
;
262 bool debReleaseIndex::GetIndexes(pkgAcquire
*Owner
, bool const &GetAll
) const
264 indexRecords
* const iR
= new indexRecords(Dist
);
265 if (Trusted
== ALWAYS_TRUSTED
)
266 iR
->SetTrusted(true);
267 else if (Trusted
== NEVER_TRUSTED
)
268 iR
->SetTrusted(false);
270 // special case for --print-uris
271 std::vector
<IndexTarget
> const targets
= GetIndexTargets();
272 #define APT_TARGET(X) IndexTarget("", X, MetaIndexInfo(X), MetaIndexURI(X), false, std::map<std::string,std::string>())
273 pkgAcqMetaBase
* const TransactionManager
= new pkgAcqMetaClearSig(Owner
,
274 APT_TARGET("InRelease"), APT_TARGET("Release"), APT_TARGET("Release.gpg"),
279 for (std::vector
<IndexTarget
>::const_iterator Target
= targets
.begin(); Target
!= targets
.end(); ++Target
)
280 new pkgAcqIndex(Owner
, TransactionManager
, *Target
);
286 void debReleaseIndex::SetTrusted(bool const Trusted
)
289 this->Trusted
= ALWAYS_TRUSTED
;
291 this->Trusted
= NEVER_TRUSTED
;
294 bool debReleaseIndex::IsTrusted() const
296 if (Trusted
== ALWAYS_TRUSTED
)
298 else if (Trusted
== NEVER_TRUSTED
)
302 if(_config
->FindB("APT::Authentication::TrustCDROM", false))
303 if(URI
.substr(0,strlen("cdrom:")) == "cdrom:")
306 string VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
307 URItoFileName(MetaIndexURI("Release")) + ".gpg";
309 if (FileExists(VerifiedSigFile
))
312 VerifiedSigFile
= _config
->FindDir("Dir::State::lists") +
313 URItoFileName(MetaIndexURI("InRelease"));
315 return FileExists(VerifiedSigFile
);
318 std::vector
<pkgIndexFile
*> *debReleaseIndex::GetIndexFiles()
323 Indexes
= new std::vector
<pkgIndexFile
*>();
324 std::vector
<IndexTarget
> const Targets
= GetIndexTargets();
325 bool const istrusted
= IsTrusted();
326 for (std::vector
<IndexTarget
>::const_iterator T
= Targets
.begin(); T
!= Targets
.end(); ++T
)
328 std::string
const TargetName
= T
->Option(IndexTarget::CREATED_BY
);
329 if (TargetName
== "Packages")
330 Indexes
->push_back(new debPackagesIndex(*T
, istrusted
));
331 else if (TargetName
== "Sources")
332 Indexes
->push_back(new debSourcesIndex(*T
, istrusted
));
333 else if (TargetName
== "Translations")
334 Indexes
->push_back(new debTranslationsIndex(*T
));
339 void debReleaseIndex::PushSectionEntry(vector
<string
> const &Archs
, const debSectionEntry
*Entry
) {
340 for (vector
<string
>::const_iterator a
= Archs
.begin();
341 a
!= Archs
.end(); ++a
)
342 ArchEntries
[*a
].push_back(new debSectionEntry(Entry
->Section
, Entry
->IsSrc
));
346 void debReleaseIndex::PushSectionEntry(string
const &Arch
, const debSectionEntry
*Entry
) {
347 ArchEntries
[Arch
].push_back(Entry
);
350 debReleaseIndex::debSectionEntry::debSectionEntry (string
const &Section
,
351 bool const &IsSrc
): Section(Section
), IsSrc(IsSrc
)
354 static bool ReleaseFileName(debReleaseIndex
const * const That
, std::string
&ReleaseFile
)
356 ReleaseFile
= That
->MetaIndexFile("InRelease");
357 bool releaseExists
= false;
358 if (FileExists(ReleaseFile
) == true)
359 releaseExists
= true;
362 ReleaseFile
= That
->MetaIndexFile("Release");
363 if (FileExists(ReleaseFile
))
364 releaseExists
= true;
366 return releaseExists
;
369 bool debReleaseIndex::Merge(pkgCacheGenerator
&Gen
,OpProgress
* /*Prog*/) const/*{{{*/
371 std::string ReleaseFile
;
372 bool const releaseExists
= ReleaseFileName(this, ReleaseFile
);
375 if (Gen
.SelectReleaseFile(ReleaseFile
, Tmp
.Host
) == false)
376 return _error
->Error("Problem with SelectReleaseFile %s", ReleaseFile
.c_str());
378 if (releaseExists
== false)
382 // Beware: The 'Release' file might be clearsigned in case the
383 // signature for an 'InRelease' file couldn't be checked
384 if (OpenMaybeClearSignedFile(ReleaseFile
, Rel
) == false)
386 if (_error
->PendingError() == true)
389 // Store the IMS information
390 pkgCache::RlsFileIterator File
= Gen
.GetCurRlsFile();
391 pkgCacheGenerator::Dynamic
<pkgCache::RlsFileIterator
> DynFile(File
);
392 // Rel can't be used as this is potentially a temporary file
394 if (stat(ReleaseFile
.c_str(), &Buf
) != 0)
395 return _error
->Errno("fstat", "Unable to stat file %s", ReleaseFile
.c_str());
396 File
->Size
= Buf
.st_size
;
397 File
->mtime
= Buf
.st_mtime
;
399 pkgTagFile
TagFile(&Rel
, Rel
.Size());
400 pkgTagSection Section
;
401 if (_error
->PendingError() == true || TagFile
.Step(Section
) == false)
405 #define APT_INRELEASE(TYPE, TAG, STORE) \
406 data = Section.FindS(TAG); \
407 if (data.empty() == false) \
409 map_stringitem_t const storage = Gen.StoreString(pkgCacheGenerator::TYPE, data); \
412 APT_INRELEASE(MIXED
, "Suite", File
->Archive
)
413 APT_INRELEASE(VERSIONNUMBER
, "Version", File
->Version
)
414 APT_INRELEASE(MIXED
, "Origin", File
->Origin
)
415 APT_INRELEASE(MIXED
, "Codename", File
->Codename
)
416 APT_INRELEASE(MIXED
, "Label", File
->Label
)
418 Section
.FindFlag("NotAutomatic", File
->Flags
, pkgCache::Flag::NotAutomatic
);
419 Section
.FindFlag("ButAutomaticUpgrades", File
->Flags
, pkgCache::Flag::ButAutomaticUpgrades
);
421 return !_error
->PendingError();
424 // ReleaseIndex::FindInCache - Find this index /*{{{*/
425 pkgCache::RlsFileIterator
debReleaseIndex::FindInCache(pkgCache
&Cache
) const
427 std::string ReleaseFile
;
428 bool const releaseExists
= ReleaseFileName(this, ReleaseFile
);
430 pkgCache::RlsFileIterator File
= Cache
.RlsFileBegin();
431 for (; File
.end() == false; ++File
)
433 if (File
->FileName
== 0 || ReleaseFile
!= File
.FileName())
436 // empty means the file does not exist by "design"
437 if (releaseExists
== false && File
->Size
== 0)
441 if (stat(File
.FileName(),&St
) != 0)
443 if (_config
->FindB("Debug::pkgCacheGen", false))
444 std::clog
<< "ReleaseIndex::FindInCache - stat failed on " << File
.FileName() << std::endl
;
445 return pkgCache::RlsFileIterator(Cache
);
447 if ((unsigned)St
.st_size
!= File
->Size
|| St
.st_mtime
!= File
->mtime
)
449 if (_config
->FindB("Debug::pkgCacheGen", false))
450 std::clog
<< "ReleaseIndex::FindInCache - size (" << St
.st_size
<< " <> " << File
->Size
451 << ") or mtime (" << St
.st_mtime
<< " <> " << File
->mtime
452 << ") doesn't match for " << File
.FileName() << std::endl
;
453 return pkgCache::RlsFileIterator(Cache
);
462 class APT_HIDDEN debSLTypeDebian
: public pkgSourceList::Type
466 bool CreateItemInternal(vector
<metaIndex
*> &List
, string
const &URI
,
467 string
const &Dist
, string
const &Section
,
468 bool const &IsSrc
, map
<string
, string
> const &Options
) const
470 // parse arch=, arch+= and arch-= settings
471 map
<string
, string
>::const_iterator arch
= Options
.find("arch");
472 vector
<string
> Archs
;
473 if (arch
!= Options
.end())
474 Archs
= VectorizeString(arch
->second
, ',');
476 Archs
= APT::Configuration::getArchitectures();
478 if ((arch
= Options
.find("arch+")) != Options
.end())
480 std::vector
<std::string
> const plusArch
= VectorizeString(arch
->second
, ',');
481 for (std::vector
<std::string
>::const_iterator plus
= plusArch
.begin(); plus
!= plusArch
.end(); ++plus
)
482 if (std::find(Archs
.begin(), Archs
.end(), *plus
) == Archs
.end())
483 Archs
.push_back(*plus
);
485 if ((arch
= Options
.find("arch-")) != Options
.end())
487 std::vector
<std::string
> const minusArch
= VectorizeString(arch
->second
, ',');
488 for (std::vector
<std::string
>::const_iterator minus
= minusArch
.begin(); minus
!= minusArch
.end(); ++minus
)
490 std::vector
<std::string
>::iterator kill
= std::find(Archs
.begin(), Archs
.end(), *minus
);
491 if (kill
!= Archs
.end())
496 map
<string
, string
>::const_iterator
const trusted
= Options
.find("trusted");
498 debReleaseIndex
*Deb
= NULL
;
499 for (vector
<metaIndex
*>::const_iterator I
= List
.begin();
500 I
!= List
.end(); ++I
)
502 // We only worry about debian entries here
503 if (strcmp((*I
)->GetType(), "deb") != 0)
506 /* This check insures that there will be only one Release file
507 queued for all the Packages files and Sources files it
509 if ((*I
)->GetURI() == URI
&& (*I
)->GetDist() == Dist
)
511 Deb
= dynamic_cast<debReleaseIndex
*>(*I
);
517 // No currently created Release file indexes this entry, so we create a new one.
520 Deb
= new debReleaseIndex(URI
, Dist
);
525 Deb
->PushSectionEntry ("source", new debReleaseIndex::debSectionEntry(Section
, IsSrc
));
528 if (Dist
[Dist
.size() - 1] == '/')
529 Deb
->PushSectionEntry ("any", new debReleaseIndex::debSectionEntry(Section
, IsSrc
));
531 Deb
->PushSectionEntry (Archs
, new debReleaseIndex::debSectionEntry(Section
, IsSrc
));
534 if (trusted
!= Options
.end())
535 Deb
->SetTrusted(StringToBool(trusted
->second
, false));
541 debDebFileMetaIndex::debDebFileMetaIndex(std::string
const &DebFile
)
542 : metaIndex(DebFile
, "local-uri", "deb-dist"), DebFile(DebFile
)
544 DebIndex
= new debDebPkgFileIndex(DebFile
);
545 Indexes
= new vector
<pkgIndexFile
*>();
546 Indexes
->push_back(DebIndex
);
550 class APT_HIDDEN debSLTypeDeb
: public debSLTypeDebian
554 bool CreateItem(vector
<metaIndex
*> &List
, string
const &URI
,
555 string
const &Dist
, string
const &Section
,
556 std::map
<string
, string
> const &Options
) const
558 return CreateItemInternal(List
, URI
, Dist
, Section
, false, Options
);
564 Label
= "Standard Debian binary tree";
568 class APT_HIDDEN debSLTypeDebSrc
: public debSLTypeDebian
572 bool CreateItem(vector
<metaIndex
*> &List
, string
const &URI
,
573 string
const &Dist
, string
const &Section
,
574 std::map
<string
, string
> const &Options
) const
576 return CreateItemInternal(List
, URI
, Dist
, Section
, true, Options
);
582 Label
= "Standard Debian source tree";
586 class APT_HIDDEN debSLTypeDebFile
: public pkgSourceList::Type
590 bool CreateItem(vector
<metaIndex
*> &List
, string
const &URI
,
591 string
const &/*Dist*/, string
const &/*Section*/,
592 std::map
<string
, string
> const &/*Options*/) const
594 metaIndex
*mi
= new debDebFileMetaIndex(URI
);
602 Label
= "Debian Deb File";
606 APT_HIDDEN debSLTypeDeb _apt_DebType
;
607 APT_HIDDEN debSLTypeDebSrc _apt_DebSrcType
;
608 APT_HIDDEN debSLTypeDebFile _apt_DebFileType
;