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.

160 lines
3.3 KiB

  1. /*++
  2. Copyright (c) 1989-2001 Microsoft Corporation
  3. Module Name:
  4. fileio.c
  5. Abstract:
  6. A set of function similar to fopen, fclose and fgetc
  7. Author:
  8. Jiandong Ruan
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #include "fileio.tmh"
  13. #pragma alloc_text(PAGE, Smb_fopen)
  14. #pragma alloc_text(PAGE, Smb_fclose)
  15. #pragma alloc_text(PAGE, Smb_fgetc)
  16. PSMB_FILE
  17. Smb_fopen(
  18. PWCHAR path,
  19. PWCHAR mode
  20. )
  21. {
  22. PSMB_FILE fp;
  23. OBJECT_ATTRIBUTES attr;
  24. UNICODE_STRING ucPath;
  25. NTSTATUS status;
  26. HANDLE handle;
  27. IO_STATUS_BLOCK iostatus;
  28. PAGED_CODE();
  29. //
  30. // Only readonly is supported
  31. //
  32. if (mode[0] != L'r' || mode[1] != L'\0') {
  33. SmbPrint(SMB_TRACE_DNS, ("Incorrect mode %d of %s\n", __LINE__, __FILE__));
  34. return NULL;
  35. }
  36. fp = (PSMB_FILE)ExAllocatePoolWithTag(
  37. PagedPool,
  38. sizeof(fp[0]),
  39. 'pBMS'
  40. );
  41. if (fp == NULL) {
  42. SmbPrint(SMB_TRACE_DNS, ("Not enough memory %d of %s\n", __LINE__, __FILE__));
  43. return NULL;
  44. }
  45. RtlInitUnicodeString(&ucPath, path);
  46. InitializeObjectAttributes(
  47. &attr,
  48. &ucPath,
  49. OBJ_CASE_INSENSITIVE| OBJ_KERNEL_HANDLE,
  50. NULL,
  51. NULL
  52. );
  53. handle = NULL;
  54. status = ZwCreateFile(
  55. &handle,
  56. SYNCHRONIZE | FILE_READ_DATA,
  57. &attr,
  58. &iostatus,
  59. 0,
  60. FILE_ATTRIBUTE_NORMAL,
  61. FILE_SHARE_READ | FILE_SHARE_WRITE,
  62. FILE_OPEN,
  63. FILE_SYNCHRONOUS_IO_NONALERT,
  64. NULL,
  65. 0
  66. );
  67. if (handle == NULL) {
  68. SmbPrint(SMB_TRACE_DNS, ("ZwCreateFile return 0x%08lx %Z %d of %s\n",
  69. status, &ucPath, __LINE__, __FILE__));
  70. ExFreePool(fp);
  71. return NULL;
  72. }
  73. RtlZeroMemory(fp, sizeof(fp[0]));
  74. fp->fd = handle;
  75. //
  76. // Make it look like we reach the end of lookahead buffer
  77. //
  78. fp->offset = fp->size = SMB_FILEIO_LOOKAHEAD_SIZE;
  79. return fp;
  80. }
  81. void
  82. Smb_fclose(
  83. PSMB_FILE fp
  84. )
  85. {
  86. if (NULL == fp) {
  87. return;
  88. }
  89. if (fp->fd) {
  90. ZwClose(fp->fd);
  91. }
  92. ExFreePool(fp);
  93. }
  94. int
  95. Smb_fgetc(
  96. PSMB_FILE fp
  97. )
  98. {
  99. NTSTATUS status;
  100. IO_STATUS_BLOCK iosb;
  101. if (fp->offset < fp->size) {
  102. return fp->Buffer[fp->offset++];
  103. }
  104. //
  105. // EOF?
  106. //
  107. if (fp->size < SMB_FILEIO_LOOKAHEAD_SIZE) {
  108. return EOF;
  109. }
  110. status = ZwReadFile(
  111. fp->fd,
  112. NULL,
  113. NULL,
  114. NULL,
  115. &iosb,
  116. fp->Buffer,
  117. SMB_FILEIO_LOOKAHEAD_SIZE,
  118. NULL,
  119. NULL
  120. );
  121. if (status != STATUS_SUCCESS) {
  122. fp->offset = fp->size = 0;
  123. SmbPrint(SMB_TRACE_DNS, ("ZwReadFile return 0x%08lx %d of %s\n", status, __LINE__, __FILE__));
  124. return EOF;
  125. }
  126. fp->size = (int)iosb.Information;
  127. SmbPrint(SMB_TRACE_DNS, ("ZwReadFile read %d bytes %d of %s\n", fp->size, __LINE__,__FILE__));
  128. fp->offset = 0;
  129. if (fp->offset < fp->size) {
  130. return fp->Buffer[fp->offset++];
  131. }
  132. return EOF;
  133. }