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.
 
 
 
 
 
 

317 lines
8.9 KiB

  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 <apt-pkg/indexfile.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <string>
  29. #include <vector>
  30. #include <apti18n.h>
  31. /*}}}*/
  32. // CacheFile::CacheFile - Constructor /*{{{*/
  33. pkgCacheFile::pkgCacheFile() : d(NULL), ExternOwner(false), Map(NULL), Cache(NULL),
  34. DCache(NULL), SrcList(NULL), Policy(NULL)
  35. {
  36. }
  37. pkgCacheFile::pkgCacheFile(pkgDepCache * const Owner) : d(NULL), ExternOwner(true),
  38. Map(&Owner->GetCache().GetMap()), Cache(&Owner->GetCache()),
  39. DCache(Owner), SrcList(NULL), Policy(NULL)
  40. {
  41. }
  42. /*}}}*/
  43. // CacheFile::~CacheFile - Destructor /*{{{*/
  44. // ---------------------------------------------------------------------
  45. /* */
  46. pkgCacheFile::~pkgCacheFile()
  47. {
  48. if (ExternOwner == false)
  49. {
  50. delete DCache;
  51. delete Cache;
  52. delete Map;
  53. }
  54. delete Policy;
  55. delete SrcList;
  56. if (ExternOwner == false)
  57. _system->UnLock(true);
  58. }
  59. /*}}}*/
  60. // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
  61. class APT_HIDDEN ScopedErrorMerge {
  62. public:
  63. ScopedErrorMerge() { _error->PushToStack(); }
  64. ~ScopedErrorMerge() { _error->MergeWithStack(); }
  65. };
  66. bool pkgCacheFile::BuildCaches(OpProgress *Progress, bool WithLock)
  67. {
  68. if (Cache != NULL)
  69. return true;
  70. ScopedErrorMerge sem;
  71. if (_config->FindB("pkgCacheFile::Generate", true) == false)
  72. {
  73. FileFd file(_config->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly);
  74. if (file.IsOpen() == false || file.Failed())
  75. return false;
  76. Map = new MMap(file, MMap::Public|MMap::ReadOnly);
  77. if (unlikely(Map->validData() == false))
  78. return false;
  79. Cache = new pkgCache(Map);
  80. return _error->PendingError() == false;
  81. }
  82. if (WithLock == true)
  83. if (_system->Lock() == false)
  84. return false;
  85. if (_error->PendingError() == true)
  86. return false;
  87. BuildSourceList(Progress);
  88. // Read the caches
  89. Cache = nullptr;
  90. bool Res = pkgCacheGenerator::MakeStatusCache(*SrcList,Progress,&Map, &Cache, true);
  91. if (Progress != NULL)
  92. Progress->Done();
  93. if (Res == false)
  94. return _error->Error(_("The package lists or status file could not be parsed or opened."));
  95. /* This sux, remove it someday */
  96. if (_error->PendingError() == true)
  97. _error->Warning(_("You may want to run apt-get update to correct these problems"));
  98. if (Cache == nullptr)
  99. Cache = new pkgCache(Map);
  100. if (_error->PendingError() == true)
  101. return false;
  102. return true;
  103. }
  104. /*}}}*/
  105. // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
  106. // ---------------------------------------------------------------------
  107. /* */
  108. bool pkgCacheFile::BuildSourceList(OpProgress * /*Progress*/)
  109. {
  110. if (SrcList != NULL)
  111. return true;
  112. SrcList = new pkgSourceList();
  113. if (SrcList->ReadMainList() == false)
  114. return _error->Error(_("The list of sources could not be read."));
  115. return true;
  116. }
  117. /*}}}*/
  118. // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
  119. // ---------------------------------------------------------------------
  120. /* */
  121. bool pkgCacheFile::BuildPolicy(OpProgress * /*Progress*/)
  122. {
  123. if (Policy != NULL)
  124. return true;
  125. Policy = new pkgPolicy(Cache);
  126. if (_error->PendingError() == true)
  127. return false;
  128. if (ReadPinFile(*Policy) == false || ReadPinDir(*Policy) == false)
  129. return false;
  130. return true;
  131. }
  132. /*}}}*/
  133. // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
  134. // ---------------------------------------------------------------------
  135. /* */
  136. bool pkgCacheFile::BuildDepCache(OpProgress *Progress)
  137. {
  138. if (DCache != NULL)
  139. return true;
  140. if (BuildPolicy(Progress) == false)
  141. return false;
  142. DCache = new pkgDepCache(Cache,Policy);
  143. if (_error->PendingError() == true)
  144. return false;
  145. return DCache->Init(Progress);
  146. }
  147. /*}}}*/
  148. // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
  149. // ---------------------------------------------------------------------
  150. /* */
  151. bool pkgCacheFile::Open(OpProgress *Progress, bool WithLock)
  152. {
  153. if (BuildCaches(Progress,WithLock) == false)
  154. return false;
  155. if (BuildPolicy(Progress) == false)
  156. return false;
  157. if (BuildDepCache(Progress) == false)
  158. return false;
  159. if (Progress != NULL)
  160. Progress->Done();
  161. if (_error->PendingError() == true)
  162. return false;
  163. return true;
  164. }
  165. /*}}}*/
  166. bool pkgCacheFile::AddIndexFile(pkgIndexFile * const File) /*{{{*/
  167. {
  168. if (SrcList == NULL)
  169. if (BuildSourceList() == false)
  170. return false;
  171. SrcList->AddVolatileFile(File);
  172. if (Cache == nullptr || File->HasPackages() == false || File->Exists() == false)
  173. return true;
  174. if (File->FindInCache(*Cache).end() == false)
  175. return _error->Warning("Duplicate sources.list entry %s",
  176. File->Describe().c_str());
  177. if (ExternOwner == false)
  178. {
  179. delete DCache;
  180. delete Cache;
  181. }
  182. delete Policy;
  183. DCache = NULL;
  184. Policy = NULL;
  185. Cache = NULL;
  186. if (ExternOwner == false)
  187. {
  188. // a dynamic mmap means that we have build at least parts of the cache
  189. // in memory – which we might or might not have written to disk.
  190. // Throwing away would therefore be a very costly operation we want to avoid
  191. DynamicMMap * dynmmap = dynamic_cast<DynamicMMap*>(Map);
  192. if (dynmmap != nullptr)
  193. {
  194. {
  195. pkgCacheGenerator Gen(dynmmap, nullptr);
  196. if (Gen.Start() == false || File->Merge(Gen, nullptr) == false)
  197. return false;
  198. }
  199. Cache = new pkgCache(Map);
  200. return _error->PendingError() == false;
  201. }
  202. else
  203. {
  204. delete Map;
  205. Map = NULL;
  206. }
  207. }
  208. else
  209. {
  210. ExternOwner = false;
  211. Map = NULL;
  212. }
  213. _system->UnLock(true);
  214. return true;
  215. }
  216. /*}}}*/
  217. // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
  218. // ---------------------------------------------------------------------
  219. /* */
  220. void pkgCacheFile::RemoveCaches()
  221. {
  222. std::string const pkgcache = _config->FindFile("Dir::cache::pkgcache");
  223. std::string const srcpkgcache = _config->FindFile("Dir::cache::srcpkgcache");
  224. if (pkgcache.empty() == false && RealFileExists(pkgcache) == true)
  225. RemoveFile("RemoveCaches", pkgcache);
  226. if (srcpkgcache.empty() == false && RealFileExists(srcpkgcache) == true)
  227. RemoveFile("RemoveCaches", srcpkgcache);
  228. if (pkgcache.empty() == false)
  229. {
  230. std::string cachedir = flNotFile(pkgcache);
  231. std::string cachefile = flNotDir(pkgcache);
  232. if (cachedir.empty() != true && cachefile.empty() != true && DirectoryExists(cachedir) == true)
  233. {
  234. cachefile.append(".");
  235. std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
  236. for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
  237. {
  238. std::string nuke = flNotDir(*file);
  239. if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
  240. continue;
  241. RemoveFile("RemoveCaches", *file);
  242. }
  243. }
  244. }
  245. if (srcpkgcache.empty() == true)
  246. return;
  247. std::string cachedir = flNotFile(srcpkgcache);
  248. std::string cachefile = flNotDir(srcpkgcache);
  249. if (cachedir.empty() == true || cachefile.empty() == true || DirectoryExists(cachedir) == false)
  250. return;
  251. cachefile.append(".");
  252. std::vector<std::string> caches = GetListOfFilesInDir(cachedir, false);
  253. for (std::vector<std::string>::const_iterator file = caches.begin(); file != caches.end(); ++file)
  254. {
  255. std::string nuke = flNotDir(*file);
  256. if (strncmp(cachefile.c_str(), nuke.c_str(), cachefile.length()) != 0)
  257. continue;
  258. RemoveFile("RemoveCaches", *file);
  259. }
  260. }
  261. /*}}}*/
  262. // CacheFile::Close - close the cache files /*{{{*/
  263. // ---------------------------------------------------------------------
  264. /* */
  265. void pkgCacheFile::Close()
  266. {
  267. if (ExternOwner == false)
  268. {
  269. delete DCache;
  270. delete Cache;
  271. delete Map;
  272. }
  273. else
  274. ExternOwner = false;
  275. delete Policy;
  276. delete SrcList;
  277. _system->UnLock(true);
  278. Map = NULL;
  279. DCache = NULL;
  280. Policy = NULL;
  281. Cache = NULL;
  282. SrcList = NULL;
  283. }
  284. /*}}}*/