#include <Filecache.h>
Collaboration diagram for ACE_Filecache_Object:
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_TCHAR * | filename (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_TCHAR * | tempname_ |
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 |
Definition at line 239 of file Filecache.h.
|
Definition at line 299 of file Filecache.h.
00300 { 00301 ACE_READING = 1, 00302 ACE_WRITING = 2 00303 }; |
|
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 }; |
|
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 } |
|
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 } |
|
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 } |
|
Prevent from being called.
Definition at line 473 of file Filecache.cpp. References init().
|
|
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 } |
|
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().
|
|
|
|
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 } |
|
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().
|
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
Definition at line 242 of file Filecache.h. |
|
Status indicators.
Definition at line 333 of file Filecache.h. Referenced by ACE_Filecache_Object(), ACE_Filecache::finish(), and release(). |
|
Definition at line 334 of file Filecache.h. |
|
Definition at line 320 of file Filecache.h. Referenced by ACE_Filecache::finish(). |
|
The descriptor to the temporary file.
Definition at line 326 of file Filecache.h. |
|
The default initializer.
Definition at line 343 of file Filecache.h. |
|
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(). |
|
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(). |
|
Security attribute object.
Definition at line 340 of file Filecache.h. |
|
Definition at line 330 of file Filecache.h. |
|
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(). |
|
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(). |
|
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(). |