cachefile.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. bool Res = pkgCacheGenerator::MakeStatusCache(*SrcList,Progress,&Map, true);
  89. if (Progress != NULL)
  90. Progress->Done();
  91. if (Res == false)
  92. return _error->Error(_("The package lists or status file could not be parsed or opened."));
  93. /* This sux, remove it someday */
  94. if (_error->PendingError() == true)
  95. _error->Warning(_("You may want to run apt-get update to correct these problems"));
  96. Cache = new pkgCache(Map);
  97. if (_error->PendingError() == true)
  98. return false;
  99. return true;
  100. }
  101. /*}}}*/
  102. // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
  103. // ---------------------------------------------------------------------
  104. /* */
  105. bool pkgCacheFile::BuildSourceList(OpProgress * /*Progress*/)
  106. {
  107. if (SrcList != NULL)
  108. return true;
  109. SrcList = new pkgSourceList();
  110. if (SrcList->ReadMainList() == false)
  111. return _error->Error(_("The list of sources could not be read."));
  112. return true;
  113. }
  114. /*}}}*/
  115. // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
  116. // ---------------------------------------------------------------------
  117. /* */
  118. bool pkgCacheFile::BuildPolicy(OpProgress * /*Progress*/)
  119. {
  120. if (Policy != NULL)
  121. return true;
  122. Policy = new pkgPolicy(Cache);
  123. if (_error->PendingError() == true)
  124. return false;
  125. if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
  126. return false;
  127. return true;
  128. }
  129. /*}}}*/
  130. // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
  131. // ---------------------------------------------------------------------
  132. /* */
  133. bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
  134. {
  135. if (DCache != NULL)
  136. return true;
  137. if (BuildPolicy(Progress) == false)
  138. return false;
  139. DCache = new pkgDepCache(Cache,Policy);
  140. if (_error->PendingError() == true)
  141. return false;
  142. return DCache->Init(Progress);
  143. }
  144. /*}}}*/
  145. // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
  146. // ---------------------------------------------------------------------
  147. /* */
  148. bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock)
  149. {
  150. if (BuildCaches(Progress,WithLock) == false)
  151. return false;
  152. if (BuildPolicy(Progress) == false)
  153. return false;
  154. if (BuildDepCache(Progress) == false)
  155. return false;
  156. if (Progress != NULL)
  157. Progress->Done();
  158. if (_error->PendingError() == true)
  159. return false;
  160. return true;
  161. }
  162. /*}}}*/
  163. // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
  164. // ---------------------------------------------------------------------
  165. /* */
  166. void pkgCacheFile::RemoveCaches()
  167. {
  168. std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
  169. std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
  170. if (pkgcache.empty() == false && RealFileExists(pkgcache) == true)
  171. RemoveFile("RemoveCaches", pkgcache);
  172. if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true)
  173. RemoveFile("RemoveCaches", srcpkgcache);
  174. if (pkgcache.empty() == false)
  175. {
  176. std::string cachedir = flNotFile(pkgcache);
  177. std::string cachefile = flNotDir(pkgcache);
  178. if (cachedir.empty() != true && cachefile.empty() != true && DirectoryExists(cachedir) == true)
  179. {
  180. cachefile.append(".");
  181. std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
  182. for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
  183. {
  184. std::string nuke = flNotDir(*file);
  185. if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
  186. continue;
  187. RemoveFile("RemoveCaches", *file);
  188. }
  189. }
  190. }
  191. if (srcpkgcache.empty() == true)
  192. return;
  193. std::string cachedir = flNotFile(srcpkgcache);
  194. std::string cachefile = flNotDir(srcpkgcache);
  195. if (cachedir.empty() == true || cachefile.empty() == true || DirectoryExists(cachedir) == false)
  196. return;
  197. cachefile.append(".");
  198. std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
  199. for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
  200. {
  201. std::string nuke = flNotDir(*file);
  202. if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
  203. continue;
  204. RemoveFile("RemoveCaches", *file);
  205. }
  206. }
  207. /*}}}*/
  208. // CacheFile::Close - close the cache files /*{{{*/
  209. // ---------------------------------------------------------------------
  210. /* */
  211. void pkgCacheFile::Close()
  212. {
  213. if (ExternOwner == false)
  214. {
  215. delete DCache;
  216. delete Cache;
  217. delete Map;
  218. }
  219. else
  220. ExternOwner = false;
  221. delete Policy;
  222. delete SrcList;
  223. _system->UnLock(true);
  224. Map = NULL;
  225. DCache = NULL;
  226. Policy = NULL;
  227. Cache = NULL;
  228. SrcList = NULL;
  229. }
  230. /*}}}*/