]>
git.saurik.com Git - apt.git/blob - apt-pkg/cachefile.cc
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 // ---------------------------------------------------------------------
40 pkgCacheFile::pkgCacheFile() : d(NULL
), Map(NULL
), Cache(NULL
), DCache(NULL
),
41 SrcList(NULL
), Policy(NULL
)
45 // CacheFile::~CacheFile - Destructor /*{{{*/
46 // ---------------------------------------------------------------------
48 pkgCacheFile::~pkgCacheFile()
55 _system
->UnLock(true);
58 // CacheFile::BuildCaches - Open and build the cache files /*{{{*/
59 // ---------------------------------------------------------------------
61 bool pkgCacheFile::BuildCaches(OpProgress
*Progress
, bool WithLock
)
66 if (_config
->FindB("pkgCacheFile::Generate", true) == false)
68 FileFd
file(_config
->FindFile("Dir::Cache::pkgcache"), FileFd::ReadOnly
);
69 Map
= new MMap(file
, MMap::Public
|MMap::ReadOnly
);
70 Cache
= new pkgCache(Map
);
71 if (_error
->PendingError() == true)
76 const bool ErrorWasEmpty
= _error
->empty();
78 if (_system
->Lock() == false)
81 if (_config
->FindB("Debug::NoLocking",false) == true)
84 if (_error
->PendingError() == true)
87 BuildSourceList(Progress
);
90 bool Res
= pkgCacheGenerator::MakeStatusCache(*SrcList
,Progress
,&Map
,!WithLock
);
94 return _error
->Error(_("The package lists or status file could not be parsed or opened."));
96 /* This sux, remove it someday */
97 if (ErrorWasEmpty
== true && _error
->empty() == false)
98 _error
->Warning(_("You may want to run apt-get update to correct these problems"));
100 Cache
= new pkgCache(Map
);
101 if (_error
->PendingError() == true)
106 // CacheFile::BuildSourceList - Open and build all relevant sources.list/*{{{*/
107 // ---------------------------------------------------------------------
109 bool pkgCacheFile::BuildSourceList(OpProgress
* /*Progress*/)
114 SrcList
= new pkgSourceList();
115 if (SrcList
->ReadMainList() == false)
116 return _error
->Error(_("The list of sources could not be read."));
120 // CacheFile::BuildPolicy - Open and build all relevant preferences /*{{{*/
121 // ---------------------------------------------------------------------
123 bool pkgCacheFile::BuildPolicy(OpProgress
* /*Progress*/)
128 Policy
= new pkgPolicy(Cache
);
129 if (_error
->PendingError() == true)
132 if (ReadPinFile(*Policy
) == false || ReadPinDir(*Policy
) == false)
138 // CacheFile::BuildDepCache - Open and build the dependency cache /*{{{*/
139 // ---------------------------------------------------------------------
141 bool pkgCacheFile::BuildDepCache(OpProgress
*Progress
)
146 if (BuildPolicy(Progress
) == false)
149 DCache
= new pkgDepCache(Cache
,Policy
);
150 if (_error
->PendingError() == true)
153 DCache
->Init(Progress
);
157 // CacheFile::Open - Open the cache files, creating if necessary /*{{{*/
158 // ---------------------------------------------------------------------
160 bool pkgCacheFile::Open(OpProgress
*Progress
, bool WithLock
)
162 if (BuildCaches(Progress
,WithLock
) == false)
165 if (BuildPolicy(Progress
) == false)
168 if (BuildDepCache(Progress
) == false)
171 if (Progress
!= NULL
)
173 if (_error
->PendingError() == true)
179 // CacheFile::RemoveCaches - remove all cache files from disk /*{{{*/
180 // ---------------------------------------------------------------------
182 void pkgCacheFile::RemoveCaches()
184 std::string
const pkgcache
= _config
->FindFile("Dir::cache::pkgcache");
185 std::string
const srcpkgcache
= _config
->FindFile("Dir::cache::srcpkgcache");
187 if (pkgcache
.empty() == false && RealFileExists(pkgcache
) == true)
188 unlink(pkgcache
.c_str());
189 if (srcpkgcache
.empty() == false && RealFileExists(srcpkgcache
) == true)
190 unlink(srcpkgcache
.c_str());
191 if (pkgcache
.empty() == false)
193 std::string cachedir
= flNotFile(pkgcache
);
194 std::string cachefile
= flNotDir(pkgcache
);
195 if (cachedir
.empty() != true && cachefile
.empty() != true && DirectoryExists(cachedir
) == true)
197 cachefile
.append(".");
198 std::vector
<std::string
> caches
= GetListOfFilesInDir(cachedir
, false);
199 for (std::vector
<std::string
>::const_iterator file
= caches
.begin(); file
!= caches
.end(); ++file
)
201 std::string nuke
= flNotDir(*file
);
202 if (strncmp(cachefile
.c_str(), nuke
.c_str(), cachefile
.length()) != 0)
204 unlink(file
->c_str());
209 if (srcpkgcache
.empty() == true)
212 std::string cachedir
= flNotFile(srcpkgcache
);
213 std::string cachefile
= flNotDir(srcpkgcache
);
214 if (cachedir
.empty() == true || cachefile
.empty() == true || DirectoryExists(cachedir
) == false)
216 cachefile
.append(".");
217 std::vector
<std::string
> caches
= GetListOfFilesInDir(cachedir
, false);
218 for (std::vector
<std::string
>::const_iterator file
= caches
.begin(); file
!= caches
.end(); ++file
)
220 std::string nuke
= flNotDir(*file
);
221 if (strncmp(cachefile
.c_str(), nuke
.c_str(), cachefile
.length()) != 0)
223 unlink(file
->c_str());
227 // CacheFile::Close - close the cache files /*{{{*/
228 // ---------------------------------------------------------------------
230 void pkgCacheFile::Close()
237 _system
->UnLock(true);