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

Ftp客户端

开发平台:

Visual C++

  1. // This is part of the WAR SOFTWARE SERIES initiated by Jarle Aase
  2. // Copyright 1996 by Jarle Aase. All rights reserved.
  3. // See the "War Software Series Licende Agreement" for details concerning 
  4. // use and distribution.
  5. // ---
  6. // This source code, executables and programs containing source code or
  7. // binaries or proprietetary technology from the War Software Series are
  8. // NOT alloed used, viewed or tested by any governmental agencies in
  9. // any countries. This includes the government, departments, police, 
  10. // military etc.
  11. // ---
  12. // This file is intended for use with Tab space = 2
  13. // Created and maintained in MSVC Developer Studio
  14. // ---
  15. // NAME : hdd.cpp
  16. // PURPOSE : Basic Win95/NT file system driver
  17. // PROGRAM : 
  18. // DATE : March 15 1997
  19. // AUTHOR : Jarle Aase
  20. // ---
  21. //
  22. // REVISION HISTORY
  23. // 
  24. #include "stdafx.h"
  25. #include <afxdllx.h>
  26. #include "WarSoftware.h"
  27. #include "WarFsys.h"
  28. #include "FsysSecurity.h"
  29. #include "hdd.h"
  30. #include "UnixFsysTypes.h"
  31. #ifdef _DEBUG
  32. #define new DEBUG_NEW
  33. #undef THIS_FILE
  34. static char THIS_FILE[] = __FILE__;
  35. #endif
  36. static AFX_EXTENSION_MODULE HddDLL = { NULL, NULL };
  37. void MapDataToFileInfo(WIN32_FIND_DATA& Data, CFileInfo& FileInfo, HANDLE hSecGrp);
  38. static CString DOSPath(LPCSTR Path);
  39. BOOL IsForbiddenFile(WIN32_FIND_DATA& Data);
  40. extern "C" int APIENTRY
  41. DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  42. {
  43. if (dwReason == DLL_PROCESS_ATTACH)
  44. {
  45. TRACE0("HDD.DLL Initializing!n");
  46. // Extension DLL one-time initialization
  47. AfxInitExtensionModule(HddDLL, hInstance);
  48. // Insert this DLL into the resource chain
  49. new CDynLinkLibrary(HddDLL);
  50. }
  51. else if (dwReason == DLL_PROCESS_DETACH)
  52. {
  53. TRACE0("HDD.DLL Terminating!n");
  54. }
  55. return 1;   // ok
  56. }
  57. // Entry point
  58. // Return INVALD_HANDLE_VALUE on error
  59. HANDLE DevOpenDevice(CFileSystemTemplate *pInfo)
  60. {
  61. pInfo->CreateDirectory = __fs__CreateDirectory;
  62. pInfo->RemoveDirectory = __fs__RemoveDirectory;
  63. pInfo->DeleteFile = __fs__DeleteFile;
  64. pInfo->GetDiskFreeSpace = __fs__GetDiskFreeSpace;
  65. pInfo->MoveFile = __fs__MoveFile;
  66. pInfo->CloseHandle = __fs__CloseHandle;
  67. pInfo->FindFirstFile = __fs__FindFirstFile;
  68. pInfo->FindNextFile = __fs__FindNextFile;
  69. pInfo->FindClose = __fs__FindClose;
  70. pInfo->GetFileAttributes = __fs__GetFileAttributes;
  71. pInfo->GetDriveType = __fs__GetDriveType;
  72. pInfo->GetFileSize = __fs__GetFileSize;
  73. pInfo->Seek = __fs__Seek;
  74. pInfo->CreateFile = __fs__CreateFile;
  75. pInfo->ReadFile = __fs__ReadFile;
  76. pInfo->WriteFile = __fs__WriteFile;
  77. pInfo->GetSecurityHandleFromFindHandle = __fs__GetSecurityHandleFromFindHandle;
  78. if (IsNT())
  79. {
  80. pInfo->ReadFileEx = __fs__ReadFileEx;
  81. pInfo->WriteFileEx = __fs__WriteFileEx;
  82. }
  83. return (HANDLE)pInfo;
  84. }
  85. void DevCloseDevice(HANDLE hVal)
  86. {
  87. }
  88. ///////////////////////////////////////////////////////////////////////////////////////
  89. // Physical IO functions
  90. // IO calll
  91. BOOL __fs__CreateDirectory(LPCTSTR Path)
  92. {
  93. return ::CreateDirectory(DOSPath(Path), NULL);
  94. }
  95. BOOL __fs__RemoveDirectory(LPCTSTR lpPathName)
  96. {
  97. return ::RemoveDirectory(DOSPath(lpPathName));
  98. }
  99. BOOL __fs__DeleteFile(LPCTSTR Path)
  100. {
  101. return DeleteFile(DOSPath(Path));
  102. }
  103. FLEN __fs__GetDiskFreeSpace(LPCSTR Path)
  104. {
  105. DWORD 
  106. SectorsPerCluster,
  107. BytesPerSector,
  108. NumberOfFreeClusters,
  109. TotalNumberOfClusters;
  110. if (!GetDiskFreeSpace(
  111. DOSPath(Path), 
  112. &SectorsPerCluster,
  113. &BytesPerSector,
  114. &NumberOfFreeClusters,
  115. &TotalNumberOfClusters))
  116. return INVALID_FLEN_VALUE;
  117. FLEN Rval = (FLEN)NumberOfFreeClusters;
  118. Rval *= (FLEN)SectorsPerCluster;
  119. Rval *= (FLEN)BytesPerSector;
  120. return Rval;
  121. }
  122. BOOL __fs__MoveFile(LPCTSTR ExistingFileName, LPCTSTR NewFileName)
  123. {
  124. return ::MoveFile(DOSPath(ExistingFileName), DOSPath(NewFileName));
  125. }
  126. void __fs__CloseHandle(HANDLE hVal)
  127. {
  128. CloseHandle(hVal);
  129. }
  130. HANDLE __fs__FindFirstFile(LPCTSTR lpFileName, CFileInfo& FileInfo)
  131. {
  132. WIN32_FIND_DATA Data;
  133. CString MyPath = DOSPath(lpFileName);
  134. HANDLE hVal = ::FindFirstFile(MyPath, &Data);
  135. if (hVal == INVALID_HANDLE_VALUE)
  136. return INVALID_HANDLE_VALUE;
  137. while(IsForbiddenFile(Data))
  138. {
  139. if (!FindNextFile(hVal, &Data))
  140. {
  141. ::FindClose(hVal);
  142. return INVALID_HANDLE_VALUE;
  143. }
  144. }
  145. CDevFindFile *pDFF = new CDevFindFile;
  146. if (!pDFF->Create(MyPath, hVal))
  147. {
  148. delete pDFF;
  149. return INVALID_HANDLE_VALUE;
  150. }
  151. MapDataToFileInfo(Data, FileInfo, pDFF->m_hSecurityGrp);
  152. return (HANDLE)pDFF;
  153. }
  154. BOOL __fs__FindNextFile(HANDLE hVal, CFileInfo& FileInfo)
  155. {
  156. WIN32_FIND_DATA Data;
  157. CDevFindFile *pDFF = (CDevFindFile *)hVal;
  158. if (!::FindNextFile(pDFF->m_hFindFile, &Data))
  159. return FALSE;
  160. while(IsForbiddenFile(Data))
  161. {
  162. if (!FindNextFile(pDFF->m_hFindFile, &Data))
  163. return FALSE;
  164. }
  165. MapDataToFileInfo(Data, FileInfo, pDFF->m_hSecurityGrp);
  166. return TRUE;
  167. }
  168. void MapDataToFileInfo(WIN32_FIND_DATA& Data, CFileInfo& FileInfo, HANDLE hSecGrp)
  169. {
  170. // Standard info
  171. FileInfo.m_Name = Data.cFileName;
  172. FileInfo.m_DOSflags = Data.dwFileAttributes;
  173. FileInfo.m_CreationTime = Data.ftCreationTime;
  174. FileInfo.m_LastModifyTime = Data.ftLastWriteTime;
  175. FileInfo.m_FileSize = (FLEN)((FLEN)Data.nFileSizeHigh * (FLEN)MAXDWORD) + (FLEN)Data.nFileSizeLow;
  176. // Security info
  177. CFsysSecurity::GetFsysSecurityProperties(hSecGrp, FileInfo);
  178. // Physical facts
  179. if (Data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  180. FileInfo.m_Flags |= NODE_DIR;
  181. if (Data.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
  182. FileInfo.m_Flags |= NODE_READONLY;
  183. }
  184. void __fs__FindClose(HANDLE hVal)
  185. {
  186. CDevFindFile *pDFF = (CDevFindFile *)hVal;
  187. delete pDFF;
  188. }
  189. DWORD __fs__GetFileAttributes(LPCTSTR lpFileName )
  190. {
  191. return ::GetFileAttributes(DOSPath(lpFileName));
  192. }
  193. UINT __fs__GetDriveType(LPCTSTR lpRootPathName)
  194. {
  195. return GetDriveType(DOSPath(lpRootPathName));
  196. }
  197. FLEN __fs__GetFileSize(HANDLE hFile)
  198. {
  199. DWORD LoDWord, HiDWord;
  200. FLEN Rval;
  201. LoDWord = GetFileSize(hFile, &HiDWord);
  202. if ((LoDWord == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
  203. return INVALID_FLEN_VALUE;
  204. Rval = (FLEN)((FLEN)HiDWord * (FLEN)MAXDWORD) + (FLEN)LoDWord;
  205. return Rval;
  206. }
  207. FLEN __fs__Seek(HANDLE hFile, FLEN flOfs, DWORD dwFrom)
  208. {
  209. DWORD LoDWord, HiDWord;
  210. FLEN Rval;
  211. LoDWord = LODWORD(flOfs);
  212. HiDWord = HIDWORD(flOfs);
  213. LoDWord = SetFilePointer(hFile, LoDWord, (PLONG)&HiDWord, dwFrom);
  214. if ((LoDWord == 0xFFFFFFFF) && (GetLastError() != NO_ERROR))
  215. return INVALID_FLEN_VALUE;
  216. Rval = (FLEN)((FLEN)HiDWord * (FLEN)MAXDWORD) + (FLEN)LoDWord;
  217. return Rval;
  218. }
  219. HANDLE __fs__CreateFile(
  220.     LPCTSTR lpFileName, // pointer to name of the file 
  221.     DWORD dwDesiredAccess, // access (read-write) mode 
  222.     DWORD dwShareMode, // share mode 
  223.     LPSECURITY_ATTRIBUTES lpSecurityAttributes, // pointer to security descriptor 
  224.     DWORD dwCreationDistribution, // how to create 
  225.     DWORD dwFlagsAndAttributes, // file attributes 
  226.     HANDLE hTemplateFile  // handle to file with attributes to copy  
  227.    )
  228. {
  229. return ::CreateFile(
  230. DOSPath(lpFileName),
  231. dwDesiredAccess,
  232. dwShareMode,
  233. lpSecurityAttributes,
  234. dwCreationDistribution,
  235. dwFlagsAndAttributes,
  236. hTemplateFile);
  237. }
  238. BOOL __fs__ReadFile(
  239.     HANDLE hFile, // handle of file to read 
  240.     LPVOID lpBuffer, // address of buffer that receives data  
  241.     DWORD nNumberOfBytesToRead, // number of bytes to read 
  242.     LPDWORD lpNumberOfBytesRead, // address of number of bytes read 
  243.     LPOVERLAPPED lpOverlapped  // address of structure for data 
  244.    )
  245. {
  246. return ::ReadFile(
  247. hFile,
  248. lpBuffer,
  249. nNumberOfBytesToRead,
  250. lpNumberOfBytesRead,
  251. lpOverlapped);
  252. }
  253. BOOL __fs__ReadFileEx(
  254.     HANDLE hFile, // handle of file to read 
  255.     LPVOID lpBuffer, // address of buffer 
  256.     DWORD nNumberOfBytesToRead, // number of bytes to read 
  257.     LPOVERLAPPED lpOverlapped, // address of offset 
  258.     LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine  // address of completion routine 
  259.    )
  260. {
  261. return ::ReadFileEx(
  262. hFile,
  263. lpBuffer,
  264. nNumberOfBytesToRead,
  265. lpOverlapped,
  266. lpCompletionRoutine);
  267. }
  268. BOOL __fs__WriteFile(
  269.     HANDLE hFile, // handle to file to write to 
  270.     LPCVOID lpBuffer, // pointer to data to write to file 
  271.     DWORD nNumberOfBytesToWrite, // number of bytes to write 
  272.     LPDWORD lpNumberOfBytesWritten, // pointer to number of bytes written 
  273.     LPOVERLAPPED lpOverlapped  // pointer to structure needed for overlapped I/O
  274.    )
  275. {
  276. return WriteFile(
  277. hFile,
  278. lpBuffer,
  279. nNumberOfBytesToWrite,
  280. lpNumberOfBytesWritten,
  281. lpOverlapped);
  282. }
  283. BOOL __fs__WriteFileEx(
  284.     HANDLE hFile,  // handle to output file
  285.     LPCVOID lpBuffer,  // pointer to input buffer
  286.     DWORD nNumberOfBytesToWrite,  // number of bytes to write
  287.     LPOVERLAPPED lpOverlapped,  // pointer to async. i/o data
  288.     LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine  // ptr. to completion routine
  289.    )
  290. {
  291. return WriteFileEx(
  292. hFile,
  293. lpBuffer,
  294. nNumberOfBytesToWrite,
  295. lpOverlapped,
  296. lpCompletionRoutine);
  297. }
  298. HANDLE __fs__GetSecurityHandleFromFindHandle(HANDLE hFind)
  299. {
  300. CDevFindFile *pDFF = (CDevFindFile *)hFind;
  301. return pDFF->m_hSecurityGrp;
  302. }
  303. CString DOSPath(LPCSTR Path)
  304. {
  305. CString cBuf = Path;
  306. LPSTR p = cBuf.GetBuffer(1);
  307. while(*p)
  308. {
  309. if (*p == '/')
  310. *p = '\';
  311. ++p;
  312. }
  313. cBuf.ReleaseBuffer();
  314. return cBuf;
  315. }
  316. //////////////////////////////////////////////////////////////////////////////////////
  317. // CDevFindFile
  318. CDevFindFile::CDevFindFile()
  319. {
  320. m_hFindFile = INVALID_HANDLE_VALUE;
  321. m_hSecurityGrp = INVALID_HANDLE_VALUE;
  322. }
  323. CDevFindFile::~CDevFindFile()
  324. {
  325. if (m_hFindFile != INVALID_HANDLE_VALUE)
  326. ::FindClose(m_hFindFile);
  327. if (m_hSecurityGrp != INVALID_HANDLE_VALUE)
  328. CFsysSecurity::CloseSecurityGroup(m_hSecurityGrp);
  329. }
  330. // The paths here is the file system paths being scanned.
  331. BOOL CDevFindFile::Create(LPCSTR Path, HANDLE hVal)
  332. {
  333. BOOL TryPrevLevel = TRUE;
  334. m_hFindFile = hVal;
  335. int Levels = 0;
  336. CString MyPath = Path;
  337. LPSTR pOrigin;
  338. while(TryPrevLevel)
  339. {
  340. LPSTR p = pOrigin = MyPath.GetBuffer(1);
  341. p = strrchr(p, '\');
  342. if ((p > pOrigin) && (p[-1] != '\') && (p[-1] != ':'))
  343. *p = 0;
  344. else if (*++p == '*')
  345. *p = 0;
  346. else
  347. {
  348. MyPath = "";
  349. TryPrevLevel = FALSE;
  350. }
  351. MyPath.ReleaseBuffer();
  352. m_hSecurityGrp = CFsysSecurity::LoadSecurityGroup(MyPath, ++Levels == 1);
  353. if (m_hSecurityGrp == INVALID_HANDLE_VALUE)
  354. continue;
  355. return TRUE;
  356. }
  357. CLog::GetLog()->LogMsg(LOGF_WARNINGS,"CDevFindFile::Create(%s) - Failed to open security group.",
  358. Path);
  359. return FALSE;
  360. }
  361. BOOL IsForbiddenFile(WIN32_FIND_DATA& Data)
  362. {
  363. if ((Data.dwFileAttributes & (FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_TEMPORARY))
  364. || ((Data.cFileName[0] == '.') 
  365. && (!stricmp(Data.cFileName,".Index.txt") || (!stricmp(Data.cFileName,SECFILE)))))
  366. return TRUE;
  367. return FALSE;
  368. }