VfSys.cpp
资源名称:warftpd.zip [点击查看]
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:29k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // VfSys.cpp : Defines the initialization routines for the DLL.
- //
- #include "stdafx.h"
- #include <afxdllx.h>
- #define EXT
- #include "WarSoftware.h"
- #include "WarFsys.h"
- #include "VfFSys.h"
- #include "UnixFsysTypes.h"
- #include "FsysSecurity.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- static AFX_EXTENSION_MODULE VfSysDLL = { NULL, NULL };
- extern "C" int APIENTRY
- DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
- {
- if (dwReason == DLL_PROCESS_ATTACH)
- {
- TRACE0("VFSYS.DLL Initializing!n");
- // Extension DLL one-time initialization
- AfxInitExtensionModule(VfSysDLL, hInstance);
- // Insert this DLL into the resource chain
- new CDynLinkLibrary(VfSysDLL);
- // Create therad sync object
- pLock = new CRWLock;
- if (!pLock->Create("VfSyslck"))
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"VfSys.dll: DllMain() - Unable to create CRWLock object.");
- return 0;
- }
- }
- else if (dwReason == DLL_PROCESS_DETACH)
- {
- TRACE0("VFSYS.DLL Terminating!n");
- if (pVfSys)
- delete pVfSys;
- pVfSys = NULL;
- if (pLock)
- delete pLock;
- pLock = NULL;
- if (pSmem)
- delete pSmem;
- pSmem = NULL;
- }
- return 1; // ok
- }
- ////////////////////////////////////////////////////////////////////////////////
- // Entry point
- BOOL OpenFsys(HWND *phWnd, LPCSTR IniFilePath)
- {
- if (pVfSys)
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"CVirtualFileSystem::OpenFsys() - File system is already initialized.");
- SetLastError(ERROR_ALREADY_EXISTS);
- return FALSE;
- }
- // Initialize the security subsystem
- // Create a new root security file it it don't already exist.
- {
- HANDLE h = CFsysSecurity::LoadSecurityGroup("",FALSE);
- if (h == INVALID_HANDLE_VALUE)
- h = CFsysSecurity::OpenSecurityGroupForUpdate("");
- if (h == INVALID_HANDLE_VALUE)
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"CVirtualFileSystem::OpenFsys() - Failed initialize the security subsystem.");
- return FALSE;
- }
- CFsysSecurity::CloseSecurityGroup(h);
- }
- pVfSys = new CVirtualFileSystem;
- if (!pVfSys->CreateEx(0, AfxRegisterWndClass(0),
- _T("Wirtual File System Notification Sink"),
- WS_OVERLAPPED, 0, 0, 0, 0, NULL, NULL))
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"CVirtualFileSystem::OpenFsys() - Unable to create Virtual File System notify window!");
- return FALSE;
- }
- ASSERT(pVfSys->m_hWnd != NULL);
- ASSERT(CWnd::FromHandlePermanent(pVfSys->m_hWnd) == pVfSys);
- *phWnd = pVfSys->m_hWnd;
- pLock->WriteLock();
- // Create shared memory
- pSmem = new CSharedBlksMem;
- if (!pSmem->Create("VfSysmem",
- GetPrivateProfileInt("fsys", "buffer", (1024 * 512), IniFilePath)))
- {
- delete pVfSys;
- pVfSys = NULL;
- phWnd = NULL;
- CLog::GetLog()->LogMsg(LOGF_ERROR,"CVirtualFileSystem::OpenFsys() - Unable to create shared memory");
- pLock->UnlockWrite();
- return FALSE;
- }
- // Create the master virtual file system
- if (pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_ROOTNODE] == NULL)
- {
- // Create global headers
- SMEM GlobalHdr = *(SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_DEVDRV]);
- if (GlobalHdr.ofs == 0)
- {
- if (!pSmem->sh_malloc(GlobalHdr, sizeof(GLOBAL_FSYS_DRIVERS_HDR)))
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"CVirtualFileSystem::OpenFsys() - Failed to allocate GLOBAL_FSYS_DRIVERS_HDR");
- goto fail;
- }
- *(SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_DEVDRV]) = GlobalHdr;
- }
- if (!CreateMasterFsys(IniFilePath))
- {
- fail:
- delete pVfSys;
- *phWnd = NULL;
- pVfSys = NULL;
- delete pSmem;
- pSmem = NULL;
- pLock->UnlockWrite();
- return FALSE;
- }
- }
- pLock->UnlockWrite();
- return TRUE;
- }
- // End of entry point
- // Mount the master file system
- BOOL CreateMasterFsys(LPCSTR IniFilePath)
- {
- int Index;
- for(Index = 0;; Index++)
- {
- CString Tag, Path, Vpath, Driver, User, Pwd;
- int Copy, Timeout;
- Tag.Format("mount%d", Index);
- GetPrivateProfileString(Tag,"Path","", Path.GetBuffer(1024), 1024, IniFilePath);
- Path.ReleaseBuffer();
- if (Path.IsEmpty())
- break;
- GetPrivateProfileString(Tag,"VPath","", Vpath.GetBuffer(1024), 1024, IniFilePath);
- Vpath.ReleaseBuffer();
- if (Vpath.IsEmpty())
- break;
- GetPrivateProfileString(Tag,"Driver","", Driver.GetBuffer(1024), 1024, IniFilePath);
- Driver.ReleaseBuffer();
- if (Driver.IsEmpty())
- break;
- GetPrivateProfileString(Tag,"User","", User.GetBuffer(32), 32, IniFilePath);
- User.ReleaseBuffer();
- GetPrivateProfileString(Tag,"passwd","", Pwd.GetBuffer(32), 32, IniFilePath);
- Pwd.ReleaseBuffer();
- Copy = GetPrivateProfileInt(Tag,"Copy", FALSE, IniFilePath);
- Timeout = GetPrivateProfileInt(Tag,"Timeout", FALSE, IniFilePath);
- MountFsys(Path, Vpath, Driver, User, Pwd, Copy, Timeout, IniFilePath);
- }
- return Index > 0;
- }
- // Mount a file system
- BOOL MountFsys(LPCSTR Path, LPCSTR Vpath, LPCSTR Driver, LPCSTR User, LPCSTR Pwd, BOOL Copy, int Timeout, LPCSTR IniFilePath)
- {
- SMEM *pMasterNode = (SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_ROOTNODE]);
- int DevIndex = -1;
- CLog::GetLog()->LogMsg(LOGF_SYSTEM,"vfsys.dll: MountFsys(%s, %s, %s) - Mounting file system.",
- Path, Vpath, Driver);
- // Check if the driver is loaded
- SyncDeviceDrivers();
- if ((DevIndex = MountDeviceDriver(Driver)) == -1)
- return FALSE;
- if (pMasterNode->ofs != 0)
- ScanPath(Vpath, TRUE, FALSE, TRUE);
- // Mount the file system
- if (!strcmp(Vpath,"/"))
- {
- // Root node.
- if (pMasterNode->ofs != 0)
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"vfsys.dll: MountFsys(%s, %s) - Root node already exist.",
- Path, Vpath);
- SetLastError(ERROR_ALREADY_EXISTS);
- return FALSE;
- }
- }
- // Create the file system structure
- int HdrLen = sizeof(FILE_SYSTEM);
- HdrLen += (strlen(Path) +1);
- HdrLen += (strlen(Vpath) +1);
- HdrLen += (strlen(User) +1);
- HdrLen += (strlen(Pwd) +1);
- SMEM smFsys;
- if (!pSmem->sh_malloc(smFsys, HdrLen))
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"vfsys.dll: MountFsys(%s, %s) - Out of shared memory.",
- Path, Vpath);
- SetLastError(ERROR_NOT_ENOUGH_MEMORY);
- return FALSE;
- }
- FILE_SYSTEM *pFsysLast = (FILE_SYSTEM *)pSmem->__Ptr(*(SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_FSYS]));
- FILE_SYSTEM *pFsys = (FILE_SYSTEM *)pSmem->__Ptr(smFsys);
- LPSTR p = pFsys->Buf;
- LPCSTR pp;
- // Initialize data members
- pFsys->DOSPath = p; pp = Path; while(*pp) *p++ = *pp++; *p++ = 0;
- pFsys->Vpath = p; pp = Vpath; while(*pp) *p++ = *pp++; *p++ = 0;
- pFsys->LoginName = p; pp = User; while(*pp) *p++ = *pp++; *p++ = 0;
- pFsys->LoginPwd = p; pp = Pwd; while(*pp) *p++ = *pp++; *p++ = 0;
- pFsys->DevIdx = DevIndex;
- pFsys->RootNode.ofs = 0;
- pFsys->RootNode.blk = 0;
- pFsys->Next = pFsys->RootNode;
- pFsys->FlushDelay = Timeout;
- pFsys->DoCopy = Copy;
- if (!CreateRootNode(smFsys))
- return FALSE;
- // Initialize
- if (!pFsysLast)
- {
- // Link as first in list
- SMEM *pShdr = (SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_FSYS]);
- *pShdr = smFsys;
- }
- else
- {
- // Find the last fsys and link to it
- while(pFsysLast->Next.ofs)
- pFsysLast = (FILE_SYSTEM *)pSmem->__Ptr(pFsysLast->Next);
- pFsysLast->Next = smFsys;
- }
- return TRUE;
- }
- // Syncronize the local driver table against the global table
- BOOL SyncDeviceDrivers()
- {
- SMEM *pSmGlobalHdr = (SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_DEVDRV]);
- GLOBAL_FSYS_DRIVERS_HDR *pGlobalHdr = (GLOBAL_FSYS_DRIVERS_HDR *)pSmem->__Ptr(*pSmGlobalHdr);
- ASSERT(AfxIsValidAddress(pGlobalHdr, sizeof(GLOBAL_FSYS_DRIVERS_HDR)));
- for(int Index = 0;Index < DRIVER_TABLE_SIZE; Index++)
- {
- if (!*pGlobalHdr->Name[Index])
- break;
- if (!LocalDeviceDrivers[Index].pDevDr)
- MountDeviceDriver(pGlobalHdr->Name[Index]);
- }
- return TRUE;
- }
- // Mount a new device driver (if don't exist)
- // Return the index to the driver
- int MountDeviceDriver(LPCSTR Driver)
- {
- SMEM *pSmGlobalHdr = (SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_DEVDRV]);
- GLOBAL_FSYS_DRIVERS_HDR *pGlobalHdr = (GLOBAL_FSYS_DRIVERS_HDR *)pSmem->__Ptr(*pSmGlobalHdr);
- ASSERT(AfxIsValidString(Driver));
- ASSERT(AfxIsValidAddress(pGlobalHdr, sizeof(GLOBAL_FSYS_DRIVERS_HDR)));
- CLog::GetLog()->LogMsg(LOGF_SYSTEM,"vfsys.dll: MountDeviceDriver(%s) - Mounting device driver.",
- Driver);
- if (strlen(Driver) >= DRIVER_NAME_LEN)
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"VfSys.dll: MountDeviceDriver() - Name of device '%s' is more than %d characters.",
- Driver, DRIVER_NAME_LEN - 1);
- return -1;
- }
- int Index = FindDeviceDriver(Driver);
- if (Index < 0)
- {
- // Find a new empty slot
- for(Index = 0;Index < DRIVER_TABLE_SIZE; Index++)
- {
- if (!*pGlobalHdr->Name[Index])
- break;
- }
- strcpy(pGlobalHdr->Name[Index], Driver);
- }
- if (Index >= DRIVER_TABLE_SIZE)
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"VfSys.dll: MountDeviceDriver() - Device table is full (%d entries). Unable to add '%s'.",
- DRIVER_TABLE_SIZE, Driver);
- return -1;
- }
- // Try to load the device driver
- if ((LocalDeviceDrivers[Index].hDll = LoadLibrary(Driver)) == NULL)
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"VfSys.dll: MountDeviceDriver() - Failed to load device driver '%s'. %s",
- Driver, GetLastErrorText());
- return -1;
- }
- HANDLE (*DevOpenDevice)(CFileSystemTemplate *pInfo);
- if ((DevOpenDevice = (HANDLE(*)(CFileSystemTemplate *pInfo))GetProcAddress(
- LocalDeviceDrivers[Index].hDll,
- "?DevOpenDevice@@YAPAXPAVCFileSystemTemplate@@@Z")) == NULL)
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"VfSys.dll: MountDeviceDriver() - Could not find 'DevOpenDevice