Flat_File_Persistence.cpp

Go to the documentation of this file.
00001 // Flat_File_Persistence.cpp,v 1.18 2006/03/14 06:14:33 jtc Exp
00002 
00003 //-----------------------------------------------------------------------------
00004 // Flat File class implementations
00005 //-----------------------------------------------------------------------------
00006 #include "orbsvcs/Naming/Flat_File_Persistence.h"
00007 
00008 #include "ace/Log_Msg.h"
00009 #include "ace/OS_NS_sys_stat.h"
00010 #include "ace/OS_NS_unistd.h"
00011 #include "ace/OS_NS_fcntl.h"
00012 
00013 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
00014 
00015 TAO_NS_FlatFileStream::TAO_NS_FlatFileStream (const ACE_CString & file,
00016                                               const char * mode)
00017   : fl_ (0)
00018 {
00019   ACE_TRACE("TAO_NS_FlatFileStream");
00020   file_ = file;
00021   mode_ = mode;
00022 }
00023 
00024 TAO_NS_FlatFileStream::~TAO_NS_FlatFileStream ()
00025 {
00026   ACE_TRACE("~TAO_NS_FlatFileStream");
00027   if ( fl_ != 0 )
00028     this->close();
00029 }
00030 
00031 void
00032 TAO_NS_FlatFileStream::remove ()
00033 {
00034   ACE_TRACE("remove");
00035   ACE_OS::unlink(ACE_TEXT_CHAR_TO_TCHAR(file_.c_str()));
00036 }
00037 
00038 int
00039 TAO_NS_FlatFileStream::exists ()
00040 {
00041   ACE_TRACE("exists");
00042   // We could check the mode for this file, but for now just check exists
00043   return ! ACE_OS::access(file_.c_str(), F_OK);
00044 }
00045 
00046 int
00047 TAO_NS_FlatFileStream::open()
00048 {
00049   ACE_TRACE("open");
00050   // For now, three flags exist "r", "w",  and "c"
00051   int flags = 0;
00052   const char *fdmode = 0;
00053   if( strchr(mode_.c_str(), 'r') )
00054     if( strchr(mode_.c_str(), 'w') )
00055       flags = O_RDWR, fdmode = "r+";
00056     else
00057       flags = O_RDONLY, fdmode = "r";
00058   else
00059     flags = O_WRONLY, fdmode = "w";
00060   if( strchr(mode_.c_str(), 'c') )
00061     flags |= O_CREAT;
00062 #ifndef ACE_WIN32
00063   if( ACE_OS::flock_init (&filelock_, flags, ACE_TEXT_CHAR_TO_TCHAR(file_.c_str()), 0666) != 0 )
00064     ACE_ERROR_RETURN ((LM_ERROR,
00065                        "Cannot open file %s for mode %s: (%d) %s\n",
00066                        file_.c_str(), mode_.c_str(),
00067                        errno, ACE_OS::strerror(errno)),
00068                       -1);
00069 #else
00070   if( (filelock_.handle_= ACE_OS::open (ACE_TEXT_CHAR_TO_TCHAR(file_.c_str()), flags, 0)) == ACE_INVALID_HANDLE )
00071     ACE_ERROR_RETURN ((LM_ERROR,
00072                        "Cannot open file %s for mode %s: (%d) %s\n",
00073                        file_.c_str(), mode_.c_str(),
00074                        errno, ACE_OS::strerror(errno)),
00075                       -1);
00076 #endif
00077   this->fl_ = ACE_OS::fdopen(filelock_.handle_, ACE_TEXT_CHAR_TO_TCHAR(fdmode));
00078   if (this->fl_ == 0)
00079     ACE_ERROR_RETURN ((LM_ERROR,
00080                        "Cannot fdopen file %s for mode %s: (%d) %s\n",
00081                        file_.c_str(), mode_.c_str(),
00082                        errno, ACE_OS::strerror(errno)),
00083                       -1);
00084   return 0;
00085 }
00086 
00087 int
00088 TAO_NS_FlatFileStream::close()
00089 {
00090   ACE_TRACE("close");
00091   ACE_OS::fflush(fl_);
00092 #ifndef ACE_WIN32
00093   ACE_OS::flock_destroy (&filelock_, 0);
00094 #endif
00095   ACE_OS::fclose (fl_);  // even though flock_destroy closes the handle
00096                          // we still need to destroy the FILE*
00097 
00098   fl_ = 0;
00099   return 0;
00100 }
00101 
00102 int
00103 TAO_NS_FlatFileStream::flock (int whence, int start, int len)
00104 {
00105   ACE_TRACE("flock");
00106 #if defined (ACE_WIN32)
00107   ACE_UNUSED_ARG (whence);
00108   ACE_UNUSED_ARG (start);
00109   ACE_UNUSED_ARG (len);
00110 #else
00111   if( ACE_OS::strcmp(mode_.c_str(), "r") == 0 )
00112     ACE_OS::flock_rdlock(&filelock_, whence, start, len);
00113   else
00114     ACE_OS::flock_wrlock(&filelock_, whence, start, len);
00115 #endif
00116   return 0;
00117 }
00118 
00119 int
00120 TAO_NS_FlatFileStream::funlock (int whence, int start, int len)
00121 {
00122   ACE_TRACE("funlock");
00123 #if defined (ACE_WIN32)
00124   ACE_UNUSED_ARG (whence);
00125   ACE_UNUSED_ARG (start);
00126   ACE_UNUSED_ARG (len);
00127 #else
00128   ACE_OS::flock_unlock(&filelock_, whence, start, len);
00129 #endif
00130   return 0;
00131 }
00132 
00133 time_t
00134 TAO_NS_FlatFileStream::last_changed(void)
00135 {
00136   ACE_TRACE("TAO_NS_FlatFileStream::last_changed");
00137   ACE_stat st;
00138   ACE_OS::fstat(filelock_.handle_, &st);
00139 #if !defined (ACE_HAS_WINCE)
00140   return st.st_mtime;
00141 #else
00142   return st.st_mtime.sec ();
00143 #endif /* ACE_HAS_WINCE */
00144 }
00145 
00146 TAO_Storable_Base &
00147 TAO_NS_FlatFileStream::operator <<(
00148                                 const TAO_NS_Persistence_Header &header)
00149 {
00150   ACE_TRACE("TAO_NS_FlatFileStream::operator <<");
00151   ACE_OS::rewind(this->fl_);
00152   ACE_OS::fprintf(this->fl_, "%d\n%d\n", header.size(), header.destroyed());
00153   ACE_OS::fflush(this->fl_);
00154 
00155   return *this;
00156 }
00157 
00158 TAO_Storable_Base &
00159 TAO_NS_FlatFileStream::operator >>(
00160                       TAO_NS_Persistence_Header &header)
00161 {
00162   ACE_TRACE("TAO_NS_FlatFileStream::operator >>");
00163   unsigned int size;
00164   int destroyed;
00165 
00166   ACE_OS::rewind(this->fl_);
00167   switch (fscanf(fl_, "%u\n", &size))
00168     {
00169     case 0:
00170       this->setstate (badbit);
00171       return *this;
00172     case EOF:
00173       this->setstate (eofbit);
00174       return *this;
00175     }
00176   header.size(size);
00177 
00178   switch (fscanf(fl_, "%d\n", &destroyed))
00179     {
00180     case 0:
00181       this->setstate (badbit);
00182       return *this;
00183     case EOF:
00184       this->setstate (eofbit);
00185       return *this;
00186     }
00187   header.destroyed(destroyed);
00188 
00189   return *this;
00190 
00191 }
00192 
00193 TAO_Storable_Base &
00194 TAO_NS_FlatFileStream::operator <<(
00195                                 const TAO_NS_Persistence_Record &record)
00196 {
00197   ACE_TRACE("TAO_NS_FlatFileStream::operator <<");
00198   TAO_NS_Persistence_Record::Record_Type type = record.type();
00199   ACE_OS::fprintf(this->fl_, "%d\n", type);
00200 
00201   ACE_CString id = record.id();
00202   ACE_OS::fprintf(this->fl_, ACE_SIZE_T_FORMAT_SPECIFIER ACE_TEXT("\n%s\n"),
00203                   id.length(), id.c_str());
00204 
00205   ACE_CString kind = record.kind();
00206   ACE_OS::fprintf(this->fl_, ACE_SIZE_T_FORMAT_SPECIFIER ACE_TEXT ("\n%s\n"),
00207                   kind.length(), kind.c_str());
00208 
00209   ACE_CString ref = record.ref();
00210   ACE_OS::fprintf(this->fl_, ACE_SIZE_T_FORMAT_SPECIFIER ACE_TEXT ("\n%s\n"),
00211                   ref.length(), ref.c_str());
00212 
00213   ACE_OS::fflush(this->fl_);
00214 
00215   return *this;
00216 }
00217 
00218 TAO_Storable_Base &
00219 TAO_NS_FlatFileStream::operator >>(
00220                        TAO_NS_Persistence_Record &record)
00221 {
00222   ACE_TRACE("TAO_NS_FlatFileStream::operator >>");
00223   TAO_NS_Persistence_Record::Record_Type type;
00224   int temp_type_in;
00225   switch (fscanf(fl_, "%d\n", &temp_type_in))
00226     {
00227     case 0:
00228       this->setstate (badbit);
00229       return *this;
00230     case EOF:
00231       this->setstate (eofbit);
00232       return *this;
00233     }
00234   type = (TAO_NS_Persistence_Record::Record_Type) temp_type_in;
00235   record.type(type);
00236 
00237   int bufSize = 0;
00238 
00239   //id
00240   switch (fscanf(fl_, "%d\n", &bufSize))
00241     {
00242     case 0:
00243       this->setstate (badbit);
00244       return *this;
00245     case EOF:
00246       this->setstate (eofbit);
00247       return *this;
00248     }
00249   char *id = new char[bufSize+1];
00250   //char *id;
00251   //ACE_NEW_RETURN (id, char[bufSize+1], 1);
00252   if (ACE_OS::fgets(ACE_TEXT_CHAR_TO_TCHAR(id), bufSize+1, fl_) == 0 &&
00253       bufSize != 0)
00254     {
00255       this->setstate (badbit);
00256       return *this;
00257     }
00258   ACE_CString newId(id);
00259   record.id(newId);
00260   delete [] id;
00261 
00262   //kind
00263   switch (fscanf(fl_, "%d\n", &bufSize))
00264     {
00265     case 0:
00266       this->setstate (badbit);
00267       return *this;
00268     case EOF:
00269       this->setstate (eofbit);
00270       return *this;
00271     }
00272   char *kind = new char[bufSize+1];
00273   //char *kind;
00274   //ACE_NEW (kind, char[bufSize+1]);
00275   if (ACE_OS::fgets(ACE_TEXT_CHAR_TO_TCHAR(kind), bufSize+1, fl_) == 0 &&
00276       bufSize != 0)
00277     {
00278       this->setstate (badbit);
00279       return *this;
00280     }
00281   kind[bufSize] = '\0';
00282   ACE_CString newKind(kind);
00283   record.kind(newKind);
00284   delete [] kind;
00285 
00286    //ref
00287   switch (fscanf(fl_, "%d\n", &bufSize))
00288     {
00289     case 0:
00290       this->setstate (badbit);
00291       return *this;
00292     case EOF:
00293       this->setstate (eofbit);
00294       return *this;
00295     }
00296   char *ref = new char[bufSize+1];
00297   //char *ref;
00298   //ACE_NEW(ref, char[bufSize+1]);
00299   if (ACE_OS::fgets(ACE_TEXT_CHAR_TO_TCHAR(ref), bufSize+1, fl_) == 0 &&
00300       bufSize != 0)
00301     {
00302       this->setstate (badbit);
00303       return *this;
00304     }
00305   ACE_CString newRef(ref);
00306   record.ref(newRef);
00307   delete [] ref;
00308 
00309   return *this;
00310 
00311 }
00312 
00313 TAO_Storable_Base &
00314 TAO_NS_FlatFileStream::operator <<(
00315                                 const TAO_NS_Persistence_Global &global)
00316 {
00317   ACE_TRACE("TAO_NS_FlatFileStream::operator <<");
00318   ACE_OS::rewind(this->fl_);
00319   ACE_OS::fprintf(this->fl_, "%d\n", global.counter());
00320   ACE_OS::fflush(this->fl_);
00321 
00322   return *this;
00323 }
00324 
00325 TAO_Storable_Base &
00326 TAO_NS_FlatFileStream::operator >>(
00327                       TAO_NS_Persistence_Global &global)
00328 {
00329   ACE_TRACE("TAO_NS_FlatFileStream::operator >>");
00330   unsigned int counter = 0;
00331 
00332   ACE_OS::rewind(this->fl_);
00333   switch (fscanf(fl_, "%u\n", &counter))
00334     {
00335     case 0:
00336       this->setstate (badbit);
00337       return *this;
00338     case EOF:
00339       this->setstate (eofbit);
00340       return *this;
00341     }
00342   global.counter(counter);
00343 
00344   return *this;
00345 
00346 }
00347 
00348 
00349 TAO_Storable_Base *
00350 TAO_NS_FlatFileFactory::create_stream (const ACE_CString & file,
00351                                        const ACE_TCHAR * mode)
00352 {
00353   ACE_TRACE("TAO_NS_FlatFileFactory::create_stream");
00354   TAO_Storable_Base *stream = 0;
00355 
00356   ACE_NEW_RETURN (stream,
00357                   TAO_NS_FlatFileStream(file, ACE_TEXT_ALWAYS_CHAR (mode)),
00358                   0);
00359   return stream;
00360 }
00361 
00362 TAO_END_VERSIONED_NAMESPACE_DECL

Generated on Thu Nov 9 13:57:01 2006 for TAO_CosNaming by doxygen 1.3.6