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>
37 // CacheFile::CacheFile - Constructor /*{{{*/
38 pkgCacheFile::pkgCacheFile() : d(NULL
), ExternOwner(false), Map(NULL
), Cache(NULL
),
39 DCache(NULL
), SrcList(NULL
), Policy(NULL
)
42 pkgCacheFile::pkgCacheFile(pkgDepCache
* const Owner
) : d(NULL
), ExternOwner(true),
43 Map(&Owner
->GetCache().GetMap()), Cache(&Owner
->GetCache()),
44 DCache(Owner
), SrcList(NULL
), Policy(NULL
)
48 // CacheFile::~CacheFile - Destructor /*{{{*/
49 // ---------------------------------------------------------------------
51 pkgCacheFile::~pkgCacheFile()
53 if (ExternOwner
== false)
61 if (ExternOwner
== false)
62 _system
->UnLock(true);
65 // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
66 class APT_HIDDEN ScopedErrorMerge
{
68 ScopedErrorMerge() { _error
->PushToStack(); }
69 ~ScopedErrorMerge() { _error
->MergeWithStack(); }
71 bool pkgCacheFile::BuildCaches(OpProgress
*Progress
, bool WithLock
)
77 if (_config
->FindB("pkgCacheFile::Generate", true) == false)
79 FileFd
file(_config
->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly
);
80 if (file
.IsOpen() == false || file
.Failed())
82 Map
= new MMap(file
, MMap::Public
|MMap::ReadOnly
);
83 Cache
= new pkgCache(Map
);
84 return _error
->PendingError() == false;
88 if (_system
->Lock() == false)
91 if (_error
->PendingError() == true)
94 BuildSourceList(Progress
);
97 bool Res
= pkgCacheGenerator::MakeStatusCache(*SrcList
,Progress
,&Map
, true);
101 return _error
->Error(_("The package lists or status file could not be parsed or opened."));
103 /* This sux, remove it someday */
104 if (_error
->PendingError() == true)
105 _error
->Warning(_("You may want to run apt-get update to correct these problems"));
107 Cache
= new pkgCache(Map
);
108 if (_error
->PendingError() == true)
113 // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
114 // ---------------------------------------------------------------------
116 bool pkgCacheFile::BuildSourceList(OpProgress
* /*Progress*/)
121 SrcList
= new pkgSourceList();
122 if (SrcList
->ReadMainList() == false)
123 return _error
->Error(_("The list of sources could not be read."));
127 // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
128 // ---------------------------------------------------------------------
130 bool pkgCacheFile::BuildPolicy(OpProgress
* /*Progress*/)
135 Policy
= new pkgPolicy(Cache
);
136 if (_error
->PendingError() == true)
139 if (ReadPinFile(*Policy
) == false || ReadPinDir(*Policy
) == false)
145 // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
146 // ---------------------------------------------------------------------
148 bool pkgCacheFile::BuildDepCache(OpProgress
*Progress
)
153 if (BuildPolicy(Progress
) == false)
156 DCache
= new pkgDepCache(Cache
,Policy
);
157 if (_error
->PendingError() == true)
160 return DCache
->Init(Progress
);
163 // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
164 // ---------------------------------------------------------------------
166 bool pkgCacheFile::Open(OpProgress
*Progress
, bool WithLock
)
168 if (BuildCaches(Progress
,WithLock
) == false)
171 if (BuildPolicy(Progress
) == false)
174 if (BuildDepCache(Progress
) == false)
177 if (Progress
!= NULL
)
179 if (_error
->PendingError() == true)
185 // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
186 // ---------------------------------------------------------------------
188 void pkgCacheFile::RemoveCaches()
190 std::string
const pkgcache
= _config
->FindFile("Dir::cache::pkgcache");
191 std::string
const srcpkgcache
= _config
->FindFile("Dir::cache::srcpkgcache");
193 if (pkgcache
.empty() == false && RealFileExists(pkgcache
) == true)
194 unlink(pkgcache
.c_str());
195 if (srcpkgcache
.empty() == false && RealFileExists(srcpkgcache
) == true)
196 unlink(srcpkgcache
.c_str());
197 if (pkgcache
.empty() == false)
199 std::string cachedir
= flNotFile(pkgcache
);
200 std::string cachefile
= flNotDir(pkgcache
);
201 if (cachedir
.empty() != true && cachefile
.empty() != true && DirectoryExists(cachedir
) == true)
203 cachefile
.append(".");
204 std::vector
<std::string
> caches
= GetListOfFilesInDir(cachedir
, false);
205 for (std::vector
<std::string
>::const_iterator file
= caches
.begin(); file
!= caches
.end(); ++file
)
207 std::string nuke
= flNotDir(*file
);
208 if (strncmp(cachefile
.c_str(), nuke
.c_str(), cachefile
.length()) != 0)
210 unlink(file
->c_str());
215 if (srcpkgcache
.empty() == true)
218 std::string cachedir
= flNotFile(srcpkgcache
);
219 std::string cachefile
= flNotDir(srcpkgcache
);
220 if (cachedir
.empty() == true || cachefile
.empty() == true || DirectoryExists(cachedir
) == false)
222 cachefile
.append(".");
223 std::vector
<std::string
> caches
= GetListOfFilesInDir(cachedir
, false);
224 for (std::vector
<std::string
>::const_iterator file
= caches
.begin(); file
!= caches
.end(); ++file
)
226 std::string nuke
= flNotDir(*file
);
227 if (strncmp(cachefile
.c_str(), nuke
.c_str(), cachefile
.length()) != 0)
229 unlink(file
->c_str());
233 // CacheFile::Close - close the cache files /*{{{*/
234 // ---------------------------------------------------------------------
236 void pkgCacheFile::Close()
238 if (ExternOwner
== false)
248 _system
->UnLock(true);