⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mcopy.c

📁 彩信MMS的全部代码
💻 C
字号:
/*
 * Copyright (C) Obigo AB, 2002-2005.
 * All rights reserved.
 *
 * This software is covered by the license agreement between
 * the end user and Obigo AB, and may be 
 * used and copied only in accordance with the terms of the 
 * said agreement.
 *
 * Obigo AB assumes no responsibility or 
 * liability for any errors or inaccuracies in this software, 
 * or any consequential, incidental or indirect damage arising
 * out of the use of the software.
 *
 */










#include "cansilib.h"   
#include "cmnconf.h"    
#include "aapifile.h"   
#include "aapicmn.h"    
#include "gmem.h"       
#include "cutils.h"     
#include "mmstypes.h"   
#include "mmsconf.h"    
#include "aapimms.h"    
#include "msig.h"       
#include "mmem.h"       
#include "mcopy.h"      






typedef struct 
{
    MmsCopyFile *request;   
    CMN_BOOL fromOpened;    
    CMN_BOOL toOpened;      
    char *buf;              
    INT32 bufSize;          
} MmsCopyData;














static void copyContinue(MmsCopyData *copy);
static void copyFinished( MmsCopyData *copy, MmsResult mmsResult);
static void copyStart(MmsCopyFile *request);
static void mmhCopyMain(MmsSignal *sig);











static void copyContinue(MmsCopyData *copy)
{
    int i;
    INT32 bytesRead;
    INT32 bytesWritten = 0;
    INT32 readSize = CMN_MIN( copy->bufSize, copy->request->from.bytes);
    MmsResult result = MMS_RESULT_OK;

    for ( i = 0; readSize > 0 && i < MMS_NR_OF_COPY_SLOTS && 
        result == MMS_RESULT_OK; i++)
    {
        bytesRead = FILEa_read( copy->request->from.category, copy->request->from.id, 
            copy->buf, copy->request->from.pos, readSize);
        if (bytesRead == -1L)
        {
            MMS_LOG_I(("%s(%d): unable to read file %lu in category %c\n", 
                __FILE__, __LINE__, copy->request->from.id, copy->request->from.category));
            result = MMS_RESULT_FILE_READ_ERROR;
        }
        else if (bytesRead > 0)
        {
            bytesWritten = FILEa_write( copy->request->to.category, 
                copy->request->to.id, copy->buf, copy->request->to.pos, bytesRead);

            if (bytesWritten < 0)
            {
                
                result = MMS_RESULT_ERROR;
                MMS_LOG_I(("%s(%d): unable to write to file id %lu in category %c\n", 
                    __FILE__, __LINE__, copy->request->to.id, copy->request->to.category));
            } 
            else if (bytesWritten < bytesRead)
            {
                
                result = MMS_RESULT_INSUFFICIENT_PERSISTENT_STORAGE;
                MMS_LOG_I(("%s(%d): unable to write to file id %lu in category %c\n", 
                    __FILE__, __LINE__, copy->request->to.id, copy->request->to.category));
            } 
        } 

        if (copy->request->from.bytes >= bytesRead)
        {
            copy->request->from.bytes -= bytesRead;
        }
        else
        {
            copy->request->from.bytes = 0;
        } 

        copy->request->from.pos += bytesRead;
        copy->request->to.pos += bytesWritten;
        readSize = CMN_MIN( copy->bufSize, copy->request->from.bytes);
    } 

    if (copy->request->from.bytes == 0 || result != MMS_RESULT_OK)
    {   
        copyFinished( copy, result);
    }
    else 
    {
        M_SIGNAL_SENDTO_P( M_FSM_MMH_COPY, MMS_SIG_MMH_COPY_CONTINUE, copy);
    } 
} 










static void copyFinished( MmsCopyData *copy, MmsResult mmsResult)
{
    if (copy->request == NULL)
    {
        MMS_LOG_I(("%s(%d): Illegal indata. Signal missing parameter.\n", 
            __FILE__, __LINE__));
        MMSa_error(MMS_RESULT_RESTART_NEEDED);
        return;
    } 

    
    if (copy->fromOpened && copy->request->from.isOpen == FALSE)
    {
        FILEa_close( copy->request->from.category, copy->request->from.id);
    } 

    
    if (copy->toOpened && copy->request->to.isOpen == FALSE)
    {
        FILEa_close( copy->request->to.category, copy->request->to.id);
    } 

    M_SIGNAL_SENDTO_IP( copy->request->sender, MMS_SIG_MMH_COPY_FINISHED, 
        mmsResult, copy->request);

    M_FREE(copy->buf);
    M_FREE(copy);
} 










