Added read-only flag

This commit is contained in:
Josh Yelon 2007-07-06 03:19:19 +00:00
parent 441e1df07c
commit e4f0fa5db8
3 changed files with 68 additions and 5 deletions

View File

@ -109,6 +109,34 @@ get_cache_max_kbytes() const {
return _max_kbytes;
}
////////////////////////////////////////////////////////////////////
// Function: BamCache::set_read_only
// Access: Published
// Description: Can be used to put the cache in read-only mode,
// or take it out of read-only mode. Note that if you
// put it into read-write mode, and it discovers that
// it does not have write access, it will put itself
// right back into read-only mode.
////////////////////////////////////////////////////////////////////
INLINE void BamCache::
set_read_only(bool ro) {
_read_only = ro;
}
////////////////////////////////////////////////////////////////////
// Function: BamCache::get_read_only
// Access: Published
// Description: Returns true if the cache is in read-only mode.
// Normally, the cache starts in read-write mode. It
// can put itself into read-only mode automatically if
// it discovers that it does not have write access to
// the cache.
////////////////////////////////////////////////////////////////////
INLINE bool BamCache::
get_read_only() const {
return _read_only;
}
////////////////////////////////////////////////////////////////////
// Function: BamCache::get_global_ptr
// Access: Published, Static

View File

@ -39,7 +39,8 @@ BamCache::
BamCache() :
_active(true),
_index(new BamCacheIndex),
_index_stale_since(0)
_index_stale_since(0),
_read_only(false)
{
ConfigVariableFilename model_cache_dir
("model-cache-dir", Filename(),
@ -182,6 +183,10 @@ store(BamCacheRecord *record) {
nassertr(!record->_cache_pathname.empty(), false);
nassertr(record->has_data(), false);
if (_read_only) {
return false;
}
consider_flush_index();
#ifndef NDEBUG
@ -206,6 +211,7 @@ store(BamCacheRecord *record) {
if (!temp_pathname.open_write(temp_file)) {
util_cat.error()
<< "Could not open cache file: " << temp_pathname << "\n";
emergency_read_only();
return false;
}
@ -214,6 +220,7 @@ store(BamCacheRecord *record) {
util_cat.error()
<< "Could not write cache file: " << temp_pathname << "\n";
temp_pathname.unlink();
emergency_read_only();
return false;
}
@ -270,6 +277,22 @@ store(BamCacheRecord *record) {
return true;
}
////////////////////////////////////////////////////////////////////
// Function: BamCache::emergency_read_only
// Access: Private
// Description: Called when an attempt to write to the cache dir
// has failed, usually for lack of disk space or
// because of incorrect file permissions. Outputs
// an error and puts the BamCache into read-only
// mode.
////////////////////////////////////////////////////////////////////
void BamCache::
emergency_read_only() {
util_cat.error() <<
"Could not write to the Bam Cache. Disabling future attempts.\n";
_read_only = true;
}
////////////////////////////////////////////////////////////////////
// Function: BamCache::consider_flush_index
// Access: Published
@ -299,9 +322,14 @@ flush_index() {
}
while (true) {
if (_read_only) {
return;
}
Filename temp_pathname = Filename::temporary(_root, "index-", ".boo");
if (!do_write_index(temp_pathname, _index)) {
emergency_read_only();
return;
}
@ -689,6 +717,7 @@ bool BamCache::
do_write_index(Filename &index_pathname, const BamCacheIndex *index) {
index_pathname.set_binary();
ofstream index_file;
if (!index_pathname.open_write(index_file)) {
util_cat.error()
<< "Could not open index file: " << index_pathname << "\n";

View File

@ -61,6 +61,9 @@ PUBLISHED:
INLINE void set_cache_max_kbytes(int max_kbytes);
INLINE int get_cache_max_kbytes() const;
INLINE void set_read_only(bool ro);
INLINE bool get_read_only() const;
PT(BamCacheRecord) lookup(const Filename &source_filename,
const string &cache_extension);
bool store(BamCacheRecord *record);
@ -83,6 +86,8 @@ private:
void check_cache_size();
void emergency_read_only();
static BamCacheIndex *do_read_index(Filename &index_pathname);
static bool do_write_index(Filename &index_pathname, const BamCacheIndex *index);
@ -97,6 +102,7 @@ private:
static void make_global();
bool _active;
bool _read_only;
Filename _root;
int _flush_time;
int _max_kbytes;