cachefile.cc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. std::unique_ptr<pkgDepCache> DCache;
  156. if (this->DCache != NULL)
  157. return true;
  158. if (BuildPolicy(Progress) == false)
  159. return false;
  160. DCache.reset(new pkgDepCache(Cache,Policy));
  161. if (_error->PendingError() == true)
  162. return false;
  163. if (DCache->Init(Progress) == false)
  164. return false;
  165. this->DCache = DCache.release();
  166. return true;
  167. }
  168. /*}}}*/
  169. // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
  170. // ---------------------------------------------------------------------
  171. /* */
  172. bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock)
  173. {
  174. if (BuildCaches(Progress,WithLock) == false)
  175. return false;
  176. if (BuildPolicy(Progress) == false)
  177. return false;
  178. if (BuildDepCache(Progress) == false)
  179. return false;
  180. if (Progress != NULL)
  181. Progress->Done();
  182. if (_error->PendingError() == true)
  183. return false;
  184. return true;
  185. }
  186. /*}}}*/
  187. bool pkgCacheFile::AddIndexFile(pkgIndexFile * const File) /*{{{*/
  188. {
  189. if (SrcList == NULL)
  190. if (BuildSourceList() == false)
  191. return false;
  192. SrcList->AddVolatileFile(File);
  193. if (Cache == nullptr || File->HasPackages() == false || File->Exists() == false)
  194. return true;
  195. if (File->FindInCache(*Cache).end() == false)
  196. return _error->Warning("Duplicate sources.list entry %s",
  197. File->Describe().c_str());
  198. if (ExternOwner == false)
  199. {
  200. delete DCache;
  201. delete Cache;
  202. }
  203. delete Policy;
  204. DCache = NULL;
  205. Policy = NULL;
  206. Cache = NULL;
  207. if (ExternOwner == false)
  208. {
  209. // a dynamic mmap means that we have build at least parts of the cache
  210. // in memory – which we might or might not have written to disk.
  211. // Throwing away would therefore be a very costly operation we want to avoid
  212. DynamicMMap * dynmmap = dynamic_cast<DynamicMMap*>(Map);
  213. if (dynmmap != nullptr)
  214. {
  215. {
  216. pkgCacheGenerator Gen(dynmmap, nullptr);
  217. if (Gen.Start() == false || File->Merge(Gen, nullptr) == false)
  218. return false;
  219. }
  220. Cache = new pkgCache(Map);
  221. if (_error->PendingError() == true) {
  222. delete Cache;
  223. Cache = nullptr;
  224. return false;
  225. }
  226. return true;
  227. }
  228. else
  229. {
  230. delete Map;
  231. Map = NULL;
  232. }
  233. }
  234. else
  235. {
  236. ExternOwner = false;
  237. Map = NULL;
  238. }
  239. _system->UnLock(true);
  240. return true;
  241. }
  242. /*}}}*/
  243. // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
  244. // ---------------------------------------------------------------------
  245. /* */
  246. void pkgCacheFile::RemoveCaches()
  247. {
  248. std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
  249. std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
  250. if (pkgcache.empty() == false && RealFileExists(pkgcache) == true)
  251. RemoveFile("RemoveCaches", pkgcache);
  252. if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true)
  253. RemoveFile("RemoveCaches", srcpkgcache);
  254. if (pkgcache.empty() == false)
  255. {
  256. std::string cachedir = flNotFile(pkgcache);
  257. std::string cachefile = flNotDir(pkgcache);
  258. if (cachedir.empty() != true && cachefile.empty() != true && DirectoryExists(cachedir) == true)
  259. {
  260. cachefile.append(".");
  261. std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
  262. for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
  263. {
  264. std::string nuke = flNotDir(*file);
  265. if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
  266. continue;
  267. RemoveFile("RemoveCaches", *file);
  268. }
  269. }
  270. }
  271. if (srcpkgcache.empty() == true)
  272. return;
  273. std::string cachedir = flNotFile(srcpkgcache);
  274. std::string cachefile = flNotDir(srcpkgcache);
  275. if (cachedir.empty() == true || cachefile.empty() == true || DirectoryExists(cachedir) == false)
  276. return;
  277. cachefile.append(".");
  278. std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
  279. for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
  280. {
  281. std::string nuke = flNotDir(*file);
  282. if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
  283. continue;
  284. RemoveFile("RemoveCaches", *file);
  285. }
  286. }
  287. /*}}}*/
  288. // CacheFile::Close - close the cache files /*{{{*/
  289. // ---------------------------------------------------------------------
  290. /* */
  291. void pkgCacheFile::Close()
  292. {
  293. if (ExternOwner == false)
  294. {
  295. delete DCache;
  296. delete Cache;
  297. delete Map;
  298. }
  299. else
  300. ExternOwner = false;
  301. delete Policy;
  302. delete SrcList;
  303. _system->UnLock(true);
  304. Map = NULL;
  305. DCache = NULL;
  306. Policy = NULL;
  307. Cache = NULL;
  308. SrcList = NULL;
  309. }
  310. /*}}}*/