fssim_core.c

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

C
1,826
字号
    TCHAR strbuf[MAX_PATH], strbuf2[MAX_PATH];
    DWORD error;

    ASSERT(dirname != NULL);

    __try {

        wcscpy(strbuf, dirname);
        wcscpy(strbuf + wcslen(strbuf), L"\\*");

        hSearch = FindFirstFile(strbuf, &data);

        if (hSearch == INVALID_HANDLE_VALUE) {

#ifdef DEBUG_FSSIM

            fssim_printf(("FindFirstFile() failed in fssim_search_dir()\n"));

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

#endif  /* DEBUG_FSSIM */

            return 0;
        }

        is_finish = 0;

        cnt = 0;    /* cnt is used to count the number of found file/directory */

        while (is_finish == 0) {

            if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {

                if(wcscmp(data.cFileName, L".") == 0 || wcscmp(data.cFileName, L"..") == 0) {

                    /* nop */

                } else {

                    /* find a directory */

                    if ((flag & FS_DIR_TYPE) != 0) {

                        /* check if filtering out system/hidden attribute */
                        if (((data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) &&
                             (flag & FS_FILTER_SYSTEM_ATTR))
                           ||
                            ((data.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) &&
                             (flag & FS_FILTER_HIDDEN_ATTR))
                           )
                        {
                           /* do nothing */

                        } else {

                            /* increase the counter */

                            cnt++;
                        }
                    }

                    /* copy the full path of the found directory to strbuf2 */

                    wcscpy(strbuf2, dirname);

                    if( strbuf2[wcslen(strbuf2) - 1] != '\\' )
                        wcscpy(strbuf2 + wcslen(strbuf2), L"\\");

                    wcscpy(strbuf2 + wcslen(strbuf2), data.cFileName);

                    if (callback != NULL) {

                        /* invoke the callback function */

                        retval = callback(strbuf2, data.cFileName, param, FSSIM_BEFORE_RECURSIVE);
                        
                        if (retval != 0) {

                            /* there is an error */

                            /* abort */

                            return retval;
                        }
                    }

                    if ((flag & FS_RECURSIVE_TYPE) != 0) {

                        /* recursive searching */

                        cnt += fssim_search_dir(strbuf2, flag, callback, param);

                    }

                    if (callback != NULL) {

                        /* invoke the callback function */

                        retval = callback(strbuf2, data.cFileName, param, FSSIM_AFTER_RECURSIVE);
                    
                        if (retval != 0) {

                            /* there is an error */

                            /* abort */

                            return retval;
                        }
                    }
                }

            } else {

                /* find a file */

                if ((flag & FS_FILE_TYPE) != 0) {

                    /* check if filtering out system attribute */
                    if (flag & FS_FILTER_SYSTEM_ATTR) {

                        if (!(data.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM))

                            /* increase the counter */

                            cnt++;

                    } else {

                        /* increase the counter */

                        cnt++;
                    }
                }

                if (callback != NULL) {

                    /* copy the full path of the found file to strbuf2 */

                    wcscpy(strbuf2, dirname);

                    if( strbuf2[wcslen(strbuf2) - 1] != '\\' )
                        wcscpy(strbuf2 + wcslen(strbuf2), L"\\");

                    wcscpy(strbuf2 + wcslen(strbuf2), data.cFileName);

                    /* invoke the callback function */

                    retval = callback(strbuf2, data.cFileName, param, FSSIM_BEFORE_RECURSIVE);
                
                    if (retval != 0) {

                        /* there is an error */

                        /* abort */

                        return retval;
                    }
                }

            }

            if (!FindNextFile(hSearch, &data)) {

                error = GetLastError();

                if (error == ERROR_NO_MORE_FILES) {

                    /* no more file */

                    is_finish = 1;

                } else {

#ifdef DEBUG_FSSIM

                    fssim_printf(("FindNextFile() failed in fssim_search_dir()\n"));

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

#endif  /* DEBUG_FSSIM */
                }
            }

        }   /* while () */

    }
    __finally {

        if (!FindClose(hSearch)) {

#ifdef DEBUG_FSSIM

            fssim_printf(("FindClose() failed in fssim_search_dir()\n"));

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

            return 0;

#endif  /* DEBUG_FSSIM */
        }
    }

    return cnt;
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_copy_dir                                                   */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is a callback function of fssim_search_dir and is  */
/*      used to copy found file and found directory                      */
/*                                                                       */
/* INPUTS                                                                */
/*                                                                       */
/*      fullname   -   full name of found file/directory                 */
/*      filename   -   file name of found file/directory                 */
/*      param      -   pointer to FSSIM_COPYDIR_PARAM_T                  */
/*      order      -   FSSIM_BEFORE_RECURSIVE or FSSIM_AFTER_RECURSIVE   */
/*                                                                       */
/* OUTPUTS                                                               */
/*                                                                       */
/*      zero for success; non-zero for failure                           */
/*                                                                       */
/*************************************************************************/
int fssim_copy_dir(TCHAR *fullname, TCHAR *filename, void *param, int order)
{
    FSSIM_COPYDIR_PARAM_T *copydir;
    DWORD attributes;
    TCHAR srcbuf[MAX_PATH], dstbuf[MAX_PATH], *pch, *pchTmp;

    ASSERT(fullname != NULL && filename != NULL && param != NULL);

    copydir = (FSSIM_COPYDIR_PARAM_T *)param;

    if (fssim_file[copydir->fh].isabort == 1) {

        /* the copy is aborted */

        return FS_ABORTED_ERROR;
    }

    attributes = GetFileAttributes(fullname);

    if (attributes & FILE_ATTRIBUTE_DIRECTORY) {

        /* a directory is found in fssim_search_dir */

        if (order == FSSIM_BEFORE_RECURSIVE) {

            /* create the directory */

            wcscpy(dstbuf, copydir->path);

            wcscpy(dstbuf + wcslen(dstbuf), L"\\");

            wcscpy(dstbuf + wcslen(dstbuf), filename);

            if (!CreateDirectory(dstbuf, NULL)) {

#ifdef DEBUG_FSSIM

                if (GetLastError() != ERROR_ALREADY_EXISTS) {

                    fssim_printf(("CreateDirectory() failed in fssim_copy_dir()\n"));

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

#endif  /* DEBUG_FSSIM */
            }

            /*
             * NoteXXX: fssim_search_dir will recursivelly search directories.
             *          We need to update copydir->path because we will copy file
             *          under the new found direcotry.
             */

            /* append created directory name after copydir->path */

            wcscpy(copydir->path + wcslen(copydir->path), L"\\");

            wcscpy(copydir->path + wcslen(copydir->path), filename);

        } else if (order == FSSIM_AFTER_RECURSIVE) {

            /*
             * NoteXXX: After recursive searching is done,
             *          we need to restore copydir->path.
             */

            /* find the last occurrence of filename in copydir->path */

            pch = wcsstr(copydir->path, filename);

            do {

                pchTmp = pch;

                pch = wcsstr(pch + wcslen(filename), filename);

            } while (pch != NULL);

            pchTmp[-1] = 0;

        } else

            ASSERT(0);

    } else {

        /* a file is found in fssim_search_dir */

        /* copy the found file */

        wcscpy(srcbuf, fullname);

        wcscpy(dstbuf, copydir->path);

        wcscpy(dstbuf + wcslen(dstbuf), L"\\");

        wcscpy(dstbuf + wcslen(dstbuf), filename);

        if (!CopyFileEx(srcbuf, dstbuf, NULL, (LPVOID)copydir->fh, NULL, 0)) {

#ifdef DEBUG_FSSIM

            fssim_printf(("CopyFile() failed in fssim_copy_dir()\n"));

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

#endif  /* DEBUG_FSSIM */
        }
        
    }

    if (order == FSSIM_BEFORE_RECURSIVE && fssim_file[copydir->fh].copyprogress != NULL){

        /* increase the complete counter */

        copydir->completed++;

        /* invoke the progress callback function */

        fssim_file[copydir->fh].copyprogress(FS_MOVE_PGS_ING, copydir->total, copydir->completed, copydir->fh);

    }

    return 0;
}


/*************************************************************************/
/*                                                                       */
/* FUNCTION                                                              */
/*                                                                       */
/*      fssim_del_dir                                                    */
/*                                                                       */
/* DESCRIPTION                                                           */
/*                                                                       */
/*      This function is a callback function of fssim_search_dir and is  */
/*      used to delete found file and found directory                    */

⌨️ 快捷键说明

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