Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1398 lines
31 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. OsLayer.c
  5. Abstract: (win95)
  6. none.
  7. Abstract: (NT)
  8. We go thru these wrapper functions but the main implementation
  9. is in ntcsclow.c
  10. Author:
  11. Shishir Pardikar [Shishirp] 01-jan-1995
  12. Revision History:
  13. Joe Linn [joelinn] 01-jan-1997 ported to NT
  14. Joe Linn [joelinn] 22-aug-1997 moved NT stuff to ntcsclow.c
  15. --*/
  16. #include "precomp.h"
  17. #pragma hdrstop
  18. #pragma code_seg("PAGE")
  19. #ifndef CSC_RECORDMANAGER_WINNT
  20. #define WIN32_APIS
  21. #include "cshadow.h"
  22. #endif //ifndef CSC_RECORDMANAGER_WINNT
  23. //already included by shdcom.h #include "ifs.h"
  24. #ifdef CSC_RECORDMANAGER_WINNT
  25. #define Dbg (DEBUG_TRACE_MRXSMBCSC_OSLAYER)
  26. RXDT_DefineCategory(MRXSMBCSC_OSLAYER);
  27. #endif //ifdef CSC_RECORDMANAGER_WINNT
  28. //#define RXJOECSC_WHACKTRACE_FOR_OSLAYER
  29. #ifdef RXJOECSC_WHACKTRACE_FOR_OSLAYER
  30. #undef RxDbgTrace
  31. #define RxDbgTrace(a,b,__d__) {DbgPrint __d__;}
  32. #endif
  33. #ifdef CSC_BUILD_W_PROGRESS_CATCHERS
  34. VOID
  35. CscProgressInit (
  36. PCSC_PROGRESS_BLOCK ProgressBlock,
  37. ULONG Counter,
  38. PVOID NearArgs
  39. )
  40. {
  41. ProgressBlock->Counter = Counter;
  42. ProgressBlock->NearTop = &NearArgs;
  43. ProgressBlock->NearArgs = NearArgs;
  44. ProgressBlock->Progress = 0x80000000;
  45. ProgressBlock->LastBit = 0;
  46. ProgressBlock->Loops = 0;
  47. ProgressBlock->StackRemaining = IoGetRemainingStackSize();
  48. ProgressBlock->RetAddrP = ((PULONG)NearArgs)-1; //that's one ulong, boys....
  49. ProgressBlock->RetAddr = *(ProgressBlock->RetAddrP);
  50. ProgressBlock->SignatureOfEnd = '!dne';
  51. }
  52. VOID
  53. CscProgress (
  54. PCSC_PROGRESS_BLOCK ProgressBlock,
  55. ULONG Bit
  56. )
  57. {
  58. if( (*(ProgressBlock->RetAddrP)) != ProgressBlock->RetAddr ) {
  59. DbgPrint("Returnaddr has been trashed %08lx %08lx %08lx %08lx\n",
  60. ProgressBlock,Bit,
  61. (*(ProgressBlock->RetAddrP)),
  62. ProgressBlock->RetAddr);
  63. DbgBreakPoint();
  64. }
  65. ProgressBlock->Progress |= (1<<Bit);
  66. if (Bit <= ProgressBlock->LastBit) {
  67. ProgressBlock->Loops++;
  68. }
  69. ProgressBlock->LastBit = Bit;
  70. }
  71. #endif //ifdef CSC_RECORDMANAGER_WINNT
  72. /*
  73. */
  74. /********************** typedefs and defines ********************************/
  75. #ifdef HISTORY
  76. #define R0_OPENCREATE 0xD500
  77. #define R0_READ 0xD600
  78. #define R0_WRITE 0xD601
  79. #define R0_CLOSE 0xD700
  80. #define R0_GETFILESIZE 0xD800
  81. #define R0_RENAMEFILE 0x5600
  82. #define R0_FILELOCK 0x5C00
  83. #define R0_GETATTRIBUTE 0x4300
  84. #define R0_SETATTRIBUTE 0x4301
  85. #define R0_DELETEFILE 0x4100
  86. #endif //HISTORY
  87. #define R0_UNLOCKFILE 0x5C01
  88. #define R0_SETATTRIBUTE 0x4301
  89. #pragma intrinsic (memcmp, memcpy, memset, strcat, strcmp, strcpy, strlen)
  90. /********************** static data *****************************************/
  91. /********************** global data *****************************************/
  92. AssertData;
  93. AssertError;
  94. /********************** function prototypes *********************************/
  95. int Ring0Api();
  96. /****************************************************************************/
  97. /************************ File I/O ******************************************/
  98. #ifndef CSC_RECORDMANAGER_WINNT
  99. #pragma VxD_LOCKED_CODE_SEG
  100. #endif //ifndef CSC_RECORDMANAGER_WINNT
  101. long R0ReadWriteFile (ULONG uOper, CSCHFILE handle, ULONG pos, PVOID, long lCount);
  102. #ifndef R0ReadWriteFileEx
  103. long R0ReadWriteFileEx (ULONG uOper, CSCHFILE handle, ULONG pos, PVOID, long lCount, BOOL fInstrument);
  104. #endif //ifndef R0ReadWriteFile
  105. #ifndef R0ReadWriteFileEx2
  106. long R0ReadWriteFileEx2 (ULONG uOper, CSCHFILE handle, ULONG pos, PVOID, long lCount, ULONG uFlags);
  107. #endif //ifndef R0ReadWriteFileEx2
  108. CSCHFILE CreateFileLocal(LPSTR lpPath)
  109. {
  110. return (R0OpenFile(ACCESS_READWRITE, ACTION_OPENALWAYS, lpPath));
  111. }
  112. CSCHFILE OpenFileLocal(LPSTR lpPath)
  113. {
  114. return (OpenFileLocalEx(lpPath, FALSE));
  115. }
  116. CSCHFILE OpenFileLocalEx(LPSTR lpPath, BOOL fInstrument)
  117. {
  118. CSCHFILE hf;
  119. // unsigned uSize;
  120. hf = R0OpenFileEx(ACCESS_READWRITE, ACTION_OPENEXISTING, 0, lpPath, fInstrument);
  121. #ifdef DEBUG
  122. if (!hf)
  123. KdPrint(("OpenFileLocal: error opening %s \r\n", lpPath));
  124. #endif //DEBUG
  125. #ifdef OBSOLETE
  126. // BUGBUG-win9xonly temporary kludge till fixed in IFS
  127. hf = R0OpenFile(ACCESS_READWRITE, ACTION_OPENALWAYS, lpPath);
  128. if (hf)
  129. {
  130. if ((GetFileSizeLocal(hf, &uSize) < 0 ) || !uSize)
  131. {
  132. CloseFileLocal(hf);
  133. hf = CSCHFILE_NULL;
  134. }
  135. }
  136. #endif //OBSOLETE
  137. return (hf);
  138. }
  139. int FileExists
  140. (
  141. LPSTR lpPath
  142. )
  143. {
  144. CSCHFILE hf;
  145. if (hf = R0OpenFile(ACCESS_READWRITE, ACTION_OPENEXISTING, lpPath))
  146. {
  147. CloseFileLocal(hf);
  148. }
  149. return (hf != CSCHFILE_NULL);
  150. }
  151. long ReadFileLocal
  152. (
  153. CSCHFILE handle,
  154. ULONG pos,
  155. LPVOID pBuff,
  156. long lCount
  157. )
  158. {
  159. return(R0ReadWriteFile(R0_READFILE, handle, pos, pBuff,lCount));
  160. }
  161. long ReadFileLocalEx
  162. (
  163. CSCHFILE handle,
  164. ULONG pos,
  165. LPVOID pBuff,
  166. long lCount,
  167. BOOL fInstrument
  168. )
  169. {
  170. return(R0ReadWriteFileEx(R0_READFILE, handle, pos, pBuff,lCount, fInstrument));
  171. }
  172. long ReadFileLocalEx2
  173. (
  174. CSCHFILE handle,
  175. ULONG pos,
  176. LPVOID pBuff,
  177. long lCount,
  178. ULONG flags
  179. )
  180. {
  181. return(R0ReadWriteFileEx2(R0_READFILE, handle, pos, pBuff, lCount, flags));
  182. }
  183. long WriteFileLocal
  184. (
  185. CSCHFILE handle,
  186. ULONG pos,
  187. LPVOID pBuff,
  188. long lCount
  189. )
  190. {
  191. return(R0ReadWriteFile(R0_WRITEFILE, handle, pos, pBuff, lCount));
  192. }
  193. long WriteFileLocalEx
  194. (
  195. CSCHFILE handle,
  196. ULONG pos,
  197. LPVOID pBuff,
  198. long lCount,
  199. BOOL fInstrument
  200. )
  201. {
  202. return(R0ReadWriteFileEx(R0_WRITEFILE, handle, pos, pBuff, lCount, FALSE));
  203. }
  204. long WriteFileLocalEx2(
  205. CSCHFILE hf,
  206. unsigned long lSeek,
  207. LPVOID lpBuff,
  208. long cLength,
  209. ULONG flags
  210. )
  211. {
  212. return (R0ReadWriteFileEx2(R0_WRITEFILE, hf, lSeek, lpBuff, cLength, flags));
  213. }
  214. long ReadFileInContextLocal
  215. (
  216. CSCHFILE handle,
  217. ULONG pos,
  218. LPVOID pBuff,
  219. long lCount
  220. )
  221. {
  222. return(R0ReadWriteFile(R0_READFILE_IN_CONTEXT, handle, pos, pBuff,lCount));
  223. }
  224. long WriteFileInContextLocal
  225. (
  226. CSCHFILE handle,
  227. ULONG pos,
  228. LPVOID pBuff,
  229. long lCount
  230. )
  231. {
  232. return(R0ReadWriteFile(R0_WRITEFILE_IN_CONTEXT, handle, pos, pBuff, lCount));
  233. }
  234. #ifndef CSC_RECORDMANAGER_WINNT
  235. ULONG CloseFileLocal
  236. (
  237. CSCHFILE handle
  238. )
  239. {
  240. ULONG uOp = R0_CLOSEFILE;
  241. _asm
  242. {
  243. mov eax, uOp
  244. mov ebx, handle
  245. call Ring0Api
  246. jc error
  247. xor eax,eax
  248. error:
  249. }
  250. }
  251. #endif //ifndef CSC_RECORDMANAGER_WINNT
  252. CSCHFILE R0OpenFile
  253. (
  254. USHORT usOpenFlags,
  255. UCHAR bAction,
  256. LPSTR lpPath
  257. )
  258. {
  259. return (R0OpenFileEx(usOpenFlags, bAction, FILE_ATTRIBUTE_NORMAL, lpPath, FALSE));
  260. }
  261. #ifndef CSC_RECORDMANAGER_WINNT
  262. CSCHFILE R0OpenFileEx
  263. (
  264. USHORT usOpenFlags,
  265. UCHAR bAction,
  266. ULONG ulAttr,
  267. LPSTR lpPath,
  268. BOOL fInstrument
  269. )
  270. {
  271. ULONG uOper = R0_OPENCREATFILE;
  272. UCHAR bR0Opt = R0_SWAPPER_CALL;
  273. ulAttr; // ignore ulAttr on win9x
  274. _asm
  275. {
  276. mov eax, uOper
  277. mov bx, usOpenFlags
  278. mov cx, 0
  279. mov dl, bAction
  280. mov dh, bR0Opt
  281. mov esi, lpPath
  282. call Ring0Api
  283. jnc ok
  284. xor eax,eax
  285. ok:
  286. }
  287. }
  288. #endif //ifndef CSC_RECORDMANAGER_WINNT
  289. long R0ReadWriteFile
  290. (
  291. ULONG uOper,
  292. CSCHFILE handle,
  293. ULONG pos,
  294. PVOID pBuff,
  295. long lCount
  296. )
  297. {
  298. return (R0ReadWriteFileEx(uOper, handle, pos, pBuff, lCount, FALSE));
  299. }
  300. #ifndef CSC_RECORDMANAGER_WINNT
  301. long R0ReadWriteFileEx (// YUK
  302. ULONG uOper,
  303. CSCHFILE handle,
  304. ULONG pos,
  305. PVOID pBuff,
  306. long lCount,
  307. BOOL fInstrument // don't care
  308. )
  309. {
  310. return (R0ReadWriteFileEx2(uOper, handle, pos, pBuff, lCount, 0));
  311. }
  312. long R0ReadWriteFileEx2
  313. (
  314. ULONG uOper,
  315. CSCHFILE handle,
  316. ULONG pos,
  317. PVOID pBuff,
  318. long lCount,
  319. ULONG ulFlags
  320. )
  321. {
  322. int retValue;
  323. if (lCount < 0 )
  324. return -1;
  325. _asm
  326. {
  327. mov eax, uOper
  328. mov ebx, handle
  329. mov ecx, lCount
  330. mov edx, pos
  331. mov esi, pBuff
  332. call Ring0Api
  333. jnc done
  334. mov eax,0xffffffff
  335. ; neg eax ; return negative error codes
  336. done:
  337. }
  338. }
  339. #endif //ifndef CSC_RECORDMANAGER_WINNT
  340. #ifndef CSC_RECORDMANAGER_WINNT
  341. int GetFileSizeLocal
  342. (
  343. CSCHFILE handle,
  344. PULONG lpuSize
  345. )
  346. {
  347. ULONG uSize;
  348. int iRes=0;
  349. _asm
  350. {
  351. mov eax, R0_GETFILESIZE
  352. mov ebx, handle
  353. call Ring0Api
  354. jnc ok
  355. mov iRes,0xffffffff
  356. ok:
  357. mov uSize, eax
  358. }
  359. *lpuSize = uSize;
  360. return (iRes);
  361. }
  362. #endif //ifndef CSC_RECORDMANAGER_WINNT
  363. #ifndef CSC_RECORDMANAGER_WINNT
  364. int GetAttributesLocal
  365. (
  366. LPSTR lpPath,
  367. ULONG *lpuAttributes
  368. )
  369. {
  370. int iRes=0;
  371. USHORT usAttrib=0;
  372. _asm
  373. {
  374. mov eax, R0_FILEATTRIBUTES
  375. mov esi, lpPath
  376. call Ring0Api
  377. jnc ok
  378. mov iRes,0xffffffff
  379. ok:
  380. mov usAttrib, cx
  381. }
  382. *lpuAttributes = (ULONG)usAttrib;
  383. return (iRes);
  384. }
  385. #endif //ifndef CSC_RECORDMANAGER_WINNT
  386. #ifndef CSC_RECORDMANAGER_WINNT
  387. int SetAttributesLocal
  388. (
  389. LPSTR lpPath,
  390. ULONG uAttributes
  391. )
  392. {
  393. int iRes=0;
  394. USHORT usAttrib=(USHORT)uAttributes;
  395. _asm
  396. {
  397. mov eax, R0_SETATTRIBUTE
  398. mov esi, lpPath
  399. mov cx, usAttrib
  400. call Ring0Api
  401. jnc ok
  402. mov iRes,0xffffffff
  403. ok:
  404. }
  405. return (iRes);
  406. }
  407. #endif //ifndef CSC_RECORDMANAGER_WINNT
  408. #ifndef CSC_RECORDMANAGER_WINNT
  409. int RenameFileLocal
  410. (
  411. LPSTR lpFrom,
  412. LPSTR lpTo
  413. )
  414. {
  415. int iRes=0;
  416. #ifdef DEBUG
  417. int iErr;
  418. #endif //DEBUG
  419. _asm
  420. {
  421. mov eax, R0_RENAMEFILE
  422. mov esi, lpFrom
  423. mov edx, lpTo
  424. call Ring0Api
  425. jnc ok
  426. mov iRes,0xffffffff
  427. #ifdef DEBUG
  428. mov iErr,eax
  429. #endif
  430. ok:
  431. }
  432. #ifdef DEBUG
  433. if (iRes == 0xffffffff)
  434. {
  435. KdPrint(("RenameFileLocal: error %d renaming %s to %s\r\n", iErr, lpFrom, lpTo));
  436. }
  437. #endif //DEBUG
  438. return (iRes);
  439. }
  440. #endif //ifndef CSC_RECORDMANAGER_WINNT
  441. #ifndef CSC_RECORDMANAGER_WINNT
  442. int DeleteFileLocal
  443. (
  444. LPSTR lpName,
  445. USHORT usAttrib
  446. )
  447. {
  448. int iRes=0;
  449. #ifdef DEBUG
  450. int iErr;
  451. #endif //DEBUG
  452. _asm
  453. {
  454. mov eax, R0_DELETEFILE
  455. mov esi, lpName
  456. mov cx, usAttrib
  457. call Ring0Api
  458. jnc ok
  459. mov iRes,0xffffffff
  460. #ifdef DEBUG
  461. mov iErr,eax
  462. #endif
  463. ok:
  464. }
  465. #ifdef DEBUG
  466. if (iRes == 0xffffffff)
  467. {
  468. KdPrint(("DeleteFileLocal: error %d deleting %s \r\n", iErr, lpName));
  469. }
  470. #endif //DEBUG
  471. return (iRes);
  472. }
  473. #endif //ifndef CSC_RECORDMANAGER_WINNT
  474. int GetDiskFreeSpaceLocal(
  475. int indx,
  476. ULONG *lpuSectorsPerCluster,
  477. ULONG *lpuBytesPerSector,
  478. ULONG *lpuFreeClusters,
  479. ULONG *lpuTotalClusters
  480. )
  481. {
  482. #ifndef CSC_RECORDMANAGER_WINNT
  483. int iRes = 0;
  484. BYTE bIndx = (BYTE)indx;
  485. USHORT usSPC, usBPS, usFC, usTC;
  486. _asm
  487. {
  488. mov eax, R0_GETDISKFREESPACE
  489. mov dl, bIndx
  490. call Ring0Api
  491. jnc ok
  492. mov iRes, 0xffffffff
  493. jmp done
  494. ok:
  495. mov usSPC, ax
  496. mov usBPS, cx
  497. mov usFC, bx
  498. mov usTC, dx
  499. done:
  500. }
  501. if (!iRes)
  502. {
  503. *lpuSectorsPerCluster = (ULONG)usSPC;
  504. *lpuBytesPerSector = (ULONG)usBPS;
  505. *lpuFreeClusters = (ULONG)usFC;
  506. *lpuTotalClusters = (ULONG)usTC;
  507. }
  508. return (iRes);
  509. #else
  510. ASSERT(FALSE);
  511. return(-1);
  512. #endif //ifndef CSC_RECORDMANAGER_WINNT
  513. }
  514. int FileLockLocal( CSCHFILE hf,
  515. ULONG offsetLock,
  516. ULONG lengthLock,
  517. ULONG idProcess,
  518. BOOL fLock
  519. )
  520. {
  521. #ifndef CSC_RECORDMANAGER_WINNT
  522. int iRes = 0;
  523. ULONG uOp = (fLock)?R0_LOCKFILE:R0_UNLOCKFILE;
  524. _asm
  525. {
  526. mov eax, uOp
  527. mov ebx, hf
  528. mov edx, offsetLock
  529. mov esi, lengthLock
  530. mov ecx, idProcess
  531. call Ring0Api
  532. jnc ok
  533. mov iRes,0xffffffff
  534. ok:
  535. }
  536. return (iRes);
  537. #else
  538. ASSERT(FALSE);
  539. return(-1);
  540. #endif //ifndef CSC_RECORDMANAGER_WINNT
  541. }
  542. int FindOpenRemote
  543. (
  544. PIOREQ pir,
  545. LPHFREMOTE lphFind
  546. )
  547. {
  548. int iRet;
  549. PRESOURCE pResource = pir->ir_rh;
  550. PFINDINFO pFindInfo;
  551. hndlfunc hfnSav;
  552. hfunc_t pSav;
  553. if(!(pFindInfo = (PFINDINFO)PAllocElem(SizeofFindRemote)))
  554. {
  555. #if VERBOSE > 1
  556. KdPrint(("FindOpenRemote: Error creating FindOpen structure \r\n"));
  557. #endif //VERBOSE > 1
  558. return -1; // BUGBUG-win9xonly return proper error code
  559. }
  560. mSetBits(pFindInfo->usLocalFlags, FLAG_FINDINFO_INTERNAL_HANDLE);
  561. // HACK save the ioreq structure
  562. pFindInfo->pnextFindInfo = (PFINDINFO)pir;
  563. // Save it
  564. memcpy(LpIoreqFromFindInfo(pFindInfo), pir, sizeof(ioreq));
  565. pir->ir_data = LpFind32FromFindInfo(pFindInfo);
  566. pir->ir_attr = FILE_ATTRIBUTE_ALL;
  567. pir->ir_error = 0;
  568. pir->ir_flags = RESTYPE_DISK;
  569. pSav = pir->ir_hfunc;
  570. pir->ir_hfunc = (hfunc_t)&hfnSav;
  571. pir->ir_rh = pResource->rhPro;
  572. (*(pResource->pVolTab->vfn_func[VFN_FINDOPEN]))(pir);
  573. iRet = pir->ir_error;
  574. pir->ir_hfunc = pSav;
  575. pir->ir_rh = (rh_t)pResource;
  576. pir->ir_error = 0;
  577. if (!iRet)
  578. {
  579. // succeeded
  580. // Save his file handle
  581. pFindInfo->fhProFind = pir->ir_fh;
  582. // Save his function table
  583. pFindInfo->hfFindHandle = hfnSav;
  584. // point back to our parent
  585. pFindInfo->pResource = pResource;
  586. #if VERBOSE > 1
  587. {
  588. KdPrint(("FindOpenRemote: lpFind32=%x Lowdate=%x Highdate=%x \r\n"
  589. , LpFind32FromFindInfo(pFindInfo)
  590. , LpFind32FromFindInfo(pFindInfo)->ftLastWriteTime.dwLowDateTime
  591. , LpFind32FromFindInfo(pFindInfo)->ftLastWriteTime.dwHighDateTime));
  592. }
  593. #endif //VERBOSE > 1
  594. }
  595. else
  596. {
  597. memcpy(pir, LpIoreqFromFindInfo(pFindInfo), sizeof(ioreq));
  598. FreeMem(pFindInfo);
  599. #if VERBOSE < 2
  600. if (iRet != ERROR_NO_MORE_FILES)
  601. #endif //VERBOSE < 2
  602. #if VERBOSE > 1
  603. KdPrint(("FindOpenRemote: Error %x \r\n", iRet));
  604. #endif //VERBOSE > 1
  605. pFindInfo = NULL;
  606. }
  607. *lphFind = (HFREMOTE)pFindInfo;
  608. return (iRet);
  609. }
  610. int FindNextRemote
  611. (
  612. PFINDINFO pFindInfo,
  613. PIOREQ pir
  614. )
  615. {
  616. PRESOURCE pResource = pir->ir_rh;
  617. int iRet;
  618. if (mQueryBits(pFindInfo->usLocalFlags, FLAG_FINDINFO_INTERNAL_HANDLE))
  619. {
  620. pir = (PIOREQ)(pFindInfo->pnextFindInfo);
  621. }
  622. else
  623. {
  624. Assert(pir);
  625. }
  626. pir->ir_data = LpFind32FromFindInfo(pFindInfo);
  627. pir->ir_error = 0;
  628. pir->ir_rh = pFindInfo->pResource->rhPro;
  629. pir->ir_fh = pFindInfo->fhProFind;
  630. (*(pFindInfo->hfFindHandle.hf_read))(pir);
  631. iRet = pir->ir_error;
  632. pir->ir_rh = (rh_t)(pFindInfo->pResource);
  633. pir->ir_error = 0;
  634. return (iRet);
  635. }
  636. int FindCloseRemote
  637. (
  638. PFINDINFO pFindInfo,
  639. PIOREQ pir
  640. )
  641. {
  642. PRESOURCE pResource = pir->ir_rh;
  643. int iRet;
  644. if (mQueryBits(pFindInfo->usLocalFlags, FLAG_FINDINFO_INTERNAL_HANDLE))
  645. {
  646. pir = (PIOREQ)(pFindInfo->pnextFindInfo);
  647. }
  648. #if VERBOSE > 1
  649. KdPrint(("FindCloseRemote: hfind= %x fh=%x\r\n", pFindInfo, pFindInfo->fhProFind));
  650. #endif //VERBOSE > 1
  651. pir->ir_error = 0;
  652. pir->ir_rh = pFindInfo->pResource->rhPro;
  653. pir->ir_fh = pFindInfo->fhProFind;
  654. (*(pFindInfo->hfFindHandle.hf_misc->hm_func[HM_CLOSE]))(pir);
  655. iRet = pir->ir_error;
  656. pir->ir_rh = (rh_t)(pFindInfo->pResource);
  657. pir->ir_error = 0;
  658. if (iRet)
  659. {
  660. #if VERBOSE > 1
  661. KdPrint(("FindCloseRemote: error hf= %x #=%x \r\n", pFindInfo, iRet));
  662. #endif //VERBOSE > 1
  663. }
  664. if (mQueryBits(pFindInfo->usLocalFlags, FLAG_FINDINFO_INTERNAL_HANDLE))
  665. {
  666. Assert(pir == (PIOREQ)(pFindInfo->pnextFindInfo));
  667. memcpy(pir, LpIoreqFromFindInfo(pFindInfo), sizeof(ioreq));
  668. FreeMem(pFindInfo);
  669. }
  670. return (iRet);
  671. }
  672. int OpenFileRemote
  673. (
  674. PIOREQ pir,
  675. LPHFREMOTE lphFile
  676. )
  677. {
  678. return (OpenFileRemoteEx(pir, ACCESS_READWRITE, ACTION_OPENEXISTING, 0, lphFile));
  679. }
  680. int OpenFileRemoteEx
  681. (
  682. PIOREQ pir,
  683. UCHAR uchAccess,
  684. USHORT usOptions,
  685. ULONG ulAttr,
  686. LPHFREMOTE lphFile
  687. )
  688. {
  689. PRESOURCE pResource = (PRESOURCE)(pir->ir_rh);
  690. PFILEINFO pFileInfo;
  691. hndlfunc hfnSav;
  692. hfunc_t pSav;
  693. int iRet;
  694. if(!(pFileInfo = (PFILEINFO)PAllocElem(SizeofFileRemote)))
  695. {
  696. KdPrint(("OpenFileRemoteEx: Error creating File structure \r\n"));
  697. return -1; // BUGBUG-win9xonly return proper error code
  698. }
  699. mSetBits(pFileInfo->usLocalFlags, FLAG_FILEINFO_INTERNAL_HANDLE);
  700. // HACK save the ioreq structure pointer
  701. pFileInfo->pnextFileInfo = (PFILEINFO)pir;
  702. // Save it
  703. memcpy(LpIoreqFromFileInfo(pFileInfo), pir, sizeof(ioreq));
  704. pir->ir_flags = uchAccess;
  705. pir->ir_options = usOptions;
  706. pir->ir_attr = ulAttr;
  707. // Plug his resource handle back
  708. pir->ir_rh = pResource->rhPro;
  709. // Plug this in so servers understanding case can use it
  710. pir->ir_uFName = IFSLastElement(pir->ir_ppath)->pe_unichars;
  711. pSav = pir->ir_hfunc;
  712. memset((LPVOID)&hfnSav, 0, sizeof(hndlfunc));
  713. pir->ir_hfunc = (hfunc_t)&hfnSav;
  714. // and call his function
  715. (*(pResource->pVolTab->vfn_func[VFN_OPEN]))(pir);
  716. iRet = pir->ir_error;
  717. pir->ir_hfunc = pSav;
  718. // Plug our resource handle
  719. pir->ir_rh = (rh_t)pResource;
  720. pir->ir_error = 0;
  721. if (!iRet)
  722. {
  723. // succeeded
  724. // Save his file handle
  725. pFileInfo->fhProFile = pir->ir_fh;
  726. // Save his function table
  727. pFileInfo->hfFileHandle = hfnSav;
  728. // point back to our parent
  729. pFileInfo->pResource = pResource;
  730. #if VERBOSE > 1
  731. KdPrint(("OpenFileRemote: pFileInfo= %x fhPro=%x, read=%x write=%x\r\n"
  732. , pFileInfo, pFileInfo->fhProFile,
  733. hfnSav.hf_read, hfnSav.hf_write));
  734. #endif //VERBOSE > 1
  735. }
  736. else
  737. {
  738. memcpy(pir, LpIoreqFromFileInfo(pFileInfo), sizeof(ioreq));
  739. FreeMem(pFileInfo);
  740. #if VERBOSE > 1
  741. KdPrint(("OpenFileRemote: Error %x \r\n", iRet));
  742. #endif //VERBOSE > 1
  743. pFileInfo = NULL;
  744. }
  745. *lphFile = (HFREMOTE)pFileInfo;
  746. return (iRet);
  747. }
  748. int ReadFileRemote(
  749. PFILEINFO pFileInfo,
  750. PIOREQ pir,
  751. ULONG pos,
  752. LPVOID lpBuff,
  753. ULONG count
  754. )
  755. {
  756. int iRet;
  757. if(mQueryBits(pFileInfo->usLocalFlags, FLAG_FILEINFO_INTERNAL_HANDLE))
  758. {
  759. pir = (PIOREQ)(pFileInfo->pnextFileInfo);
  760. }
  761. pir->ir_data = lpBuff;
  762. pir->ir_length = count;
  763. pir->ir_pos = pos;
  764. pir->ir_options = 0;
  765. pir->ir_sfn = pFileInfo->sfnFile;
  766. pir->ir_pid = pFileInfo->pidFile;
  767. pir->ir_user = pFileInfo->userFile;
  768. pir->ir_rh = pFileInfo->pResource->rhPro;
  769. pir->ir_fh = pFileInfo->fhProFile;
  770. (*(pFileInfo->hfFileHandle.hf_read))(pir);
  771. iRet = pir->ir_error;
  772. pir->ir_rh = (rh_t)(pFileInfo->pResource);
  773. return ((!iRet)?pir->ir_length:-1);
  774. }
  775. int WriteFileRemote(
  776. PFILEINFO pFileInfo,
  777. PIOREQ pir,
  778. ULONG pos,
  779. LPVOID lpBuff,
  780. ULONG count
  781. )
  782. {
  783. int iRet;
  784. if(mQueryBits(pFileInfo->usLocalFlags, FLAG_FILEINFO_INTERNAL_HANDLE))
  785. {
  786. pir = (PIOREQ)(pFileInfo->pnextFileInfo);
  787. }
  788. pir->ir_data = lpBuff;
  789. pir->ir_length = count;
  790. pir->ir_pos = pos;
  791. pir->ir_options = 0;
  792. pir->ir_sfn = pFileInfo->sfnFile;
  793. pir->ir_pid = pFileInfo->pidFile;
  794. pir->ir_user = pFileInfo->userFile;
  795. pir->ir_rh = pFileInfo->pResource->rhPro;
  796. pir->ir_fh = pFileInfo->fhProFile;
  797. (*(pFileInfo->hfFileHandle.hf_write))(pir);
  798. iRet = pir->ir_error;
  799. pir->ir_rh = (rh_t)(pFileInfo->pResource);
  800. return ((!iRet)?pir->ir_length:-1);
  801. }
  802. int TimeStampRemote(
  803. PFILEINFO pFileInfo,
  804. LPFILETIME lpFt,
  805. int type
  806. )
  807. {
  808. BOOL fGetType;
  809. ioreq ir;
  810. PIOREQ pir = &ir;
  811. int iRet;
  812. fGetType = ((type==GET_MODIFY_DATETIME)||
  813. (type==GET_LAST_ACCESS_DATETIME)||
  814. (type==GET_CREATION_DATETIME));
  815. pir->ir_flags = (UCHAR)type;
  816. pir->ir_sfn = pFileInfo->sfnFile;
  817. pir->ir_pid = pFileInfo->pidFile;
  818. pir->ir_user = pFileInfo->userFile;
  819. pir->ir_rh = pFileInfo->pResource->rhPro;
  820. pir->ir_fh = pFileInfo->fhProFile;
  821. pir->ir_options = 0;
  822. if (!fGetType)
  823. {
  824. pir->ir_dostime = IFSMgr_Win32ToDosTime(*lpFt);
  825. }
  826. (*(pFileInfo->hfFileHandle.hf_write))(pir);
  827. iRet = pir->ir_error;
  828. if (!iRet && fGetType)
  829. {
  830. *lpFt = IFSMgr_DosToWin32Time(pir->ir_dostime);
  831. }
  832. return (iRet);
  833. }
  834. int CloseFileRemote(
  835. PFILEINFO pFileInfo,
  836. PIOREQ pir
  837. )
  838. {
  839. int iRet;
  840. if(mQueryBits(pFileInfo->usLocalFlags, FLAG_FILEINFO_INTERNAL_HANDLE))
  841. {
  842. pir = (PIOREQ)(pFileInfo->pnextFileInfo);
  843. }
  844. pir->ir_rh = pFileInfo->pResource->rhPro;
  845. pir->ir_fh = pFileInfo->fhProFile;
  846. pir->ir_options = 0;
  847. pir->ir_flags = CLOSE_FINAL;
  848. (*(pFileInfo->hfFileHandle.hf_misc->hm_func[HM_CLOSE]))(pir);
  849. iRet = pir->ir_error;
  850. if (iRet)
  851. {
  852. KdPrint(("CloseFileRemote: error hf= %x #=%x \r\n", pFileInfo, iRet));
  853. }
  854. else
  855. {
  856. }
  857. pir->ir_rh = (rh_t)(pFileInfo->pResource);
  858. if (mQueryBits(pFileInfo->usLocalFlags, FLAG_FILEINFO_INTERNAL_HANDLE))
  859. {
  860. memcpy((PIOREQ)(pFileInfo->pnextFileInfo), LpIoreqFromFileInfo(pFileInfo), sizeof(ioreq));
  861. FreeMem(pFileInfo);
  862. }
  863. return (iRet);
  864. }
  865. int CloseAllRemoteFiles( PRESOURCE pResource
  866. )
  867. {
  868. PFILEINFO pFileInfo = NULL;
  869. PFDB pFdb = NULL;
  870. ioreq sIoreq;
  871. for (pFileInfo = pResource->pheadFileInfo; pFileInfo; pFileInfo=pFileInfo->pnextFileInfo)
  872. {
  873. if (IsDupHandle(pFileInfo))
  874. continue;
  875. pFdb = pFileInfo->pFdb;
  876. if (pFileInfo->fhProFile)
  877. {
  878. // Do final close only once
  879. if (!(pFdb->usLocalFlags & FLAG_FDB_FINAL_CLOSE_DONE))
  880. {
  881. memset(&sIoreq, 0, sizeof(ioreq));
  882. CloseFileRemote(pFileInfo, &sIoreq);
  883. pFdb->usLocalFlags |= FLAG_FDB_FINAL_CLOSE_DONE;
  884. }
  885. pFileInfo->fhProFile = 0;
  886. if (pFileInfo->hfShadow)
  887. {
  888. if (mShadowSparse(pFdb->usFlags))
  889. {
  890. CloseFileLocal(pFileInfo->hfShadow);
  891. pFileInfo->hfShadow = 0;
  892. mSetBits(pFileInfo->usLocalFlags, FLAG_FILEINFO_INVALID_HANDLE);
  893. }
  894. }
  895. else
  896. {
  897. mSetBits(pFileInfo->usLocalFlags, FLAG_FILEINFO_INVALID_HANDLE);
  898. }
  899. }
  900. }
  901. for (pFdb = pResource->pheadFdb; pFdb; pFdb = pFdb->pnextFdb)
  902. {
  903. pFdb->usLocalFlags &= ~FLAG_FDB_FINAL_CLOSE_DONE;
  904. }
  905. return SRET_OK;
  906. }
  907. int CloseAllRemoteFinds( PRESOURCE pResource
  908. )
  909. {
  910. PFINDINFO pFindInfo = NULL;
  911. ioreq sIoreq;
  912. for (pFindInfo = pResource->pheadFindInfo; pFindInfo; pFindInfo=pFindInfo->pnextFindInfo)
  913. {
  914. if (pFindInfo->fhProFind)
  915. {
  916. memset(&sIoreq, 0, sizeof(ioreq));
  917. FindCloseRemote(pFindInfo, &sIoreq);
  918. pFindInfo->fhProFind = 0;
  919. pFindInfo->usLocalFlags |= FLAG_FINDINFO_INVALID_HANDLE;
  920. }
  921. }
  922. return SRET_OK;
  923. }
  924. int DisconnectResource(
  925. PRESOURCE pResource
  926. )
  927. {
  928. #ifdef RESOURCE
  929. ioreq sIoreq;
  930. CloseAllRemoteFiles(pResource);
  931. CloseAllRemoteFinds(pResource);
  932. memset(&sIoreq, 0, sizeof(ioreq));
  933. Assert(pResource->rhPro);
  934. Assert(pResource->pVolTab);
  935. // Let us restore his rh
  936. sIoreq.ir_rh = pResource->rhPro;
  937. // and call his function
  938. (*(pResource->pVolTab->vfn_func[VFN_DISCONNECT]))(&sIoreq);
  939. pResource->rhPro = 0;
  940. pResource->pVolTab = 0;
  941. return(sIoreq.ir_error);
  942. #endif //RESOURCE
  943. return (0);
  944. }
  945. #ifdef LATER
  946. int PUBLIC CommitFile(
  947. CSCHFILE hf
  948. )
  949. {
  950. return (-1);
  951. }
  952. #endif
  953. /*************************** Utility Functions ******************************/
  954. #ifndef CSC_RECORDMANAGER_WINNT
  955. //for NT, these have been macro-ed to the appropriate Rx Functions
  956. LPVOID AllocMem
  957. (
  958. ULONG uSize
  959. )
  960. {
  961. LPVOID lpBuff;
  962. if (lpBuff = (LPVOID)FGHS(uSize))
  963. {
  964. memset(lpBuff, 0, uSize);
  965. }
  966. return (lpBuff);
  967. }
  968. VOID FreeMem
  969. (
  970. LPVOID lp
  971. )
  972. {
  973. // CheckHeap(lp);
  974. if (lp)
  975. {
  976. RetHeap(lp);
  977. }
  978. }
  979. //for NT, these have been macro-ed to the appropriate Rx Functions
  980. LPVOID AllocMemPaged(
  981. ULONG uSize
  982. )
  983. {
  984. return NULL;
  985. }
  986. //for NT, these have been macro-ed to the appropriate Rx Functions
  987. VOID FreeMemPaged(
  988. LPVOID lp
  989. )
  990. {
  991. lp;
  992. }
  993. int GetAttributesLocalEx
  994. (
  995. LPSTR lpPath,
  996. BOOL fFile,
  997. ULONG *lpuAttributes
  998. )
  999. {
  1000. return(GetAttributesLocal(lpPath, lpuAttributes));
  1001. }
  1002. int
  1003. CreateDirectoryLocal(
  1004. LPSTR lpPath
  1005. )
  1006. {
  1007. return -1;
  1008. }
  1009. #endif //ifndef CSC_RECORDMANAGER_WINNT
  1010. #ifdef CSC_RECORDMANAGER_WINNT
  1011. PELEM PAllocElem
  1012. (
  1013. int cbSize
  1014. )
  1015. {
  1016. return ((PELEM)AllocMem(cbSize));
  1017. }
  1018. void FreeElem
  1019. (
  1020. PELEM p
  1021. )
  1022. {
  1023. FreeMem(p);
  1024. }
  1025. void LinkElem
  1026. (
  1027. PELEM pElem,
  1028. PPELEM ppHead
  1029. )
  1030. {
  1031. pElem->pnextElem = *ppHead;
  1032. *ppHead = pElem;
  1033. }
  1034. PELEM PUnlinkElem
  1035. (
  1036. PELEM pElem,
  1037. PPELEM ppHead
  1038. )
  1039. {
  1040. PELEM p;
  1041. for(;p = *ppHead; ppHead = &(p->pnextElem))
  1042. {
  1043. if (p == pElem)
  1044. {
  1045. // Found the guy
  1046. *ppHead = p->pnextElem;
  1047. return (p);
  1048. }
  1049. }
  1050. return (NULL);
  1051. }
  1052. #endif //ifdef CSC_RECORDMANAGER_WINNT
  1053. #ifndef CSC_RECORDMANAGER_WINNT
  1054. VOID
  1055. GetSystemTime(
  1056. _FILETIME *lpft
  1057. )
  1058. {
  1059. LONG ltime = IFSMgr_Get_NetTime();
  1060. *lpft = IFSMgr_NetToWin32Time(ltime);
  1061. }
  1062. #endif
  1063. ULONG
  1064. GetTimeInSecondsSince1970(
  1065. VOID
  1066. )
  1067. {
  1068. return ((ULONG)IFSMgr_Get_NetTime());
  1069. }
  1070. BOOL
  1071. IterateOnUNCPathElements(
  1072. USHORT *lpuPath,
  1073. PATHPROC lpfn,
  1074. LPVOID lpCookie
  1075. )
  1076. /*++
  1077. Routine Description:
  1078. This routine takes a unicode UNC path and iterates over each path element, calling the
  1079. callback function. Thus for a path \\server\share\dir1\dir2\file1.txt, the function makes
  1080. the following calls to the lpfn callback function
  1081. (lpfn)(\\server\share, \\server\share, lpCookie)
  1082. (lpfn)(\\server\share\dir1, dir1, lpCookie)
  1083. (lpfn)(\\server\share\dir1\dir2, dir2, lpCookie)
  1084. (lpfn)(\\server\share\dir1\dir2\file1, file1, lpCookie)
  1085. Arguments:
  1086. lpuPath NULL terminated unicode string (NOT NT style, just a plain unicode string)
  1087. lpfn callback function. If the function returns TRUE on a callback, the iteration
  1088. proceeds, else it terminates
  1089. lpCookie context passed back on each callback
  1090. Returns:
  1091. return TRUE if the entire iteration went through, FALSE if some error occurred or the callback
  1092. function terminated the iteration
  1093. Notes:
  1094. --*/
  1095. {
  1096. int cnt, cntSlashes=0, cbSize;
  1097. USHORT *lpuT, *lpuLastElement = NULL, *lpuCopy = NULL;
  1098. BOOL fRet = FALSE;
  1099. // DEBUG_PRINT(("InterateOnUNCPathElements:Path on entry =%ws\r\n", lpuPath));
  1100. if (!lpuPath || ((cnt = wstrlen(lpuPath)) <= 3))
  1101. {
  1102. return FALSE;
  1103. }
  1104. // check for the first two backslashes
  1105. if (!(*lpuPath == (USHORT)'\\') && (*(lpuPath+1) == (USHORT)'\\'))
  1106. {
  1107. return FALSE;
  1108. }
  1109. // ensure that the server field is not NULL
  1110. if (*(lpuPath+2) == (USHORT)'\\')
  1111. {
  1112. return FALSE;
  1113. }
  1114. cbSize = (wstrlen(lpuPath)+1) * sizeof(USHORT);
  1115. lpuCopy = (USHORT *)AllocMem(cbSize);
  1116. if (!lpuCopy)
  1117. {
  1118. return FALSE;
  1119. }
  1120. memcpy(lpuCopy, lpuPath, cbSize);
  1121. cntSlashes = 2;
  1122. lpuLastElement = lpuCopy;
  1123. for (lpuT= lpuCopy+2;; ++lpuT)
  1124. {
  1125. if (*lpuT == (USHORT)'\\')
  1126. {
  1127. BOOL fContinue;
  1128. ++cntSlashes;
  1129. if (cntSlashes == 3)
  1130. {
  1131. if (lpuT == (lpuCopy+2))
  1132. {
  1133. goto bailout;
  1134. }
  1135. continue;
  1136. }
  1137. *lpuT = 0;
  1138. fContinue = (lpfn)(lpuCopy, lpuLastElement, lpCookie);
  1139. *lpuT = (USHORT)'\\';
  1140. if (!fContinue)
  1141. {
  1142. goto bailout;
  1143. }
  1144. lpuLastElement = (lpuT+1);
  1145. }
  1146. else if (!*lpuT)
  1147. {
  1148. (lpfn)(lpuCopy, lpuLastElement, lpCookie);
  1149. break;
  1150. }
  1151. }
  1152. fRet = TRUE;
  1153. bailout:
  1154. if (lpuCopy)
  1155. {
  1156. FreeMem(lpuCopy);
  1157. }
  1158. return (fRet);
  1159. }
  1160. BOOL
  1161. IsPathUNC(
  1162. USHORT *lpuPath,
  1163. int cntMaxChars
  1164. )
  1165. {
  1166. USHORT *lpuT;
  1167. int i, cntSlash=0;
  1168. BOOL fRet = FALSE;
  1169. for(lpuT = lpuPath, i=0; (i < cntMaxChars) && *lpuT; lpuT++, ++i)
  1170. {
  1171. if (cntSlash <= 1)
  1172. {
  1173. // look for the first two backslashes
  1174. if (*lpuT != (USHORT)'\\')
  1175. {
  1176. break;
  1177. }
  1178. ++cntSlash;
  1179. }
  1180. else if (cntSlash == 2)
  1181. {
  1182. // look for the 3rd one
  1183. if (*lpuT == (USHORT)'\\')
  1184. {
  1185. if (((DWORD_PTR)lpuT - (DWORD_PTR)lpuPath) < 3)
  1186. {
  1187. // NULL server field
  1188. break;
  1189. }
  1190. else
  1191. {
  1192. ++cntSlash;
  1193. }
  1194. }
  1195. }
  1196. else // all three slashes accounted for
  1197. {
  1198. Assert(cntSlash == 3);
  1199. // if a non-slash character, then this path is OK
  1200. fRet = (*lpuT != (USHORT)'\\');
  1201. break;
  1202. }
  1203. }
  1204. return (fRet);
  1205. }