VfSys.cpp
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:29k
源码类别:

Ftp客户端

开发平台:

Visual C++

  1. // VfSys.cpp : Defines the initialization routines for the DLL.
  2. //
  3. #include "stdafx.h"
  4. #include <afxdllx.h>
  5. #define EXT
  6. #include "WarSoftware.h"
  7. #include "WarFsys.h"
  8. #include "VfFSys.h"
  9. #include "UnixFsysTypes.h"
  10. #include "FsysSecurity.h"
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16. static AFX_EXTENSION_MODULE VfSysDLL = { NULL, NULL };
  17. extern "C" int APIENTRY
  18. DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  19. {
  20. if (dwReason == DLL_PROCESS_ATTACH)
  21. {
  22. TRACE0("VFSYS.DLL Initializing!n");
  23. // Extension DLL one-time initialization
  24. AfxInitExtensionModule(VfSysDLL, hInstance);
  25. // Insert this DLL into the resource chain
  26. new CDynLinkLibrary(VfSysDLL);
  27. // Create therad sync object
  28. pLock = new CRWLock;
  29. if (!pLock->Create("VfSyslck"))
  30. {
  31. CLog::GetLog()->LogMsg(LOGF_ERROR,"VfSys.dll: DllMain() - Unable to create CRWLock object.");
  32. return 0;
  33. }
  34. }
  35. else if (dwReason == DLL_PROCESS_DETACH)
  36. {
  37. TRACE0("VFSYS.DLL Terminating!n");
  38. if (pVfSys)
  39. delete pVfSys;
  40. pVfSys = NULL;
  41. if (pLock)
  42. delete pLock;
  43. pLock = NULL;
  44. if (pSmem)
  45. delete pSmem;
  46. pSmem = NULL;
  47. }
  48. return 1;   // ok
  49. }
  50. ////////////////////////////////////////////////////////////////////////////////
  51. // Entry point
  52. BOOL OpenFsys(HWND *phWnd, LPCSTR IniFilePath)
  53. {
  54. if (pVfSys)
  55. {
  56. CLog::GetLog()->LogMsg(LOGF_ERROR,"CVirtualFileSystem::OpenFsys() - File system is already initialized.");
  57. SetLastError(ERROR_ALREADY_EXISTS);
  58. return FALSE;
  59. }
  60. // Initialize the security subsystem
  61. // Create a new root security file it it don't already exist.
  62. {
  63. HANDLE h = CFsysSecurity::LoadSecurityGroup("",FALSE);
  64. if (h == INVALID_HANDLE_VALUE)
  65. h = CFsysSecurity::OpenSecurityGroupForUpdate("");
  66. if (h == INVALID_HANDLE_VALUE)
  67. {
  68. CLog::GetLog()->LogMsg(LOGF_ERROR,"CVirtualFileSystem::OpenFsys() - Failed initialize the security subsystem.");
  69. return FALSE;
  70. }
  71. CFsysSecurity::CloseSecurityGroup(h);
  72. }
  73. pVfSys = new CVirtualFileSystem;
  74. if (!pVfSys->CreateEx(0, AfxRegisterWndClass(0),
  75. _T("Wirtual File System Notification Sink"),
  76. WS_OVERLAPPED, 0, 0, 0, 0, NULL, NULL))
  77. {
  78. CLog::GetLog()->LogMsg(LOGF_ERROR,"CVirtualFileSystem::OpenFsys() - Unable to create Virtual File System notify window!");
  79. return FALSE;
  80. }
  81. ASSERT(pVfSys->m_hWnd != NULL);
  82. ASSERT(CWnd::FromHandlePermanent(pVfSys->m_hWnd) == pVfSys);
  83. *phWnd = pVfSys->m_hWnd;
  84. pLock->WriteLock();
  85. // Create shared memory
  86. pSmem = new CSharedBlksMem;
  87. if (!pSmem->Create("VfSysmem", 
  88. GetPrivateProfileInt("fsys", "buffer", (1024 * 512), IniFilePath)))
  89. {
  90. delete pVfSys;
  91. pVfSys = NULL;
  92. phWnd = NULL;
  93. CLog::GetLog()->LogMsg(LOGF_ERROR,"CVirtualFileSystem::OpenFsys() - Unable to create shared memory");
  94. pLock->UnlockWrite();
  95. return FALSE;
  96. }
  97. // Create the master virtual file system
  98. if (pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_ROOTNODE] == NULL)
  99. {
  100. // Create global headers
  101. SMEM GlobalHdr = *(SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_DEVDRV]);
  102. if (GlobalHdr.ofs == 0)
  103. {
  104. if (!pSmem->sh_malloc(GlobalHdr, sizeof(GLOBAL_FSYS_DRIVERS_HDR)))
  105. {
  106. CLog::GetLog()->LogMsg(LOGF_ERROR,"CVirtualFileSystem::OpenFsys() - Failed to allocate GLOBAL_FSYS_DRIVERS_HDR");
  107. goto fail;
  108. }
  109. *(SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_DEVDRV]) = GlobalHdr;
  110. }
  111. if (!CreateMasterFsys(IniFilePath))
  112. {
  113. fail:
  114. delete pVfSys;
  115. *phWnd = NULL;
  116. pVfSys = NULL;
  117. delete pSmem;
  118. pSmem = NULL;
  119. pLock->UnlockWrite();
  120. return FALSE;
  121. }
  122. }
  123. pLock->UnlockWrite();
  124. return TRUE;
  125. }
  126. // End of entry point
  127. // Mount the master file system
  128. BOOL CreateMasterFsys(LPCSTR IniFilePath)
  129. {
  130. int Index;
  131. for(Index = 0;; Index++)
  132. {
  133. CString Tag, Path, Vpath, Driver, User, Pwd;
  134. int Copy, Timeout;
  135. Tag.Format("mount%d", Index);
  136. GetPrivateProfileString(Tag,"Path","", Path.GetBuffer(1024), 1024, IniFilePath);
  137. Path.ReleaseBuffer();
  138. if (Path.IsEmpty())
  139. break;
  140. GetPrivateProfileString(Tag,"VPath","", Vpath.GetBuffer(1024), 1024, IniFilePath);
  141. Vpath.ReleaseBuffer();
  142. if (Vpath.IsEmpty())
  143. break;
  144. GetPrivateProfileString(Tag,"Driver","", Driver.GetBuffer(1024), 1024, IniFilePath);
  145. Driver.ReleaseBuffer();
  146. if (Driver.IsEmpty())
  147. break;
  148. GetPrivateProfileString(Tag,"User","", User.GetBuffer(32), 32, IniFilePath);
  149. User.ReleaseBuffer();
  150. GetPrivateProfileString(Tag,"passwd","", Pwd.GetBuffer(32), 32, IniFilePath);
  151. Pwd.ReleaseBuffer();
  152. Copy = GetPrivateProfileInt(Tag,"Copy", FALSE, IniFilePath);
  153. Timeout = GetPrivateProfileInt(Tag,"Timeout", FALSE, IniFilePath);
  154. MountFsys(Path, Vpath, Driver, User, Pwd, Copy, Timeout, IniFilePath);
  155. }
  156. return Index > 0;
  157. }
  158. // Mount a file system
  159. BOOL MountFsys(LPCSTR Path, LPCSTR Vpath, LPCSTR Driver, LPCSTR User, LPCSTR Pwd, BOOL Copy, int Timeout, LPCSTR IniFilePath)
  160. {
  161. SMEM *pMasterNode = (SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_ROOTNODE]);
  162. int DevIndex = -1;
  163. CLog::GetLog()->LogMsg(LOGF_SYSTEM,"vfsys.dll: MountFsys(%s, %s, %s) - Mounting file system.",
  164. Path, Vpath, Driver);
  165. // Check if the driver is loaded
  166. SyncDeviceDrivers();
  167. if ((DevIndex = MountDeviceDriver(Driver)) == -1)
  168. return FALSE;
  169. if (pMasterNode->ofs != 0)
  170. ScanPath(Vpath, TRUE, FALSE, TRUE);
  171. // Mount the file system
  172. if (!strcmp(Vpath,"/"))
  173. {
  174. // Root node.
  175. if (pMasterNode->ofs != 0)
  176. {
  177. CLog::GetLog()->LogMsg(LOGF_ERROR,"vfsys.dll: MountFsys(%s, %s) - Root node already exist.",
  178. Path, Vpath);
  179. SetLastError(ERROR_ALREADY_EXISTS);
  180. return FALSE;
  181. }
  182. }
  183. // Create the file system structure
  184. int HdrLen = sizeof(FILE_SYSTEM);
  185. HdrLen += (strlen(Path) +1);
  186. HdrLen += (strlen(Vpath) +1);
  187. HdrLen += (strlen(User) +1);
  188. HdrLen += (strlen(Pwd) +1);
  189. SMEM smFsys;
  190. if (!pSmem->sh_malloc(smFsys, HdrLen))
  191. {
  192. CLog::GetLog()->LogMsg(LOGF_ERROR,"vfsys.dll: MountFsys(%s, %s) - Out of shared memory.",
  193. Path, Vpath);
  194. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  195. return FALSE;
  196. }
  197. FILE_SYSTEM *pFsysLast = (FILE_SYSTEM *)pSmem->__Ptr(*(SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_FSYS]));
  198. FILE_SYSTEM *pFsys = (FILE_SYSTEM *)pSmem->__Ptr(smFsys);
  199. LPSTR p = pFsys->Buf;
  200. LPCSTR pp;
  201. // Initialize data members
  202. pFsys->DOSPath = p; pp = Path; while(*pp) *p++ = *pp++; *p++ = 0;
  203. pFsys->Vpath = p; pp = Vpath; while(*pp) *p++ = *pp++; *p++ = 0;
  204. pFsys->LoginName = p; pp = User; while(*pp) *p++ = *pp++; *p++ = 0;
  205. pFsys->LoginPwd = p; pp = Pwd; while(*pp) *p++ = *pp++; *p++ = 0;
  206. pFsys->DevIdx = DevIndex;
  207. pFsys->RootNode.ofs = 0;
  208. pFsys->RootNode.blk = 0;
  209. pFsys->Next = pFsys->RootNode;
  210. pFsys->FlushDelay = Timeout;
  211. pFsys->DoCopy = Copy;
  212. if (!CreateRootNode(smFsys))
  213. return FALSE;
  214. // Initialize
  215. if (!pFsysLast)
  216. {
  217. // Link as first in list
  218. SMEM *pShdr = (SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_FSYS]);
  219. *pShdr = smFsys;
  220. }
  221. else
  222. {
  223. // Find the last fsys and link to it
  224. while(pFsysLast->Next.ofs)
  225. pFsysLast = (FILE_SYSTEM *)pSmem->__Ptr(pFsysLast->Next);
  226. pFsysLast->Next = smFsys;
  227. }
  228. return TRUE;
  229. }
  230. // Syncronize the local driver table against the global table
  231. BOOL SyncDeviceDrivers()
  232. {
  233. SMEM *pSmGlobalHdr = (SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_DEVDRV]);
  234. GLOBAL_FSYS_DRIVERS_HDR *pGlobalHdr = (GLOBAL_FSYS_DRIVERS_HDR *)pSmem->__Ptr(*pSmGlobalHdr);
  235. ASSERT(AfxIsValidAddress(pGlobalHdr, sizeof(GLOBAL_FSYS_DRIVERS_HDR)));
  236. for(int Index = 0;Index < DRIVER_TABLE_SIZE; Index++)
  237. {
  238. if (!*pGlobalHdr->Name[Index])
  239. break;
  240. if (!LocalDeviceDrivers[Index].pDevDr)
  241. MountDeviceDriver(pGlobalHdr->Name[Index]);
  242. }
  243. return TRUE;
  244. }
  245. // Mount a new device driver (if don't exist)
  246. // Return the index to the driver
  247. int MountDeviceDriver(LPCSTR Driver)
  248. {
  249. SMEM *pSmGlobalHdr = (SMEM *)(&pSmem->m_Segments[0].m_pSMH->pUserPtr[USERPTR_DEVDRV]);
  250. GLOBAL_FSYS_DRIVERS_HDR *pGlobalHdr = (GLOBAL_FSYS_DRIVERS_HDR *)pSmem->__Ptr(*pSmGlobalHdr);
  251. ASSERT(AfxIsValidString(Driver));
  252. ASSERT(AfxIsValidAddress(pGlobalHdr, sizeof(GLOBAL_FSYS_DRIVERS_HDR)));
  253. CLog::GetLog()->LogMsg(LOGF_SYSTEM,"vfsys.dll: MountDeviceDriver(%s) - Mounting device driver.",
  254. Driver);
  255. if (strlen(Driver) >= DRIVER_NAME_LEN)
  256. {
  257. CLog::GetLog()->LogMsg(LOGF_ERROR,"VfSys.dll: MountDeviceDriver() - Name of device '%s' is more than %d characters.",
  258. Driver, DRIVER_NAME_LEN - 1);
  259. return -1;
  260. }
  261. int Index = FindDeviceDriver(Driver);
  262. if (Index < 0)
  263. {
  264. // Find a new empty slot
  265. for(Index = 0;Index < DRIVER_TABLE_SIZE; Index++)
  266. {
  267. if (!*pGlobalHdr->Name[Index])
  268. break;
  269. }
  270. strcpy(pGlobalHdr->Name[Index], Driver);
  271. }
  272. if (Index >= DRIVER_TABLE_SIZE)
  273. {
  274. CLog::GetLog()->LogMsg(LOGF_ERROR,"VfSys.dll: MountDeviceDriver() - Device table is full (%d entries). Unable to add '%s'.",
  275. DRIVER_TABLE_SIZE, Driver);
  276. return -1;
  277. }
  278. // Try to load the device driver
  279. if ((LocalDeviceDrivers[Index].hDll = LoadLibrary(Driver)) == NULL)
  280. {
  281. CLog::GetLog()->LogMsg(LOGF_ERROR,"VfSys.dll: MountDeviceDriver() - Failed to load device driver '%s'. %s",
  282. Driver, GetLastErrorText());
  283. return -1;
  284. }
  285. HANDLE (*DevOpenDevice)(CFileSystemTemplate *pInfo);
  286. if ((DevOpenDevice = (HANDLE(*)(CFileSystemTemplate *pInfo))GetProcAddress(
  287. LocalDeviceDrivers[Index].hDll, 
  288. "?DevOpenDevice@@YAPAXPAVCFileSystemTemplate@@@Z")) == NULL)
  289. {
  290. CLog::GetLog()->LogMsg(LOGF_ERROR,"VfSys.dll: MountDeviceDriver() - Could not find 'DevOpenDevice