INT32 _dmsTmpBlkUnit::_removeFile() { INT32 rc = SDB_OK ; if ( _opened ) { rc = ossClose( _file ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "failed to close file:%s, rc=%d", _fullPath.c_str(), rc ) ; } rc = ossDelete( _fullPath.c_str() ) ; if ( SDB_OK != rc ) { PD_LOG( PDERROR, "failed to remove file:%s, rc=%d", _fullPath.c_str(), rc ) ; goto error ; } _fullPath.clear() ; _opened = FALSE ; } done: return rc ; error: goto done ; }
// PD_TRACE_DECLARE_FUNCTION ( SDB__DMSROUNIT_CLNUP, "_dmsReorgUnit::cleanup" ) INT32 _dmsReorgUnit::cleanup () { INT32 rc = SDB_OK ; PD_TRACE_ENTRY ( SDB__DMSROUNIT_CLNUP ); close() ; rc = ossDelete ( _fileName ) ; if ( rc ) { PD_LOG ( PDERROR, "Failed to delete reorg unit temp file, rc = %d", rc ) ; goto error ; } done : PD_TRACE_EXITRC ( SDB__DMSROUNIT_CLNUP, rc ); return rc ; error : goto done ; }
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 ; }
// 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; }