ACE_Filecache_Object Class Reference

Abstraction over a real file. This is what the Virtual Filesystem contains. This class is not intended for general consumption. Please consult a physician before attempting to use this class. More...

#include <Filecache.h>

Collaboration diagram for ACE_Filecache_Object:

Collaboration graph
[legend]
List of all members.

Public Types

enum  Creation_States { ACE_READING = 1, ACE_WRITING = 2 }
enum  Error_Conditions {
  ACE_SUCCESS = 0, ACE_ACCESS_FAILED, ACE_OPEN_FAILED, ACE_COPY_FAILED,
  ACE_STAT_FAILED, ACE_MEMMAP_FAILED, ACE_WRITE_FAILED
}

Public Member Functions

 ACE_Filecache_Object (const ACE_TCHAR *filename, ACE_SYNCH_RW_MUTEX &lock, LPSECURITY_ATTRIBUTES sa=0, int mapit=1)
 Creates a file for reading.

 ACE_Filecache_Object (const ACE_TCHAR *filename, off_t size, ACE_SYNCH_RW_MUTEX &lock, LPSECURITY_ATTRIBUTES sa=0)
 Creates a file for writing.

 ~ACE_Filecache_Object (void)
 Only if reference count is zero should this be called.

int acquire (void)
 Increment the reference_count_.

int release (void)
 Decrement the reference_count_.

int error (void) const
int error (int error_value, const ACE_TCHAR *s=ACE_LIB_TEXT("ACE_Filecache_Object"))
const ACE_TCHARfilename (void) const
 filename_ accessor

ACE_HANDLE handle (void) const
 handle_ accessor.

void * address (void) const
 Base memory address for memory mapped file.

ACE_LOFF_T size (void) const
 size_ accessor.

int update (void) const
 True if file on disk is newer than cached file.


Protected Member Functions

 ACE_Filecache_Object (void)
 Prevent from being called.

void init (void)
 Common initialization code,.


Private Member Functions

int error_i (int error_value, const ACE_TCHAR *s=ACE_LIB_TEXT("ACE_Filecache_Object"))
 Internal error logging method, no locking.


Private Attributes

ACE_TCHARtempname_
ACE_TCHAR filename_ [MAXPATHLEN+1]
ACE_Mem_Map mmap_
 Holds the memory mapped version of the temporary file.

ACE_HANDLE handle_
 The descriptor to the temporary file.

ACE_stat stat_
 Used to compare against the real file to test if an update is needed.

ACE_LOFF_T size_
int action_
 Status indicators.

int error_
int stale_
 If set to 1, means the object is flagged for removal.

LPSECURITY_ATTRIBUTES sa_
 Security attribute object.

ACE_SYNCH_RW_MUTEX junklock_
 The default initializer.

ACE_SYNCH_RW_MUTEX & lock_
 Provides a bookkeeping mechanism for users of this object.


Friends

class ACE_Filecache

Detailed Description

Abstraction over a real file. This is what the Virtual Filesystem contains. This class is not intended for general consumption. Please consult a physician before attempting to use this class.

Definition at line 239 of file Filecache.h.


Member Enumeration Documentation

enum ACE_Filecache_Object::Creation_States
 

Enumeration values:
ACE_READING 
ACE_WRITING 

Definition at line 299 of file Filecache.h.

00300   {
00301     ACE_READING = 1,
00302     ACE_WRITING = 2
00303   };

enum ACE_Filecache_Object::Error_Conditions
 

Enumeration values:
ACE_SUCCESS 
ACE_ACCESS_FAILED 
ACE_OPEN_FAILED 
ACE_COPY_FAILED 
ACE_STAT_FAILED 
ACE_MEMMAP_FAILED 
ACE_WRITE_FAILED 

Definition at line 305 of file Filecache.h.

00306   {
00307     ACE_SUCCESS = 0,
00308     ACE_ACCESS_FAILED,
00309     ACE_OPEN_FAILED,
00310     ACE_COPY_FAILED,
00311     ACE_STAT_FAILED,
00312     ACE_MEMMAP_FAILED,
00313     ACE_WRITE_FAILED
00314   };


Constructor & Destructor Documentation

ACE_Filecache_Object::ACE_Filecache_Object const ACE_TCHAR filename,
ACE_SYNCH_RW_MUTEX &  lock,
LPSECURITY_ATTRIBUTES  sa = 0,
int  mapit = 1
 

Creates a file for reading.

Definition at line 489 of file Filecache.cpp.

References ACE_OS::access(), ACE_ACCESS_FAILED, ACE_LIB_TEXT, ACE_MAP_PRIVATE, ACE_MEMMAP_FAILED, ACE_OPEN_FAILED, ACE_READING, ACE_STAT_FAILED, ACE_TCHAR, action_, ACE_OS::close(), error_i(), init(), ACE_Mem_Map::map(), mmap_, ACE_OS::open(), PROT_READ, R_MASK, R_OK, READ_FLAGS, ACE_OS::stat(), stat_, ACE_OS::strcpy(), and tempname_.

00493   : tempname_ (0),
00494     mmap_ (),
00495     handle_ (0),
00496     // stat_ (),
00497     size_ (0),
00498     action_ (0),
00499     error_ (0),
00500     stale_ (0),
00501     sa_ (sa),
00502     junklock_ (),
00503     lock_ (lock)
00504 {
00505   this->init ();
00506 
00507   // ASSERT strlen(filename) < sizeof (this->filename_)
00508   ACE_OS::strcpy (this->filename_, filename);
00509   this->action_ = ACE_Filecache_Object::ACE_READING;
00510   // place ourselves into the READING state
00511 
00512   // Can we access the file?
00513   if (ACE_OS::access (this->filename_, R_OK) == -1)
00514     {
00515       this->error_i (ACE_Filecache_Object::ACE_ACCESS_FAILED);
00516       return;
00517     }
00518 
00519   // Can we stat the file?
00520   if (ACE_OS::stat (this->filename_, &this->stat_) == -1)
00521     {
00522       this->error_i (ACE_Filecache_Object::ACE_STAT_FAILED);
00523       return;
00524     }
00525 
00526   this->size_ = this->stat_.st_size;
00527   this->tempname_ = this->filename_;
00528 
00529   // Can we open the file?
00530   this->handle_ = ACE_OS::open (this->tempname_,
00531                                 READ_FLAGS, R_MASK, this->sa_);
00532   if (this->handle_ == ACE_INVALID_HANDLE)
00533     {
00534       this->error_i (ACE_Filecache_Object::ACE_OPEN_FAILED,
00535                      ACE_LIB_TEXT ("ACE_Filecache_Object::ctor: open"));
00536       return;
00537     }
00538 
00539   if (mapit)
00540     {
00541       // Can we map the file?
00542       if (this->mmap_.map (this->handle_, -1,
00543                            PROT_READ, ACE_MAP_PRIVATE, 0, 0, this->sa_) != 0)
00544         {
00545           this->error_i (ACE_Filecache_Object::ACE_MEMMAP_FAILED,
00546                          ACE_LIB_TEXT ("ACE_Filecache_Object::ctor: map"));
00547           ACE_OS::close (this->handle_);
00548           this->handle_ = ACE_INVALID_HANDLE;
00549           return;
00550         }
00551     }
00552 
00553    // Ok, finished!
00554    this->action_ = ACE_Filecache_Object::ACE_READING;
00555 }

ACE_Filecache_Object::ACE_Filecache_Object const ACE_TCHAR filename,
off_t  size,
ACE_SYNCH_RW_MUTEX &  lock,
LPSECURITY_ATTRIBUTES  sa = 0
 

Creates a file for writing.

Definition at line 557 of file Filecache.cpp.

References ACE_OS::access(), ACE_ACCESS_FAILED, ACE_LIB_TEXT, ACE_MEMMAP_FAILED, ACE_OPEN_FAILED, ACE_TCHAR, ACE_WRITE_FAILED, ACE_WRITING, action_, ACE_OS::close(), error_i(), F_OK, init(), ACE_Mem_Map::map(), MAP_SHARED, mmap_, ACE_OS::open(), PROT_RDWR, ACE_OS::pwrite(), R_OK, ACE_OS::strcpy(), tempname_, W_MASK, W_OK, and WRITE_FLAGS.

00561   : stale_ (0),
00562     sa_ (sa),
00563     lock_ (lock)
00564 {
00565   this->init ();
00566 
00567   this->size_ = size;
00568   ACE_OS::strcpy (this->filename_, filename);
00569   this->action_ = ACE_Filecache_Object::ACE_WRITING;
00570 
00571   // Can we access the file?
00572   if (ACE_OS::access (this->filename_, R_OK|W_OK) == -1
00573       // Does it exist?
00574       && ACE_OS::access (this->filename_, F_OK) != -1)
00575     {
00576       // File exists, but we cannot access it.
00577       this->error_i (ACE_Filecache_Object::ACE_ACCESS_FAILED);
00578       return;
00579     }
00580 
00581   this->tempname_ = this->filename_;
00582 
00583   // Can we open the file?
00584   this->handle_ = ACE_OS::open (this->tempname_, WRITE_FLAGS, W_MASK, this->sa_);
00585   if (this->handle_ == ACE_INVALID_HANDLE)
00586     {
00587       this->error_i (ACE_Filecache_Object::ACE_OPEN_FAILED,
00588                      ACE_LIB_TEXT ("ACE_Filecache_Object::acquire: open"));
00589       return;
00590     }
00591 
00592   // Can we write?
00593   if (ACE_OS::pwrite (this->handle_, "", 1, this->size_ - 1) != 1)
00594     {
00595       this->error_i (ACE_Filecache_Object::ACE_WRITE_FAILED,
00596                      ACE_LIB_TEXT ("ACE_Filecache_Object::acquire: write"));
00597       ACE_OS::close (this->handle_);
00598       return;
00599     }
00600 
00601   // Can we map?
00602   if (this->mmap_.map (this->handle_, this->size_, PROT_RDWR, MAP_SHARED,
00603                        0, 0, this->sa_) != 0)
00604     {
00605       this->error_i (ACE_Filecache_Object::ACE_MEMMAP_FAILED,
00606                      ACE_LIB_TEXT ("ACE_Filecache_Object::acquire: map"));
00607       ACE_OS::close (this->handle_);
00608     }
00609 
00610   // Ok, done!
00611 }

ACE_Filecache_Object::~ACE_Filecache_Object void   ) 
 

Only if reference count is zero should this be called.

Definition at line 613 of file Filecache.cpp.

References ACE_OS::close(), mmap_, and ACE_Mem_Map::unmap().

00614 {
00615   if (this->error_ == ACE_SUCCESS)
00616     {
00617       this->mmap_.unmap ();
00618       ACE_OS::close (this->handle_);
00619       this->handle_ = ACE_INVALID_HANDLE;
00620     }
00621 }

ACE_Filecache_Object::ACE_Filecache_Object void   )  [protected]
 

Prevent from being called.

Definition at line 473 of file Filecache.cpp.

References init().

00474   : tempname_ (0),
00475     mmap_ (),
00476     handle_ (0),
00477     // stat_ (),
00478     size_ (0),
00479     action_ (0),
00480     error_ (0),
00481     stale_ (0),
00482     // sa_ (),
00483     junklock_ (),
00484     lock_ (junklock_)
00485 {
00486   this->init ();
00487 }


Member Function Documentation

int ACE_Filecache_Object::acquire void   ) 
 

Increment the reference_count_.

Definition at line 624 of file Filecache.cpp.

Referenced by ACE_Filecache::create(), and ACE_Filecache::finish().

00625 {
00626   return this->lock_.tryacquire_read ();
00627 }

void * ACE_Filecache_Object::address void   )  const
 

Base memory address for memory mapped file.

Definition at line 722 of file Filecache.cpp.

References ACE_Mem_Map::addr(), and mmap_.

Referenced by ACE_Filecache_Handle::address().

00723 {
00724   // The existence of the object means a read lock is being held.
00725   return this->mmap_.addr ();
00726 }

int ACE_Filecache_Object::error int  error_value,
const ACE_TCHAR s = ACE_LIB_TEXT("ACE_Filecache_Object")
 

int ACE_Filecache_Object::error void   )  const
 

Definition at line 685 of file Filecache.cpp.

Referenced by ACE_Filecache_Handle::error().

00686 {
00687   // The existence of the object means a read lock is being held.
00688   return this->error_;
00689 }

int ACE_Filecache_Object::error_i int  error_value,
const ACE_TCHAR s = ACE_LIB_TEXT("ACE_Filecache_Object")
[private]
 

Internal error logging method, no locking.

Definition at line 692 of file Filecache.cpp.

References ACE_ERROR, ACE_LIB_TEXT, ACE_TCHAR, and LM_ERROR.

Referenced by ACE_Filecache_Object(), and release().

00693 {
00694   s = s;
00695   ACE_ERROR ((LM_ERROR, ACE_LIB_TEXT ("%p.\n"), s));
00696   this->error_ = error_value;
00697   return error_value;
00698 }

const ACE_TCHAR * ACE_Filecache_Object::filename void   )  const
 

filename_ accessor

Definition at line 701 of file Filecache.cpp.

Referenced by ACE_Filecache::finish().

00702 {
00703   // The existence of the object means a read lock is being held.
00704   return this->filename_;
00705 }

ACE_HANDLE ACE_Filecache_Object::handle void   )  const
 

handle_ accessor.

Definition at line 715 of file Filecache.cpp.

00716 {
00717   // The existence of the object means a read lock is being held.
00718   return this->handle_;
00719 }

void ACE_Filecache_Object::init void   )  [protected]
 

Common initialization code,.

Definition at line 462 of file Filecache.cpp.

References ACE_OS::memset(), and tempname_.

Referenced by ACE_Filecache_Object().

00463 {
00464   this->filename_[0] = '\0';
00465   this->handle_ = ACE_INVALID_HANDLE;
00466   this->error_ = ACE_SUCCESS;
00467   this->tempname_ = 0;
00468   this->size_ = 0;
00469 
00470   ACE_OS::memset (&(this->stat_), 0, sizeof (this->stat_));
00471 }

int ACE_Filecache_Object::release void   ) 
 

Decrement the reference_count_.

Definition at line 630 of file Filecache.cpp.

References ACE_MAP_PRIVATE, ACE_MEMMAP_FAILED, ACE_OPEN_FAILED, ACE_READING, ACE_STAT_FAILED, ACE_WRITE_FAILED, ACE_WRITING, action_, ACE_OS::close(), error_i(), ACE_Mem_Map::map(), mmap_, ACE_OS::open(), PROT_READ, R_MASK, READ_FLAGS, ACE_OS::stat(), ACE_OS::unlink(), ACE_Mem_Map::unmap(), W_MASK, ACE_OS::write(), and WRITE_FLAGS.

Referenced by ACE_Filecache::finish().

00631 {
00632   if (this->action_ == ACE_WRITING)
00633     {
00634       // We are safe since only one thread has a writable Filecache_Object
00635 
00636 #if 0
00637       ACE_HANDLE original = ACE_OS::open (this->filename_, WRITE_FLAGS, W_MASK,
00638                                           this->sa_);
00639       if (original == ACE_INVALID_HANDLE)
00640         this->error_ = ACE_Filecache_Object::ACE_OPEN_FAILED;
00641       else if (ACE_OS::write (original, this->mmap_.addr (),
00642                               this->size_) == -1)
00643         {
00644           this->error_ = ACE_Filecache_Object::ACE_WRITE_FAILED;
00645           ACE_OS::close (original);
00646           ACE_OS::unlink (this->filename_);
00647         }
00648       else if (ACE_OS::stat (this->filename_, &this->stat_) == -1)
00649         this->error_ = ACE_Filecache_Object::ACE_STAT_FAILED;
00650 #endif
00651 
00652       this->mmap_.unmap ();
00653       ACE_OS::close (this->handle_);
00654       this->handle_ = ACE_INVALID_HANDLE;
00655 
00656 #if 0
00657       // Leave the file in an acquirable state.
00658       this->handle_ = ACE_OS::open (this->tempname_, READ_FLAGS, R_MASK);
00659       if (this->handle_ == ACE_INVALID_HANDLE)
00660         {
00661           this->error_i (ACE_Filecache_Object::ACE_OPEN_FAILED,
00662                          "ACE_Filecache_Object::acquire: open");
00663         }
00664       else if (this->mmap_.map (this->handle_, -1,
00665                                 PROT_READ,
00666                                 ACE_MAP_PRIVATE,
00667                                 0,
00668                                 0,
00669                                 this->sa_) != 0)
00670         {
00671           this->error_i (ACE_Filecache_Object::ACE_MEMMAP_FAILED,
00672                          "ACE_Filecache_Object::acquire: map");
00673           ACE_OS::close (this->handle_);
00674           this->handle_ = ACE_INVALID_HANDLE;
00675         }
00676 
00677       this->action_ = ACE_Filecache_Object::ACE_READING;
00678 #endif
00679     }
00680 
00681   return this->lock_.release ();
00682 }

