From e4f0fa5db8622cc380df486094cffa1f26632904 Mon Sep 17 00:00:00 2001 From: Josh Yelon Date: Fri, 6 Jul 2007 03:19:19 +0000 Subject: [PATCH] Added read-only flag --- panda/src/putil/bamCache.I | 28 +++++++++++++++++++++++++++ panda/src/putil/bamCache.cxx | 37 ++++++++++++++++++++++++++++++++---- panda/src/putil/bamCache.h | 8 +++++++- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/panda/src/putil/bamCache.I b/panda/src/putil/bamCache.I index 164b74ec5c..6f186dcf34 100644 --- a/panda/src/putil/bamCache.I +++ b/panda/src/putil/bamCache.I @@ -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 diff --git a/panda/src/putil/bamCache.cxx b/panda/src/putil/bamCache.cxx index 2c798ecf8b..7f15017d64 100644 --- a/panda/src/putil/bamCache.cxx +++ b/panda/src/putil/bamCache.cxx @@ -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,9 +220,10 @@ store(BamCacheRecord *record) { util_cat.error() << "Could not write cache file: " << temp_pathname << "\n"; temp_pathname.unlink(); + emergency_read_only(); return false; } - + if (!dout.write_header(_bam_header)) { util_cat.error() << "Unable to write to " << temp_pathname << "\n"; @@ -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,12 +717,13 @@ 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"; return false; } - + DatagramOutputFile dout; if (!dout.open(index_file)) { util_cat.error() diff --git a/panda/src/putil/bamCache.h b/panda/src/putil/bamCache.h index 82e0d87229..fbee1fb152 100644 --- a/panda/src/putil/bamCache.h +++ b/panda/src/putil/bamCache.h @@ -61,13 +61,16 @@ 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); void consider_flush_index(); void flush_index(); - + INLINE static BamCache *get_global_ptr(); private: @@ -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;