static void copyStart(MmsCopyFile *request)
{
    INT32 newSize;
    MmsCopyData *copy = M_CALLOC(sizeof(MmsCopyData));
    MmsResult result = MMS_RESULT_OK;

    copy->buf = NULL;
    copy->request = request;

     
    if (request->from.bytes == MMH_COPY_ENTIRE_FILE)   
    {
        request->from.bytes = FILEa_getSize(request->from.category, request->from.id);
        
        if (request->from.bytes > -1 && request->from.bytes > request->from.pos)
        {   
            request->from.bytes -= request->from.pos;
        }
        else
        {
            MMS_LOG_I(("%s(%d): 'from' file size error: bytes=%d, pos=%d\n", 
                __FILE__, __LINE__, request->from.bytes, request->from.pos));
            result = MMS_RESULT_FILE_READ_ERROR;
        } 
    }  

    if (result != MMS_RESULT_OK)
    {
        
    }
    else 
    {
        copy->bufSize = CMN_MIN( request->from.bytes, (INT32)MMS_MAX_CHUNK_SIZE);
        copy->buf = M_CALLOC((unsigned)copy->bufSize);
        if (request->from.isOpen == FALSE)
        {
            if (FILEa_open( request->from.category, request->from.id, 
                FILE_OPTION_READ) == 0)
            {
                copy->fromOpened = TRUE;
            }
            else
            {
                MMS_LOG_I(("%s(%d): Couldn't open 'from' file %ld in category %c\n", 
                    __FILE__, __LINE__, request->from.id, request->from.category));
                result = MMS_RESULT_FILE_NOT_FOUND;
            } 
        } 

        if (result != MMS_RESULT_OK)
        {
            
        }
        else if (request->to.shouldCreate)
        {
            request->to.pos = 0;
            if ( FILEa_create( request->to.category, NULL, &request->to.id) == 0)
            {
                copy->toOpened = TRUE;
            }
            else
            {
                MMS_LOG_I(("%s(%d): Couldn't create file for %d\n", 
                    __FILE__, __LINE__, (int)request->sender));
                result = MMS_RESULT_INSUFFICIENT_PERSISTENT_STORAGE;
            }  
        }
        else if (request->to.isOpen == FALSE)
        {
            if (FILEa_open( request->to.category, request->to.id, 
                FILE_OPTION_WRITE) == 0)
            {
                copy->toOpened = TRUE;
            }
            else
            {
                MMS_LOG_I(("%s(%d): Couldn't open 'to' file %ld in category %c\n", 
                    __FILE__, __LINE__, request->to.id, request->to.category));
                result = MMS_RESULT_FILE_NOT_FOUND;
            } 
        } 
        
        if (result != MMS_RESULT_OK)
        {
            
        }
        else if (request->to.pos == MMH_COPY_APPEND) 
        {
            request->to.pos = FILEa_getSize(request->to.category, request->to.id);
            if (request->to.pos == -1)
            {
                MMS_LOG_I(("%s(%d): Unable to get size of 'to' file %ld in category %c\n", 
                    __FILE__, __LINE__, request->to.id, request->to.category));
                request->to.pos = 0;
                result = MMS_RESULT_FILE_READ_ERROR;
            } 
        }  

        
        newSize = request->from.bytes + request->to.pos;
        if (result != MMS_RESULT_OK)
        {
            
        }
        else if (newSize > 0)
        {
            if (FILEa_setSize(request->to.category,request->to.id, newSize) != newSize)
            {
                MMS_LOG_I(("%s(%d): Not enough persistent storage, newSize=%d\n", 
                    __FILE__, __LINE__, newSize));
                result = MMS_RESULT_INSUFFICIENT_PERSISTENT_STORAGE;
            } 
        }  
    } 

    if (result != MMS_RESULT_OK)
    {
        copyFinished( copy, result);
    }
    else
    {
        M_SIGNAL_SENDTO_P( M_FSM_MMH_COPY, MMS_SIG_MMH_COPY_CONTINUE, copy);
    } 
} 




void mmhCopyInit(void)
{
    mSignalRegisterDst(M_FSM_MMH_COPY, mmhCopyMain);
    
    MMS_LOG_I(("MMS FSM MMH COPY: initialized\n"));
} 






static void mmhCopyMain(MmsSignal *sig)
{
    switch (sig->type)
    {
    case MMS_SIG_MMH_COPY_START :
        MMS_LOG_I(("MMS FSM MMH COPY: MMS_SIG_MMH_COPY_START\n"));

        copyStart((MmsCopyFile *)sig->p_param);
        break;
    case MMS_SIG_MMH_COPY_CONTINUE :
        MMS_LOG_I(("MMS FSM MMH COPY: MMS_SIG_MMH_COPY_CONTINUE\n"));

        copyContinue((MmsCopyData *)sig->p_param);
        break;
    default :
        MMS_LOG_I(("MMS FSM MMH COPY: Unknown sig %d\n", sig->type));
        break;
    } 

    mSignalDelete(sig);
} 




void mmhCopyTerminate(void)
{
    mSignalDeregister(M_FSM_MMH_COPY);
    
    MMS_LOG_I(("MMS FSM MMH COPY: terminated\n"));
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -