indexfile.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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: return _config->FindDir("Dir::State::lists") + URItoFileName(URI);
  144. case EXISTING_FILENAME:
  145. std::string const filename = Option(FILENAME);
  146. std::vector<std::string> const types = VectorizeString(Option(COMPRESSIONTYPES), ' ');
  147. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  148. {
  149. if (t->empty())
  150. continue;
  151. std::string const file = (*t == "uncompressed") ? filename : (filename + "." + *t);
  152. if (FileExists(file))
  153. return file;
  154. }
  155. return "";
  156. }
  157. std::map<std::string,std::string>::const_iterator const M = Options.find(Key);
  158. if (M == Options.end())
  159. return "";
  160. return M->second;
  161. }
  162. /*}}}*/
  163. bool IndexTarget::OptionBool(OptionKeys const EnumKey) const /*{{{*/
  164. {
  165. return StringToBool(Option(EnumKey), false);
  166. }
  167. /*}}}*/
  168. std::string IndexTarget::Format(std::string format) const /*{{{*/
  169. {
  170. for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
  171. {
  172. format = SubstVar(format, std::string("$(") + O->first + ")", O->second);
  173. }
  174. format = SubstVar(format, "$(METAKEY)", MetaKey);
  175. format = SubstVar(format, "$(SHORTDESC)", ShortDesc);
  176. format = SubstVar(format, "$(DESCRIPTION)", Description);
  177. format = SubstVar(format, "$(URI)", URI);
  178. format = SubstVar(format, "$(FILENAME)", Option(IndexTarget::FILENAME));
  179. return format;
  180. }
  181. /*}}}*/
  182. pkgDebianIndexTargetFile::pkgDebianIndexTargetFile(IndexTarget const &Target, bool const Trusted) :/*{{{*/
  183. pkgDebianIndexFile(Trusted), d(NULL), Target(Target)
  184. {
  185. }
  186. /*}}}*/
  187. std::string pkgDebianIndexTargetFile::ArchiveURI(std::string const &File) const/*{{{*/
  188. {
  189. return Target.Option(IndexTarget::REPO_URI) + File;
  190. }
  191. /*}}}*/
  192. std::string pkgDebianIndexTargetFile::Describe(bool const Short) const /*{{{*/
  193. {
  194. if (Short)
  195. return Target.Description;
  196. return Target.Description + " (" + IndexFileName() + ")";
  197. }
  198. /*}}}*/
  199. std::string pkgDebianIndexTargetFile::IndexFileName() const /*{{{*/
  200. {
  201. std::string const s = Target.Option(IndexTarget::FILENAME);
  202. if (FileExists(s))
  203. return s;
  204. std::vector<std::string> const types = VectorizeString(Target.Option(IndexTarget::COMPRESSIONTYPES), ' ');
  205. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  206. {
  207. std::string p = s + '.' + *t;
  208. if (FileExists(p))
  209. return p;
  210. }
  211. return s;
  212. }
  213. /*}}}*/
  214. unsigned long pkgDebianIndexTargetFile::Size() const /*{{{*/
  215. {
  216. unsigned long size = 0;
  217. /* we need to ignore errors here; if the lists are absent, just return 0 */
  218. _error->PushToStack();
  219. FileFd f(IndexFileName(), FileFd::ReadOnly, FileFd::Extension);
  220. if (!f.Failed())
  221. size = f.Size();
  222. if (_error->PendingError() == true)
  223. size = 0;
  224. _error->RevertToStack();
  225. return size;
  226. }
  227. /*}}}*/
  228. bool pkgDebianIndexTargetFile::Exists() const /*{{{*/
  229. {
  230. return FileExists(IndexFileName());
  231. }
  232. /*}}}*/
  233. std::string pkgDebianIndexTargetFile::GetArchitecture() const /*{{{*/
  234. {
  235. return Target.Option(IndexTarget::ARCHITECTURE);
  236. }
  237. /*}}}*/
  238. std::string pkgDebianIndexTargetFile::GetComponent() const /*{{{*/
  239. {
  240. return Target.Option(IndexTarget::COMPONENT);
  241. }
  242. /*}}}*/
  243. bool pkgDebianIndexTargetFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
  244. {
  245. if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
  246. return _error->Error("Problem opening %s",FileName.c_str());
  247. return true;
  248. }
  249. /*}}}*/
  250. std::string pkgDebianIndexTargetFile::GetProgressDescription() const
  251. {
  252. return Target.Description;
  253. }
  254. pkgDebianIndexRealFile::pkgDebianIndexRealFile(std::string const &pFile, bool const Trusted) :/*{{{*/
  255. pkgDebianIndexFile(Trusted), d(NULL)
  256. {
  257. if (pFile.empty())
  258. ;
  259. else if (pFile == "/nonexistent/stdin")
  260. File = pFile;
  261. else
  262. File = flAbsPath(pFile);
  263. }
  264. /*}}}*/
  265. // IndexRealFile::Size - Return the size of the index /*{{{*/
  266. unsigned long pkgDebianIndexRealFile::Size() const
  267. {
  268. struct stat S;
  269. if (stat(File.c_str(),&S) != 0)
  270. return 0;
  271. return S.st_size;
  272. }
  273. /*}}}*/
  274. bool pkgDebianIndexRealFile::Exists() const /*{{{*/
  275. {
  276. return FileExists(File);
  277. }
  278. /*}}}*/
  279. std::string pkgDebianIndexRealFile::Describe(bool const /*Short*/) const/*{{{*/
  280. {
  281. return File;
  282. }
  283. /*}}}*/
  284. std::string pkgDebianIndexRealFile::ArchiveURI(std::string const &/*File*/) const/*{{{*/
  285. {
  286. return "file:" + File;
  287. }
  288. /*}}}*/
  289. std::string pkgDebianIndexRealFile::IndexFileName() const /*{{{*/
  290. {
  291. return File;
  292. }
  293. /*}}}*/
  294. std::string pkgDebianIndexRealFile::GetProgressDescription() const
  295. {
  296. return File;
  297. }
  298. bool pkgDebianIndexRealFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
  299. {
  300. if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
  301. return _error->Error("Problem opening %s",FileName.c_str());
  302. return true;
  303. }
  304. /*}}}*/
  305. pkgDebianIndexFile::pkgDebianIndexFile(bool const Trusted) : pkgIndexFile(Trusted)
  306. {
  307. }
  308. pkgDebianIndexFile::~pkgDebianIndexFile()
  309. {
  310. }
  311. pkgCacheListParser * pkgDebianIndexFile::CreateListParser(FileFd &Pkg)
  312. {
  313. if (Pkg.IsOpen() == false)
  314. return nullptr;
  315. _error->PushToStack();
  316. pkgCacheListParser * const Parser = new debListParser(&Pkg);
  317. bool const newError = _error->PendingError();
  318. _error->MergeWithStack();
  319. if (newError)
  320. {
  321. delete Parser;
  322. return nullptr;
  323. }
  324. else
  325. return Parser;
  326. }
  327. bool pkgDebianIndexFile::Merge(pkgCacheGenerator &Gen,OpProgress * const Prog)
  328. {
  329. std::string const PackageFile = IndexFileName();
  330. FileFd Pkg;
  331. if (OpenListFile(Pkg, PackageFile) == false)
  332. return false;
  333. _error->PushToStack();
  334. std::unique_ptr<pkgCacheListParser> Parser(CreateListParser(Pkg));
  335. bool const newError = _error->PendingError();
  336. _error->MergeWithStack();
  337. if (newError == false && Parser == nullptr)
  338. return true;
  339. if (Parser == NULL)
  340. return false;
  341. if (Prog != NULL)
  342. Prog->SubProgress(0, GetProgressDescription());
  343. if (Gen.SelectFile(PackageFile, *this, GetArchitecture(), GetComponent(), GetIndexFlags()) == false)
  344. return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
  345. // Store the IMS information
  346. pkgCache::PkgFileIterator File = Gen.GetCurFile();
  347. pkgCacheGenerator::Dynamic<pkgCache::PkgFileIterator> DynFile(File);
  348. File->Size = Pkg.FileSize();
  349. File->mtime = Pkg.ModificationTime();
  350. if (Gen.MergeList(*Parser) == false)
  351. return _error->Error("Problem with MergeList %s",PackageFile.c_str());
  352. return true;
  353. }
  354. pkgCache::PkgFileIterator pkgDebianIndexFile::FindInCache(pkgCache &Cache) const
  355. {
  356. std::string const FileName = IndexFileName();
  357. pkgCache::PkgFileIterator File = Cache.FileBegin();
  358. for (; File.end() == false; ++File)
  359. {
  360. if (File.FileName() == NULL || FileName != File.FileName())
  361. continue;
  362. struct stat St;
  363. if (stat(File.FileName(),&St) != 0)
  364. {
  365. if (_config->FindB("Debug::pkgCacheGen", false))
  366. std::clog << "DebianIndexFile::FindInCache - stat failed on " << File.FileName() << std::endl;
  367. return pkgCache::PkgFileIterator(Cache);
  368. }
  369. if ((map_filesize_t)St.st_size != File->Size || St.st_mtime != File->mtime)
  370. {
  371. if (_config->FindB("Debug::pkgCacheGen", false))
  372. std::clog << "DebianIndexFile::FindInCache - size (" << St.st_size << " <> " << File->Size
  373. << ") or mtime (" << St.st_mtime << " <> " << File->mtime
  374. << ") doesn't match for " << File.FileName() << std::endl;
  375. return pkgCache::PkgFileIterator(Cache);
  376. }
  377. return File;
  378. }
  379. return File;
  380. }
  381. APT_CONST pkgIndexFile::~pkgIndexFile() {}
  382. APT_CONST pkgDebianIndexTargetFile::~pkgDebianIndexTargetFile() {}
  383. APT_CONST pkgDebianIndexRealFile::~pkgDebianIndexRealFile() {}