indexfile.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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/pkgcachegen.h>
  17. #include <apt-pkg/cacheiterators.h>
  18. #include <apt-pkg/srcrecords.h>
  19. #include <apt-pkg/strutl.h>
  20. #include <apt-pkg/progress.h>
  21. #include <apt-pkg/deblistparser.h>
  22. #include <apt-pkg/macros.h>
  23. #include <apt-pkg/debindexfile.h>
  24. #include <sys/stat.h>
  25. #include <string>
  26. #include <vector>
  27. #include <clocale>
  28. #include <cstring>
  29. #include <memory>
  30. /*}}}*/
  31. // Global list of Item supported
  32. static pkgIndexFile::Type *ItmList[10];
  33. pkgIndexFile::Type **pkgIndexFile::Type::GlobalList = ItmList;
  34. unsigned long pkgIndexFile::Type::GlobalListLen = 0;
  35. // Type::Type - Constructor /*{{{*/
  36. // ---------------------------------------------------------------------
  37. /* */
  38. pkgIndexFile::Type::Type()
  39. {
  40. ItmList[GlobalListLen] = this;
  41. GlobalListLen++;
  42. Label = NULL;
  43. }
  44. /*}}}*/
  45. // Type::GetType - Locate the type by name /*{{{*/
  46. // ---------------------------------------------------------------------
  47. /* */
  48. pkgIndexFile::Type *pkgIndexFile::Type::GetType(const char *Type)
  49. {
  50. for (unsigned I = 0; I != GlobalListLen; I++)
  51. if (strcmp(GlobalList[I]->Label,Type) == 0)
  52. return GlobalList[I];
  53. return 0;
  54. }
  55. /*}}}*/
  56. pkgIndexFile::pkgIndexFile(bool const Trusted) : /*{{{*/
  57. d(NULL), Trusted(Trusted)
  58. {
  59. }
  60. /*}}}*/
  61. // IndexFile::ArchiveInfo - Stub /*{{{*/
  62. std::string pkgIndexFile::ArchiveInfo(pkgCache::VerIterator const &Ver) const
  63. {
  64. debDebPkgFileIndex const * const debfile = dynamic_cast<debDebPkgFileIndex const*>(this);
  65. if (debfile != nullptr)
  66. return debfile->ArchiveInfo_impl(Ver);
  67. return std::string();
  68. }
  69. /*}}}*/
  70. // IndexFile::FindInCache - Stub /*{{{*/
  71. pkgCache::PkgFileIterator pkgIndexFile::FindInCache(pkgCache &Cache) const
  72. {
  73. return pkgCache::PkgFileIterator(Cache);
  74. }
  75. /*}}}*/
  76. // IndexFile::SourceIndex - Stub /*{{{*/
  77. std::string pkgIndexFile::SourceInfo(pkgSrcRecords::Parser const &/*Record*/,
  78. pkgSrcRecords::File const &/*File*/) const
  79. {
  80. return std::string();
  81. }
  82. /*}}}*/
  83. // IndexFile::TranslationsAvailable - Check if will use Translation /*{{{*/
  84. bool pkgIndexFile::TranslationsAvailable() {
  85. return (APT::Configuration::getLanguages().empty() != true);
  86. }
  87. /*}}}*/
  88. // IndexFile::CheckLanguageCode - Check the Language Code /*{{{*/
  89. bool pkgIndexFile::CheckLanguageCode(const char * const Lang)
  90. {
  91. if (strlen(Lang) == 2 || (strlen(Lang) == 5 && Lang[2] == '_'))
  92. return true;
  93. if (strcmp(Lang,"C") != 0)
  94. _error->Warning("Wrong language code %s", Lang);
  95. return false;
  96. }
  97. /*}}}*/
  98. // IndexFile::LanguageCode - Return the Language Code /*{{{*/
  99. std::string pkgIndexFile::LanguageCode() {
  100. APT_IGNORE_DEPRECATED_PUSH
  101. if (TranslationsAvailable() == false)
  102. return "";
  103. return APT::Configuration::getLanguages()[0];
  104. APT_IGNORE_DEPRECATED_POP
  105. }
  106. /*}}}*/
  107. // IndexTarget - Constructor /*{{{*/
  108. IndexTarget::IndexTarget(std::string const &MetaKey, std::string const &ShortDesc,
  109. std::string const &LongDesc, std::string const &URI, bool const IsOptional,
  110. bool const KeepCompressed, std::map<std::string, std::string> const &Options) :
  111. URI(URI), Description(LongDesc), ShortDesc(ShortDesc), MetaKey(MetaKey),
  112. IsOptional(IsOptional), KeepCompressed(KeepCompressed), Options(Options)
  113. {
  114. }
  115. /*}}}*/
  116. std::string IndexTarget::Option(OptionKeys const EnumKey) const /*{{{*/
  117. {
  118. std::string Key;
  119. switch (EnumKey)
  120. {
  121. #define APT_CASE(X) case X: Key = #X; break
  122. APT_CASE(SITE);
  123. APT_CASE(RELEASE);
  124. APT_CASE(COMPONENT);
  125. APT_CASE(LANGUAGE);
  126. APT_CASE(ARCHITECTURE);
  127. APT_CASE(BASE_URI);
  128. APT_CASE(REPO_URI);
  129. APT_CASE(IDENTIFIER);
  130. APT_CASE(TARGET_OF);
  131. APT_CASE(CREATED_BY);
  132. APT_CASE(FALLBACK_OF);
  133. APT_CASE(PDIFFS);
  134. APT_CASE(DEFAULTENABLED);
  135. APT_CASE(COMPRESSIONTYPES);
  136. APT_CASE(SOURCESENTRY);
  137. APT_CASE(BY_HASH);
  138. APT_CASE(KEEPCOMPRESSEDAS);
  139. APT_CASE(ALLOW_INSECURE);
  140. APT_CASE(ALLOW_WEAK);
  141. APT_CASE(ALLOW_DOWNGRADE_TO_INSECURE);
  142. #undef APT_CASE
  143. case FILENAME:
  144. {
  145. auto const M = Options.find("FILENAME");
  146. if (M == Options.end())
  147. return _config->FindDir("Dir::State::lists") + URItoFileName(URI);
  148. return M->second;
  149. }
  150. case EXISTING_FILENAME:
  151. std::string const filename = Option(FILENAME);
  152. std::vector<std::string> const types = VectorizeString(Option(COMPRESSIONTYPES), ' ');
  153. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  154. {
  155. if (t->empty())
  156. continue;
  157. std::string const file = (*t == "uncompressed") ? filename : (filename + "." + *t);
  158. if (FileExists(file))
  159. return file;
  160. }
  161. return "";
  162. }
  163. std::map<std::string,std::string>::const_iterator const M = Options.find(Key);
  164. if (M == Options.end())
  165. return "";
  166. return M->second;
  167. }
  168. /*}}}*/
  169. bool IndexTarget::OptionBool(OptionKeys const EnumKey) const /*{{{*/
  170. {
  171. return StringToBool(Option(EnumKey), false);
  172. }
  173. /*}}}*/
  174. std::string IndexTarget::Format(std::string format) const /*{{{*/
  175. {
  176. for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
  177. {
  178. format = SubstVar(format, std::string("$(") + O->first + ")", O->second);
  179. }
  180. format = SubstVar(format, "$(METAKEY)", MetaKey);
  181. format = SubstVar(format, "$(SHORTDESC)", ShortDesc);
  182. format = SubstVar(format, "$(DESCRIPTION)", Description);
  183. format = SubstVar(format, "$(URI)", URI);
  184. format = SubstVar(format, "$(FILENAME)", Option(IndexTarget::FILENAME));
  185. return format;
  186. }
  187. /*}}}*/
  188. pkgDebianIndexTargetFile::pkgDebianIndexTargetFile(IndexTarget const &Target, bool const Trusted) :/*{{{*/
  189. pkgDebianIndexFile(Trusted), d(NULL), Target(Target)
  190. {
  191. }
  192. /*}}}*/
  193. std::string pkgDebianIndexTargetFile::ArchiveURI(std::string const &File) const/*{{{*/
  194. {
  195. return Target.Option(IndexTarget::REPO_URI) + File;
  196. }
  197. /*}}}*/
  198. std::string pkgDebianIndexTargetFile::Describe(bool const Short) const /*{{{*/
  199. {
  200. if (Short)
  201. return Target.Description;
  202. return Target.Description + " (" + IndexFileName() + ")";
  203. }
  204. /*}}}*/
  205. std::string pkgDebianIndexTargetFile::IndexFileName() const /*{{{*/
  206. {
  207. std::string const s = Target.Option(IndexTarget::FILENAME);
  208. if (FileExists(s))
  209. return s;
  210. std::vector<std::string> const types = VectorizeString(Target.Option(IndexTarget::COMPRESSIONTYPES), ' ');
  211. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  212. {
  213. std::string p = s + '.' + *t;
  214. if (FileExists(p))
  215. return p;
  216. }
  217. return s;
  218. }
  219. /*}}}*/
  220. unsigned long pkgDebianIndexTargetFile::Size() const /*{{{*/
  221. {
  222. unsigned long size = 0;
  223. /* we need to ignore errors here; if the lists are absent, just return 0 */
  224. _error->PushToStack();
  225. FileFd f(IndexFileName(), FileFd::ReadOnly, FileFd::Extension);
  226. if (!f.Failed())
  227. size = f.Size();
  228. if (_error->PendingError() == true)
  229. size = 0;
  230. _error->RevertToStack();
  231. return size;
  232. }
  233. /*}}}*/
  234. bool pkgDebianIndexTargetFile::Exists() const /*{{{*/
  235. {
  236. return FileExists(IndexFileName());
  237. }
  238. /*}}}*/
  239. std::string pkgDebianIndexTargetFile::GetArchitecture() const /*{{{*/
  240. {
  241. return Target.Option(IndexTarget::ARCHITECTURE);
  242. }
  243. /*}}}*/
  244. std::string pkgDebianIndexTargetFile::GetComponent() const /*{{{*/
  245. {
  246. return Target.Option(IndexTarget::COMPONENT);
  247. }
  248. /*}}}*/
  249. bool pkgDebianIndexTargetFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
  250. {
  251. if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
  252. return _error->Error("Problem opening %s",FileName.c_str());
  253. return true;
  254. }
  255. /*}}}*/
  256. std::string pkgDebianIndexTargetFile::GetProgressDescription() const
  257. {
  258. return Target.Description;
  259. }
  260. pkgDebianIndexRealFile::pkgDebianIndexRealFile(std::string const &pFile, bool const Trusted) :/*{{{*/
  261. pkgDebianIndexFile(Trusted), d(NULL)
  262. {
  263. if (pFile.empty())
  264. ;
  265. else if (pFile == "/nonexistent/stdin")
  266. File = pFile;
  267. else
  268. File = flAbsPath(pFile);
  269. }
  270. /*}}}*/
  271. // IndexRealFile::Size - Return the size of the index /*{{{*/
  272. unsigned long pkgDebianIndexRealFile::Size() const
  273. {
  274. struct stat S;
  275. if (stat(File.c_str(),&S) != 0)
  276. return 0;
  277. return S.st_size;
  278. }
  279. /*}}}*/
  280. bool pkgDebianIndexRealFile::Exists() const /*{{{*/
  281. {
  282. return FileExists(File);
  283. }
  284. /*}}}*/
  285. std::string pkgDebianIndexRealFile::Describe(bool const /*Short*/) const/*{{{*/
  286. {
  287. return File;
  288. }
  289. /*}}}*/
  290. std::string pkgDebianIndexRealFile::ArchiveURI(std::string const &/*File*/) const/*{{{*/
  291. {
  292. return "file:" + File;
  293. }
  294. /*}}}*/
  295. std::string pkgDebianIndexRealFile::IndexFileName() const /*{{{*/
  296. {
  297. return File;
  298. }
  299. /*}}}*/
  300. std::string pkgDebianIndexRealFile::GetProgressDescription() const
  301. {
  302. return File;
  303. }
  304. bool pkgDebianIndexRealFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
  305. {
  306. if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
  307. return _error->Error("Problem opening %s",FileName.c_str());
  308. return true;
  309. }
  310. /*}}}*/
  311. pkgDebianIndexFile::pkgDebianIndexFile(bool const Trusted) : pkgIndexFile(Trusted)
  312. {
  313. }
  314. pkgDebianIndexFile::~pkgDebianIndexFile()
  315. {
  316. }
  317. pkgCacheListParser * pkgDebianIndexFile::CreateListParser(FileFd &Pkg)
  318. {
  319. if (Pkg.IsOpen() == false)
  320. return nullptr;
  321. _error->PushToStack();
  322. pkgCacheListParser * const Parser = new debListParser(&Pkg);
  323. bool const newError = _error->PendingError();
  324. _error->MergeWithStack();
  325. if (newError)
  326. {
  327. delete Parser;
  328. _error->ReturnError();
  329. return nullptr;
  330. }
  331. else
  332. return Parser;
  333. }
  334. bool pkgDebianIndexFile::Merge(pkgCacheGenerator &Gen,OpProgress * const Prog)
  335. {
  336. std::string const PackageFile = IndexFileName();
  337. FileFd Pkg;
  338. if (OpenListFile(Pkg, PackageFile) == false)
  339. return false;
  340. _error->PushToStack();
  341. std::unique_ptr<pkgCacheListParser> Parser(CreateListParser(Pkg));
  342. bool const newError = _error->PendingError();
  343. _error->MergeWithStack();
  344. if (newError == false && Parser == nullptr)
  345. return true;
  346. if (Parser == NULL)
  347. return false;
  348. if (Prog != NULL)
  349. Prog->SubProgress(0, GetProgressDescription());
  350. if (Gen.SelectFile(PackageFile, *this, GetArchitecture(), GetComponent(), GetIndexFlags()) == false)
  351. return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
  352. // Store the IMS information
  353. pkgCache::PkgFileIterator File = Gen.GetCurFile();
  354. pkgCacheGenerator::Dynamic<pkgCache::PkgFileIterator> DynFile(File);
  355. File->Size = Pkg.FileSize();
  356. File->mtime = Pkg.ModificationTime();
  357. if (Gen.MergeList(*Parser) == false)
  358. return _error->Error("Problem with MergeList %s",PackageFile.c_str());
  359. return true;
  360. }
  361. pkgCache::PkgFileIterator pkgDebianIndexFile::FindInCache(pkgCache &Cache) const
  362. {
  363. std::string const FileName = IndexFileName();
  364. pkgCache::PkgFileIterator File = Cache.FileBegin();
  365. for (; File.end() == false; ++File)
  366. {
  367. if (File.FileName() == NULL || FileName != File.FileName())
  368. continue;
  369. struct stat St;
  370. if (stat(File.FileName(),&St) != 0)
  371. {
  372. if (_config->FindB("Debug::pkgCacheGen", false))
  373. std::clog << "DebianIndexFile::FindInCache - stat failed on " << File.FileName() << std::endl;
  374. return pkgCache::PkgFileIterator(Cache);
  375. }
  376. if ((map_filesize_t)St.st_size != File->Size || St.st_mtime != File->mtime)
  377. {
  378. if (_config->FindB("Debug::pkgCacheGen", false))
  379. std::clog << "DebianIndexFile::FindInCache - size (" << St.st_size << " <> " << File->Size
  380. << ") or mtime (" << St.st_mtime << " <> " << File->mtime
  381. << ") doesn't match for " << File.FileName() << std::endl;
  382. return pkgCache::PkgFileIterator(Cache);
  383. }
  384. return File;
  385. }
  386. return File;
  387. }
  388. APT_CONST pkgIndexFile::~pkgIndexFile() {}
  389. APT_CONST pkgDebianIndexTargetFile::~pkgDebianIndexTargetFile() {}
  390. APT_CONST pkgDebianIndexRealFile::~pkgDebianIndexRealFile() {}