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>
39 // CacheFile::CacheFile - Constructor /*{{{*/
40 pkgCacheFile::pkgCacheFile() : d(NULL
), ExternOwner(false), Map(NULL
), Cache(NULL
),
41 DCache(NULL
), SrcList(NULL
), Policy(NULL
)
44 pkgCacheFile::pkgCacheFile(pkgDepCache
* const Owner
) : d(NULL
), ExternOwner(true),
45 Map(&Owner
->GetCache().GetMap()), Cache(&Owner
->GetCache()),
46 DCache(Owner
), SrcList(NULL
), Policy(NULL
)
50 // CacheFile::~CacheFile - Destructor /*{{{*/
51 // ---------------------------------------------------------------------
53 pkgCacheFile::~pkgCacheFile()
55 if (ExternOwner
== false)
63 if (ExternOwner
== false)
64 _system
->UnLock(true);
67 // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
68 class APT_HIDDEN ScopedErrorMerge
{
70 ScopedErrorMerge() { _error
->PushToStack(); }
71 ~ScopedErrorMerge() { _error
->MergeWithStack(); }
74 bool pkgCacheFile::BuildCaches(OpProgress
*Progress
, bool WithLock
)
76 std::unique_ptr
<pkgCache
> Cache
;
77 std::unique_ptr
<MMap
> Map
;
79 if (this->Cache
!= NULL
)
83 if (_config
->FindB("pkgCacheFile::Generate", true) == false)
85 FileFd
file(_config
->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly
);
86 if (file
.IsOpen() == false || file
.Failed())
88 Map
.reset(new MMap(file
, MMap::Public
|MMap::ReadOnly
));
89 if (unlikely(Map
->validData() == false))
91 Cache
.reset(new pkgCache(Map
.get()));
92 if (_error
->PendingError() == true)
93 return _error
->ReturnError();
95 this->Cache
= Cache
.release();
96 this->Map
= Map
.release();
100 if (WithLock
== true)
101 if (_system
->Lock() == false)
104 if (_error
->PendingError() == true)
105 return _error
->ReturnError();
107 if (BuildSourceList(Progress
) == false)
111 MMap
*TmpMap
= nullptr;
112 pkgCache
*TmpCache
= nullptr;
113 bool Res
= pkgCacheGenerator::MakeStatusCache(*SrcList
,Progress
,&TmpMap
, &TmpCache
, true);
115 Cache
.reset(TmpCache
);
116 if (Progress
!= NULL
)
119 return _error
->Error(_("The package lists or status file could not be parsed or opened."));
121 if (Cache
== nullptr)
122 Cache
.reset(new pkgCache(Map
.get()));
123 this->Map
= Map
.release();
124 this->Cache
= Cache
.release();
129 // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
130 // ---------------------------------------------------------------------
132 bool pkgCacheFile::BuildSourceList(OpProgress
* /*Progress*/)
134 std::unique_ptr
<pkgSourceList
> SrcList
;
135 if (this->SrcList
!= NULL
)
138 SrcList
.reset(new pkgSourceList());
139 if (SrcList
->ReadMainList() == false)
140 return _error
->Error(_("The list of sources could not be read."));
141 this->SrcList
= SrcList
.release();
145 // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
146 // ---------------------------------------------------------------------
148 bool pkgCacheFile::BuildPolicy(OpProgress
* /*Progress*/)
150 std::unique_ptr
<pkgPolicy
> Policy
;
151 if (this->Policy
!= NULL
)
154 Policy
.reset(new pkgPolicy(Cache
));
155 if (_error
->PendingError() == true)
156 return _error
->ReturnError();
158 if (ReadPinFile(*Policy
) == false || ReadPinDir(*Policy
) == false)
161 this->Policy
= Policy
.release();
165 // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
166 // ---------------------------------------------------------------------
168 bool pkgCacheFile::BuildDepCache(OpProgress
*Progress
)
170 if (BuildCaches(Progress
, false) == false)
173 std::unique_ptr
<pkgDepCache
> DCache
;
174 if (this->DCache
!= NULL
)
177 if (BuildPolicy(Progress
) == false)
180 DCache
.reset(new pkgDepCache(Cache
,Policy
));
181 if (_error
->PendingError() == true)
182 return _error
->ReturnError();
183 if (DCache
->Init(Progress
) == false)
186 this->DCache
= DCache
.release();
190 // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
191 // ---------------------------------------------------------------------
193 bool pkgCacheFile::Open(OpProgress
*Progress
, bool WithLock
)
195 if (BuildCaches(Progress
,WithLock
) == false)
198 if (BuildPolicy(Progress
) == false)
201 if (BuildDepCache(Progress
) == false)
204 if (Progress
!= NULL
)
210 bool pkgCacheFile::AddIndexFile(pkgIndexFile
* const File
) /*{{{*/
213 if (BuildSourceList() == false)
215 SrcList
->AddVolatileFile(File
);
217 if (Cache
== nullptr || File
->HasPackages() == false || File
->Exists() == false)
220 if (File
->FindInCache(*Cache
).end() == false)
221 return _error
->Warning("Duplicate sources.list entry %s",
222 File
->Describe().c_str());
224 if (ExternOwner
== false)
234 if (ExternOwner
== false)
236 // a dynamic mmap means that we have build at least parts of the cache
237 // in memory – which we might or might not have written to disk.
238 // Throwing away would therefore be a very costly operation we want to avoid
239 DynamicMMap
* dynmmap
= dynamic_cast<DynamicMMap
*>(Map
);
240 if (dynmmap
!= nullptr)
243 pkgCacheGenerator
Gen(dynmmap
, nullptr);
244 if (Gen
.Start() == false || File
->Merge(Gen
, nullptr) == false)
247 Cache
= new pkgCache(Map
);
248 if (_error
->PendingError() == true) {
251 return _error
->ReturnError();
266 _system
->UnLock(true);
270 // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
271 // ---------------------------------------------------------------------
273 void pkgCacheFile::RemoveCaches()
275 std::string
const pkgcache
= _config
->FindFile("Dir::cache::pkgcache");
276 std::string
const srcpkgcache
= _config
->FindFile("Dir::cache::srcpkgcache");
278 if (pkgcache
.empty() == false && RealFileExists(pkgcache
) == true)
279 RemoveFile("RemoveCaches", pkgcache
);
280 if (srcpkgcache
.empty() == false && RealFileExists(srcpkgcache
) == true)
281 RemoveFile("RemoveCaches", srcpkgcache
);
282 if (pkgcache
.empty() == false)
284 std::string cachedir
= flNotFile(pkgcache
);
285 std::string cachefile
= flNotDir(pkgcache
);
286 if (cachedir
.empty() != true && cachefile
.empty() != true && DirectoryExists(cachedir
) == true)
288 cachefile
.append(".");
289 std::vector
<std::string
> caches
= GetListOfFilesInDir(cachedir
, false);
290 for (std::vector
<std::string
>::const_iterator file
= caches
.begin(); file
!= caches
.end(); ++file
)
292 std::string nuke
= flNotDir(*file
);
293 if (strncmp(cachefile
.c_str(), nuke
.c_str(), cachefile
.length()) != 0)
295 RemoveFile("RemoveCaches", *file
);
300 if (srcpkgcache
.empty() == true)
303 std::string cachedir
= flNotFile(srcpkgcache
);
304 std::string cachefile
= flNotDir(srcpkgcache
);
305 if (cachedir
.empty() == true || cachefile
.empty() == true || DirectoryExists(cachedir
) == false)
307 cachefile
.append(".");
308 std::vector
<std::string
> caches
= GetListOfFilesInDir(cachedir
, false);
309 for (std::vector
<std::string
>::const_iterator file
= caches
.begin(); file
!= caches
.end(); ++file
)
311 std::string nuke
= flNotDir(*file
);
312 if (strncmp(cachefile
.c_str(), nuke
.c_str(), cachefile
.length()) != 0)
314 RemoveFile("RemoveCaches", *file
);
318 // CacheFile::Close - close the cache files /*{{{*/
319 // ---------------------------------------------------------------------
321 void pkgCacheFile::Close()
323 if (ExternOwner
== false)
333 _system
->UnLock(true);