1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cachefile.cc,v 1.8 2002/04/27 04:28:04 jgg Exp $
4 /* ######################################################################
6 CacheFile - Simple wrapper class for opening, generating and whatnot
8 This class implements a simple 2 line mechanism to open various sorts
9 of caches. It can operate as root, as not root, show progress and so on,
10 it transparently handles everything necessary.
12 ##################################################################### */
14 // Include Files /*{{{*/
17 #include <apt-pkg/cachefile.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/sourcelist.h>
20 #include <apt-pkg/pkgcachegen.h>
21 #include <apt-pkg/configuration.h>
22 #include <apt-pkg/policy.h>
23 #include <apt-pkg/pkgsystem.h>
24 #include <apt-pkg/fileutl.h>
25 #include <apt-pkg/progress.h>
26 #include <apt-pkg/depcache.h>
27 #include <apt-pkg/mmap.h>
28 #include <apt-pkg/pkgcache.h>
29 #include <apt-pkg/indexfile.h>
38 // CacheFile::CacheFile - Constructor /*{{{*/
39 pkgCacheFile::pkgCacheFile() : d(NULL
), ExternOwner(false), Map(NULL
), Cache(NULL
),
40 DCache(NULL
), SrcList(NULL
), Policy(NULL
)
43 pkgCacheFile::pkgCacheFile(pkgDepCache
* const Owner
) : d(NULL
), ExternOwner(true),
44 Map(&Owner
->GetCache().GetMap()), Cache(&Owner
->GetCache()),
45 DCache(Owner
), SrcList(NULL
), Policy(NULL
)
49 // CacheFile::~CacheFile - Destructor /*{{{*/
50 // ---------------------------------------------------------------------
52 pkgCacheFile::~pkgCacheFile()
54 if (ExternOwner
== false)
62 if (ExternOwner
== false)
63 _system
->UnLock(true);
66 // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
67 class APT_HIDDEN ScopedErrorMerge
{
69 ScopedErrorMerge() { _error
->PushToStack(); }
70 ~ScopedErrorMerge() { _error
->MergeWithStack(); }
72 bool pkgCacheFile::BuildCaches(OpProgress
*Progress
, bool WithLock
)
78 if (_config
->FindB("pkgCacheFile::Generate", true) == false)
80 FileFd
file(_config
->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly
);
81 if (file
.IsOpen() == false || file
.Failed())
83 Map
= new MMap(file
, MMap::Public
|MMap::ReadOnly
);
84 if (unlikely(Map
->validData() == false))
86 Cache
= new pkgCache(Map
);
87 return _error
->PendingError() == false;
91 if (_system
->Lock() == false)
94 if (_error
->PendingError() == true)
97 BuildSourceList(Progress
);
101 bool Res
= pkgCacheGenerator::MakeStatusCache(*SrcList
,Progress
,&Map
, &Cache
, true);
102 if (Progress
!= NULL
)
105 return _error
->Error(_("The package lists or status file could not be parsed or opened."));
107 /* This sux, remove it someday */
108 if (_error
->PendingError() == true)
109 _error
->Warning(_("You may want to run apt-get update to correct these problems"));
111 if (Cache
== nullptr)
112 Cache
= new pkgCache(Map
);
113 if (_error
->PendingError() == true)
118 // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
119 // ---------------------------------------------------------------------
121 bool pkgCacheFile::BuildSourceList(OpProgress
* /*Progress*/)
126 SrcList
= new pkgSourceList();
127 if (SrcList
->ReadMainList() == false)
128 return _error
->Error(_("The list of sources could not be read."));
132 // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
133 // ---------------------------------------------------------------------
135 bool pkgCacheFile::BuildPolicy(OpProgress
* /*Progress*/)
140 Policy
= new pkgPolicy(Cache
);
141 if (_error
->PendingError() == true)
144 if (ReadPinFile(*Policy
) == false || ReadPinDir(*Policy
) == false)
150 // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
151 // ---------------------------------------------------------------------
153 bool pkgCacheFile::BuildDepCache(OpProgress
*Progress
)
158 if (BuildPolicy(Progress
) == false)
161 DCache
= new pkgDepCache(Cache
,Policy
);
162 if (_error
->PendingError() == true)
165 return DCache
->Init(Progress
);
168 // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
169 // ---------------------------------------------------------------------
171 bool pkgCacheFile::Open(OpProgress
*Progress
, bool WithLock
)
173 if (BuildCaches(Progress
,WithLock
) == false)
176 if (BuildPolicy(Progress
) == false)
179 if (BuildDepCache(Progress
) == false)
182 if (Progress
!= NULL
)
184 if (_error
->PendingError() == true)
190 bool pkgCacheFile::AddIndexFile(pkgIndexFile
* const File
) /*{{{*/
193 if (BuildSourceList() == false)
195 SrcList
->AddVolatileFile(File
);
197 if (Cache
== nullptr || File
->HasPackages() == false || File
->Exists() == false)
200 if (File
->FindInCache(*Cache
).end() == false)
201 return _error
->Warning("Duplicate sources.list entry %s",
202 File
->Describe().c_str());
204 if (ExternOwner
== false)
214 if (ExternOwner
== false)
216 // a dynamic mmap means that we have build at least parts of the cache
217 // in memory – which we might or might not have written to disk.
218 // Throwing away would therefore be a very costly operation we want to avoid
219 DynamicMMap
* dynmmap
= dynamic_cast<DynamicMMap
*>(Map
);
220 if (dynmmap
!= nullptr)
223 pkgCacheGenerator
Gen(dynmmap
, nullptr);
224 if (Gen
.Start() == false || File
->Merge(Gen
, nullptr) == false)
227 Cache
= new pkgCache(Map
);
228 return _error
->PendingError() == false;
244 // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
245 // ---------------------------------------------------------------------
247 void pkgCacheFile::RemoveCaches()
249 std::string
const pkgcache
= _config
->FindFile("Dir::cache::pkgcache");
250 std::string
const srcpkgcache
= _config
->FindFile("Dir::cache::srcpkgcache");
252 if (pkgcache
.empty() == false && RealFileExists(pkgcache
) == true)
253 RemoveFile("RemoveCaches", pkgcache
);
254 if (srcpkgcache
.empty() == false && RealFileExists(srcpkgcache
) == true)
255 RemoveFile("RemoveCaches", srcpkgcache
);
256 if (pkgcache
.empty() == false)
258 std::string cachedir
= flNotFile(pkgcache
);
259 std::string cachefile
= flNotDir(pkgcache
);
260 if (cachedir
.empty() != true && cachefile
.empty() != true && DirectoryExists(cachedir
) == true)
262 cachefile
.append(".");
263 std::vector
<std::string
> caches
= GetListOfFilesInDir(cachedir
, false);
264 for (std::vector
<std::string
>::const_iterator file
= caches
.begin(); file
!= caches
.end(); ++file
)
266 std::string nuke
= flNotDir(*file
);
267 if (strncmp(cachefile
.c_str(), nuke
.c_str(), cachefile
.length()) != 0)
269 RemoveFile("RemoveCaches", *file
);
274 if (srcpkgcache
.empty() == true)
277 std::string cachedir
= flNotFile(srcpkgcache
);
278 std::string cachefile
= flNotDir(srcpkgcache
);
279 if (cachedir
.empty() == true || cachefile
.empty() == true || DirectoryExists(cachedir
) == false)
281 cachefile
.append(".");
282 std::vector
<std::string
> caches
= GetListOfFilesInDir(cachedir
, false);
283 for (std::vector
<std::string
>::const_iterator file
= caches
.begin(); file
!= caches
.end(); ++file
)
285 std::string nuke
= flNotDir(*file
);
286 if (strncmp(cachefile
.c_str(), nuke
.c_str(), cachefile
.length()) != 0)
288 RemoveFile("RemoveCaches", *file
);
292 // CacheFile::Close - close the cache files /*{{{*/
293 // ---------------------------------------------------------------------
295 void pkgCacheFile::Close()
297 if (ExternOwner
== false)
307 _system
->UnLock(true);