diff options
Diffstat (limited to 'src/core/xbfile.cpp')
-rwxr-xr-x | src/core/xbfile.cpp | 268 |
1 files changed, 146 insertions, 122 deletions
diff --git a/src/core/xbfile.cpp b/src/core/xbfile.cpp index 3b798a2..6376e9a 100755 --- a/src/core/xbfile.cpp +++ b/src/core/xbfile.cpp @@ -49,119 +49,6 @@ xbFile::~xbFile(){ } /************************************************************************/ -//! @brief Create Home Folders. -/*! - Create xbase64 log and data folders in the home directory for current usre. - - \returns <a href="xbretcod_8h.html">Return Codes</a> -*/ - -xbInt16 xbFile::SetHomeFolders(){ - - xbInt16 iErrorStop = 0; - xbInt16 iRc = XB_NO_ERROR; - xbString sHomeDir; - char cPathSeperator; - xbString sDflt; - - try{ - - GetHomeDir( sHomeDir ); - //std::cout << "CreateHomeFolders() home dir = [" << sHomeDir.Str() << "]\n"; - - if( FileExists( sHomeDir ) == xbFalse ){ - iErrorStop = 100; - iRc = XB_DIRECTORY_ERROR; - throw iRc; - } - - #ifdef WIN32 - cPathSeperator = '\\'; - #else - cPathSeperator = '/'; - #endif - sDflt.Sprintf( ".%c", cPathSeperator ); - // set the default folders just in case later steps fail - xbase->SetDataDirectory( sDflt ); - #ifdef XB_LOGGING_SUPPORT - xbase->SetLogDirectory( sDflt ); - #endif - - if( sHomeDir[sHomeDir.Len()] != cPathSeperator ) - sHomeDir += cPathSeperator; - - xbString sWork( sHomeDir ); - sWork += "xbase64"; - - if( FileExists( sWork ) == xbFalse ){ - #ifdef WIN32 - if( CreateDirectory( sWork.Str(), NULL ) == 0 ){ - iErrorStop = 130; - iRc = XB_DIRECTORY_ERROR; - throw iRc; - } - #else - // 0777 mode is correct, the mode will be modified by the user's umask - if( mkdir( sWork.Str(), 0777 ) == -1 ){ - iErrorStop = 140; - iRc = XB_DIRECTORY_ERROR; - throw iRc; - } - #endif - } - - #ifdef XB_LOGGING_SUPPORT - sWork.Sprintf( "%sxbase64%clogs", sHomeDir.Str(), cPathSeperator ); - // std::cout << "logdir = " << sWork.Str() << "\n"; - - if( FileExists( sWork ) == xbFalse ){ - #ifdef WIN32 - if( CreateDirectory( sWork.Str(), NULL ) == 0 ){ - iErrorStop = 110; - iRc = XB_DIRECTORY_ERROR; - throw iRc; - } - #else - if( mkdir( sWork.Str(), 0777 ) == -1 ){ - iErrorStop = 120; - iRc = XB_DIRECTORY_ERROR; - throw iRc; - } - #endif - } - xbase->SetLogDirectory( sWork ); - #endif // XB_LOGGING_SUPPORT - - sWork.Sprintf( "%sxbase64%cdata", sHomeDir.Str(), cPathSeperator ); - // std::cout << "datadir = " << sWork.Str() << "\n"; - if( FileExists( sWork ) == xbFalse ){ - #ifdef WIN32 - if( CreateDirectory( sWork.Str(), NULL ) == 0 ){ - iErrorStop = 130; - iRc = XB_DIRECTORY_ERROR; - throw iRc; - } - #else - if( mkdir( sWork.Str(), 0777 ) == -1 ){ - iErrorStop = 140; - iRc = XB_DIRECTORY_ERROR; - throw iRc; - } - #endif - } - xbase->SetDataDirectory( sWork ); - - } - catch (xbInt16 iRc ){ - xbString sMsg; - sMsg.Sprintf( "xbFile::CreateHomeFolders() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc ); - xbase->WriteLogMessage( sMsg.Str() ); - xbase->WriteLogMessage( GetErrorMessage( iRc )); - } - return iRc; -} - -/************************************************************************/ //! @brief Create a unique file name. /*! Given a directory and file extension as inputs, create a unique file name. @@ -171,15 +58,16 @@ xbInt16 xbFile::SetHomeFolders(){ \param sFqnOut A fully qualifed unique file name as output \returns <a href="xbretcod_8h.html">Return Codes</a> */ +/* xbInt16 xbFile::CreateUniqueFileName( const xbString & sDirIn, const xbString & sExtIn, xbString &sFqnOut ){ return CreateUniqueFileName( sDirIn, sExtIn, sFqnOut, 0 ); } - +*/ /************************************************************************/ //! @brief Create a unique file name. /*! Given a directory and file extension as inputs, create a unique file name. - + \param sDirIn Directory \param sExtIn File Extension \param iOption 0 - look only for one file for a given directory and extension<br> @@ -192,12 +80,16 @@ xbInt16 xbFile::CreateUniqueFileName( const xbString & sDirIn, const xbString & xbBool bUniqueFileNameFound = xbFalse; xbFile f( xbase); - xbInt32 l = 1; + xbInt32 l = 1; xbString sMemoFileName; + xbString sDir = sDirIn; + char c = GetPathSeparator(); + if( sDirIn.Len() > 0 && sDirIn[sDirIn.Len()] != c ) + sDir += c; while( !bUniqueFileNameFound ){ - sFqnOut.Sprintf( "%sxbTmp%03d.%s", sDirIn.Str(), l, sExtIn.Str()); + sFqnOut.Sprintf( "%sxbTmp%03d.%s", sDir.Str(), l, sExtIn.Str()); if( iOption == 1 && sExtIn == "DBF" ){ sMemoFileName.Sprintf( "%sxbTmp%03d.DBT", sDirIn.Str(), l ); } @@ -693,7 +585,6 @@ const xbString & xbFile::GetDirectory() const { \returns <a href="xbretcod_8h.html">Return Codes</a> */ - xbInt16 xbFile::GetFileDirPart( xbString & sFileDirPartOut ) const { return GetFileDirPart( sFqFileName, sFileDirPartOut ); } @@ -1139,7 +1030,6 @@ xbInt16 xbFile::SetBlockSize( xbUInt32 ulBlockSize ){ \param sDirectory - Valid directory name */ - void xbFile::SetDirectory( const xbString & sDirectory ){ this->sDirectory = sDirectory; @@ -1242,6 +1132,136 @@ void xbFile::SetFqFileName( const xbString & sFqFileName ){ } /************************************************************************/ +//! @brief Set Home Folders. +/*! + Create xbase64 log, data and temp folders in the home directory for current usre. + + \returns <a href="xbretcod_8h.html">Return Codes</a> +*/ + +xbInt16 xbFile::SetHomeFolders(){ + + xbInt16 iErrorStop = 0; + xbInt16 iRc = XB_NO_ERROR; + xbString sHomeDir; + char cPathSeparator; + xbString sDflt; + + try{ + + GetHomeDir( sHomeDir ); + //std::cout << "CreateHomeFolders() home dir = [" << sHomeDir.Str() << "]\n"; + + if( FileExists( sHomeDir ) == xbFalse ){ + iErrorStop = 100; + iRc = XB_DIRECTORY_ERROR; + throw iRc; + } + + #ifdef WIN32 + cPathSeparator = '\\'; + #else + cPathSeparator = '/'; + #endif + sDflt.Sprintf( ".%c", cPathSeparator ); + // set the default folders just in case later steps fail + xbase->SetDataDirectory( sDflt ); + #ifdef XB_LOGGING_SUPPORT + xbase->SetLogDirectory( sDflt ); + #endif + + if( sHomeDir[sHomeDir.Len()] != cPathSeparator ) + sHomeDir += cPathSeparator; + + xbString sWork( sHomeDir ); + sWork += "xbase64"; + + if( FileExists( sWork ) == xbFalse ){ + #ifdef WIN32 + if( CreateDirectory( sWork.Str(), NULL ) == 0 ){ + iErrorStop = 130; + iRc = XB_DIRECTORY_ERROR; + throw iRc; + } + #else + // 0777 mode is correct, the mode will be modified by the user's umask + if( mkdir( sWork.Str(), 0777 ) == -1 ){ + iErrorStop = 140; + iRc = XB_DIRECTORY_ERROR; + throw iRc; + } + #endif + } + + #ifdef XB_LOGGING_SUPPORT + sWork.Sprintf( "%sxbase64%clogs", sHomeDir.Str(), cPathSeparator ); + // std::cout << "logdir = " << sWork.Str() << "\n"; + + if( FileExists( sWork ) == xbFalse ){ + #ifdef WIN32 + if( CreateDirectory( sWork.Str(), NULL ) == 0 ){ + iErrorStop = 110; + iRc = XB_DIRECTORY_ERROR; + throw iRc; + } + #else + if( mkdir( sWork.Str(), 0777 ) == -1 ){ + iErrorStop = 120; + iRc = XB_DIRECTORY_ERROR; + throw iRc; + } + #endif + } + xbase->SetLogDirectory( sWork ); + #endif // XB_LOGGING_SUPPORT + + sWork.Sprintf( "%sxbase64%cdata", sHomeDir.Str(), cPathSeparator ); + // std::cout << "datadir = " << sWork.Str() << "\n"; + if( FileExists( sWork ) == xbFalse ){ + #ifdef WIN32 + if( CreateDirectory( sWork.Str(), NULL ) == 0 ){ + iErrorStop = 130; + iRc = XB_DIRECTORY_ERROR; + throw iRc; + } + #else + if( mkdir( sWork.Str(), 0777 ) == -1 ){ + iErrorStop = 140; + iRc = XB_DIRECTORY_ERROR; + throw iRc; + } + #endif + } + + sWork.Sprintf( "%sxbase64%ctemp%c", sHomeDir.Str(), cPathSeparator, cPathSeparator ); + // std::cout << "tempdir = " << sWork.Str() << "\n"; + if( FileExists( sWork ) == xbFalse ){ + #ifdef WIN32 + if( CreateDirectory( sWork.Str(), NULL ) == 0 ){ + iErrorStop = 150; + iRc = XB_DIRECTORY_ERROR; + throw iRc; + } + #else + if( mkdir( sWork.Str(), 0777 ) == -1 ){ + iErrorStop = 160; + iRc = XB_DIRECTORY_ERROR; + throw iRc; + } + #endif + } + xbase->SetTempDirectory( sWork ); + } + catch (xbInt16 iRc ){ + xbString sMsg; + sMsg.Sprintf( "xbFile::CreateHomeFolders() Exception Caught. Error Stop = [%d] iRc = [%d]", iErrorStop, iRc ); + xbase->WriteLogMessage( sMsg.Str() ); + xbase->WriteLogMessage( GetErrorMessage( iRc )); + } + return iRc; +} + +/************************************************************************/ //! @brief Write a block of data to file. /*! @@ -1744,6 +1764,7 @@ xbInt16 xbFile::xbRemove( const xbString & sFileNameIn ) { xbInt16 xbFile::xbRemove( const xbString & sFileNameIn, xbInt16 iOption ) { xbInt32 iRc = remove( sFileNameIn.Str()); + if( iRc != 0 ) return XB_DELETE_FAILED; @@ -1904,7 +1925,7 @@ xbInt16 xbFile::xbLock( xbInt16 iFunction, xbInt64 lOffset, size_t stLen ){ Qsplit lPos; Qsplit lLen; - lPos.Qword = lOffset; + lPos.Qword = (size_t) lOffset; lLen.Qword = stLen; do{ @@ -2037,7 +2058,9 @@ xbInt16 xbFile::DumpMemToDisk( char *p, size_t lSize ){ try{ - sDir = GetLogDirectory(); + // sDir = GetLogDirectory(); + sDir = xbase->GetLogDirectory(); + char cLastChar = sDir[sDir.Len()]; // build logfile name @@ -2127,7 +2150,8 @@ xbInt16 xbFile::DumpBlockToDisk( xbUInt32 ulBlockNo, size_t lBlkSize ){ throw iRc; } - sDir = GetLogDirectory(); +// sDir = GetLogDirectory(); + sDir = xbase->GetLogDirectory(); char cLastChar = sDir[sDir.Len()]; for( xbUInt32 l = ulStartBlock; l < ulEndBlock; l++ ){ |