indexfile.cc 12 KB

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