cachefile.cc 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cachefile.cc,v 1.8 2002/04/27 04:28:04 jgg Exp $
  4. /* ######################################################################
  5. CacheFile - Simple wrapper class for opening, generating and whatnot
  6. This class implements a simple 2 line mechanism to open various sorts
  7. of caches. It can operate as root, as not root, show progress and so on,
  8. it transparently handles everything necessary.
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #include <config.h>
  13. #include <apt-pkg/cachefile.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/sourcelist.h>
  16. #include <apt-pkg/pkgcachegen.h>
  17. #include <apt-pkg/configuration.h>
  18. #include <apt-pkg/policy.h>
  19. #include <apt-pkg/pkgsystem.h>
  20. #include <apt-pkg/fileutl.h>
  21. #include <apt-pkg/progress.h>
  22. #include <apt-pkg/depcache.h>
  23. #include <apt-pkg/mmap.h>
  24. #include <apt-pkg/pkgcache.h>
  25. #include <apt-pkg/indexfile.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <string>
  29. #include <vector>
  30. #include <memory>
  31. #include <apti18n.h>
  32. /*}}}*/
  33. // CacheFile::CacheFile - Constructor /*{{{*/
  34. pkgCacheFile::pkgCacheFile() : d(NULL), ExternOwner(false), Map(NULL), Cache(NULL),
  35. DCache(NULL), SrcList(NULL), Policy(NULL)
  36. {
  37. }
  38. pkgCacheFile::pkgCacheFile(pkgDepCache * const Owner) : d(NULL), ExternOwner(true),
  39. Map(&Owner->GetCache().GetMap()), Cache(&Owner->GetCache()),
  40. DCache(Owner), SrcList(NULL), Policy(NULL)
  41. {
  42. }
  43. /*}}}*/
  44. // CacheFile::~CacheFile - Destructor /*{{{*/
  45. // ---------------------------------------------------------------------
  46. /* */
  47. pkgCacheFile::~pkgCacheFile()
  48. {
  49. if (ExternOwner == false)
  50. {
  51. delete DCache;
  52. delete Cache;
  53. delete Map;
  54. }
  55. delete Policy;
  56. delete SrcList;
  57. if (ExternOwner == false)
  58. _system->UnLock(true);
  59. }
  60. /*}}}*/
  61. // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
  62. class APT_HIDDEN ScopedErrorMerge {
  63. public:
  64. ScopedErrorMerge() { _error->PushToStack(); }
  65. ~ScopedErrorMerge() { _error->MergeWithStack(); }
  66. };
  67. bool pkgCacheFile::BuildCaches(OpProgress *Progress, bool WithLock)
  68. {
  69. std::unique_ptr<pkgCache> Cache;
  70. std::unique_ptr<MMap> Map;
  71. if (this->Cache != NULL)
  72. return true;
  73. ScopedErrorMerge sem;
  74. if (_config->FindB("pkgCacheFile::Generate", true) == false)
  75. {
  76. FileFd file(_config->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly);
  77. if (file.IsOpen() == false || file.Failed())
  78. return false;
  79. Map.reset(new MMap(file, MMap::Public|MMap::ReadOnly));
  80. if (unlikely(Map->validData() == false))
  81. return false;
  82. Cache.reset(new pkgCache(Map.get()));
  83. if (_error->PendingError() == true)
  84. return false;
  85. this->Cache = Cache.release();
  86. this->Map = Map.release();
  87. return true;
  88. }
  89. if (WithLock == true)
  90. if (_system->Lock() == false)
  91. return false;
  92. if (_error->PendingError() == true)
  93. return false;
  94. if (BuildSourceList(Progress) == false)
  95. return false;
  96. // Read the caches
  97. MMap *TmpMap = nullptr;
  98. pkgCache *TmpCache = nullptr;
  99. bool Res = pkgCacheGenerator::MakeStatusCache(*SrcList,Progress,&TmpMap, &TmpCache, true);
  100. Map.reset(TmpMap);
  101. Cache.reset(TmpCache);
  102. if (Progress != NULL)
  103. Progress->Done();
  104. if (Res == false)
  105. return _error->Error(_("The package lists or status file could not be parsed or opened."));
  106. /* This sux, remove it someday */
  107. if (_error->PendingError() == true)
  108. _error->Warning(_("You may want to run apt-get update to correct these problems"));
  109. if (Cache == nullptr)
  110. Cache.reset(new pkgCache(Map.get()));
  111. if (_error->PendingError() == true)
  112. return false;
  113. this->Map = Map.release();
  114. this->Cache = Cache.release();
  115. return true;
  116. }
  117. /*}}}*/
  118. // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
  119. // ---------------------------------------------------------------------
  120. /* */
  121. bool pkgCacheFile::BuildSourceList(OpProgress * /*Progress*/)
  122. {
  123. std::unique_ptr<pkgSourceList> SrcList;
  124. if (this->SrcList != NULL)
  125. return true;
  126. SrcList.reset(new pkgSourceList());
  127. if (SrcList->ReadMainList() == false)
  128. return _error->Error(_("The list of sources could not be read."));
  129. this->SrcList = SrcList.release();
  130. return true;
  131. }
  132. /*}}}*/
  133. // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
  134. // ---------------------------------------------------------------------
  135. /* */
  136. bool pkgCacheFile::BuildPolicy(OpProgress * /*Progress*/)
  137. {
  138. std::unique_ptr<pkgPolicy> Policy;
  139. if (this->Policy != NULL)
  140. return true;
  141. Policy.reset(new pkgPolicy(Cache));
  142. if (_error->PendingError() == true)
  143. return false;
  144. if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
  145. return false;
  146. this->Policy = Policy.release();
  147. return true;
  148. }
  149. /*}}}*/
  150. // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
  151. // ---------------------------------------------------------------------
  152. /* */
  153. bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
  154. {
  155. if (BuildCaches(Progress, false) == false)
  156. return false;
  157. std::unique_ptr<pkgDepCache> DCache;
  158. if (this->DCache != NULL)
  159. return true;
  160. if (BuildPolicy(Progress) == false)
  161. return false;
  162. DCache.reset(new pkgDepCache(Cache,Policy));
  163. if (_error->PendingError() == true)
  164. return false;
  165. if (DCache->Init(Progress) == false)
  166. return false;
  167. this->DCache = DCache.release();
  168. return true;
  169. }
  170. /*}}}*/
  171. // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
  172. // ---------------------------------------------------------------------
  173. /* */
  174. bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock)
  175. {
  176. if (BuildCaches(Progress,WithLock) == false)
  177. return false;
  178. if (BuildPolicy(Progress) == false)
  179. return false;
  180. if (BuildDepCache(Progress) == false)
  181. return false;
  182. if (Progress != NULL)
  183. Progress->Done();
  184. if (_error->PendingError() == true)
  185. return false;
  186. return true;
  187. }
  188. /*}}}*/
  189. bool pkgCacheFile::AddIndexFile(pkgIndexFile * const File) /*{{{*/
  190. {
  191. if (SrcList == NULL)
  192. if (BuildSourceList() == false)
  193. return false;
  194. SrcList->AddVolatileFile(File);
  195. if (Cache == nullptr || File->HasPackages() == false || File->Exists() == false)
  196. return true;
  197. if (File->FindInCache(*Cache).end() == false)
  198. return _error->Warning("Duplicate sources.list entry %s",
  199. File->Describe().c_str());
  200. if (ExternOwner == false)
  201. {
  202. delete DCache;
  203. delete Cache;
  204. }
  205. delete Policy;
  206. DCache = NULL;
  207. Policy = NULL;
  208. Cache = NULL;
  209. if (ExternOwner == false)
  210. {
  211. // a dynamic mmap means that we have build at least parts of the cache
  212. // in memory – which we might or might not have written to disk.
  213. // Throwing away would therefore be a very costly operation we want to avoid
  214. DynamicMMap * dynmmap = dynamic_cast<DynamicMMap*>(Map);
  215. if (dynmmap != nullptr)
  216. {
  217. {
  218. pkgCacheGenerator Gen(dynmmap, nullptr);
  219. if (Gen.Start() == false || File->Merge(Gen, nullptr) == false)
  220. return false;
  221. }
  222. Cache = new pkgCache(Map);
  223. if (_error->PendingError() == true) {
  224. delete Cache;
  225. Cache = nullptr;
  226. return false;
  227. }
  228. return true;
  229. }
  230. else
  231. {
  232. delete Map;
  233. Map = NULL;
  234. }
  235. }
  236. else
  237. {
  238. ExternOwner = false;
  239. Map = NULL;
  240. }
  241. _system->UnLock(true);
  242. return true;
  243. }
  244. /*}}}*/
  245. // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
  246. // ---------------------------------------------------------------------
  247. /* */
  248. void pkgCacheFile::RemoveCaches()
  249. {
  250. std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
  251. std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
  252. if (pkgcache.empty() == false && RealFileExists(pkgcache) == true)
  253. RemoveFile("RemoveCaches", pkgcache);
  254. if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true)
  255. RemoveFile("RemoveCaches", srcpkgcache);
  256. if (pkgcache.empty() == false)
  257. {
  258. std::string cachedir = flNotFile(pkgcache);
  259. std::string cachefile = flNotDir(pkgcache);
  260. if (cachedir.empty() != true && cachefile.empty() != true && DirectoryExists(cachedir) == true)
  261. {
  262. cachefile.append(".");
  263. std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
  264. for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
  265. {
  266. std::string nuke = flNotDir(*file);
  267. if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
  268. continue;
  269. RemoveFile("RemoveCaches", *file);
  270. }
  271. }
  272. }
  273. if (srcpkgcache.empty() == true)
  274. return;
  275. std::string cachedir = flNotFile(srcpkgcache);
  276. std::string cachefile = flNotDir(srcpkgcache);
  277. if (cachedir.empty() == true || cachefile.empty() == true || DirectoryExists(cachedir) == false)
  278. return;
  279. cachefile.append(".");
  280. std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
  281. for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
  282. {
  283. std::string nuke = flNotDir(*file);
  284. if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
  285. continue;
  286. RemoveFile("RemoveCaches", *file);
  287. }
  288. }
  289. /*}}}*/
  290. // CacheFile::Close - close the cache files /*{{{*/
  291. // ---------------------------------------------------------------------
  292. /* */
  293. void pkgCacheFile::Close()
  294. {
  295. if (ExternOwner == false)
  296. {
  297. delete DCache;
  298. delete Cache;
  299. delete Map;
  300. }
  301. else
  302. ExternOwner = false;
  303. delete Policy;
  304. delete SrcList;
  305. _system->UnLock(true);
  306. Map = NULL;
  307. DCache = NULL;
  308. Policy = NULL;
  309. Cache = NULL;
  310. SrcList = NULL;
  311. }
  312. /*}}}*/