hdd.cpp
资源名称:warftpd.zip [点击查看]
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:11k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // This is part of the WAR SOFTWARE SERIES initiated by Jarle Aase
- // Copyright 1996 by Jarle Aase. All rights reserved.
- // See the "War Software Series Licende Agreement" for details concerning
- // use and distribution.
- // ---
- // This source code, executables and programs containing source code or
- // binaries or proprietetary technology from the War Software Series are
- // NOT alloed used, viewed or tested by any governmental agencies in
- // any countries. This includes the government, departments, police,
- // military etc.
- // ---
- // This file is intended for use with Tab space = 2
- // Created and maintained in MSVC Developer Studio
- // ---
- // NAME : hdd.cpp
- // PURPOSE : Basic Win95/NT file system driver
- // PROGRAM :
- // DATE : March 15 1997
- // AUTHOR : Jarle Aase
- // ---
- //
- // REVISION HISTORY
- //
- #include "stdafx.h"
- #include <afxdllx.h>
- #include "WarSoftware.h"
- #include "WarFsys.h"
- #include "FsysSecurity.h"
- #include "hdd.h"
- #include "UnixFsysTypes.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- static AFX_EXTENSION_MODULE HddDLL = { NULL, NULL };
- void MapDataToFileInfo(WIN32_FIND_DATA& Data, CFileInfo& FileInfo, HANDLE hSecGrp);
- static CString DOSPath(LPCSTR Path);
- BOOL IsForbiddenFile(WIN32_FIND_DATA& Data);
- extern "C" int APIENTRY
- DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
- {
- if (dwReason == DLL_PROCESS_ATTACH)
- {
- TRACE0("HDD.DLL Initializing!n");
- // Extension DLL one-time initialization
- AfxInitExtensionModule(HddDLL, hInstance);
- // Insert this DLL into the resource chain
- new CDynLinkLibrary(HddDLL);
- }
- else if (dwReason == DLL_PROCESS_DETACH)
- {
- TRACE0("HDD.DLL Terminating!n");
- }
- return 1; // ok
- }
- // Entry point
- // Return INVALD_HANDLE_VALUE on error
- HANDLE DevOpenDevice(CFileSystemTemplate *pInfo)
- {
- pInfo->CreateDirectory = __fs__CreateDirectory;
- pInfo->RemoveDirectory = __fs__RemoveDirectory;
- pInfo->DeleteFile = __fs__DeleteFile;
- pInfo->GetDiskFreeSpace = __fs__GetDiskFreeSpace;
- pInfo->MoveFile = __fs__MoveFile;
- pInfo->CloseHandle = __fs__CloseHandle;
- pInfo->FindFirstFile = __fs__FindFirstFile;
- pInfo->FindNextFile = __fs__FindNextFile;
- pInfo->FindClose = __fs__FindClose;
- pInfo->GetFileAttributes = __fs__GetFileAttributes;
- pInfo->GetDriveType = __fs__GetDriveType;
- pInfo->GetFileSize = __fs__GetFileSize;
- pInfo->Seek = __fs__Seek;
- pInfo->CreateFile = __fs__CreateFile;
- pInfo->ReadFile = __fs__ReadFile;
- pInfo->WriteFile = __fs__WriteFile;
- pInfo->GetSecurityHandleFromFindHandle = __fs__GetSecurityHandleFromFindHandle;
- if (IsNT())
- {
- pInfo->ReadFileEx = __fs__ReadFileEx;
- pInfo->WriteFileEx = __fs__WriteFileEx;
- }
- return (HANDLE)pInfo;
- }
- void DevCloseDevice(HANDLE hVal)
- {
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- // Physical IO functions
- // IO calll
- BOOL __fs__CreateDirectory(LPCTSTR Path)
- {
- return ::CreateDirectory(DOSPath(Path), NULL);
- }
- BOOL __fs__RemoveDirectory(LPCTSTR lpPathName)
- {
- return ::RemoveDirectory(DOSPath(lpPathName));
- }
- BOOL __fs__DeleteFile(LPCTSTR Path)
- {
- return DeleteFile(DOSPath(Path));
- }
- FLEN __fs__GetDiskFreeSpace(LPCSTR Path)
- {
- DWORD
- SectorsPerCluster,
- BytesPerSector,
- NumberOfFreeClusters,
- TotalNumberOfClusters;
- if (!GetDiskFreeSpace(
- DOSPath(Path),
- &SectorsPerCluster,
- &BytesPerSector,
- &NumberOfFreeClusters,
- &TotalNumberOfClusters))
- return INVALID_FLEN_VALUE;
- FLEN Rval = (FLEN)NumberOfFreeClusters;
- Rval *= (FLEN)SectorsPerCluster;
- Rval *= (FLEN)BytesPerSector;
- return Rval;
- }
- BOOL __fs__MoveFile(LPCTSTR ExistingFileName, LPCTSTR NewFileName)
- {
- return ::MoveFile(DOSPath(ExistingFileName), DOSPath(NewFileName));
- }
- void __fs__CloseHandle(HANDLE hVal)
- {
- CloseHandle(hVal);
- }
- HANDLE __fs__FindFirstFile(LPCTSTR lpFileName, CFileInfo& FileInfo)
- {
- WIN32_FIND_DATA Data;
- CString MyPath = DOSPath(lpFileName);
- HANDLE hVal = ::FindFirstFile(MyPath, &Data);
- if (hVal == INVALID_HANDLE_VALUE)
- return INVALID_HANDLE_VALUE;
- while(IsForbiddenFile(Data))
- {
- if (!FindNextFile(hVal, &Data))
- {
- ::FindClose(hVal);
- return INVALID_HANDLE_VALUE;
- }
- }
- CDevFindFile *pDFF = new CDevFindFile;
- if (!pDFF->Create(MyPath, hVal))
- {
- delete pDFF;
- return INVALID_HANDLE_VALUE;
- }
- MapDataToFileInfo(Data, FileInfo, pDFF->m_hSecurityGrp);
- return (HANDLE)pDFF;
- }
- BOOL __fs__FindNextFile(HANDLE hVal, CFileInfo& FileInfo)
- {
- WIN32_FIND_DATA Data;
- CDevFindFile *pDFF = (CDevFindFile *)hVal;
- if (!::FindNextFile(pDFF->m_hFindFile, &Data))
- return FALSE;
- while(IsForbiddenFile(Data))
- {
- if (!FindNextFile(pDFF->m_hFindFile, &Data))
- return FALSE;
- }
- MapDataToFileInfo(Data, FileInfo, pDFF->m_hSecurityGrp);
- return TRUE;
- }
- void MapDataToFileInfo(WIN32_FIND_DATA& Data, CFileInfo& FileInfo, HANDLE hSecGrp)
- {
- // Standard info
- FileInfo.m_Name = Data.cFileName;
- FileInfo.m_DOSflags = Data.dwFileAttributes;
- FileInfo.m_CreationTime = Data.ftCreationTime;
- FileInfo.m_LastModifyTime = Data.ftLastWriteTime;
- FileInfo.m_FileSize = (FLEN)((FLEN)Data.nFileSizeHigh * (FLEN)MAXDWORD) + (FLEN)Data.nFileSizeLow;
- // Security info
- CFsysSecurity::GetFsysSecurityProperties(hSecGrp, FileInfo);
- // Physical facts
- if (Data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
- FileInfo.m_Flags |= NODE_DIR;
- if (Data.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
- FileInfo.m_Flags |= NODE_READONLY;
- }
- void __fs__FindClose(HANDLE hVal)
- {
- CDevFindFile *pDFF = (CDevFindFile *)hVal;
- delete pDFF;
- }
- DWORD __fs__GetFileAttributes(LPCTSTR lpFileName )
- {
- return ::GetFileAttributes(DOSPath(lpFileName));
- }
- UINT __fs__GetDriveType(LPCTSTR lpRootPathName)
- {
- return GetDriveType(DOSPath(lpRootPathName));
- }
- FLEN __fs__GetFileSize(HANDLE hFile)
- {
- DWORD LoDWord, HiDWord;
- FLEN Rval;
- LoDWord = GetFileSize(hFile, &HiDWord);
- if ((LoDWord == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
- return INVALID_FLEN_VALUE;
- Rval = (FLEN)((FLEN)HiDWord * (FLEN)MAXDWORD) + (FLEN)LoDWord;
- return Rval;
- }
- FLEN __fs__Seek(HANDLE hFile, FLEN flOfs, DWORD dwFrom)
- {
- DWORD LoDWord, HiDWord;
- FLEN Rval;
- LoDWord = LODWORD(flOfs);
- HiDWord = HIDWORD(flOfs);
- LoDWord = SetFilePointer(hFile, LoDWord, (PLONG)&HiDWord, dwFrom);
- if ((LoDWord == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
- return INVALID_FLEN_VALUE;
- Rval = (FLEN)((FLEN)HiDWord * (FLEN)MAXDWORD) + (FLEN)LoDWord;
- return Rval;
- }
- HANDLE __fs__CreateFile(
- LPCTSTR lpFileName, // pointer to name of the file
- DWORD dwDesiredAccess, // access (read-write) mode
- DWORD dwShareMode, // share mode
- LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security descriptor
- DWORD dwCreationDistribution, // how to create
- DWORD dwFlagsAndAttributes, // file attributes
- HANDLE hTemplateFile // handle to file with attributes to copy
- )
- {
- return ::CreateFile(
- DOSPath(lpFileName),
- dwDesiredAccess,
- dwShareMode,
- lpSecurityAttributes,
- dwCreationDistribution,
- dwFlagsAndAttributes,
- hTemplateFile);
- }
- BOOL __fs__ReadFile(
- HANDLE hFile, // handle of file to read
- LPVOID lpBuffer, // address of buffer that receives data
- DWORD nNumberOfBytesToRead, // number of bytes to read
- LPDWORD lpNumberOfBytesRead, // address of number of bytes read
- LPOVERLAPPED lpOverlapped // address of structure for data
- )
- {
- return ::ReadFile(
- hFile,
- lpBuffer,
- nNumberOfBytesToRead,
- lpNumberOfBytesRead,
- lpOverlapped);
- }
- BOOL __fs__ReadFileEx(
- HANDLE hFile, // handle of file to read
- LPVOID lpBuffer, // address of buffer
- DWORD nNumberOfBytesToRead, // number of bytes to read
- LPOVERLAPPED lpOverlapped, // address of offset
- LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine // address of completion routine
- )
- {
- return ::ReadFileEx(
- hFile,
- lpBuffer,
- nNumberOfBytesToRead,
- lpOverlapped,
- lpCompletionRoutine);
- }
- BOOL __fs__WriteFile(
- HANDLE hFile, // handle to file to write to
- LPCVOID lpBuffer, // pointer to data to write to file
- DWORD nNumberOfBytesToWrite, // number of bytes to write
- LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written
- LPOVERLAPPED lpOverlapped // pointer to structure needed for overlapped I/O
- )
- {
- return WriteFile(
- hFile,
- lpBuffer,
- nNumberOfBytesToWrite,
- lpNumberOfBytesWritten,
- lpOverlapped);
- }
- BOOL __fs__WriteFileEx(
- HANDLE hFile, // handle to output file
- LPCVOID lpBuffer, // pointer to input buffer
- DWORD nNumberOfBytesToWrite, // number of bytes to write
- LPOVERLAPPED lpOverlapped, // pointer to async. i/o data
- LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine // ptr. to completion routine
- )
- {
- return WriteFileEx(
- hFile,
- lpBuffer,
- nNumberOfBytesToWrite,
- lpOverlapped,
- lpCompletionRoutine);
- }
- HANDLE __fs__GetSecurityHandleFromFindHandle(HANDLE hFind)
- {
- CDevFindFile *pDFF = (CDevFindFile *)hFind;
- return pDFF->m_hSecurityGrp;
- }
- CString DOSPath(LPCSTR Path)
- {
- CString cBuf = Path;
- LPSTR p = cBuf.GetBuffer(1);
- while(*p)
- {
- if (*p == '/')
- *p = '\';
- ++p;
- }
- cBuf.ReleaseBuffer();
- return cBuf;
- }
- //////////////////////////////////////////////////////////////////////////////////////
- // CDevFindFile
- CDevFindFile::CDevFindFile()
- {
- m_hFindFile = INVALID_HANDLE_VALUE;
- m_hSecurityGrp = INVALID_HANDLE_VALUE;
- }
- CDevFindFile::~CDevFindFile()
- {
- if (m_hFindFile != INVALID_HANDLE_VALUE)
- ::FindClose(m_hFindFile);
- if (m_hSecurityGrp != INVALID_HANDLE_VALUE)
- CFsysSecurity::CloseSecurityGroup(m_hSecurityGrp);
- }
- // The paths here is the file system paths being scanned.
- BOOL CDevFindFile::Create(LPCSTR Path, HANDLE hVal)
- {
- BOOL TryPrevLevel = TRUE;
- m_hFindFile = hVal;
- int Levels = 0;
- CString MyPath = Path;
- LPSTR pOrigin;
- while(TryPrevLevel)
- {
- LPSTR p = pOrigin = MyPath.GetBuffer(1);
- p = strrchr(p, '\');
- if ((p > pOrigin) && (p[-1] != '\') && (p[-1] != ':'))
- *p = 0;
- else if (*++p == '*')
- *p = 0;
- else
- {
- MyPath = "";
- TryPrevLevel = FALSE;
- }
- MyPath.ReleaseBuffer();
- m_hSecurityGrp = CFsysSecurity::LoadSecurityGroup(MyPath, ++Levels == 1);
- if (m_hSecurityGrp == INVALID_HANDLE_VALUE)
- continue;
- return TRUE;
- }
- CLog::GetLog()->LogMsg(LOGF_WARNINGS,"CDevFindFile::Create(%s) - Failed to open security group.",
- Path);
- return FALSE;
- }
- BOOL IsForbiddenFile(WIN32_FIND_DATA& Data)
- {
- if ((Data.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_TEMPORARY))
- || ((Data.cFileName[0] == '.')
- && (!stricmp(Data.cFileName,".Index.txt") || (!stricmp(Data.cFileName,SECFILE)))))
- return TRUE;
- return FALSE;
- }