cachefile.cc 7.3 KB

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