fssim_core.c

来自「最新MTK手机软件源码」· C语言 代码 · 共 1,826 行 · 第 1/5 页

C
1,826
字号
    }
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_get_short_name                                             */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is used to get short file name from a given path.  */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      src   -   source path                                            */
/*      dst   -   destination buffer                                     */
/*      len   -   length of dst                                          */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      Number of bytes transfered                                       */
/*                                                                       */
/*************************************************************************/
static int fssim_get_short_name(const TCHAR *src, TCHAR *dst, DWORD len)
{
    DWORD retval;
    TCHAR strbuf[MAX_PATH], *pch, *pchOld;

    ASSERT(src != NULL && dst != NULL);

    retval = GetShortPathName(src, strbuf, MAX_PATH);

    if (retval == 0) {

#ifdef DEBUG_FSSIM

        fssim_printf(("GetShortPathName() failed in fssim_get_short_name()\n"));
        fssim_printf(("error code = %d\n", GetLastError()));

#endif  /* DEBUG_FSSIM */

        return 0;
    }

    if (!wcscmp(strbuf + wcslen(strbuf) - 1, L"\\")) {

        /* the last character is "\\" */

        /* remove it */

        wcsncpy(strbuf + wcslen(strbuf) - 1, L"\0", 1);
    }

    /* extract last name from the path */

    pch = wcsstr(strbuf, L"\\");

    pchOld = pch;

    while (pch != NULL) {

        pchOld = pch;

        pch = wcsstr(pch + 1, L"\\");
    }

    pch = (pchOld == NULL)? strbuf: pchOld + 1;

    if (len < wcslen(pch)) {

        wcsncpy(dst, pch, len);

        dst[ len ] = '\0';

        return len;
    } else {

        wcsncpy(dst, pch, wcslen(pch));

        dst[ wcslen(pch) ] = '\0';

        return wcslen(pch);
    }
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_isopen                                                     */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is used to check whether is file is opened or not. */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      filename  -  file name of the file to check                      */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      Non-zero if it is opened; zero if it is not opened               */
/*                                                                       */
/*************************************************************************/
static int fssim_isopen(const TCHAR *filename)
{
    int i, result = 0;

    if (fssim_mutex == NULL) {
        fssim_init();
    }

    fssim_take_mutex(fssim_mutex);

    for (i = 1; i <= FSSIM_MAX_FILE; i++) {

        if (fssim_file[i].state == STATE_INUSE) {

            if (!_wcsicmp(fssim_file[i].filename, filename)) {

                result = 1;

                break;
            }
        }
    }

    fssim_give_mutex(fssim_mutex);

    return result;
}

/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_progress                                                   */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is the callback routine for move/copy operation.   */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*                                                                       */
/*************************************************************************/
static DWORD CALLBACK fssim_progress(
  LARGE_INTEGER TotalFileSize,          // file size
  LARGE_INTEGER TotalBytesTransferred,  // bytes transferred
  LARGE_INTEGER StreamSize,             // bytes in stream
  LARGE_INTEGER StreamBytesTransferred, // bytes transferred for stream
  DWORD dwStreamNumber,                 // current stream
  DWORD dwCallbackReason,               // callback reason
  HANDLE hSourceFile,                   // handle to source file
  HANDLE hDestinationFile,              // handle to destination file
  LPVOID lpData                         // from CopyFileEx
)
{
    int fh = (int)lpData;

    if (fssim_file[fh].isabort == 1)

        return PROGRESS_STOP;

    if (fh >= 0 && fssim_file[fh].copyprogress != NULL)

        fssim_file[fh].copyprogress(FS_MOVE_PGS_ING, (DWORD)TotalFileSize.QuadPart, (DWORD)TotalBytesTransferred.QuadPart, fh);

    return PROGRESS_CONTINUE;
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_conv_errcode                                               */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is used to convert WIN32 error code to FS error    */
/*      code.                                                            */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      err   -   WIN32 error code                                       */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      FS error code                                                    */
/*                                                                       */
/*************************************************************************/
static int fssim_conv_errcode(DWORD err, char *msg)
{
    int ret_err;

    switch (err) {

    case ERROR_INVALID_NAME:

        /* the name is not valid */

        ret_err = FS_INVALID_FILENAME;

        break;

    case ERROR_FILE_NOT_FOUND:

        /* the file doesn't exist */

        ret_err = FS_FILE_NOT_FOUND;

        break;

    case ERROR_SHARING_VIOLATION:

        ret_err = FS_ACCESS_DENIED;

        break;

    case ERROR_ACCESS_DENIED:

        ret_err = FS_ACCESS_DENIED;

        break;

    case ERROR_REQUEST_ABORTED:

        ret_err = FS_ABORTED_ERROR;

        break;

    case ERROR_PATH_NOT_FOUND:

        ret_err = FS_PATH_NOT_FOUND;

        break;

    default:

        /* un-handled error */

#ifdef DEBUG_FSSIM

        fssim_printf((msg));

        fssim_printf(("error code = %d\n", err));

#endif  /* DEBUG_FSSIM */

        ret_err = FS_NO_ERROR;

        break;
    }

    return ret_err;
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_is_file_exist                                              */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is used to check whether the file exists or not.   */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      filename   -   file name to check                                */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      1 if the file exists; 0 if the file doesn't exist                */
/*                                                                       */
/*************************************************************************/
static int fssim_is_file_exist(TCHAR *filename)
{
    DWORD error;

    SetLastError(ERROR_SUCCESS);

    GetFileAttributes(filename);

    error = GetLastError();

    if (error == ERROR_PATH_NOT_FOUND || error == ERROR_FILE_NOT_FOUND)

        return 0;

    else

        return 1;
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_is_name_valid                                              */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is used to check whether the file is valid or not. */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      filename   -   file name to check                                */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      1 if the file name is valid; 0 if the file is not valid          */
/*                                                                       */
/*************************************************************************/
static int fssim_is_name_valid(TCHAR *filename)
{
    SetLastError(ERROR_SUCCESS);

    GetFileAttributes(filename);

    if (GetLastError() == ERROR_INVALID_NAME)

        return 0;

    else

        return 1;
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_search_dir                                                 */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is used to search a directory and to invoke        */
/*      the callback function for each found file/directory.             */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      dirname    -   name of the directory to search                   */
/*      flag       -   flag                                              */
/*      callback   -   callback function                                 */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      number of found find/directory                                   */
/*                                                                       */
/*************************************************************************/
static int fssim_search_dir(TCHAR *dirname, DWORD flag, fssim_search_callbak callback, void *param)
{
    WIN32_FIND_DATA data;
    HANDLE hSearch;
    int is_finish, cnt, retval;

⌨️ 快捷键说明

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