cachefile.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 <string.h>
  26. #include <unistd.h>
  27. #include <string>
  28. #include <vector>
  29. #include <apti18n.h>
  30. /*}}}*/
  31. // CacheFile::CacheFile - Constructor /*{{{*/
  32. pkgCacheFile::pkgCacheFile() : d(NULL), ExternOwner(false), Map(NULL), Cache(NULL),
  33. DCache(NULL), SrcList(NULL), Policy(NULL)
  34. {
  35. }
  36. pkgCacheFile::pkgCacheFile(pkgDepCache * const Owner) : d(NULL), ExternOwner(true),
  37. Map(&Owner->GetCache().GetMap()), Cache(&Owner->GetCache()),
  38. DCache(Owner), SrcList(NULL), Policy(NULL)
  39. {
  40. }
  41. /*}}}*/
  42. // CacheFile::~CacheFile - Destructor /*{{{*/
  43. // ---------------------------------------------------------------------
  44. /* */
  45. pkgCacheFile::~pkgCacheFile()
  46. {
  47. if (ExternOwner == false)
  48. {
  49. delete DCache;
  50. delete Cache;
  51. delete Map;
  52. }
  53. delete Policy;
  54. delete SrcList;
  55. if (ExternOwner == false)
  56. _system->UnLock(true);
  57. }
  58. /*}}}*/
  59. // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
  60. class APT_HIDDEN ScopedErrorMerge {
  61. public:
  62. ScopedErrorMerge() { _error->PushToStack(); }
  63. ~ScopedErrorMerge() { _error->MergeWithStack(); }
  64. };
  65. bool pkgCacheFile::BuildCaches(OpProgress *Progress, bool WithLock)
  66. {
  67. if (Cache != NULL)
  68. return true;
  69. ScopedErrorMerge sem;
  70. if (_config->FindB("pkgCacheFile::Generate", true) == false)
  71. {
  72. FileFd file(_config->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly);
  73. if (file.IsOpen() == false || file.Failed())
  74. return false;
  75. Map = new MMap(file, MMap::Public|MMap::ReadOnly);
  76. if (unlikely(Map->validData() == false))
  77. return false;
  78. Cache = new pkgCache(Map);
  79. return _error->PendingError() == false;
  80. }
  81. if (WithLock == true)
  82. if (_system->Lock() == false)
  83. return false;
  84. if (_error->PendingError() == true)
  85. return false;
  86. BuildSourceList(Progress);
  87. // Read the caches
  88. Cache = nullptr;
  89. bool Res = pkgCacheGenerator::MakeStatusCache(*SrcList,Progress,&Map, &Cache, true);
  90. if (Progress != NULL)
  91. Progress->Done();
  92. if (Res == false)
  93. return _error->Error(_("The package lists or status file could not be parsed or opened."));
  94. /* This sux, remove it someday */
  95. if (_error->PendingError() == true)
  96. _error->Warning(_("You may want to run apt-get update to correct these problems"));
  97. if (Cache == nullptr)
  98. Cache = new pkgCache(Map);
  99. if (_error->PendingError() == true)
  100. return false;
  101. return true;
  102. }
  103. /*}}}*/
  104. // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
  105. // ---------------------------------------------------------------------
  106. /* */
  107. bool pkgCacheFile::BuildSourceList(OpProgress * /*Progress*/)
  108. {
  109. if (SrcList != NULL)
  110. return true;
  111. SrcList = new pkgSourceList();
  112. if (SrcList->ReadMainList() == false)
  113. return _error->Error(_("The list of sources could not be read."));
  114. return true;
  115. }
  116. /*}}}*/
  117. // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
  118. // ---------------------------------------------------------------------
  119. /* */
  120. bool pkgCacheFile::BuildPolicy(OpProgress * /*Progress*/)
  121. {
  122. if (Policy != NULL)
  123. return true;
  124. Policy = new pkgPolicy(Cache);
  125. if (_error->PendingError() == true)
  126. return false;
  127. if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
  128. return false;
  129. return true;
  130. }
  131. /*}}}*/
  132. // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
  133. // ---------------------------------------------------------------------
  134. /* */
  135. bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
  136. {
  137. if (DCache != NULL)
  138. return true;
  139. if (BuildPolicy(Progress) == false)
  140. return false;
  141. DCache = new pkgDepCache(Cache,Policy);
  142. if (_error->PendingError() == true)
  143. return false;
  144. return DCache->Init(Progress);
  145. }
  146. /*}}}*/
  147. // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
  148. // ---------------------------------------------------------------------
  149. /* */
  150. bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock)
  151. {
  152. if (BuildCaches(Progress,WithLock) == false)
  153. return false;
  154. if (BuildPolicy(Progress) == false)
  155. return false;
  156. if (BuildDepCache(Progress) == false)
  157. return false;
  158. if (Progress != NULL)
  159. Progress->Done();
  160. if (_error->PendingError() == true)
  161. return false;
  162. return true;
  163. }
  164. /*}}}*/
  165. // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
  166. // ---------------------------------------------------------------------
  167. /* */
  168. void pkgCacheFile::RemoveCaches()
  169. {
  170. std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
  171. std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
  172. if (pkgcache.empty() == false && RealFileExists(pkgcache) == true)
  173. RemoveFile("RemoveCaches", pkgcache);
  174. if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true)
  175. RemoveFile("RemoveCaches", srcpkgcache);
  176. if (pkgcache.empty() == false)
  177. {
  178. std::string cachedir = flNotFile(pkgcache);
  179. std::string cachefile = flNotDir(pkgcache);
  180. if (cachedir.empty() != true && cachefile.empty() != true && DirectoryExists(cachedir) == true)
  181. {
  182. cachefile.append(".");
  183. std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
  184. for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
  185. {
  186. std::string nuke = flNotDir(*file);
  187. if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
  188. continue;
  189. RemoveFile("RemoveCaches", *file);
  190. }
  191. }
  192. }
  193. if (srcpkgcache.empty() == true)
  194. return;
  195. std::string cachedir = flNotFile(srcpkgcache);
  196. std::string cachefile = flNotDir(srcpkgcache);
  197. if (cachedir.empty() == true || cachefile.empty() == true || DirectoryExists(cachedir) == false)
  198. return;
  199. cachefile.append(".");
  200. std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
  201. for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
  202. {
  203. std::string nuke = flNotDir(*file);
  204. if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
  205. continue;
  206. RemoveFile("RemoveCaches", *file);
  207. }
  208. }
  209. /*}}}*/
  210. // CacheFile::Close - close the cache files /*{{{*/
  211. // ---------------------------------------------------------------------
  212. /* */
  213. void pkgCacheFile::Close()
  214. {
  215. if (ExternOwner == false)
  216. {
  217. delete DCache;
  218. delete Cache;
  219. delete Map;
  220. }
  221. else
  222. ExternOwner = false;
  223. delete Policy;
  224. delete SrcList;
  225. _system->UnLock(true);
  226. Map = NULL;
  227. DCache = NULL;
  228. Policy = NULL;
  229. Cache = NULL;
  230. SrcList = NULL;
  231. }
  232. /*}}}*/