You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cachefile.cc 7.4 KiB

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