INT32 _readFile( const CHAR *pFilePath, CHAR **ppBuff, INT64 *pBuffLen ) { INT32 rc = SDB_OK ; BOOLEAN hasOpen = FALSE ; INT64 fileSize = 0 ; SINT64 readSize = 0 ; OSSFILE file ; stringstream ss ; if ( !ppBuff ) { rc = SDB_INVALIDARG ; goto error ; } rc = ossAccess( pFilePath ) ; if ( rc ) { ss << "Failed to access file[" << pFilePath << "], rc = " << rc << ERROR_END ; goto error ; } rc = ossOpen( pFilePath, OSS_READONLY, 0, file ) ; if ( rc ) { ss << "Failed to open file[" << pFilePath << "], rc = " << rc << ERROR_END ; goto error ; } hasOpen = TRUE ; rc = ossGetFileSize( &file, &fileSize ) ; if ( rc ) { ss << "Failed to get the size of file[" << pFilePath << "], rc = " << rc << ERROR_END ; goto error ; } rc = _checkBuffer( ppBuff, pBuffLen, fileSize ) ; if ( rc ) { ss << "Failed to check the size of buffer when read[" << pFilePath << "], rc = " << rc << ERROR_END ; goto error ; } rc = ossReadN( &file, fileSize, *ppBuff, readSize ) ; if ( rc ) { ss << "Failed to read content from file[" << pFilePath << "], rc = " << rc << ERROR_END ; } done: if ( hasOpen ) { ossClose( file ) ; } return rc ; error: cout << ss.str().c_str() << endl ; goto done ; }
INT32 utilGetServiceByConfigPath( const string & confPath, string & svcname, const string &defaultName, BOOLEAN allowFileNotExist ) { INT32 rc = SDB_OK ; po::options_description desc ; po::variables_map vm ; desc.add_options() ( PMD_OPTION_SVCNAME, po::value<string>(), "" ) ; CHAR conf[OSS_MAX_PATHSIZE + 1] = { 0 } ; if ( defaultName.empty() ) { svcname = boost::lexical_cast<string>(OSS_DFT_SVCPORT) ; } else { svcname = defaultName ; } rc = utilBuildFullPath ( confPath.c_str(), PMD_DFT_CONF, OSS_MAX_PATHSIZE, conf ) ; if ( rc ) { std::cerr << "Failed to build full path, rc: " << rc << std::endl ; goto error ; } if ( allowFileNotExist && SDB_OK != ossAccess( conf ) ) { goto done ; } rc = utilReadConfigureFile( conf, desc, vm ) ; if ( allowFileNotExist && SDB_IO == rc ) { rc = SDB_OK ; goto done ; } if ( rc ) { goto error ; } if ( vm.count ( PMD_OPTION_SVCNAME ) ) { svcname = vm [ PMD_OPTION_SVCNAME ].as<string>() ; } done : return rc ; error : goto done ; }
INT32 utilGetCMService( const string & rootPath, const string & hostname, string & svcname, BOOLEAN allowFileNotExist ) { INT32 rc = SDB_OK ; po::options_description desc ; po ::variables_map vm ; CHAR confFile[ OSS_MAX_PATHSIZE + 1 ] = { 0 } ; desc.add_options() ( "*", po::value<string>(), "" ) ; string hostnameKey = hostname + string( SDBCM_CONF_PORT ) ; svcname = boost::lexical_cast<string>( SDBCM_DFT_PORT ) ; rc = utilBuildFullPath( rootPath.c_str(), SDBCM_CONF_PATH_FILE, OSS_MAX_PATHSIZE, confFile ) ; if ( rc ) { std::cerr << "Failed to build full path, rc: " << rc << std::endl ; goto error ; } if ( allowFileNotExist && SDB_OK != ossAccess( confFile ) ) { goto done ; } rc = utilReadConfigureFile( confFile, desc, vm ) ; if ( allowFileNotExist && SDB_IO == rc ) { rc = SDB_OK ; goto done ; } if ( rc ) { goto error ; } if ( vm.count ( hostnameKey ) ) { svcname = vm [ hostnameKey ].as<string>() ; } else if ( vm.count( SDBCM_CONF_DFTPORT ) ) { svcname = vm [ SDBCM_CONF_DFTPORT ].as<string>() ; } done: return rc ; error: goto done ; }
INT32 _sptClassMetaInfo::_loadTroffFile() { INT32 rc = SDB_OK ; stringstream ss ; CHAR filePath[ OSS_MAX_PATHSIZE + 1 ] = { 0 } ; vector<string> vec ; multimap< string, string > mapFiles ; multimap< string, string >::iterator it ; string filter = string("*") + (_lang == SPT_LANG_EN ? SPT_TROFF_SUFFIX_EN : SPT_TROFF_SUFFIX_CN) ; rc = ossGetEWD( filePath, OSS_MAX_PATHSIZE ) ; if ( rc ) { ss << "Failed to current work directory, rc = " << rc << ERROR_END ; goto error ; } rc = utilCatPath( filePath, OSS_MAX_PATHSIZE, SPT_SDB_SHELL_TROFF_DIR ) ; if ( rc ) { ss << "Failed to cat path[" << SPT_SDB_SHELL_TROFF_DIR << "], rc = " << rc << ERROR_END ; goto error ; } rc = ossAccess( filePath ) ; if ( rc ) { ss << "Failed to access file[" << filePath << "], rc = " << rc << ERROR_END ; goto error ; } rc = ossEnumFiles( filePath, mapFiles, filter.c_str(), 2 ) ; if ( rc ) { ss << "Failed to enum files from[" << filePath << "], " << "rc = " << rc << ERROR_END ; goto error ; } for ( it = mapFiles.begin(); it != mapFiles.end(); it++ ) { vec.clear() ; boost::split( vec, it->second, boost::is_any_of( DIRECTORY_DELIMITER ) ) ; _mapFiles.insert( pair<string, string>( vec[vec.size() - 2], it->second ) ) ; } done: return rc ; error: PRINT_ERROR ; goto done ; }
INT32 utilGetInstallInfo( utilInstallInfo & info ) { INT32 rc = SDB_OK ; po::options_description desc ; po::variables_map vm ; PMD_ADD_PARAM_OPTIONS_BEGIN( desc ) ( SDB_INSTALL_RUN_FILED, po::value<string>(), "after to run cmd" ) \ ( SDB_INSTALL_USER_FIELD, po::value<string>(), "user" ) \ ( SDB_INSTALL_PATH_FIELD, po::value<string>(), "install path" ) PMD_ADD_PARAM_OPTIONS_END rc = ossAccess( SDB_INSTALL_FILE_NAME ) ; if ( rc ) { PD_LOG( PDERROR, "Access file[%s] failed, rc: %d", SDB_INSTALL_FILE_NAME, rc ) ; goto error ; } rc = utilReadConfigureFile( SDB_INSTALL_FILE_NAME, desc, vm ) ; if ( rc ) { PD_LOG( PDERROR, "Failed to read install info from file, rc: %d", rc ) ; goto error ; } if ( vm.count( SDB_INSTALL_RUN_FILED ) ) { info._run = vm[ SDB_INSTALL_RUN_FILED ].as<string>() ; } if ( vm.count( SDB_INSTALL_USER_FIELD ) ) { info._user = vm[ SDB_INSTALL_USER_FIELD ].as<string>() ; } if ( vm.count( SDB_INSTALL_PATH_FIELD ) ) { info._path = vm[ SDB_INSTALL_PATH_FIELD ].as<string>() ; } done: return rc ; error: goto done ; }
INT32 utilGetDBPathByConfigPath( const string & confPath, string &dbPath, BOOLEAN allowFileNotExist ) { INT32 rc = SDB_OK ; po::options_description desc ; po::variables_map vm ; desc.add_options() ( PMD_OPTION_DBPATH, po::value<string>(), "" ) ; CHAR conf[OSS_MAX_PATHSIZE + 1] = { 0 } ; dbPath = PMD_CURRENT_PATH ; rc = utilBuildFullPath ( confPath.c_str(), PMD_DFT_CONF, OSS_MAX_PATHSIZE, conf ) ; if ( rc ) { std::cerr << "Failed to build full path, rc: " << rc << std::endl ; goto error ; } if ( allowFileNotExist && SDB_OK != ossAccess( conf ) ) { goto done ; } rc = utilReadConfigureFile( conf, desc, vm ) ; if ( allowFileNotExist && SDB_IO == rc ) { rc = SDB_OK ; goto done ; } if ( rc ) { goto error ; } if ( vm.count ( PMD_OPTION_DBPATH ) ) { dbPath = vm [ PMD_OPTION_DBPATH ].as<string>() ; } done : return rc ; error : goto done ; }
INT32 utilGetRoleByConfigPath( const string &confPath, INT32 &role, BOOLEAN allowFileNotExist ) { INT32 rc = SDB_OK ; po::options_description desc ; po::variables_map vm ; desc.add_options() ( PMD_OPTION_ROLE, po::value<string>(), "" ) ; CHAR conf[OSS_MAX_PATHSIZE + 1] = { 0 } ; role = SDB_ROLE_STANDALONE ; rc = utilBuildFullPath ( confPath.c_str(), PMD_DFT_CONF, OSS_MAX_PATHSIZE, conf ) ; if ( rc ) { std::cerr << "Failed to build full path, rc: " << rc << std::endl ; goto error ; } if ( allowFileNotExist && SDB_OK != ossAccess( conf ) ) { goto done ; } rc = utilReadConfigureFile( conf, desc, vm ) ; if ( allowFileNotExist && SDB_IO == rc ) { rc = SDB_OK ; goto done ; } if ( rc ) { goto error ; } if ( vm.count ( PMD_OPTION_ROLE ) ) { string roleStr = vm [ PMD_OPTION_ROLE ].as<string>() ; role = utilGetRoleEnum( roleStr.c_str() ) ; } done : return rc ; error : goto done ; }
BOOLEAN _dpsLogFilter::isDir( const CHAR *path ) { BOOLEAN rc = FALSE ; SDB_OSS_FILETYPE fileType = SDB_OSS_UNK ; INT32 retVal = ossAccess( path, W_OK ) ; if ( SDB_OK == retVal ) { INT32 retValue = ossGetPathType( path, &fileType ) ; if( SDB_OSS_DIR == fileType && !retValue ) { rc = TRUE ; goto done ; } } done: return rc ; }
INT32 utilWriteConfigFile( const CHAR * pFile, const CHAR * pData, BOOLEAN createOnly ) { INT32 rc = SDB_OK ; std::string tmpFile = pFile ; tmpFile += ".tmp" ; OSSFILE file ; BOOLEAN isOpen = FALSE ; BOOLEAN isBak = FALSE ; if ( SDB_OK == ossAccess( tmpFile.c_str() ) ) { ossDelete( tmpFile.c_str() ) ; } if ( SDB_OK == ossAccess( pFile ) ) { if ( createOnly ) { rc = SDB_FE ; goto error ; } if ( SDB_OK == ossRenamePath( pFile, tmpFile.c_str() ) ) { isBak = TRUE ; } } rc = ossOpen ( pFile, OSS_READWRITE|OSS_SHAREWRITE|OSS_REPLACE, OSS_RWXU, file ) ; if ( rc ) { goto error ; } isOpen = TRUE ; { SINT64 written = 0 ; SINT64 len = ossStrlen( pData ) ; while ( 0 < len ) { SINT64 tmpWritten = 0 ; rc = ossWrite( &file, pData + written , len, &tmpWritten ) ; if ( rc && SDB_INTERRUPT != rc ) { PD_LOG( PDERROR, "Failed to write file[%s]:%d", pFile, rc ) ; goto error ; } written += tmpWritten ; len -= tmpWritten ; rc = SDB_OK ; } } if ( SDB_OK == ossAccess( tmpFile.c_str() ) ) { ossDelete( tmpFile.c_str() ) ; } done: if ( isOpen ) { ossClose( file ) ; } return rc ; error: if ( isBak ) { if ( isOpen ) { ossClose( file ) ; isOpen = FALSE ; ossDelete( pFile ) ; } ossRenamePath( tmpFile.c_str(), pFile ) ; } goto done ; }
INT32 _ossModuleHandle::init () { INT32 rc = SDB_OK ; CHAR strPath [ 2*OSS_MAX_PATHSIZE + 1 ] = {0} ; CHAR strModule [ OSS_MAX_PATHSIZE + 1 ] = {0} ; OSS_MHANDLE handle = 0 ; CHAR *p = NULL ; #if defined (_WINDOWS) UINT32 errorMode ; #endif PD_TRACE_ENTRY ( SDB_OSSMODULEHANDLE_INIT ) ; if ( _moduleName[0] == '\0' ) { PD_LOG ( PDERROR, "Module name can't be empty" ) ; rc = SDB_INVALIDARG ; goto error ; } PD_TRACE3 ( SDB_OSSMODULEHANDLE_INIT, PD_PACK_STRING(_moduleName), PD_PACK_STRING(_libPath), PD_PACK_UINT(_flags) ) ; ossStrncpy ( strModule, _moduleName, sizeof(strModule) ) ; p = ossStrchr ( strModule, '(' ) ; if ( p ) { *p = '\0' ; } rc = patchModuleName( strModule, _moduleName, sizeof(_moduleName) ); PD_RC_CHECK ( rc, PDERROR, "Failed to patch module name, rc = %d", rc ) ; if ( _libPath[0] ) { INT32 pathLen = 0 ; rc = ossAccess ( _libPath, #if defined (_LINUX) F_OK #elif defined (_WINDOWS) 0 #endif ) ; PD_RC_CHECK ( rc, PDERROR, "Failed to access path %s, rc = %d", _libPath, rc ) ; ossStrncat ( strPath, _libPath, sizeof(strPath) ) ; pathLen = ossStrlen ( strPath ) ; if ( strPath[pathLen-1] != OSS_FILE_SEP_CHAR ) { if ( pathLen >= OSS_MAX_PATHSIZE ) { PD_LOG ( PDERROR, "library path is too long: %s", _libPath ) ; rc = SDB_INVALIDARG ; goto error ; } strPath[pathLen-1] = OSS_FILE_SEP_CHAR ; strPath[pathLen] = '\0' ; } } if ( ossStrlen ( strPath ) + ossStrlen ( _moduleName ) >= sizeof(strPath) ) { PD_LOG ( PDERROR, "path + module name is too long: %s:%s", strPath, _moduleName ) ; rc = SDB_INVALIDARG ; goto error ; } ossStrncat ( strPath, _moduleName, OSS_MAX_PATHSIZE ) ; #if defined (_LINUX) handle = dlopen ( strPath, _flags | RTLD_NOW ) ; if ( !handle ) { PD_LOG ( PDERROR, "Failed to load module %s, error = %s", strPath, dlerror() ) ; rc = SDB_SYS ; goto error ; } _isInitialized = TRUE ; _moduleHandle = handle ; dlerror() ; #elif defined (_WINDOWS) errorMode = SetErrorMode ( SEM_NOOPENFILEERRORBOX | SEM_FAILCRITICALERRORS ) ; _moduleHandle = LoadLibrary ( (LPCTSTR)strPath ) ; SetErrorMode ( errorMode ) ; if ( NULL == _moduleHandle ) { rc = ossGetLastError () ; PD_LOG ( PDERROR, "Failed to load module %s, error = %d", _moduleName, rc ) ; OSSMODULEHANDLE_ERR(rc) ; goto error ; } #endif done : PD_TRACE_EXITRC ( SDB_OSSMODULEHANDLE_INIT, rc ) ; return rc ; error : _isInitialized = FALSE ; _moduleHandle = OSS_MINVALIDHANDLE ; goto done ; }
// initialize a log file, file size max 4GB // PD_TRACE_DECLARE_FUNCTION ( SDB__DPSLOGFILE_INIT, "_dpsLogFile::init" ) INT32 _dpsLogFile::init( const CHAR *path, UINT32 size, UINT32 fileNum ) { INT32 rc = SDB_OK ; PD_TRACE_ENTRY ( SDB__DPSLOGFILE_INIT ) ; BOOLEAN created = FALSE ; SDB_ASSERT ( 0 == ( _fileSize % DPS_DEFAULT_PAGE_SIZE ), "Size must be multiple of DPS_DEFAULT_PAGE_SIZE bytes" ) ; _fileSize = size ; _fileNum = fileNum ; _idleSize = _fileSize ; // allocate OSS_FILE, free in destructor _file = SDB_OSS_NEW _OSS_FILE(); if ( !_file ) { rc = SDB_OOM; PD_LOG ( PDERROR, "new _OSS_FILE failed!" ); goto error; } // if the file exist, restore if ( SDB_OK == ossAccess( path ) ) { rc = ossOpen ( path, OSS_READWRITE|OSS_SHAREWRITE, OSS_RWXU, *_file ) ; if ( rc == SDB_OK ) { rc = _restore () ; if ( rc == SDB_OK ) { UINT32 startOffset = 0 ; if ( DPS_INVALID_LSN_OFFSET != _logHeader._firstLSN.offset ) { startOffset = (UINT32)( _logHeader._firstLSN.offset % _fileSize ) ; } PD_LOG ( PDEVENT, "Restore dps log file[%s] succeed, " "firstLsn[%lld], idle space: %u, start offset: %d", path, getFirstLSN().offset, getIdleSize(), startOffset ) ; goto done ; } else { close () ; PD_LOG ( PDEVENT, "Restore dps log file[%s] failed[rc:%d]", path, rc ) ; goto error ; } } } if ( SDB_OK == ossAccess( path ) ) { rc = ossDelete ( path ); if ( SDB_IO == rc ) { PD_LOG ( PDERROR, "Failed to delete file at %s", path ) ; goto error; } } // open the file with "create only" and "read write" mode, for rx-r----- rc = ossOpen( path, OSS_CREATEONLY |OSS_READWRITE | OSS_SHAREWRITE, OSS_RWXU, *_file ); if ( rc ) { PD_LOG ( PDERROR, "Failed to open log file %s, rc = %d", path, rc ) ; goto error; } created = TRUE ; // increase the file size to the given size plus log file header rc = ossExtendFile( _file, (SINT64)_fileSize + DPS_LOG_HEAD_LEN ); if ( rc ) { close() ; PD_LOG ( PDERROR, "Failed to extend log file size to %d, rc = %d", size + DPS_LOG_HEAD_LEN, rc ) ; goto error; } _initHead ( DPS_INVALID_LOG_FILE_ID ) ; rc = _flushHeader () ; if ( rc ) { close () ; PD_LOG ( PDERROR, "Failed to flush header, rc = %d", rc ) ; goto error ; } // Currently let's just skip head rc = ossSeek ( _file, DPS_LOG_HEAD_LEN, OSS_SEEK_SET ) ; if ( rc ) { close() ; PD_LOG ( PDERROR, "Failed to seek to %d offset in log file, rc = %d", DPS_LOG_HEAD_LEN, rc ) ; goto error ; } done: PD_TRACE_EXITRC ( SDB__DPSLOGFILE_INIT, rc ); return rc; error: if ( NULL != _file ) { SDB_OSS_DEL _file; _file = NULL ; } if ( created ) { INT32 rcTmp = SDB_OK ; rcTmp = ossDelete( path ) ; if ( SDB_OK != rcTmp ) { PD_LOG( PDERROR, "failed to remove new file[%s], rc:%d", path, rc ) ; } } goto done; }