Go to the documentation of this file.00001
00002
00003 #include "orbsvcs/Notify/Random_File.h"
00004
00005 #include "ace/OS.h"
00006 #include "ace/Log_Msg.h"
00007 #include "tao/debug.h"
00008
00009 #ifndef DEBUG_LEVEL
00010 # define DEBUG_LEVEL TAO_debug_level
00011 #endif //DEBUG_LEVEL
00012
00013 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00014
00015 namespace TAO_Notify
00016 {
00017
00018 Random_File::Random_File()
00019 : block_size_(512)
00020 {
00021 }
00022
00023 Random_File::~Random_File()
00024 {
00025 this->close();
00026 }
00027
00028 size_t
00029 Random_File::block_size() const
00030 {
00031 return this->block_size_;
00032 }
00033
00034 ACE_OFF_T
00035 Random_File::size() const
00036 {
00037 Random_File * const mutable_this = const_cast<Random_File *> (this);
00038 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, 0);
00039 ACE_OFF_T original_pos = mutable_this->tell ();
00040 mutable_this->ACE_FILE::seek(0, SEEK_END);
00041 ACE_OFF_T cursize = mutable_this->tell();
00042 mutable_this->ACE_FILE::seek (original_pos, SEEK_SET);
00043 if ((cursize % this->block_size_) != 0)
00044 {
00045 cursize += this->block_size_;
00046 }
00047 return cursize / this->block_size_;
00048 }
00049
00050 bool
00051 Random_File::open(const ACE_TCHAR* filename, size_t block_size)
00052 {
00053 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, false);
00054 this->block_size_ = block_size;
00055 bool result = (this->close() == 0);
00056
00057 if (result)
00058 {
00059 if (DEBUG_LEVEL > 8) ACE_DEBUG ((LM_DEBUG,
00060 ACE_TEXT ("(%P|%t) Opening file %s\n")
00061 , filename
00062 ));
00063 ACE_HANDLE handle = ACE_OS::open(filename,
00064 O_CREAT | O_RDWR | O_BINARY,
00065 ACE_DEFAULT_FILE_PERMS);
00066
00067 if (handle == ACE_INVALID_HANDLE)
00068 {
00069 result = false;
00070 }
00071 else
00072 {
00073 this->set_handle(handle);
00074 if (this->get_handle() == 0)
00075 {
00076 result = false;
00077 }
00078 else
00079 {
00080 result = (this->addr_.set(filename) == 0);
00081 }
00082 }
00083 }
00084 return result;
00085 }
00086
00087 bool
00088 Random_File::write(const size_t block_number, void* buf, bool atomic)
00089 {
00090 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, false);
00091 if (DEBUG_LEVEL > 8) ACE_DEBUG ((LM_DEBUG,
00092 ACE_TEXT ("(%P|%t) Write block %d %c\n"),
00093 static_cast<int> (block_number),
00094 (atomic ? '*' : ' ')
00095 ));
00096 bool result = this->seek(block_number);
00097 if (result)
00098 {
00099 if (atomic)
00100 {
00101
00102
00103 result = sync();
00104 }
00105
00106 ssize_t block_size = this->block_size_;
00107 if (result && (block_size !=
00108 ACE_OS::write(this->get_handle(), buf, block_size)))
00109 {
00110 result = false;
00111 }
00112 if (result && atomic)
00113 {
00114
00115
00116 result = sync();
00117 }
00118 }
00119 return result;
00120 }
00121
00122 bool
00123 Random_File::read(const size_t block_number, void* buf)
00124 {
00125 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX, ace_mon, this->lock_, false);
00126 if (DEBUG_LEVEL > 8) ACE_DEBUG ((LM_DEBUG,
00127 ACE_TEXT ("(%P|%t) Read block %d\n"),
00128 static_cast<int> (block_number)
00129 ));
00130 bool result = this->seek(block_number);
00131 if (result)
00132 {
00133 ssize_t block_size = this->block_size_;
00134 if (block_size !=
00135 ACE_OS::read(this->get_handle(), buf, block_size))
00136 {
00137 result = false;
00138 }
00139 }
00140 return result;
00141 }
00142
00143 bool
00144 Random_File::seek(const size_t block_number)
00145 {
00146 ssize_t destloc = block_number * this->block_size_;
00147 bool result = (destloc == this->ACE_FILE::seek(destloc, SEEK_SET));
00148 return result;
00149 }
00150
00151 bool
00152 Random_File::sync()
00153 {
00154 bool result = false;
00155 result = (0 == ACE_OS::fsync(this->get_handle()));
00156 return result;
00157 }
00158
00159 }
00160
00161 TAO_END_VERSIONED_NAMESPACE_DECL