You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

414 lines
12 KiB

  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/aptconfiguration.h>
  11. #include <apt-pkg/configuration.h>
  12. #include <apt-pkg/deblistparser.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/fileutl.h>
  15. #include <apt-pkg/indexfile.h>
  16. #include <apt-pkg/macros.h>
  17. #include <apt-pkg/pkgcache.h>
  18. #include <apt-pkg/pkgcachegen.h>
  19. #include <apt-pkg/progress.h>
  20. #include <apt-pkg/srcrecords.h>
  21. #include <apt-pkg/strutl.h>
  22. #include <apt-pkg/debindexfile.h>
  23. #include <sys/stat.h>
  24. #include <clocale>
  25. #include <cstring>
  26. #include <memory>
  27. #include <string>
  28. #include <vector>
  29. /*}}}*/
  30. // Global list of Item supported
  31. static pkgIndexFile::Type *ItmList[10];
  32. pkgIndexFile::Type **pkgIndexFile::Type::GlobalList = ItmList;
  33. unsigned long pkgIndexFile::Type::GlobalListLen = 0;
  34. // Type::Type - Constructor /*{{{*/
  35. // ---------------------------------------------------------------------
  36. /* */
  37. pkgIndexFile::Type::Type()
  38. {
  39. ItmList[GlobalListLen] = this;
  40. GlobalListLen++;
  41. Label = NULL;
  42. }
  43. /*}}}*/
  44. // Type::GetType - Locate the type by name /*{{{*/
  45. // ---------------------------------------------------------------------
  46. /* */
  47. pkgIndexFile::Type *pkgIndexFile::Type::GetType(const char *Type)
  48. {
  49. for (unsigned I = 0; I != GlobalListLen; I++)
  50. if (strcmp(GlobalList[I]->Label,Type) == 0)
  51. return GlobalList[I];
  52. return 0;
  53. }
  54. /*}}}*/
  55. pkgIndexFile::pkgIndexFile(bool const Trusted) : /*{{{*/
  56. d(NULL), Trusted(Trusted)
  57. {
  58. }
  59. /*}}}*/
  60. // IndexFile::ArchiveInfo - Stub /*{{{*/
  61. std::string pkgIndexFile::ArchiveInfo(pkgCache::VerIterator const &Ver) const
  62. {
  63. debDebPkgFileIndex const * const debfile = dynamic_cast<debDebPkgFileIndex const*>(this);
  64. if (debfile != nullptr)
  65. return debfile->ArchiveInfo_impl(Ver);
  66. return std::string();
  67. }
  68. /*}}}*/
  69. // IndexFile::FindInCache - Stub /*{{{*/
  70. pkgCache::PkgFileIterator pkgIndexFile::FindInCache(pkgCache &Cache) const
  71. {
  72. return pkgCache::PkgFileIterator(Cache);
  73. }
  74. /*}}}*/
  75. // IndexFile::SourceIndex - Stub /*{{{*/
  76. std::string pkgIndexFile::SourceInfo(pkgSrcRecords::Parser const &/*Record*/,
  77. pkgSrcRecords::File const &/*File*/) const
  78. {
  79. return std::string();
  80. }
  81. /*}}}*/
  82. // IndexFile::TranslationsAvailable - Check if will use Translation /*{{{*/
  83. bool pkgIndexFile::TranslationsAvailable() {
  84. return (APT::Configuration::getLanguages().empty() != true);
  85. }
  86. /*}}}*/
  87. // IndexFile::CheckLanguageCode - Check the Language Code /*{{{*/
  88. bool pkgIndexFile::CheckLanguageCode(const char * const Lang)
  89. {
  90. if (strlen(Lang) == 2 || (strlen(Lang) == 5 && Lang[2] == '_'))
  91. return true;
  92. if (strcmp(Lang,"C") != 0)
  93. _error->Warning("Wrong language code %s", Lang);
  94. return false;
  95. }
  96. /*}}}*/
  97. // IndexFile::LanguageCode - Return the Language Code /*{{{*/
  98. std::string pkgIndexFile::LanguageCode() {
  99. APT_IGNORE_DEPRECATED_PUSH
  100. if (TranslationsAvailable() == false)
  101. return "";
  102. return APT::Configuration::getLanguages()[0];
  103. APT_IGNORE_DEPRECATED_POP
  104. }
  105. /*}}}*/
  106. // IndexTarget - Constructor /*{{{*/
  107. IndexTarget::IndexTarget(std::string const &MetaKey, std::string const &ShortDesc,
  108. std::string const &LongDesc, std::string const &URI, bool const IsOptional,
  109. bool const KeepCompressed, std::map<std::string, std::string> const &Options) :
  110. URI(URI), Description(LongDesc), ShortDesc(ShortDesc), MetaKey(MetaKey),
  111. IsOptional(IsOptional), KeepCompressed(KeepCompressed), Options(Options)
  112. {
  113. }
  114. /*}}}*/
  115. std::string IndexTarget::Option(OptionKeys const EnumKey) const /*{{{*/
  116. {
  117. std::string Key;
  118. switch (EnumKey)
  119. {
  120. #define APT_CASE(X) case X: Key = #X; break
  121. APT_CASE(SITE);
  122. APT_CASE(RELEASE);
  123. APT_CASE(COMPONENT);
  124. APT_CASE(LANGUAGE);
  125. APT_CASE(ARCHITECTURE);
  126. APT_CASE(BASE_URI);
  127. APT_CASE(REPO_URI);
  128. APT_CASE(IDENTIFIER);
  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. APT_CASE(ALLOW_INSECURE);
  139. APT_CASE(ALLOW_WEAK);
  140. APT_CASE(ALLOW_DOWNGRADE_TO_INSECURE);
  141. APT_CASE(INRELEASE_PATH);
  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. IndexTarget pkgDebianIndexTargetFile::GetIndexTarget() const
  261. {
  262. return Target;
  263. }
  264. pkgDebianIndexRealFile::pkgDebianIndexRealFile(std::string const &pFile, bool const Trusted) :/*{{{*/
  265. pkgDebianIndexFile(Trusted), d(NULL)
  266. {
  267. if (pFile.empty())
  268. ;
  269. else if (pFile == "/nonexistent/stdin")
  270. File = pFile;
  271. else
  272. File = flAbsPath(pFile);
  273. }
  274. /*}}}*/
  275. // IndexRealFile::Size - Return the size of the index /*{{{*/
  276. unsigned long pkgDebianIndexRealFile::Size() const
  277. {
  278. struct stat S;
  279. if (stat(File.c_str(),&S) != 0)
  280. return 0;
  281. return S.st_size;
  282. }
  283. /*}}}*/
  284. bool pkgDebianIndexRealFile::Exists() const /*{{{*/
  285. {
  286. return FileExists(File);
  287. }
  288. /*}}}*/
  289. std::string pkgDebianIndexRealFile::Describe(bool const /*Short*/) const/*{{{*/
  290. {
  291. return File;
  292. }
  293. /*}}}*/
  294. std::string pkgDebianIndexRealFile::ArchiveURI(std::string const &/*File*/) const/*{{{*/
  295. {
  296. return "file:" + File;
  297. }
  298. /*}}}*/
  299. std::string pkgDebianIndexRealFile::IndexFileName() const /*{{{*/
  300. {
  301. return File;
  302. }
  303. /*}}}*/
  304. std::string pkgDebianIndexRealFile::GetProgressDescription() const
  305. {
  306. return File;
  307. }
  308. bool pkgDebianIndexRealFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
  309. {
  310. if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
  311. return _error->Error("Problem opening %s",FileName.c_str());
  312. return true;
  313. }
  314. /*}}}*/
  315. pkgDebianIndexFile::pkgDebianIndexFile(bool const Trusted) : pkgIndexFile(Trusted)
  316. {
  317. }
  318. pkgDebianIndexFile::~pkgDebianIndexFile()
  319. {
  320. }
  321. pkgCacheListParser * pkgDebianIndexFile::CreateListParser(FileFd &Pkg)
  322. {
  323. if (Pkg.IsOpen() == false)
  324. return nullptr;
  325. _error->PushToStack();
  326. std::unique_ptr<pkgCacheListParser> Parser(new debListParser(&Pkg));
  327. bool const newError = _error->PendingError();
  328. _error->MergeWithStack();
  329. return newError ? nullptr : Parser.release();
  330. }
  331. bool pkgDebianIndexFile::Merge(pkgCacheGenerator &Gen,OpProgress * const Prog)
  332. {
  333. std::string const PackageFile = IndexFileName();
  334. FileFd Pkg;
  335. if (OpenListFile(Pkg, PackageFile) == false)
  336. return false;
  337. _error->PushToStack();
  338. std::unique_ptr<pkgCacheListParser> Parser(CreateListParser(Pkg));
  339. bool const newError = _error->PendingError();
  340. _error->MergeWithStack();
  341. if (newError == false && Parser == nullptr)
  342. return true;
  343. if (Parser == NULL)
  344. return false;
  345. if (Prog != NULL)
  346. Prog->SubProgress(0, GetProgressDescription());
  347. if (Gen.SelectFile(PackageFile, *this, GetArchitecture(), GetComponent(), GetIndexFlags()) == false)
  348. return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
  349. // Store the IMS information
  350. pkgCache::PkgFileIterator File = Gen.GetCurFile();
  351. pkgCacheGenerator::Dynamic<pkgCache::PkgFileIterator> DynFile(File);
  352. File->Size = Pkg.FileSize();
  353. File->mtime = Pkg.ModificationTime();
  354. if (Gen.MergeList(*Parser) == false)
  355. return _error->Error("Problem with MergeList %s",PackageFile.c_str());
  356. return true;
  357. }
  358. pkgCache::PkgFileIterator pkgDebianIndexFile::FindInCache(pkgCache &Cache) const
  359. {
  360. std::string const FileName = IndexFileName();
  361. pkgCache::PkgFileIterator File = Cache.FileBegin();
  362. for (; File.end() == false; ++File)
  363. {
  364. if (File.FileName() == NULL || FileName != File.FileName())
  365. continue;
  366. struct stat St;
  367. if (stat(File.FileName(),&St) != 0)
  368. {
  369. if (_config->FindB("Debug::pkgCacheGen", false))
  370. std::clog << "DebianIndexFile::FindInCache - stat failed on " << File.FileName() << std::endl;
  371. return pkgCache::PkgFileIterator(Cache);
  372. }
  373. if ((map_filesize_t)St.st_size != File->Size || St.st_mtime != File->mtime)
  374. {
  375. if (_config->FindB("Debug::pkgCacheGen", false))
  376. std::clog << "DebianIndexFile::FindInCache - size (" << St.st_size << " <> " << File->Size
  377. << ") or mtime (" << St.st_mtime << " <> " << File->mtime
  378. << ") doesn't match for " << File.FileName() << std::endl;
  379. return pkgCache::PkgFileIterator(Cache);
  380. }
  381. return File;
  382. }
  383. return File;
  384. }
  385. pkgIndexFile::~pkgIndexFile() {}
  386. pkgDebianIndexTargetFile::~pkgDebianIndexTargetFile() {}
  387. pkgDebianIndexRealFile::~pkgDebianIndexRealFile() {}