Ejemplo n.º 1
0
bool
RenameFileAsBackup( const CORE::CString& filepath )
{GUCEF_TRACE;

    CORE::CString newFilepath = filepath + ".backup";     
    if ( 0 != CORE::Move_File( newFilepath.C_String() , 
                               filepath.C_String()    ) )
    {
        GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "File \"" + filepath + "\" has been renamed to \"" + newFilepath + "\"" );
        return true;
    }
    else
    {
        GUCEF_ERROR_LOG( CORE::LOGLEVEL_NORMAL, "Failed to rename file \"" + filepath + "\" to \"" + newFilepath + "\"" );
        return false;
    }
}
void 
Win32EnumPorts( std::list<Int32>& portlist )
{
	portlist.clear();

	// use querydosdevice on NT/2000/xp (no need to open ports...)
	if ( VER_PLATFORM_WIN32_NT == Win32GetOSVersion() )
	{
		const int bufferSize = 65535;        
        CORE::CDynamicBuffer deviceNameBuffer( bufferSize, true );

        // Read the list of devices into the buffer
		Int32 len = ::QueryDosDevice( 0, deviceNameBuffer.AsTypePtr< char >(), bufferSize );
		for ( Int32 n=0; n<len; ++n )
		{
			// if found "COM", then add number to list
			if ( 0 == _stricmp( deviceNameBuffer.AsConstTypePtr< char >( n ), "COM" ) )
            {
                portlist.push_back( atoi( deviceNameBuffer.AsConstTypePtr< char >( n+3 ) ) );
            }

			// find next null pointer
			while ( deviceNameBuffer.AsConstType< char >( n ) != '\0' )
            {
				++n;
            }
		}
	}
	
	// else, open port 1-255
	else
	{
		CORE::CString basePath( "\\\\.\\COM" );
        for ( Int32 i=1; i<255; ++i )
		{
			CORE::CString portPath = basePath + CORE::Int32ToString( i );

			// try to open port
			bool result = false;			
			::HANDLE handle = ::CreateFile( portPath.C_String(), GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 );
			
			if ( INVALID_HANDLE_VALUE == handle ) continue;
			::CloseHandle( handle );
  		
			portlist.push_back( i );
		}
	}
}
Ejemplo n.º 3
0
bool
DeleteFileBackup( const CORE::CString& filepath )
{GUCEF_TRACE;

    CORE::CString newFilepath = filepath + ".backup";
    if ( CORE::FileExists( newFilepath ) )
    {
        if ( 0 != CORE::Delete_File( newFilepath.C_String() ) )
        {
            GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "File \"" + newFilepath + "\" has been deleted" );
            return true;
        }
        else
        {
            GUCEF_ERROR_LOG( CORE::LOGLEVEL_NORMAL, "Failed to delete file \"" + newFilepath + "\"" );
            return false;
        }
    }
    return true;
}
bool
CLogSvcClient::OnTaskCycleLog( const TLogMsgType logMsgType    ,
                               const CORE::Int32 logLevel      ,
                               const CORE::CString& logMessage ,
                               const CORE::UInt32 threadId     )
{GUCEF_TRACE;

    CORE::Int16 logMsgTypeValue = logMsgType;
    CORE::Int8 msgHeader[ 16 ];  // 16 = 1+4+1+2+4+4
    CORE::UInt32 logMsgLength = logMessage.Length() + 11;

    msgHeader[ 0 ] = (CORE::Int8) LOGSVCMSGTYPE_DELIMITER;   // set delimiter for message: 1 byte
    memcpy( msgHeader+1, &logMsgLength, 4 );                 // set the total message length : 4 bytes
    msgHeader[ 5 ] = (CORE::Int8) LOGSVCMSGTYPE_LOGMSG;      // set TCP msg type: 1 byte
    memcpy( msgHeader+6, &logMsgTypeValue, 2 );              // set log msg type: 2 bytes
    memcpy( msgHeader+8, &logLevel, 4 );                     // set log level: 4 bytes
    memcpy( msgHeader+12, &threadId, 4 );                    // set thread id: 4 bytes

    if ( m_connectionInitialized )
    {
        // Send the logging msg header
        if ( m_tcpClient.Send( msgHeader, 16 ) )
        {
            // Now send the logging text
            m_tcpClient.Send( logMessage.C_String() ,
                              logMessage.Length()   );
        }
    }
    else
    {
        // The logging connection is not initialized yet
        // queue the msg until the connection is initialized

        TLogMessage queueItem;
        memcpy( queueItem.msgHeader, msgHeader, 16 );
        queueItem.logMsg = logMessage;

        m_logQueue.push_back( queueItem );
    }
    return true;
}
Ejemplo n.º 5
0
void
MirrorDirsAndFiles( const CORE::CString& srcDir    ,
                    const CORE::CString& dstDir    ,
                    bool mirrorExistingFilesOnly   ,
                    const TStringSet* fileTypes    ,
                    const TStringSet* dirsToIgnore )
{GUCEF_TRACE;

    if ( mirrorExistingFilesOnly )    
    {
        // We have a seperare function for mirroring existing files only
        // It is more efficient since it can use the destination as the template
        // avoiding unnessary operations
        MirrorExistingDirsAndFiles( srcDir       ,
                                    dstDir       ,
                                    fileTypes    ,
                                    dirsToIgnore );
        return;
    }

    // Get a list of all source files
    TFileEntryVector files;
    BuildFileList( srcDir       ,
                   files        ,
                   fileTypes    ,
                   dirsToIgnore );

    // Iterate the list of files and make sure we mirror all of them
    TFileEntryVector::iterator i = files.begin();
    while ( i != files.end() )
    {
        TFileEntry& fileEntry = (*i);

        CORE::CString srcFilePath = srcDir;
        CORE::AppendToPath( srcFilePath, fileEntry.filedir );
        CORE::AppendToPath( srcFilePath, fileEntry.filename );
        
        CORE::CString destFilePath = dstDir;
        CORE::AppendToPath( destFilePath, fileEntry.filedir );
        
        // Make sure destination directory exists
        if ( CORE::CreateDirs( destFilePath ) )
        {
            CORE::AppendToPath( destFilePath, fileEntry.filename );
            
            bool fileExisted = CORE::FileExists( destFilePath );                        
            if ( fileExisted )
            {
                if ( !RenameFileAsBackup( destFilePath ) )
                {
                    GUCEF_ERROR_LOG( CORE::LOGLEVEL_NORMAL, "Unable to rename existing destination file: " + destFilePath );
                    ++i;
                    continue;
                }
                GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Successfully renamed destination file \"" + destFilePath + "\"" );
            }
                        
            if ( 0 != CORE::Copy_File( destFilePath.C_String()  , 
                                       srcFilePath.C_String() ) )
            {
                GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Successfully copied file from \"" + srcFilePath + "\" to \"" + destFilePath + "\"" );
                if ( fileExisted )
                {
                    if ( DeleteFileBackup( destFilePath ) )
                    {
                        GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Successfully deleted backup file after successfull copy for file \"" + destFilePath + "\"" );
                    }
                }
            }
            else
            {
                GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Failed to copy file from \"" + srcFilePath + "\" to \"" + destFilePath + "\"" );
                if ( fileExisted )
                {
                    if ( !UndoRenameFileAsBackup( destFilePath ) )
                    {
                        GUCEF_ERROR_LOG( CORE::LOGLEVEL_NORMAL, "Failed to undo renaming of file: " + destFilePath );
                    }
                }
            }
        }
        else
        {
            GUCEF_ERROR_LOG( CORE::LOGLEVEL_NORMAL, "Unable to create directory: " + destFilePath );
        }
        ++i;
    }
}
Ejemplo n.º 6
0
void
MirrorExistingDirsAndFiles( const CORE::CString& srcDir    ,
                            const CORE::CString& dstDir    ,
                            const TStringSet* fileTypes    ,
                            const TStringSet* dirsToIgnore )
{
    // Get a list of all destination files
    TFileEntryVector files;
    BuildFileList( dstDir       ,
                   files        ,
                   fileTypes    ,
                   dirsToIgnore );

    // Iterate the list of files and make sure we mirror all of them
    TFileEntryVector::iterator i = files.begin();
    while ( i != files.end() )
    {
        TFileEntry& fileEntry = (*i);

        CORE::CString srcFilePath = srcDir;
        CORE::AppendToPath( srcFilePath, fileEntry.filedir );
        CORE::AppendToPath( srcFilePath, fileEntry.filename );
        
        CORE::CString destFilePath = dstDir;
        CORE::AppendToPath( destFilePath, fileEntry.filedir );
        CORE::AppendToPath( destFilePath, fileEntry.filename );
        
        if ( !CORE::FileExists( srcFilePath ) )
        {
            // Skip this one, we are configured to only mirror files for which 
            // a target file already exists
            GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Skipping file \"" + destFilePath + "\" because there is no source file available" );
            ++i;
            continue;
        }

        if ( !RenameFileAsBackup( destFilePath ) )
        {
            GUCEF_ERROR_LOG( CORE::LOGLEVEL_NORMAL, "Unable to rename existing destination file: " + destFilePath );
            ++i;
            continue;
        }
            
        GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Successfully renamed destination file \"" + destFilePath + "\"" );
            
        if ( 0 != CORE::Copy_File( destFilePath.C_String()  , 
                                   srcFilePath.C_String() ) )
        {
            GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Successfully copied file from \"" + srcFilePath + "\" to \"" + destFilePath + "\"" ); 
            if ( DeleteFileBackup( destFilePath ) )
            {
                GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Successfully deleted backup file after successfull copy for file \"" + destFilePath + "\"" );
            }
        }
        else
        {
            GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Failed to copy file from \"" + srcFilePath + "\" to \"" + destFilePath + "\"" );
            if ( !UndoRenameFileAsBackup( destFilePath ) )
            {
                GUCEF_ERROR_LOG( CORE::LOGLEVEL_NORMAL, "Failed to undo renaming of file: " + destFilePath );
            }
        }
        ++i;
    }
}
Ejemplo n.º 7
0
void
CopyOverMatchedFiles( const CORE::CString& srcDirRoot  ,
                      const CORE::CString& destDirRoot ,
                      TMatchEntryVector& matches       )
{GUCEF_TRACE;

    TMatchEntryVector::iterator i = matches.begin();
    while ( i != matches.end() )
    {
        TMatchEntry& matchEntry = (*i);
        
        CORE::CString sourceFilePath = srcDirRoot;
        CORE::AppendToPath( sourceFilePath, matchEntry.source.filedir );
        CORE::AppendToPath( sourceFilePath, matchEntry.source.filename );
        
        TFileEntryVector& destinations = matchEntry.destinations;

        if ( destinations.empty() )
        {
            GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "File \"" + sourceFilePath + "\" cannot be copied since it doesnt have any destination locations" );
        }
        else
        { 
            TFileEntryVector::iterator n = destinations.begin();
            while ( n != destinations.end() )
            {
                TFileEntry& destEntry = (*n);
                
                CORE::CString destFilePath = destDirRoot;
                CORE::AppendToPath( destFilePath, destEntry.filedir );
                CORE::AppendToPath( destFilePath, destEntry.filename );
                
                // Delete whatever backup file might have gotten left behind from a previous run                
                DeleteFileBackup( destFilePath );
                
                GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Preparing to copy file from \"" + sourceFilePath + "\" to \"" + destFilePath + "\"" );            
                GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Backing up destination file \"" + destFilePath + "\"" ); 
                if ( RenameFileAsBackup( destFilePath ) )
                {
                    GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Successfully renamed destination file \"" + destFilePath + "\"" );
                    
                    if ( 0 != CORE::Copy_File( destFilePath.C_String()   , 
                                               sourceFilePath.C_String() ) )
                    {
                        GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Successfully copied file from \"" + sourceFilePath + "\" to \"" + destFilePath + "\"" );
                        DeleteFileBackup( destFilePath );
                    }
                    else
                    {
                        GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Failed to copy file from \"" + sourceFilePath + "\" to \"" + destFilePath + "\"" );
                        UndoRenameFileAsBackup( destFilePath );
                    }
                }
                else
                {
                    GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Failed to delete destination file \"" + destFilePath + "\"" );
                }
                
                ++n;
            }
        }        
        ++i;
    }
}
Ejemplo n.º 8
0
void
BuildFileList( const CORE::CString& srcDir         ,
               const CORE::CString& relativeSrcDir ,
               TFileEntryVector& files             ,
               const TStringSet* fileTypes         ,
               const TStringSet* dirsToIgnore      )
{GUCEF_TRACE;

    struct CORE::SDI_Data* dirEntry = CORE::DI_First_Dir_Entry( srcDir.C_String() );
    if ( dirEntry != NULL )    
    {
        do 
        {
            CORE::CString entryName = CORE::DI_Name( dirEntry );
            if ( ( entryName != "." )  &&
                 ( entryName != ".." )  )
            {
                // Check if this is a dir we have to ignore
                if ( 0 == CORE::DI_Is_It_A_File( dirEntry ) )
                {
                    if ( NULL != dirsToIgnore )
                    {
                        if ( dirsToIgnore->find( entryName.Lowercase() ) != dirsToIgnore->end() )
                        {
                            // do not process further
                            CORE::CString ignoredDir = srcDir;
                            CORE::AppendToPath( ignoredDir, entryName );
                            GUCEF_LOG( CORE::LOGLEVEL_NORMAL, "Ignoring directory \"" + ignoredDir + "\"" );
                            continue;
                        }
                    }
                }
                
                if ( 0 != CORE::DI_Is_It_A_File( dirEntry ) )
                {
                    if ( NULL != fileTypes )
                    {
                        CORE::CString fileExt = CORE::Extract_File_Ext( entryName.C_String() );
                        fileExt = fileExt.Lowercase();
                        if ( fileTypes->find( fileExt ) == fileTypes->end() )
                        {
                            // skip this entry as instructed, not a matching filetype
                            continue;
                        }
                    }
                    
                    TFileEntry fileEntry;
                    fileEntry.filedir = relativeSrcDir;
                    fileEntry.filename = entryName;
                    
                    files.push_back( fileEntry );
                }
                else
                {
                    // We found a sub-dir, process it
                    CORE::CString subDirPath = srcDir;
                    CORE::AppendToPath( subDirPath, entryName );
                    
                    CORE::CString subRelSrcDir = relativeSrcDir;
                    CORE::AppendToPath( subRelSrcDir, entryName );
                    
                    BuildFileList( subDirPath   ,
                                   subRelSrcDir ,
                                   files        ,
                                   fileTypes    ,
                                   dirsToIgnore );
                }
            }            
        } 
        while ( CORE::DI_Next_Dir_Entry( dirEntry ) != 0 );
        
        // clean up our toys
        CORE::DI_Cleanup( dirEntry );
    }
}