ACE_LOFF_T ACE_Filecache_Object::size void   )  const
 

size_ accessor.

Definition at line 708 of file Filecache.cpp.

Referenced by ACE_Filecache_Handle::size().

00709 {
00710   // The existence of the object means a read lock is being held.
00711   return this->size_;
00712 }

int ACE_Filecache_Object::update void   )  const
 

True if file on disk is newer than cached file.

Definition at line 729 of file Filecache.cpp.

References ACE_stat, ACE_OS::difftime(), and ACE_OS::stat().

Referenced by ACE_Filecache::fetch().

00730 {
00731   // The existence of the object means a read lock is being held.
00732   int result;
00733   ACE_stat statbuf;
00734 
00735   if (ACE_OS::stat (this->filename_, &statbuf) == -1)
00736     result = 1;
00737   else
00738     // non-portable code may follow
00739 #if defined (ACE_HAS_WINCE)
00740     // Yup, non-portable... there's probably a way to safely implement
00741     // difftime() on WinCE, but for now, this will have to do. It flags
00742     // every file as having changed since cached.
00743     result = 1;
00744 #else
00745     result = ACE_OS::difftime (this->stat_.st_mtime, statbuf.st_mtime) < 0;
00746 #endif /* ACE_HAS_WINCE */
00747 
00748   return result;
00749 }


Friends And Related Function Documentation

friend class ACE_Filecache [friend]
 

Definition at line 242 of file Filecache.h.


Member Data Documentation

int ACE_Filecache_Object::action_ [private]
 

Status indicators.

Definition at line 333 of file Filecache.h.

Referenced by ACE_Filecache_Object(), ACE_Filecache::finish(), and release().

int ACE_Filecache_Object::error_ [private]
 

Definition at line 334 of file Filecache.h.

ACE_TCHAR ACE_Filecache_Object::filename_[MAXPATHLEN + 1] [private]
 

Definition at line 320 of file Filecache.h.

Referenced by ACE_Filecache::finish().

ACE_HANDLE ACE_Filecache_Object::handle_ [private]
 

The descriptor to the temporary file.

Definition at line 326 of file Filecache.h.

ACE_SYNCH_RW_MUTEX ACE_Filecache_Object::junklock_ [private]
 

The default initializer.

Definition at line 343 of file Filecache.h.

ACE_SYNCH_RW_MUTEX& ACE_Filecache_Object::lock_ [private]
 

Provides a bookkeeping mechanism for users of this object.

Definition at line 346 of file Filecache.h.

Referenced by ACE_Filecache::finish(), and ACE_Filecache::remove_i().

ACE_Mem_Map ACE_Filecache_Object::mmap_ [private]
 

Holds the memory mapped version of the temporary file.

Definition at line 323 of file Filecache.h.

Referenced by ACE_Filecache_Object(), address(), release(), and ~ACE_Filecache_Object().

LPSECURITY_ATTRIBUTES ACE_Filecache_Object::sa_ [private]
 

Security attribute object.

Definition at line 340 of file Filecache.h.

ACE_LOFF_T ACE_Filecache_Object::size_ [private]
 

Definition at line 330 of file Filecache.h.

int ACE_Filecache_Object::stale_ [private]
 

If set to 1, means the object is flagged for removal.

Definition at line 337 of file Filecache.h.

Referenced by ACE_Filecache::finish(), and ACE_Filecache::remove_i().

ACE_stat ACE_Filecache_Object::stat_ [private]
 

Used to compare against the real file to test if an update is needed.

Definition at line 329 of file Filecache.h.

Referenced by ACE_Filecache_Object().

ACE_TCHAR* ACE_Filecache_Object::tempname_ [private]
 

The temporary file name and the real file name. The real file is copied into the temporary file for safety reasons.

Definition at line 319 of file Filecache.h.

Referenced by ACE_Filecache_Object(), and init().


The documentation for this class was generated from the following files:
Generated on Thu Nov 9 11:22:19 2006 for ACE by doxygen 1.3.6