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.
 
 
 
 
 
 

395 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/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/macros.h>
  22. #include <apt-pkg/deblistparser.h>
  23. #include <sys/stat.h>
  24. #include <string>
  25. #include <vector>
  26. #include <clocale>
  27. #include <cstring>
  28. #include <memory>
  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. return std::string();
  64. }
  65. /*}}}*/
  66. // IndexFile::FindInCache - Stub /*{{{*/
  67. pkgCache::PkgFileIterator pkgIndexFile::FindInCache(pkgCache &Cache) const
  68. {
  69. return pkgCache::PkgFileIterator(Cache);
  70. }
  71. /*}}}*/
  72. // IndexFile::SourceIndex - Stub /*{{{*/
  73. std::string pkgIndexFile::SourceInfo(pkgSrcRecords::Parser const &/*Record*/,
  74. pkgSrcRecords::File const &/*File*/) const
  75. {
  76. return std::string();
  77. }
  78. /*}}}*/
  79. // IndexFile::TranslationsAvailable - Check if will use Translation /*{{{*/
  80. // ---------------------------------------------------------------------
  81. /* */
  82. bool pkgIndexFile::TranslationsAvailable() {
  83. return (APT::Configuration::getLanguages().empty() != true);
  84. }
  85. /*}}}*/
  86. // IndexFile::CheckLanguageCode - Check the Language Code /*{{{*/
  87. // ---------------------------------------------------------------------
  88. /* No intern need for this method anymore as the check for correctness
  89. is already done in getLanguages(). Note also that this check is
  90. rather bad (doesn't take three character like ast into account).
  91. TODO: Remove method with next API break */
  92. APT_DEPRECATED bool pkgIndexFile::CheckLanguageCode(const char * const Lang)
  93. {
  94. if (strlen(Lang) == 2 || (strlen(Lang) == 5 && Lang[2] == '_'))
  95. return true;
  96. if (strcmp(Lang,"C") != 0)
  97. _error->Warning("Wrong language code %s", Lang);
  98. return false;
  99. }
  100. /*}}}*/
  101. // IndexFile::LanguageCode - Return the Language Code /*{{{*/
  102. // ---------------------------------------------------------------------
  103. /* As we have now possibly more than one LanguageCode this method is
  104. supersided by a) private classmembers or b) getLanguages().
  105. TODO: Remove method with next API break */
  106. APT_DEPRECATED std::string pkgIndexFile::LanguageCode() {
  107. if (TranslationsAvailable() == false)
  108. return "";
  109. return APT::Configuration::getLanguages()[0];
  110. }
  111. /*}}}*/
  112. // IndexTarget - Constructor /*{{{*/
  113. IndexTarget::IndexTarget(std::string const &MetaKey, std::string const &ShortDesc,
  114. std::string const &LongDesc, std::string const &URI, bool const IsOptional,
  115. bool const KeepCompressed, std::map<std::string, std::string> const &Options) :
  116. URI(URI), Description(LongDesc), ShortDesc(ShortDesc), MetaKey(MetaKey),
  117. IsOptional(IsOptional), KeepCompressed(KeepCompressed), Options(Options)
  118. {
  119. }
  120. /*}}}*/
  121. std::string IndexTarget::Option(OptionKeys const EnumKey) const /*{{{*/
  122. {
  123. std::string Key;
  124. switch (EnumKey)
  125. {
  126. #define APT_CASE(X) case X: Key = #X; break
  127. APT_CASE(SITE);
  128. APT_CASE(RELEASE);
  129. APT_CASE(COMPONENT);
  130. APT_CASE(LANGUAGE);
  131. APT_CASE(ARCHITECTURE);
  132. APT_CASE(BASE_URI);
  133. APT_CASE(REPO_URI);
  134. APT_CASE(TARGET_OF);
  135. APT_CASE(CREATED_BY);
  136. APT_CASE(PDIFFS);
  137. APT_CASE(DEFAULTENABLED);
  138. APT_CASE(COMPRESSIONTYPES);
  139. APT_CASE(SOURCESENTRY);
  140. #undef APT_CASE
  141. case FILENAME: return _config->FindDir("Dir::State::lists") + URItoFileName(URI);
  142. case EXISTING_FILENAME:
  143. std::string const filename = Option(FILENAME);
  144. std::vector<std::string> const types = VectorizeString(Option(COMPRESSIONTYPES), ' ');
  145. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  146. {
  147. if (t->empty())
  148. continue;
  149. std::string const file = (*t == "uncompressed") ? filename : (filename + "." + *t);
  150. if (FileExists(file))
  151. return file;
  152. }
  153. return "";
  154. }
  155. std::map<std::string,std::string>::const_iterator const M = Options.find(Key);
  156. if (M == Options.end())
  157. return "";
  158. return M->second;
  159. }
  160. /*}}}*/
  161. bool IndexTarget::OptionBool(OptionKeys const EnumKey) const /*{{{*/
  162. {
  163. return StringToBool(Option(EnumKey));
  164. }
  165. /*}}}*/
  166. std::string IndexTarget::Format(std::string format) const /*{{{*/
  167. {
  168. for (std::map<std::string, std::string>::const_iterator O = Options.begin(); O != Options.end(); ++O)
  169. {
  170. format = SubstVar(format, std::string("$(") + O->first + ")", O->second);
  171. }
  172. format = SubstVar(format, "$(METAKEY)", MetaKey);
  173. format = SubstVar(format, "$(SHORTDESC)", ShortDesc);
  174. format = SubstVar(format, "$(DESCRIPTION)", Description);
  175. format = SubstVar(format, "$(URI)", URI);
  176. format = SubstVar(format, "$(FILENAME)", Option(IndexTarget::FILENAME));
  177. return format;
  178. }
  179. /*}}}*/
  180. pkgDebianIndexTargetFile::pkgDebianIndexTargetFile(IndexTarget const &Target, bool const Trusted) :/*{{{*/
  181. pkgDebianIndexFile(Trusted), d(NULL), Target(Target)
  182. {
  183. }
  184. /*}}}*/
  185. std::string pkgDebianIndexTargetFile::ArchiveURI(std::string const &File) const/*{{{*/
  186. {
  187. return Target.Option(IndexTarget::REPO_URI) + File;
  188. }
  189. /*}}}*/
  190. std::string pkgDebianIndexTargetFile::Describe(bool const Short) const /*{{{*/
  191. {
  192. if (Short)
  193. return Target.Description;
  194. return Target.Description + " (" + IndexFileName() + ")";
  195. }
  196. /*}}}*/
  197. std::string pkgDebianIndexTargetFile::IndexFileName() const /*{{{*/
  198. {
  199. std::string const s = Target.Option(IndexTarget::FILENAME);
  200. if (FileExists(s))
  201. return s;
  202. std::vector<std::string> const types = VectorizeString(Target.Option(IndexTarget::COMPRESSIONTYPES), ' ');
  203. for (std::vector<std::string>::const_iterator t = types.begin(); t != types.end(); ++t)
  204. {
  205. std::string p = s + '.' + *t;
  206. if (FileExists(p))
  207. return p;
  208. }
  209. return s;
  210. }
  211. /*}}}*/
  212. unsigned long pkgDebianIndexTargetFile::Size() const /*{{{*/
  213. {
  214. unsigned long size = 0;
  215. /* we need to ignore errors here; if the lists are absent, just return 0 */
  216. _error->PushToStack();
  217. FileFd f(IndexFileName(), FileFd::ReadOnly, FileFd::Extension);
  218. if (!f.Failed())
  219. size = f.Size();
  220. if (_error->PendingError() == true)
  221. size = 0;
  222. _error->RevertToStack();
  223. return size;
  224. }
  225. /*}}}*/
  226. bool pkgDebianIndexTargetFile::Exists() const /*{{{*/
  227. {
  228. return FileExists(IndexFileName());
  229. }
  230. /*}}}*/
  231. std::string pkgDebianIndexTargetFile::GetArchitecture() const /*{{{*/
  232. {
  233. return Target.Option(IndexTarget::ARCHITECTURE);
  234. }
  235. /*}}}*/
  236. std::string pkgDebianIndexTargetFile::GetComponent() const /*{{{*/
  237. {
  238. return Target.Option(IndexTarget::COMPONENT);
  239. }
  240. /*}}}*/
  241. bool pkgDebianIndexTargetFile::OpenListFile(FileFd &Pkg, std::string const &FileName)/*{{{*/
  242. {
  243. if (Pkg.Open(FileName, FileFd::ReadOnly, FileFd::Extension) == false)
  244. return _error->Error("Problem opening %s",FileName.c_str());
  245. return true;
  246. }
  247. /*}}}*/
  248. std::string pkgDebianIndexTargetFile::GetProgressDescription() const
  249. {
  250. return Target.Description;
  251. }
  252. pkgDebianIndexRealFile::pkgDebianIndexRealFile(std::string const &File, bool const Trusted) :/*{{{*/
  253. pkgDebianIndexFile(Trusted), d(NULL), File(flAbsPath(File))
  254. {
  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. if (newError == false && Parser == nullptr)
  323. return true;
  324. if (Parser == NULL)
  325. return false;
  326. if (Prog != NULL)
  327. Prog->SubProgress(0, GetProgressDescription());
  328. if (Gen.SelectFile(PackageFile, *this, GetArchitecture(), GetComponent(), GetIndexFlags()) == false)
  329. return _error->Error("Problem with SelectFile %s",PackageFile.c_str());
  330. // Store the IMS information
  331. pkgCache::PkgFileIterator File = Gen.GetCurFile();
  332. pkgCacheGenerator::Dynamic<pkgCache::PkgFileIterator> DynFile(File);
  333. File->Size = Pkg.FileSize();
  334. File->mtime = Pkg.ModificationTime();
  335. if (Gen.MergeList(*Parser) == false)
  336. return _error->Error("Problem with MergeList %s",PackageFile.c_str());
  337. return true;
  338. }
  339. pkgCache::PkgFileIterator pkgDebianIndexFile::FindInCache(pkgCache &Cache) const
  340. {
  341. std::string const FileName = IndexFileName();
  342. pkgCache::PkgFileIterator File = Cache.FileBegin();
  343. for (; File.end() == false; ++File)
  344. {
  345. if (File.FileName() == NULL || FileName != File.FileName())
  346. continue;
  347. struct stat St;
  348. if (stat(File.FileName(),&St) != 0)
  349. {
  350. if (_config->FindB("Debug::pkgCacheGen", false))
  351. std::clog << "DebianIndexFile::FindInCache - stat failed on " << File.FileName() << std::endl;
  352. return pkgCache::PkgFileIterator(Cache);
  353. }
  354. if ((map_filesize_t)St.st_size != File->Size || St.st_mtime != File->mtime)
  355. {
  356. if (_config->FindB("Debug::pkgCacheGen", false))
  357. std::clog << "DebianIndexFile::FindInCache - size (" << St.st_size << " <> " << File->Size
  358. << ") or mtime (" << St.st_mtime << " <> " << File->mtime
  359. << ") doesn't match for " << File.FileName() << std::endl;
  360. return pkgCache::PkgFileIterator(Cache);
  361. }
  362. return File;
  363. }
  364. return File;
  365. }
  366. APT_CONST pkgIndexFile::~pkgIndexFile() {}
  367. APT_CONST pkgDebianIndexTargetFile::~pkgDebianIndexTargetFile() {}
  368. APT_CONST pkgDebianIndexRealFile::~pkgDebianIndexRealFile() {}