Public Types | Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Friends

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, 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 242 of file Filecache.h.


Member Enumeration Documentation

Enumerator:
ACE_READING 
ACE_WRITING 

Definition at line 302 of file Filecache.h.

  {
    ACE_READING = 1,
    ACE_WRITING = 2
  };

Enumerator:
ACE_SUCCESS 
ACE_ACCESS_FAILED 
ACE_OPEN_FAILED 
ACE_COPY_FAILED 
ACE_STAT_FAILED 
ACE_MEMMAP_FAILED 
ACE_WRITE_FAILED 

Definition at line 308 of file Filecache.h.

  {
    ACE_SUCCESS = 0,
    ACE_ACCESS_FAILED,
    ACE_OPEN_FAILED,
    ACE_COPY_FAILED,
    ACE_STAT_FAILED,
    ACE_MEMMAP_FAILED,
    ACE_WRITE_FAILED
  };


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.

  : tempname_ (0),
    mmap_ (),
    handle_ (0),
    // stat_ (),
    size_ (0),
    action_ (0),
    error_ (0),
    stale_ (0),
    sa_ (sa),
    junklock_ (),
    lock_ (lock)
{
  this->init ();

  // ASSERT strlen(filename) < sizeof (this->filename_)
  ACE_OS::strcpy (this->filename_, filename);
  this->action_ = ACE_Filecache_Object::ACE_READING;
  // place ourselves into the READING state

  // Can we access the file?
  if (ACE_OS::access (this->filename_, R_OK) == -1)
    {
      this->error_i (ACE_Filecache_Object::ACE_ACCESS_FAILED);
      return;
    }

  // Can we stat the file?
  if (ACE_OS::stat (this->filename_, &this->stat_) == -1)
    {
      this->error_i (ACE_Filecache_Object::ACE_STAT_FAILED);
      return;
    }

  this->size_ = ACE_Utils::truncate_cast<ACE_OFF_T> (this->stat_.st_size);
  this->tempname_ = this->filename_;

  // Can we open the file?
  this->handle_ = ACE_OS::open (this->tempname_,
                                READ_FLAGS, R_MASK, this->sa_);
  if (this->handle_ == ACE_INVALID_HANDLE)
    {
      this->error_i (ACE_Filecache_Object::ACE_OPEN_FAILED,
                     ACE_TEXT ("ACE_Filecache_Object::ctor: open"));
      return;
    }

  if (mapit)
    {
      // Can we map the file?
      if (this->mmap_.map (this->handle_, static_cast<size_t> (-1),
                           PROT_READ, ACE_MAP_PRIVATE, 0, 0, this->sa_) != 0)
        {
          this->error_i (ACE_Filecache_Object::ACE_MEMMAP_FAILED,
                         ACE_TEXT ("ACE_Filecache_Object::ctor: map"));
          ACE_OS::close (this->handle_);
          this->handle_ = ACE_INVALID_HANDLE;
          return;
        }
    }

   // Ok, finished!
   this->action_ = ACE_Filecache_Object::ACE_READING;
}

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.

  : stale_ (0),
    sa_ (sa),
    lock_ (lock)
{
  this->init ();

  this->size_ = size;
  ACE_OS::strcpy (this->filename_, filename);
  this->action_ = ACE_Filecache_Object::ACE_WRITING;

  // Can we access the file?
  if (ACE_OS::access (this->filename_, R_OK|W_OK) == -1
      // Does it exist?
      && ACE_OS::access (this->filename_, F_OK) != -1)
    {
      // File exists, but we cannot access it.
      this->error_i (ACE_Filecache_Object::ACE_ACCESS_FAILED);
      return;
    }

  this->tempname_ = this->filename_;

  // Can we open the file?
  this->handle_ = ACE_OS::open (this->tempname_, WRITE_FLAGS, W_MASK, this->sa_);
  if (this->handle_ == ACE_INVALID_HANDLE)
    {
      this->error_i (ACE_Filecache_Object::ACE_OPEN_FAILED,
                     ACE_TEXT ("ACE_Filecache_Object::acquire: open"));
      return;
    }

  // Can we write?
  if (ACE_OS::pwrite (this->handle_, "", 1, this->size_ - 1) != 1)
    {
      this->error_i (ACE_Filecache_Object::ACE_WRITE_FAILED,
                     ACE_TEXT ("ACE_Filecache_Object::acquire: write"));
      ACE_OS::close (this->handle_);
      return;
    }

  // Can we map?
  if (this->mmap_.map (this->handle_, this->size_, PROT_RDWR, MAP_SHARED,
                       0, 0, this->sa_) != 0)
    {
      this->error_i (ACE_Filecache_Object::ACE_MEMMAP_FAILED,
                     ACE_TEXT ("ACE_Filecache_Object::acquire: map"));
      ACE_OS::close (this->handle_);
    }

  // Ok, done!
}

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.

{
  if (this->error_ == ACE_SUCCESS)
    {
      this->mmap_.unmap ();
      ACE_OS::close (this->handle_);
      this->handle_ = ACE_INVALID_HANDLE;
    }

  this->lock_.release ();
}

ACE_Filecache_Object::ACE_Filecache_Object ( void   )  [protected]

