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

📄 store.cpp

📁 WinCE5.0部分核心源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//
// 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.
//
#include <fsdmgrp.h>
#include <storemain.h>
#include <strsafe.h>


DWORD  g_dwBlockDevCount = 0;


CStore::CStore(TCHAR *szDeviceName, GUID DeviceGuid) :
        m_pNextStore(NULL),
        m_dwStoreId(NULL),
        m_dwFlags(0),
        m_pPartDriver(NULL),
        m_pPartitionList(NULL),
        m_dwPartCount(0),
        m_dwMountCount(0),
        m_pRootHandle(NULL),
        m_hActivityEvent(NULL),
        m_dwMountFlags(0),
        m_hDisk(NULL),
        m_pStorageId(NULL),
        m_pBlockDevice(NULL),
        m_dwRefCount(0),
        m_pDskStore(NULL)
{
    memset( &m_si, 0, sizeof(PD_STOREINFO));
    memset( &m_sdi, 0, sizeof(STORAGEDEVICEINFO));
    memcpy( &m_DeviceGuid, &DeviceGuid, sizeof(GUID));
    wcscpy( m_szPartDriver, L"");
    wcscpy( m_szPartDriverName, L"");
    wcsncpy( m_szDeviceName, szDeviceName, DEVICENAMESIZE-1);
    m_szDeviceName[DEVICENAMESIZE-1] = 0;
    wcscpy( m_szStoreName, g_szDEFAULT_STORAGE_NAME);
    wcscpy( m_szDefaultFileSystem, g_szDEFAULT_FILESYSTEM);
    wcscpy( m_szRootRegKey, L"");
    wcscpy( m_szOldDeviceName, L"");
    wcscpy( m_szFolderName, g_szDEFAULT_FOLDER_NAME);
    wcscpy( m_szActivityName, L"");
    InitializeCriticalSection( &m_cs);
   
// TODO: Read this from the registry
    m_dwFlags = 0;
}


CStore::~CStore()
{
   DEBUGMSG( 1, (L"CStore Destructor(%08X)\r\n", this));
// Delete the Partitions
// Delete the Partition Driver
    if (m_pPartDriver) {
        if (m_dwStoreId)
            m_pPartDriver->CloseStore( m_dwStoreId);
        delete m_pPartDriver;
    }   
    if (m_pStorageId) {
        delete [] m_pStorageId;
    }    
    if (m_hDisk) {
        CloseHandle( m_hDisk);
    }    
    if (m_pBlockDevice) {
        delete m_pBlockDevice;
    }   
    if (m_hActivityEvent) {
        CloseHandle( m_hActivityEvent);
    }   
    if (m_pDskStore) {
        delete m_pDskStore;
    }
    DeleteCriticalSection(&m_cs);
}


BOOL CStore::GetStoreInfo(STOREINFO *pInfo)
{
    __try {
        pInfo->dwAttributes = m_si.dwAttributes;
        pInfo->snBiggestPartCreatable = m_si.snBiggestPartCreatable;
        pInfo->dwBytesPerSector = m_si.dwBytesPerSector;
        pInfo->snFreeSectors = m_si.snFreeSectors;
        pInfo->snNumSectors = m_si.snNumSectors;
        pInfo->dwPartitionCount = m_dwPartCount;
        pInfo->dwMountCount = m_dwMountCount;
        wcscpy( pInfo->szDeviceName, m_szDeviceName);
        wcscpy( pInfo->szStoreName, m_szStoreName);
        memcpy( &pInfo->ftCreated, &m_si.ftCreated, sizeof(FILETIME));
        memcpy( &pInfo->ftLastModified, &m_si.ftLastModified, sizeof(FILETIME));
        memcpy( &pInfo->sdi, &m_sdi, sizeof(STORAGEDEVICEINFO));
    } __except( EXCEPTION_EXECUTE_HANDLER) {
        return FALSE;
    }
    return TRUE;
}

