cachefile.cc 7.1 KB

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