cachefile.cc 7.0 KB

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