30 #include <sys/types.h>
38 m_base_dir(base_dir) ,
39 m_prefix(name.empty()?std::string():(name+
".")) ,
40 m_cache_dir(base_dir+
".cache") ,
45 G_ASSERT( name.find(
'/') == std::string::npos ) ;
48 G_LOG(
"Gv::Cache::ctor: cache dir=[" << m_cache_dir <<
"] size=" << m_list.size() ) ;
56 for( List::iterator p = m_list.begin() ; p != m_list.end() ; ++p , i++ )
63 for( List::iterator p = m_list.begin() ; p != m_list.end() ; ++p )
67 ::unlink( (*p).cache_path.c_str() ) ;
73 return m_base_dir.str() ;
76 void Gv::Cache::fail(
const char * where )
78 G_DEBUG(
"Gv::Cache::fail: failed: " << where ) ;
79 G_WARNING_ONCE(
"Gv::Cache::fail: one or more cache failures" ) ;
84 const std::string & commit_path_other ,
const std::string & same_as_path )
86 if( m_list.empty() )
return ;
87 if( ++m_p == m_list.end() )
88 m_p = m_list.begin() ;
91 e.commit_path = commit_path ;
92 e.commit_path_other = commit_path_other ;
93 e.same_as_path = same_as_path ;
95 write( e , image_buffer ) ;
98 void Gv::Cache::open( Entry & e )
104 e.fd = ::open( e.cache_path.c_str() , O_CREAT | O_TRUNC | O_WRONLY ,
105 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH ) ;
112 void Gv::Cache::write( Entry & e ,
const Gr::ImageBuffer & image_buffer )
114 if( ::lseek( e.fd , 0L , SEEK_SET ) < 0 )
118 for( row_iterator part_p = Gr::imagebuffer::row_begin(image_buffer) ; part_p != Gr::imagebuffer::row_end(image_buffer) ; ++part_p )
120 const char * p = Gr::imagebuffer::row_ptr( part_p ) ;
121 size_t n = Gr::imagebuffer::row_size( part_p ) ;
122 if( n != 0U && ::write( e.fd , p , n ) != static_cast<ssize_t>(n) )
133 for( List::iterator p = m_list.begin() ; p != m_list.end() ; ++p )
137 commit( *p , other ) ;
141 G_DEBUG(
"Gv::Cache::commit: commited " << n <<
"/" << m_list.size() ) ;
147 const std::string & commit_path = other && !e.commit_path_other.empty() ? e.commit_path_other : e.commit_path ;
148 if( !e.same_as_path.empty() && move(e.same_as_path,commit_path) )
155 move( e.cache_path , commit_path ) ;
159 void Gv::Cache::close( Entry & e )
163 if( ::close( fd ) < 0 )
167 bool Gv::Cache::move(
const std::string & src ,
const std::string & dst )
172 rc = ::rename( src.c_str() , dst.c_str() ) ;
181 rc = ::rename( src.c_str() , dst.c_str() ) ;
187 G_DEBUG(
"Gv::Cache::move: [" << src <<
"] -> [" << dst <<
"]" ) ;
191 void Gv::Cache::mkdirs(
const G::Path & path )
A traits class that can be specialised for Gr::ImageBuffer candidates.
A class which acquires the process's special privileges on construction and releases them on destruct...
Vectors ImageBuffer
An ImageBuffer is used to hold raw image data, typically in more than one chunk.
static bool mkdirs(const Path &dir, const NoThrow &, int=100)
Creates a directory and all necessary parents.
static std::string fromUInt(unsigned int ui)
Converts unsigned int 'ui' to a string.
static unsigned int replaceAll(std::string &s, const std::string &from, const std::string &to)
Does a global replace on string 's', replacing all occurances of sub-string 'from' with 'to'...
Cache(const G::Path &base_dir, const std::string &name, size_t size)
Constructor. Paths are like "<base-dir>/.cache/<name>.123".
void store(const Gr::ImageBuffer &, const std::string &commit_path, const std::string &commit_path_other=std::string(), const std::string &same_as=std::string())
Stores an image in the cache.
Path dirname() const
Returns the path without the rightmost part, ignoring "." parts.
static bool remove(const Path &path, const NoThrow &)
Deletes the file or directory. Returns false on error.
void commit(bool other=false)
Commits all cached images to their non-cache location.
An overload discriminator class for File methods.
std::string base() const
Returns the base directory, as passed to the constructor.
A Path object represents a file system path.