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

📄 udfsfind.cpp

📁 WinCE5.0部分核心源码
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// This source code is licensed under Microsoft Shared Source License
// Version 1.0 for Windows CE.
// For a copy of the license visit http://go.microsoft.com/fwlink/?LinkId=3223.
//
//+-------------------------------------------------------------------------
//
//
//  File:       udfsfind.cpp
//
//  Contents:
//
//  Classes:
//
//  Functions:
//
//--------------------------------------------------------------------------

#include "udfs.h"


//+-------------------------------------------------------------------------
//
//  Member:     CReadOnlyFileSystemDriver::FindFile
//
//  Synopsis:
//
//  Arguments:  [pFileName]  --
//              [ppFileInfo] --
//
//  Returns:
//
//  Notes:
//
//--------------------------------------------------------------------------

BOOL CReadOnlyFileSystemDriver::FindFile( LPCWSTR pFileName, PPDIRECTORY_ENTRY   ppDirEntry)
{
    LPWSTR              pParsedFileName;
    LPWSTR              pLastFileName;
    DWORD               c;
    BOOL                fRet;

    if (m_State != StateClean) {
        Clean();
    }

    //
    //  Make a local copy.  Need the 2 to double null terminate.
    //

    pLastFileName = pParsedFileName = new WCHAR[lstrlen(pFileName) + 2];

    if (pParsedFileName == NULL) {
        SetLastError(ERROR_NOT_ENOUGH_MEMORY);
        return FALSE;
    }

    //
    //  Make sure the root is read in
    //

    if (m_RootDirectoryPointer.cbSize == 0) {
        fRet = Mount();

        if (fRet == FALSE) {
            goto error;
        }
    }

    //
    //  If the file name starts with \ then move beyond it
    //

    if (pFileName[0] == '\\') {
        pFileName++;
    }

    //
    //  Convert all \ characters into nulls
    //

    for (c = 0; c < lstrlen(pFileName) + 1; c++) {
        if (pFileName[c] == '\\') {
            pParsedFileName[c] = 0;
            pLastFileName = pParsedFileName + c + 1;
        } else {
            pParsedFileName[c] = pFileName[c];
        }
    }

    pParsedFileName[c] = 0; // double null terminate.

    if (wcscmp(pLastFileName, L"*.*") == 0) {
        pLastFileName[1] = 0;
        pLastFileName[2] = 0;
    }

    if (m_RootDirectoryPointer.pCacheLocation == NULL) {
        fRet = m_pFileSystem->ReadDirectory(L"", &m_RootDirectoryPointer);

        if (fRet == FALSE) {
            goto error; 
        }
    }

    //
    //  Search for each entry recursively
    //

    fRet = FindFileRecurse(
        pParsedFileName,
        pParsedFileName,
        m_RootDirectoryPointer.pCacheLocation,
        ppDirEntry);

error:
    if (pParsedFileName) 
    		delete [] pParsedFileName;

    return fRet;
}

//+-------------------------------------------------------------------------
//
//  Member:     CReadOnlyFileSystemDriver::FindFileRecurse
//
//  Synopsis:
//
//  Arguments:  [pFileName]              --
//              [pFullPath]              --
//              [pInitialDirectoryEntry] --
//              [ppFileInfo]             --
//
//  Returns:
//
//  Notes:
//
//--------------------------------------------------------------------------

BOOL CReadOnlyFileSystemDriver::FindFileRecurse( LPWSTR pFileName, LPWSTR pFullPath, PDIRECTORY_ENTRY    pInitialDirectoryEntry, PPDIRECTORY_ENTRY   ppFileInfo)
{
    PDIRECTORY_ENTRY    pCurrentDirectoryEntry;
    BOOL                fRet;
    DWORD               ccNameLen;

    int                 lLen = lstrlen(pFileName);

    //
    //  We're actually reading the cbSize from the header,
    //  but what the heck
    //

    pCurrentDirectoryEntry = (PDIRECTORY_ENTRY) ((DWORD)pInitialDirectoryEntry + (DWORD)pInitialDirectoryEntry->cbSize);
    
    //
    //  Loop through all entries in this directory
    //

    while (TRUE) {
        DWORD   dwCharLen;

        if (IsLastDirectoryEntry(pCurrentDirectoryEntry)) {
            //
            //  If there is still more to search
            //

            if (pFileName[lLen + 1] != 0) {
                SetLastError(ERROR_PATH_NOT_FOUND);
            } else {
                SetLastError(ERROR_FILE_NOT_FOUND);
            }
            return FALSE;
        }

        if ((pCurrentDirectoryEntry->fFlags & IS_ANSI_FILENAME) == 0) {
            ccNameLen = lstrlen(pCurrentDirectoryEntry->szNameW);
            dwCharLen = 2;
        } else {
            ccNameLen = strlen(pCurrentDirectoryEntry->szNameA);
            dwCharLen = 1;
        }

        fRet = UDFSMatchesWildcard(lLen, pFileName, ccNameLen, pCurrentDirectoryEntry->szNameA ,dwCharLen);

        if (fRet == TRUE) {
            break;
        }

        //
        //  Go to the next one
        //

        pCurrentDirectoryEntry = NextDirectoryEntry(pCurrentDirectoryEntry);

    }

    if (pFileName[lLen + 1] != 0) {
        //
        //  There is still more to search, make sure this is a directory
        //

        if ((pCurrentDirectoryEntry->fFlags & FILE_ATTRIBUTE_DIRECTORY) == 0) {
            //
            //  They have asked to a path where one of the elements before
            //  the end isn't a directory.  Needless to say that doesn't
            //  work.
            //

            SetLastError(ERROR_ACCESS_DENIED);
            return FALSE;
        }

        if (pCurrentDirectoryEntry->pCacheLocation == NULL) {
            //
            //  If this directory isn't cached then read it in.
            //

            fRet = m_pFileSystem->ReadDirectory(pFullPath, pCurrentDirectoryEntry);

            if (fRet == FALSE) {
                // oops, need better error here

                SetLastError(ERROR_PATH_NOT_FOUND);
                return FALSE;
            }
        }

        //
        //  When we first came around we turned all the \'s into nulls.
        //  We turn then back so we can look up the whole path in the
        //  second arg.
        //

        pFileName[lLen] = '\\';

        return FindFileRecurse( pFileName + lLen + 1, pFullPath, pCurrentDirectoryEntry->pCacheLocation,  ppFileInfo);
    }

    *ppFileInfo = pCurrentDirectoryEntry;

    return TRUE;
}


⌨️ 快捷键说明

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