Prevent from being called.

Definition at line 474 of file Filecache.cpp.

  : tempname_ (0),
    mmap_ (),
    handle_ (0),
    // stat_ (),
    size_ (0),
    action_ (0),
    error_ (0),
    stale_ (0),
    // sa_ (),
    junklock_ (),
    lock_ (junklock_)
{
  this->init ();
}


Member Function Documentation

int ACE_Filecache_Object::acquire ( void   ) 

Increment the reference_count_.

Definition at line 627 of file Filecache.cpp.

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

void * ACE_Filecache_Object::address ( void   )  const

Base memory address for memory mapped file.

Definition at line 725 of file Filecache.cpp.

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

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.

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

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.

{
  s = s;
  ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p.\n"), s));
  this->error_ = error_value;
  return error_value;
}

const ACE_TCHAR * ACE_Filecache_Object::filename ( void   )  const

filename_ accessor

Definition at line 704 of file Filecache.cpp.

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

ACE_HANDLE ACE_Filecache_Object::handle ( void   )  const

handle_ accessor.

Definition at line 718 of file Filecache.cpp.

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

void ACE_Filecache_Object::init ( void   )  [protected]

Common initialization code,.

Definition at line 463 of file Filecache.cpp.

{
  this->filename_[0] = '\0';
  this->handle_ = ACE_INVALID_HANDLE;
  this->error_ = ACE_SUCCESS;
  this->tempname_ = 0;
  this->size_ = 0;

  ACE_OS::memset (&(this->stat_), 0, sizeof (this->stat_));
}

int ACE_Filecache_Object::release ( void   ) 

Decrement the reference_count_.

Definition at line 633 of file Filecache.cpp.

{
  if (this->action_ == ACE_WRITING)
    {
      // We are safe since only one thread has a writable Filecache_Object

#if 0
      ACE_HANDLE original = ACE_OS::open (this->filename_, WRITE_FLAGS, W_MASK,
                                          this->sa_);
      if (original == ACE_INVALID_HANDLE)
        this->error_ = ACE_Filecache_Object::ACE_OPEN_FAILED;
      else if (ACE_OS::write (original, this->mmap_.addr (),
                              this->size_) == -1)
        {
          this->error_ = ACE_Filecache_Object::ACE_WRITE_FAILED;
          ACE_OS::close (original);
          ACE_OS::unlink (this->filename_);
        }
      else if (ACE_OS::stat (this->filename_, &this->stat_) == -1)
        this->error_ = ACE_Filecache_Object::ACE_STAT_FAILED;
#endif

      this->mmap_.unmap ();
      ACE_OS::close (this->handle_);
      this->handle_ = ACE_INVALID_HANDLE;

#if 0
      // Leave the file in an acquirable state.
      this->handle_ = ACE_OS::open (this->tempname_, READ_FLAGS, R_MASK);
      if (this->handle_ == ACE_INVALID_HANDLE)
        {
          this->error_i (ACE_Filecache_Object::ACE_OPEN_FAILED,
                         "ACE_Filecache_Object::acquire: open");
        }
      else if (this->mmap_.map (this->handle_, -1,
                                PROT_READ,
                                ACE_MAP_PRIVATE,
                                0,
                                0,
                                this->sa_) != 0)
        {
          this->error_i (ACE_Filecache_Object::ACE_MEMMAP_FAILED,
                         "ACE_Filecache_Object::acquire: map");
          ACE_OS::close (this->handle_);
          this->handle_ = ACE_INVALID_HANDLE;
        }

      this->action_ = ACE_Filecache_Object::ACE_READING;
#endif
    }

  return this->lock_.release ();
}

ACE_OFF_T ACE_Filecache_Object::size ( void   )  const

size_ accessor.

Definition at line 711 of file Filecache.cpp.

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

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.

{
  // The existence of the object means a read lock is being held.
  int result;
  ACE_stat statbuf;

  if (ACE_OS::stat (this->filename_, &statbuf) == -1)
    result = 1;
  else
    result = ACE_OS::difftime (this->stat_.st_mtime, statbuf.st_mtime) < 0;

  return result;
}


Friends And Related Function Documentation

friend class ACE_Filecache [friend]

Definition at line 245 of file Filecache.h.


Member Data Documentation

Status indicators.

Definition at line 336 of file Filecache.h.

Definition at line 337 of file Filecache.h.

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

Definition at line 323 of file Filecache.h.

ACE_HANDLE ACE_Filecache_Object::handle_ [private]

The descriptor to the temporary file.

Definition at line 329 of file Filecache.h.

ACE_SYNCH_RW_MUTEX ACE_Filecache_Object::junklock_ [private]

The default initializer.

Definition at line 346 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 349 of file Filecache.h.

Holds the memory mapped version of the temporary file.

Definition at line 326 of file Filecache.h.

LPSECURITY_ATTRIBUTES ACE_Filecache_Object::sa_ [private]

Security attribute object.

Definition at line 343 of file Filecache.h.

Definition at line 333 of file Filecache.h.

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

Definition at line 340 of file Filecache.h.

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

Definition at line 332 of file Filecache.h.

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

Definition at line 322 of file Filecache.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines