cachefile.cc 9.6 KB

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