cachefile.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <apti18n.h>
  31. /*}}}*/
  32. // CacheFile::CacheFile - Constructor /*{{{*/
  33. pkgCacheFile::pkgCacheFile() : d(NULL), ExternOwner(false), Map(NULL), Cache(NULL),
  34. DCache(NULL), SrcList(NULL), Policy(NULL)
  35. {
  36. }
  37. pkgCacheFile::pkgCacheFile(pkgDepCache * const Owner) : d(NULL), ExternOwner(true),
  38. Map(&Owner->GetCache().GetMap()), Cache(&Owner->GetCache()),
  39. DCache(Owner), SrcList(NULL), Policy(NULL)
  40. {
  41. }
  42. /*}}}*/
  43. // CacheFile::~CacheFile - Destructor /*{{{*/
  44. // ---------------------------------------------------------------------
  45. /* */
  46. pkgCacheFile::~pkgCacheFile()
  47. {
  48. if (ExternOwner == false)
  49. {
  50. delete DCache;
  51. delete Cache;
  52. delete Map;
  53. }
  54. delete Policy;
  55. delete SrcList;
  56. if (ExternOwner == false)
  57. _system->UnLock(true);
  58. }
  59. /*}}}*/
  60. // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
  61. class APT_HIDDEN ScopedErrorMerge {
  62. public:
  63. ScopedErrorMerge() { _error->PushToStack(); }
  64. ~ScopedErrorMerge() { _error->MergeWithStack(); }
  65. };
  66. bool pkgCacheFile::BuildCaches(OpProgress *Progress, bool WithLock)
  67. {
  68. if (Cache != NULL)
  69. return true;
  70. ScopedErrorMerge sem;
  71. if (_config->FindB("pkgCacheFile::Generate", true) == false)
  72. {
  73. FileFd file(_config->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly);
  74. if (file.IsOpen() == false || file.Failed())
  75. return false;
  76. Map = new MMap(file, MMap::Public|MMap::ReadOnly);
  77. if (unlikely(Map->validData() == false))
  78. return false;
  79. Cache = new pkgCache(Map);
  80. return _error->PendingError() == false;
  81. }
  82. if (WithLock == true)
  83. if (_system->Lock() == false)
  84. return false;
  85. if (_error->PendingError() == true)
  86. return false;
  87. BuildSourceList(Progress);
  88. // Read the caches
  89. Cache = nullptr;
  90. bool Res = pkgCacheGenerator::MakeStatusCache(*SrcList,Progress,&Map, &Cache, true);
  91. if (Progress != NULL)
  92. Progress->Done();
  93. if (Res == false)
  94. return _error->Error(_("The package lists or status file could not be parsed or opened."));
  95. /* This sux, remove it someday */
  96. if (_error->PendingError() == true)
  97. _error->Warning(_("You may want to run apt-get update to correct these problems"));
  98. if (Cache == nullptr)
  99. Cache = new pkgCache(Map);
  100. if (_error->PendingError() == true)
  101. return false;
  102. return true;
  103. }
  104. /*}}}*/
  105. // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
  106. // ---------------------------------------------------------------------
  107. /* */
  108. bool pkgCacheFile::BuildSourceList(OpProgress * /*Progress*/)
  109. {
  110. if (SrcList != NULL)
  111. return true;
  112. SrcList = new pkgSourceList();
  113. if (SrcList->ReadMainList() == false)
  114. return _error->Error(_("The list of sources could not be read."));
  115. return true;
  116. }
  117. /*}}}*/
  118. // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
  119. // ---------------------------------------------------------------------
  120. /* */
  121. bool pkgCacheFile::BuildPolicy(OpProgress * /*Progress*/)
  122. {
  123. if (Policy != NULL)
  124. return true;
  125. Policy = new pkgPolicy(Cache);
  126. if (_error->PendingError() == true)
  127. return false;
  128. if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
  129. return false;
  130. return true;
  131. }
  132. /*}}}*/
  133. // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
  134. // ---------------------------------------------------------------------
  135. /* */
  136. bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
  137. {
  138. if (DCache != NULL)
  139. return true;
  140. if (BuildPolicy(Progress) == false)
  141. return false;
  142. DCache = new pkgDepCache(Cache,Policy);
  143. if (_error->PendingError() == true)
  144. return false;
  145. return DCache->Init(Progress);
  146. }
  147. /*}}}*/
  148. // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
  149. // ---------------------------------------------------------------------
  150. /* */
  151. bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock)
  152. {
  153. if (BuildCaches(Progress,WithLock) == false)
  154. return false;
  155. if (BuildPolicy(Progress) == false)
  156. return false;
  157. if (BuildDepCache(Progress) == false)
  158. return false;
  159. if (Progress != NULL)
  160. Progress->Done();
  161. if (_error->PendingError() == true)
  162. return false;
  163. return true;
  164. }
  165. /*}}}*/
  166. bool pkgCacheFile::AddIndexFile(pkgIndexFile * const File) /*{{{*/
  167. {
  168. if (SrcList == NULL)
  169. if (BuildSourceList() == false)
  170. return false;
  171. SrcList->AddVolatileFile(File);
  172. if (Cache == nullptr || File->HasPackages() == false || File->Exists() == false)
  173. return true;
  174. if (File->FindInCache(*Cache).end() == false)
  175. return _error->Warning("Duplicate sources.list entry %s",
  176. File->Describe().c_str());
  177. if (ExternOwner == false)
  178. {
  179. delete DCache;
  180. delete Cache;
  181. }
  182. delete Policy;
  183. DCache = NULL;
  184. Policy = NULL;
  185. Cache = NULL;
  186. if (ExternOwner == false)
  187. {
  188. // a dynamic mmap means that we have build at least parts of the cache
  189. // in memory – which we might or might not have written to disk.
  190. // Throwing away would therefore be a very costly operation we want to avoid
  191. DynamicMMap * dynmmap = dynamic_cast<DynamicMMap*>(Map);
  192. if (dynmmap != nullptr)
  193. {
  194. {
  195. pkgCacheGenerator Gen(dynmmap, nullptr);
  196. if (Gen.Start() == false || File->Merge(Gen, nullptr) == false)
  197. return false;
  198. }
  199. Cache = new pkgCache(Map);
  200. return _error->PendingError() == false;
  201. }
  202. else
  203. {
  204. delete Map;
  205. Map = NULL;
  206. }
  207. }
  208. else
  209. {
  210. ExternOwner = false;
  211. Map = NULL;
  212. }
  213. return true;
  214. }
  215. /*}}}*/
  216. // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
  217. // ---------------------------------------------------------------------
  218. /* */
  219. void pkgCacheFile::RemoveCaches()
  220. {
  221. std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
  222. std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
  223. if (pkgcache.empty() == false && RealFileExists(pkgcache) == true)
  224. RemoveFile("RemoveCaches", pkgcache);
  225. if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true)
  226. RemoveFile("RemoveCaches", srcpkgcache);
  227. if (pkgcache.empty() == false)
  228. {
  229. std::string cachedir = flNotFile(pkgcache);
  230. std::string cachefile = flNotDir(pkgcache);
  231. if (cachedir.empty() != true && cachefile.empty() != true && DirectoryExists(cachedir) == true)
  232. {
  233. cachefile.append(".");
  234. std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
  235. for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
  236. {
  237. std::string nuke = flNotDir(*file);
  238. if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
  239. continue;
  240. RemoveFile("RemoveCaches", *file);
  241. }
  242. }
  243. }
  244. if (srcpkgcache.empty() == true)
  245. return;
  246. std::string cachedir = flNotFile(srcpkgcache);
  247. std::string cachefile = flNotDir(srcpkgcache);
  248. if (cachedir.empty() == true || cachefile.empty() == true || DirectoryExists(cachedir) == false)
  249. return;
  250. cachefile.append(".");
  251. std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
  252. for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
  253. {
  254. std::string nuke = flNotDir(*file);
  255. if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
  256. continue;
  257. RemoveFile("RemoveCaches", *file);
  258. }
  259. }
  260. /*}}}*/
  261. // CacheFile::Close - close the cache files /*{{{*/
  262. // ---------------------------------------------------------------------
  263. /* */
  264. void pkgCacheFile::Close()
  265. {
  266. if (ExternOwner == false)
  267. {
  268. delete DCache;
  269. delete Cache;
  270. delete Map;
  271. }
  272. else
  273. ExternOwner = false;
  274. delete Policy;
  275. delete SrcList;
  276. _system->UnLock(true);
  277. Map = NULL;
  278. DCache = NULL;
  279. Policy = NULL;
  280. Cache = NULL;
  281. SrcList = NULL;
  282. }
  283. /*}}}*/