VfSys.cpp
资源名称:warftpd.zip [点击查看]
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:41k
源码类别:
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 : VfFsys.cpp
- // PURPOSE : War Daemon Virtual File System
- // PROGRAM :
- // DATE : March 13 1997
- // AUTHOR : Jarle Aase
- // ---
- //
- // Exports are done in the .def file.
- //
- // REVISION HISTORY
- //
- #include "stdafx.h"
- #include <afxdllx.h>
- #include <ctype.h>
- #define EXT
- // For now, we don't support non-daemon programs
- //#include "WarSoftware.h"
- #include "WarDaemon.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)
- {
- int Index;
- 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] == 0)
- {
- // Create global headers
- SMEM GlobalHdr = *(SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_DEVDRV]);
- if (GlobalHdr == 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;
- }
- // Create the global plugin table
- ASSERT(pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_PLUGINS] == 0);
- {
- SMEM smPluginTable;
- smPluginTable = 0;
- if (!pSmem->sh_malloc(smPluginTable, sizeof(GLOBAL_VFSYS_PLUGIN_HDR)))
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"CVirtualFileSystem::OpenFsys() - Failed to allocate GLOBAL_VFSYS_PLUGIN_HDR");
- goto fail;
- }
- *(SMEM *)&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_PLUGINS] = smPluginTable;
- pGlobalPluginHdr = (GLOBAL_VFSYS_PLUGIN_HDR *)pSmem->__Ptr(smPluginTable);
- }
- for(Index = 0; Index < PLUGIN_TABLE_SIZE; Index++)
- {
- CString Tag, Name, Pattern;
- Tag.Format("Plugin%d", Index);
- GetPrivateProfileString(Tag,"Plugin","", Name.GetBuffer(32), 32, IniFilePath);
- Name.ReleaseBuffer();
- if (Name.IsEmpty())
- break;
- GetPrivateProfileString(Tag,"Pattern","", Pattern.GetBuffer(64), 64, IniFilePath);
- Pattern.ReleaseBuffer();
- strcpy(pGlobalPluginHdr->Plugin[Index].Name, Name);
- strcpy(pGlobalPluginHdr->Plugin[Index].Pattern, Pattern);
- pGlobalPluginHdr->Plugin[Index].dwFlags = GetPrivateProfileInt(Tag,"Flags",0, IniFilePath);
- }
- SyncPlugins(); // We only need to do this once
- if (!CreateMasterFsys(IniFilePath))
- {
- fail:
- delete pVfSys;
- *phWnd = NULL;
- pVfSys = NULL;
- delete pSmem;
- pSmem = NULL;
- pLock->UnlockWrite();
- return FALSE;
- }
- }
- else
- {
- // Initialize only..
- SyncPlugins();
- SyncDeviceDrivers();
- }
- pLock->UnlockWrite();
- return TRUE;
- }
- // End of entry point
- void SyncPlugins()
- {
- pGlobalPluginHdr = (GLOBAL_VFSYS_PLUGIN_HDR *)pSmem->__Ptr(
- *(SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_PLUGINS]));
- if (pGlobalPluginHdr)
- {
- for(int Index = 0; Index < PLUGIN_TABLE_SIZE; Index++)
- {
- if (!pGlobalPluginHdr->Plugin[Index].Name[0])
- break;
- if (!LocalPluginTable[Index].hDll)
- {
- // Load it
- // Try to load the device driver
- if ((LocalPluginTable[Index].hDll = LoadLibrary(pGlobalPluginHdr->Plugin[Index].Name)) == NULL)
- {
- CLog::GetLog()->LogMsg(LOGF_ERROR,"VfSys.dll: SyncPlugins() - Failed to load plugin'%s'. %s",
- pGlobalPluginHdr->Plugin[Index].Name, GetLastErrorText());
- continue;
- }
- // Find function(s)
- LocalPluginTable[Index].OnFoundFile =
- (DWORD (*)(
- SMEM smDir, // Parent directory
- CFileInfo& Info, // File info from device
- SMEM smNode, // Node, 0 if new file
- CString& RealName, //File system name
- CString& VisualName, // The name the user will see
- CString& LinkTo, // Link to
- LPCSTR FsysPath, // File system path to parent dir
- FILE_SYSTEM *pFsys, // Current file system
- CDeviceDriver *pDev // Current device
- ))GetProcAddress(
- LocalPluginTable[Index].hDll,
- "?OnFoundFile@@YAKUSMEM@@AAVCFileInfo@@0AAVCString@@22PBDPAUFILE_SYSTEM@@PAVCDeviceDriver@@@Z");
- CLog::GetLog()->LogMsg(LOGF_DEBUG,"VfSys.dll: SyncPlugins() - loaded plugin '%s'",
- pGlobalPluginHdr->Plugin[Index].Name);
- }
- }
- }
- }
- // 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, Tag);
- }
- 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, LPCSTR Tag)
- {
- 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)
- ScanPath(Vpath, TRUE, FALSE, TRUE);
- // Mount the file system
- if (!strcmp(Vpath,"/"))
- {
- // Root node.
- if (*pMasterNode)
- {
- 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);
- // Initialize data members
- strcpy(pFsys->DOSPath, Path);
- strcpy(pFsys->Vpath, Vpath);
- strcpy(pFsys->LoginName, User ? User : "");
- strcpy(pFsys->LoginPwd, Pwd ? Pwd : "");
- pFsys->DevIdx = DevIndex;
- pFsys->RootNode = 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)
- pFsysLast = (FILE_SYSTEM *)pSmem->__Ptr(pFsysLast->Next);
- if (pFsysLast)
- pFsysLast->Next = smFsys;
- }
- // Check for additional parameters..
- if (!GetPrivateProfileInt(Tag,"ScanOnDemand", TRUE, IniFilePath))
- ScanPath(Vpath, FALSE, TRUE, TRUE);
- 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