indexfile.cc 12 KB

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