BOOL CStore::GetPartitionDriver(HKEY hKeyStorage, HKEY hKeyProfile)
{
    BOOL fValidDriver = FALSE, fValidName = FALSE;

    if (hKeyProfile) {
        // try to read the partition driver name from the profile key
        fValidName = FsdGetRegistryString(hKeyProfile, g_szPART_DRIVER_NAME_STRING, m_szPartDriverName, sizeof(m_szPartDriverName)/sizeof(WCHAR)); 
    }

    if (!fValidName && hKeyProfile) {
        // allow the legacy partition driver DLL name specified in the profile
        fValidDriver = FsdGetRegistryString(hKeyProfile, g_szPART_DRIVER_STRING, m_szPartDriver, sizeof(m_szPartDriver)/sizeof(WCHAR));        
    }

    if (!fValidName && !fValidDriver && hKeyStorage) {
        // try to read the partition driver name from the storage key
        fValidName = FsdGetRegistryString(hKeyStorage, g_szPART_DRIVER_NAME_STRING, m_szPartDriverName, sizeof(m_szPartDriverName)/sizeof(WCHAR));
    }

    if (fValidName && !fValidDriver) {
        // open the partition driver sub key
        HKEY hKeyPartition;
        TCHAR szSubKey[MAX_PATH];

        if (hKeyProfile) {
            // try to find the dll name under the profile partition driver sub key
            if (ERROR_SUCCESS == FsdRegOpenSubKey(hKeyProfile, m_szPartDriverName, &hKeyPartition)) {
                fValidDriver = FsdGetRegistryString(hKeyPartition, g_szPART_DRIVER_MODULE_STRING, m_szPartDriver, sizeof(m_szPartDriver)/sizeof(WCHAR));
                FsdRegCloseKey(hKeyPartition);
            }
        }

        if (!fValidDriver && hKeyStorage) {
            // try to find the dll name under the storage root partition driver key
            VERIFY(SUCCEEDED(StringCchPrintf(szSubKey, MAX_PATH, L"%s\\%s", g_szSTORAGE_PATH, m_szPartDriverName)));
            if (ERROR_SUCCESS == FsdRegOpenKey(szSubKey, &hKeyPartition)) {
                fValidDriver = FsdGetRegistryString(hKeyPartition, g_szPART_DRIVER_MODULE_STRING, m_szPartDriver, sizeof(m_szPartDriver)/sizeof(WCHAR));
                FsdRegCloseKey(hKeyPartition);
            }        
        }

    }

    if (!fValidDriver) {
        // unable to find a valid driver in the registry
        DEBUGMSG( ZONE_INIT, (L"Using the default HARDCODED partitioning driver (%s)!!!\r\n", g_szDEFAULT_PARTITION_DRIVER));
        wcscpy( m_szPartDriver, g_szDEFAULT_PARTITION_DRIVER);
    }

    if (!fValidName) {
        // unable to find a valid partition driver name in the registry,
        // generate the name from the name of the driver
        TCHAR *pszTmp;
        wcsncpy( m_szPartDriverName, m_szPartDriver, sizeof(m_szPartDriverName)/sizeof(WCHAR));
        if (pszTmp = wcsstr( m_szPartDriverName, L".")) {
            *pszTmp = L'\0';
        }
        DEBUGMSG( ZONE_INIT, (L"Using the generated partition driver name (%s)!!!\r\n", m_szPartDriverName));
    }
    
    return TRUE;
}

BOOL CStore::GetStoreSettings()
{   
    HKEY hKeyStorage=NULL, hKeyProfile = NULL;
    BOOL bRet = FALSE;
    if (ERROR_SUCCESS == FsdRegOpenKey( g_szPROFILE_PATH, &hKeyStorage)) {
        DUMPREGKEY(ZONE_INIT, g_szPROFILE_PATH, hKeyStorage);
        VERIFY(SUCCEEDED(StringCchPrintf(m_szRootRegKey, MAX_PATH, L"%s\\%s", g_szPROFILE_PATH, m_sdi.szProfile)));
        if (ERROR_SUCCESS != FsdRegOpenKey( m_szRootRegKey, &hKeyProfile)) {
            hKeyProfile = NULL;
        } else {
            DUMPREGKEY(ZONE_INIT, m_sdi.szProfile, hKeyProfile);
        }   
        if (!hKeyProfile || !FsdLoadFlag(hKeyProfile, g_szAUTO_MOUNT_STRING, &m_dwFlags, STORE_ATTRIBUTE_AUTOMOUNT))
            if (!FsdLoadFlag(hKeyStorage, g_szAUTO_MOUNT_STRING, &m_dwFlags, STORE_ATTRIBUTE_AUTOMOUNT))
                        m_dwFlags |= STORE_ATTRIBUTE_AUTOMOUNT;
        if (!hKeyProfile || !FsdLoadFlag(hKeyProfile, g_szAUTO_FORMAT_STRING, &m_dwFlags, STORE_ATTRIBUTE_AUTOFORMAT))
            FsdLoadFlag(hKeyStorage, g_szAUTO_FORMAT_STRING, &m_dwFlags, STORE_ATTRIBUTE_AUTOFORMAT);
        if (!hKeyProfile || !FsdLoadFlag(hKeyProfile, g_szAUTO_PART_STRING, &m_dwFlags, STORE_ATTRIBUTE_AUTOPART))
            FsdLoadFlag(hKeyStorage, g_szAUTO_PART_STRING, &m_dwFlags, STORE_ATTRIBUTE_AUTOPART);
        if (!hKeyProfile || !FsdGetRegistryString(hKeyProfile, g_szFILE_SYSTEM_STRING, m_szDefaultFileSystem, sizeof(m_szDefaultFileSystem)/sizeof(WCHAR)))
            if (!FsdGetRegistryString(hKeyStorage, g_szFILE_SYSTEM_STRING, m_szDefaultFileSystem, sizeof(m_szDefaultFileSystem)/sizeof(WCHAR)))
                wcscpy( m_szDefaultFileSystem, g_szDEFAULT_FILESYSTEM);
        if (!hKeyProfile || !FsdGetRegistryString(hKeyProfile, g_szFOLDER_NAME_STRING, m_szFolderName, sizeof(m_szFolderName)/sizeof(WCHAR)))
            if (!FsdGetRegistryString(hKeyStorage, g_szFOLDER_NAME_STRING, m_szFolderName, sizeof(m_szFolderName)/sizeof(WCHAR)))
                wcscpy( m_szFolderName, g_szDEFAULT_FOLDER_NAME);
        if (!hKeyProfile || !FsdGetRegistryString(hKeyProfile, g_szSTORE_NAME_STRING, m_szStoreName, sizeof(m_szStoreName)/sizeof(WCHAR)))
            if (!FsdGetRegistryString(hKeyStorage, g_szSTORE_NAME_STRING, m_szStoreName, sizeof(m_szStoreName)/sizeof(WCHAR)))
                wcscpy( m_szStoreName, g_szDEFAULT_STORAGE_NAME);
        // By default activity timer is enabled.  The Evnet Strings will specify the name.
        if (!hKeyProfile || !FsdLoadFlag(hKeyProfile, g_szACTIVITY_TIMER_ENABLE_STRING, &m_dwFlags, STORE_FLAG_ACTIVITYTIMER)) 
            if (!FsdLoadFlag(hKeyStorage, g_szACTIVITY_TIMER_ENABLE_STRING, &m_dwFlags, STORE_FLAG_ACTIVITYTIMER))
                m_dwFlags |= STORE_FLAG_ACTIVITYTIMER; 
        if (!hKeyProfile || !FsdGetRegistryString( hKeyProfile, g_szACTIVITY_TIMER_STRING, m_szActivityName, sizeof( m_szActivityName)/sizeof(WCHAR)))
            if (!FsdGetRegistryString(hKeyStorage, g_szACTIVITY_TIMER_STRING, m_szActivityName, sizeof(m_szActivityName)/sizeof(WCHAR)))
                wcscpy( m_szActivityName, g_szDEFAULT_ACTIVITY_NAME); 

        GetMountSettings(hKeyStorage, &m_dwMountFlags); // First storage manager
        GetMountSettings(hKeyProfile, &m_dwMountFlags); // Override in profile
        
        DWORD dwAttribs = 0;
        if (!hKeyProfile || !FsdGetRegistryValue(hKeyProfile, g_szATTRIB_STRING, &dwAttribs)) 
            FsdGetRegistryValue(hKeyStorage, g_szATTRIB_STRING, &dwAttribs);
        if (dwAttribs & STORE_ATTRIBUTE_READONLY)
                m_dwFlags |= STORE_ATTRIBUTE_READONLY;
            
    }
    if (!GetPartitionDriver(hKeyStorage, hKeyProfile))
        goto ExitFalse;
    bRet = TRUE;
ExitFalse:
    if (hKeyStorage) {
        FsdRegCloseKey( hKeyStorage);
    }    
    if (hKeyProfile) {
        FsdRegCloseKey( hKeyProfile);
    }
    return bRet;
}

