indexfile.cc 12 KB

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