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

 ACE_READING = 1
 ACE_WRITING = 2
 ACE_SUCCESS = 0
 ACE_ACCESS_FAILED
 ACE_OPEN_FAILED
 ACE_COPY_FAILED
 ACE_STAT_FAILED
 ACE_MEMMAP_FAILED
 ACE_WRITE_FAILED
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, ACE_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_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_OFF_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_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_OFF_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

Enumerator:
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

Enumerator:
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 490 of file Filecache.cpp.

References ACE_OS::access(), ACE_ACCESS_FAILED, ACE_MAP_PRIVATE, ACE_MEMMAP_FAILED, ACE_OPEN_FAILED, ACE_READING, ACE_STAT_FAILED, ACE_TEXT, action_, ACE_OS::close(), error_i(), filename_, handle_, init(), ACE_OS::open(), R_MASK, R_OK, READ_FLAGS, size_, ACE_OS::stat(), stat_, ACE_OS::strcpy(), and tempname_.

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

ACE_Filecache_Object::ACE_Filecache_Object ( const ACE_TCHAR filename,
ACE_OFF_T  size,
ACE_SYNCH_RW_MUTEX &  lock,
LPSECURITY_ATTRIBUTES  sa = 0 
)

Creates a file for writing.

Definition at line 558 of file Filecache.cpp.

References ACE_OS::access(), ACE_ACCESS_FAILED, ACE_MEMMAP_FAILED, ACE_OPEN_FAILED, ACE_TEXT, ACE_WRITE_FAILED, ACE_WRITING, action_, ACE_OS::close(), error_i(), F_OK, filename_, handle_, init(), ACE_OS::open(), PROT_RDWR, ACE_OS::pwrite(), R_OK, size_, ACE_OS::strcpy(), tempname_, W_MASK, W_OK, and WRITE_FLAGS.

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

ACE_Filecache_Object::~ACE_Filecache_Object ( void   ) 

Only if reference count is zero should this be called.

Definition at line 614 of file Filecache.cpp.

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

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

ACE_Filecache_Object::ACE_Filecache_Object ( void   )  [protected]

Prevent from being called.

Definition at line 474 of file Filecache.cpp.

References init().

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


Member Function Documentation

int ACE_Filecache_Object::acquire ( void   ) 

Increment the reference_count_.

Definition at line 627 of file Filecache.cpp.

References lock_.

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

00628 {
00629   return this->lock_.tryacquire_read ();
00630 }

void * ACE_Filecache_Object::address ( void   )  const

Base memory address for memory mapped file.

Definition at line 725 of file Filecache.cpp.

References ACE_Mem_Map::addr(), and mmap_.

Referenced by ACE_Filecache_Handle::address().

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

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

int ACE_Filecache_Object::error ( void   )  const

Definition at line 688 of file Filecache.cpp.

References error_.

Referenced by ACE_Filecache_Handle::error().

00689 {
00690   // The existence of the object means a read lock is being held.
00691   return this->error_;
00692 }

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

Internal error logging method, no locking.

Definition at line 695 of file Filecache.cpp.

References ACE_ERROR, ACE_TEXT, error_, and LM_ERROR.

Referenced by ACE_Filecache_Object(), and release().

00696 {
00697   s = s;
00698   ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p.\n"), s));
00699   this->error_ = error_value;
00700   return error_value;
00701 }

const ACE_TCHAR * ACE_Filecache_Object::filename ( void   )  const

filename_ accessor

Definition at line 704 of file Filecache.cpp.

References filename_.

Referenced by ACE_Filecache::finish().

00705 {
00706   // The existence of the object means a read lock is being held.
00707   return this->filename_;
00708 }

ACE_HANDLE ACE_Filecache_Object::handle ( void   )  const

handle_ accessor.

Definition at line 718 of file Filecache.cpp.

References handle_.

00719 {
00720   // The existence of the object means a read lock is being held.
00721   return this->handle_;
00722 }

void ACE_Filecache_Object::init ( void   )  [protected]

Common initialization code,.

Definition at line 463 of file Filecache.cpp.

References ACE_SUCCESS, error_, filename_, handle_, ACE_OS::memset(), size_, and tempname_.

Referenced by ACE_Filecache_Object().

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

int ACE_Filecache_Object::release ( void   ) 

Decrement the reference_count_.

Definition at line 633 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_, error_i(), handle_, lock_, mmap_, ACE_OS::open(), R_MASK, READ_FLAGS, size_, ACE_OS::stat(), ACE_OS::unlink(), ACE_Mem_Map::unmap(), W_MASK, ACE_OS::write(), and WRITE_FLAGS.

Referenced by ACE_Filecache::finish().

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

ACE_OFF_T ACE_Filecache_Object::size ( void   )  const

size_ accessor.

Definition at line 711 of file Filecache.cpp.

References size_.

Referenced by ACE_Filecache_Handle::size().

00712 {
00713   // The existence of the object means a read lock is being held.
00714   return this->size_;
00715 }

int ACE_Filecache_Object::update ( void   )  const

True if file on disk is newer than cached file.

Definition at line 732 of file Filecache.cpp.

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

Referenced by ACE_Filecache::fetch().

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


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.

Referenced by error(), error_i(), init(), and release().

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

Definition at line 320 of file Filecache.h.

Referenced by ACE_Filecache_Object(), filename(), ACE_Filecache::finish(), and init().

ACE_HANDLE ACE_Filecache_Object::handle_ [private]

The descriptor to the temporary file.

Definition at line 326 of file Filecache.h.

Referenced by ACE_Filecache_Object(), handle(), init(), release(), and ~ACE_Filecache_Object().

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 acquire(), ACE_Filecache::finish(), release(), ACE_Filecache::remove_i(), and ~ACE_Filecache_Object().

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 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_OFF_T ACE_Filecache_Object::size_ [private]

Definition at line 330 of file Filecache.h.

Referenced by ACE_Filecache_Object(), init(), release(), and size().

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 Tue Feb 2 17:35:08 2010 for ACE by  doxygen 1.4.7