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)
95 this->Cache
= Cache
.release();
96 this->Map
= Map
.release();
100 if (WithLock
== true)
101 if (_system
->Lock() == false)
104 if (_error
->PendingError() == true)
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 /* This sux, remove it someday */
122 if (_error
->PendingError() == true)
123 _error
->Warning(_("You may want to run apt-get update to correct these problems"));
125 if (Cache
== nullptr)
126 Cache
.reset(new pkgCache(Map
.get()));
127 if (_error
->PendingError() == true)
129 this->Map
= Map
.release();
130 this->Cache
= Cache
.release();
135 // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
136 // ---------------------------------------------------------------------
138 bool pkgCacheFile::BuildSourceList(OpProgress
* /*Progress*/)
140 std::unique_ptr
<pkgSourceList
> SrcList
;
141 if (this->SrcList
!= NULL
)
144 SrcList
.reset(new pkgSourceList());
145 if (SrcList
->ReadMainList() == false)
146 return _error
->Error(_("The list of sources could not be read."));
147 this->SrcList
= SrcList
.release();
151 // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
152 // ---------------------------------------------------------------------
154 bool pkgCacheFile::BuildPolicy(OpProgress
* /*Progress*/)
156 std::unique_ptr
<pkgPolicy
> Policy
;
157 if (this->Policy
!= NULL
)
160 Policy
.reset(new pkgPolicy(Cache
));
161 if (_error
->PendingError() == true)
164 if (ReadPinFile(*Policy
) == false || ReadPinDir(*Policy
) == false)
167 this->Policy
= Policy
.release();
171 // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
172 // ---------------------------------------------------------------------
174 bool pkgCacheFile::BuildDepCache(OpProgress
*Progress
)
176 if (BuildCaches(Progress
, false) == false)
179 std::unique_ptr
<pkgDepCache
> DCache
;
180 if (this->DCache
!= NULL
)
183 if (BuildPolicy(Progress
) == false)
186 DCache
.reset(new pkgDepCache(Cache
,Policy
));
187 if (_error
->PendingError() == true)
189 if (DCache
->Init(Progress
) == false)
192 this->DCache
= DCache
.release();
196 // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
197 // ---------------------------------------------------------------------
199 bool pkgCacheFile::Open(OpProgress
*Progress
, bool WithLock
)
201 if (BuildCaches(Progress
,WithLock
) == false)
204 if (BuildPolicy(Progress
) == false)
207 if (BuildDepCache(Progress
) == false)
210 if (Progress
!= NULL
)
212 if (_error
->PendingError() == true)
218 bool pkgCacheFile::AddIndexFile(pkgIndexFile
* const File
) /*{{{*/
221 if (BuildSourceList() == false)
223 SrcList
->AddVolatileFile(File
);
225 if (Cache
== nullptr || File
->HasPackages() == false || File
->Exists() == false)
228 if (File
->FindInCache(*Cache
).end() == false)
229 return _error
->Warning("Duplicate sources.list entry %s",
230 File
->Describe().c_str());
232 if (ExternOwner
== false)
242 if (ExternOwner
== false)
244 // a dynamic mmap means that we have build at least parts of the cache
245 // in memory – which we might or might not have written to disk.
246 // Throwing away would therefore be a very costly operation we want to avoid
247 DynamicMMap
* dynmmap
= dynamic_cast<DynamicMMap
*>(Map
);
248 if (dynmmap
!= nullptr)
251 pkgCacheGenerator
Gen(dynmmap
, nullptr);
252 if (Gen
.Start() == false || File
->Merge(Gen
, nullptr) == false)
255 Cache
= new pkgCache(Map
);
256 if (_error
->PendingError() == true) {
274 _system
->UnLock(true);
278 // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
279 // ---------------------------------------------------------------------
281 void pkgCacheFile::RemoveCaches()
283 std::string
const pkgcache
= _config
->FindFile("Dir::cache::pkgcache");
284 std::string
const srcpkgcache
= _config
->FindFile("Dir::cache::srcpkgcache");
286 if (pkgcache
.empty() == false && RealFileExists(pkgcache
) == true)
287 RemoveFile("RemoveCaches", pkgcache
);
288 if (srcpkgcache
.empty() == false && RealFileExists(srcpkgcache
) == true)
289 RemoveFile("RemoveCaches", srcpkgcache
);
290 if (pkgcache
.empty() == false)
292 std::string cachedir
= flNotFile(pkgcache
);
293 std::string cachefile
= flNotDir(pkgcache
);
294 if (cachedir
.empty() != true && cachefile
.empty() != true && DirectoryExists(cachedir
) == true)
296 cachefile
.append(".");
297 std::vector
<std::string
> caches
= GetListOfFilesInDir(cachedir
, false);
298 for (std::vector
<std::string
>::const_iterator file
= caches
.begin(); file
!= caches
.end(); ++file
)
300 std::string nuke
= flNotDir(*file
);
301 if (strncmp(cachefile
.c_str(), nuke
.c_str(), cachefile
.length()) != 0)
303 RemoveFile("RemoveCaches", *file
);
308 if (srcpkgcache
.empty() == true)
311 std::string cachedir
= flNotFile(srcpkgcache
);
312 std::string cachefile
= flNotDir(srcpkgcache
);
313 if (cachedir
.empty() == true || cachefile
.empty() == true || DirectoryExists(cachedir
) == false)
315 cachefile
.append(".");
316 std::vector
<std::string
> caches
= GetListOfFilesInDir(cachedir
, false);
317 for (std::vector
<std::string
>::const_iterator file
= caches
.begin(); file
!= caches
.end(); ++file
)
319 std::string nuke
= flNotDir(*file
);
320 if (strncmp(cachefile
.c_str(), nuke
.c_str(), cachefile
.length()) != 0)
322 RemoveFile("RemoveCaches", *file
);
326 // CacheFile::Close - close the cache files /*{{{*/
327 // ---------------------------------------------------------------------
329 void pkgCacheFile::Close()
331 if (ExternOwner
== false)
341 _system
->UnLock(true);