indexfile.cc 12 KB

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