BOOL CStore::IsValidPartition(CPartition * pPartition)
{
    CPartition *pTemp = m_pPartitionList;
    if (!pPartition || (pPartition == INVALID_PARTITION))
        return FALSE;
    while(pTemp) {
        if (pTemp == pPartition) {
            break;
        }
        pTemp = pTemp->m_pNextPartition;
    }
    return pTemp != NULL;
}

DWORD CStore::OpenDisk()
{
    DWORD dwError = ERROR_SUCCESS;
    STORAGE_IDENTIFICATION storageid;

    if (m_pBlockDevice) {
        m_hDisk = m_pBlockDevice->OpenBlockDevice();
    } else {
        m_hDisk = CreateFileW(m_szDeviceName,
                           GENERIC_READ | GENERIC_WRITE,
                           0,
                           NULL, OPEN_EXISTING, 0, NULL);
    
        if (m_hDisk == INVALID_HANDLE_VALUE) {
            dwError = GetLastError();
            if (dwError == ERROR_ACCESS_DENIED) {
                dwError = ERROR_SUCCESS;
                m_hDisk = CreateFileW(m_szDeviceName,
                                   GENERIC_READ,
                                   FILE_SHARE_READ,
                                   NULL, OPEN_EXISTING, 0, NULL);
                if (m_hDisk != INVALID_HANDLE_VALUE) {
                    m_dwFlags |= STORE_ATTRIBUTE_READONLY;
                } else {
                    dwError = GetLastError();
                }    
            }    
        }
    }   
    if (m_hDisk != INVALID_HANDLE_VALUE) {
        DWORD dwRet;
        dwError = DeviceIoControl( NULL, DISK_IOCTL_GETINFO, &m_di, sizeof(DISK_INFO), NULL, 0, &dwRet);
        if ((dwError == ERROR_BAD_COMMAND) || (dwError == ERROR_NOT_SUPPORTED)){
            memset( &m_di, 0, sizeof(DISK_INFO));   
            dwError = ERROR_SUCCESS;
        }
        if (dwError == ERROR_SUCCESS) {
            if (ERROR_SUCCESS != DeviceIoControl( NULL, IOCTL_DISK_DEVICE_INFO, &m_sdi, sizeof(STORAGEDEVICEINFO), NULL, 0, &dwRet)) {
                DEBUGMSG( ZONE_INIT, (L"FSDMGR!CStore::OpenDisk(0x%08X) call to IOCTL_DISK_DEVICE_INFO failed..filling info\r\n", this));
                m_sdi.dwDeviceClass = STORAGE_DEVICE_CLASS_BLOCK;
                m_sdi.dwDeviceFlags = STORAGE_DEVICE_FLAG_READWRITE;
                m_sdi.dwDeviceType = STORAGE_DEVICE_TYPE_UNKNOWN;
                wcscpy( m_sdi.szProfile, L"Default");
            }

⌨️ 快捷键说明

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