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.

349 lines
10 KiB

  1. #define UNICODE
  2. #include <nt.h>
  3. #include <ntdef.h>
  4. #include <ntrtl.h>
  5. #include <nturtl.h>
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include <ntddvol.h>
  9. #include <string.h>
  10. #include <assert.h>
  11. #include "fs.h"
  12. #include "fsp.h"
  13. #include "fsutil.h"
  14. #include "pipe.h"
  15. #define DUMB_CLIENT
  16. #define QFS_DO_NOT_UNMAP_WIN32
  17. #include "..\..\..\service\inc\qfs.h"
  18. #include "..\..\..\service\init\qfs.c"
  19. char msg[] = "Gorik\n";
  20. char buf[1024];
  21. void Dir(LPCWSTR path)
  22. {
  23. WIN32_FIND_DATA data;
  24. QfsHANDLE hdl = QfsFindFirstFile(path, &data);
  25. if (!QfsIsHandleValid(hdl)) {
  26. printf("Failed to open %ws, error %d\n", path, GetLastError());
  27. } else {
  28. printf(" Directory of %ws\n", path);
  29. printf(" %ws\n", data.cFileName);
  30. while (QfsFindNextFile(hdl, &data)) {
  31. printf(" %ws\n", data.cFileName);
  32. }
  33. if (GetLastError() != ERROR_NO_MORE_FILES) {
  34. printf("Strange error returned %d\n", GetLastError());
  35. }
  36. QfsFindClose(hdl);
  37. }
  38. }
  39. //#define PREFIX L"\\\\GORN$QFS\\ROOT\\"
  40. //#define PREFIX L"\\\\qfs\\root$\\"
  41. //#define PREFIX L"\\\\c67c3538-5167-4\\c67c3538-5167-40f0-a1dd-83f8bf41c932$\\"
  42. // \\?\UNC\ //
  43. #define PREFIX L"\\\\?\\UNC\\c67c3538-5167-4\\c67c3538-5167-40f0-a1dd-83f8bf41c932$\\"
  44. //#define PREFIX L"z:\\"
  45. //
  46. // OPEN_EXISTING,
  47. // OPEN_ALWAYS,
  48. // #define CREATE_NEW 1
  49. // #define CREATE_ALWAYS 2
  50. // #define OPEN_EXISTING 3
  51. // #define OPEN_ALWAYS 4
  52. // #define TRUNCATE_EXISTING 5
  53. char* dispName[] = {"0 disp",
  54. "CREATE_NEW",
  55. "CREATE_ALWAYS",
  56. "OPEN_EXISTING",
  57. "OPEN_ALWAYS",
  58. "TRUNCATE_EXISTING"
  59. };
  60. //#define FNAME PREFIX L"GOR.txt"
  61. #define FNAME L"C:\\GOR.txt"
  62. void cf(LPCWSTR Name, DWORD disp, DWORD hardError, DWORD softError)
  63. {
  64. QfsHANDLE Handle;
  65. char buf[128];
  66. DWORD Status;
  67. BOOL good;
  68. Handle = QfsCreateFile(Name,
  69. GENERIC_READ | GENERIC_WRITE,
  70. FILE_SHARE_READ,
  71. // 0,
  72. NULL,
  73. disp,
  74. // FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED,
  75. // FILE_FLAG_WRITE_THROUGH | FILE_FLAG_OVERLAPPED,
  76. 0,
  77. NULL);
  78. Status = GetLastError();
  79. if (hardError == 0) {
  80. good = QfsIsHandleValid(Handle) && softError == Status;
  81. sprintf(buf, "should succeed with gle = %d", softError);
  82. } else {
  83. good = !QfsIsHandleValid(Handle) && hardError == Status;
  84. sprintf(buf, "should fail, status %d", hardError);
  85. }
  86. printf("%s %s handle %x gle %d (%s)\n", good?"":"****", dispName[disp], Handle, GetLastError(), buf);
  87. QfsCloseHandleIfValid(Handle);
  88. }
  89. DWORD cat(LPCWSTR Name)
  90. {
  91. PCHAR buf[1024];
  92. DWORD dwBytes;
  93. QfsHANDLE hFile = QfsCreateFile(Name,
  94. GENERIC_READ | GENERIC_WRITE,
  95. FILE_SHARE_READ,
  96. NULL,
  97. OPEN_EXISTING,
  98. 0,
  99. NULL);
  100. if (!QfsIsHandleValid(hFile)) {
  101. return FALSE;
  102. }
  103. while (QfsReadFile(hFile, buf, sizeof(buf), &dwBytes, NULL) && dwBytes) {
  104. printf("%.*s", dwBytes, buf);
  105. }
  106. printf("\n");
  107. QfsCloseHandle(hFile);
  108. return TRUE;
  109. }
  110. void MiniShell()
  111. {
  112. DWORD len;
  113. DWORD err;
  114. WCHAR TempDir[1024];
  115. WCHAR HomeDir[1024];
  116. WCHAR CurrentDir[1024];
  117. WCHAR cmd[1024];
  118. QfsLogLevel = 1;
  119. len = GetCurrentDirectory(1024, CurrentDir);
  120. if (len == 1024 || len == 0) {
  121. printf("GetCurrentDir failed, error %d\n", GetLastError());
  122. return;
  123. }
  124. printf("Quorum Minishell\n(C) Copyright 1985-2001 Microsoft Corp.\n\n");
  125. wcscpy(HomeDir, CurrentDir);
  126. for(;;) {
  127. printf("%ws>", CurrentDir);
  128. if ((_getws(cmd) == NULL) || wcscmp(cmd, L"exit")==0) {
  129. break;
  130. }
  131. if (cmd[0] == 0) continue;
  132. if (wcsncmp(cmd, L"cd",2) == 0) {
  133. if (cmd[2] == 0) {
  134. wcscpy(CurrentDir, HomeDir);
  135. continue;
  136. }
  137. if (cmd[2] != ' ') {
  138. goto invalid_command;
  139. }
  140. if (cmd[3] == '.') {
  141. if (cmd[4] == '.') {
  142. WCHAR* p = wcsrchr(CurrentDir, '\\');
  143. if (p) *p = 0;
  144. }
  145. continue;
  146. }
  147. if (cmd[3] == '\\' || cmd[4] == ':') {
  148. wcscpy(CurrentDir, cmd+3);
  149. } else {
  150. wcscat(CurrentDir, cmd+3);
  151. }
  152. len = wcslen(CurrentDir);
  153. if (len > 0 && CurrentDir[len-1] == '\\') {
  154. CurrentDir[len-1] = 0;
  155. }
  156. continue;
  157. }
  158. if (wcscmp(cmd, L"dir")==0) {
  159. wcscpy(TempDir, CurrentDir);
  160. wcscat(TempDir, L"\\*");
  161. Dir(TempDir);
  162. continue;
  163. }
  164. if (wcsncmp(cmd, L"mkdir ", 6)==0) {
  165. err = QfsCreateDirectory(cmd + 6, NULL);
  166. } else if (wcsncmp(cmd, L"rmdir ", 6) == 0) {
  167. err = QfsRemoveDirectory(cmd + 6);
  168. } else if (wcsncmp(cmd, L"del ", 4) == 0) {
  169. err = QfsDeleteFile(cmd + 4);
  170. } else if (wcsncmp(cmd, L"cat ", 4) == 0) {
  171. wcscpy(TempDir, CurrentDir);
  172. wcscat(TempDir, L"\\");
  173. wcscat(TempDir, cmd + 4);
  174. err = cat(TempDir);
  175. }
  176. continue;
  177. invalid_command:
  178. printf(" Invalid command \"%ws\"\n", cmd);
  179. }
  180. printf("Bye\n\n");
  181. }
  182. _cdecl
  183. main()
  184. {
  185. MiniShell();
  186. #if 0
  187. QfsHANDLE Handle;
  188. DWORD dwSize;
  189. DWORD status;
  190. int i;
  191. // CREATE_NEW Creates a new file. The function fails if the specified file already exists.
  192. // CREATE_ALWAYS Creates a new file. If the file exists, the function overwrites the file and clears the existing attributes.
  193. // OPEN_EXISTING Opens the file. The function fails if the file does not exist.
  194. // OPEN_ALWAYS Opens the file, if it exists. If the file does not exist, the function creates the file as if dwCreationDisposition were CREATE_NEW.
  195. // TRUNCATE_EXISTING
  196. QfsDeleteFile(FNAME);
  197. cf(FNAME, CREATE_NEW, 0,0);
  198. cf(FNAME, CREATE_NEW, 80,80);
  199. QfsDeleteFile(FNAME);
  200. cf(FNAME, CREATE_ALWAYS, 0,0);
  201. cf(FNAME, CREATE_ALWAYS, 0,183);
  202. cf(FNAME, OPEN_EXISTING, 0,0);
  203. QfsDeleteFile(FNAME);
  204. cf(FNAME, OPEN_EXISTING, 2,2);
  205. QfsDeleteFile(FNAME);
  206. cf(FNAME, OPEN_ALWAYS, 0,0);
  207. cf(FNAME, OPEN_ALWAYS, 0,183);
  208. cf(FNAME, TRUNCATE_EXISTING, 0,0);
  209. QfsDeleteFile(FNAME);
  210. cf(FNAME, TRUNCATE_EXISTING, 2,2);
  211. return 0;
  212. QfsDeleteFile(PREFIX L"gor.txt");
  213. for(i = 0; i < 2; ++i) {
  214. status = QfsCreateFile(&Handle, PREFIX L"gor.txt",
  215. GENERIC_READ | GENERIC_WRITE,
  216. FILE_SHARE_READ,
  217. // 0,
  218. NULL,
  219. OPEN_ALWAYS,
  220. // FILE_FLAG_WRITE_THROUGH | FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED,
  221. // FILE_FLAG_WRITE_THROUGH | FILE_FLAG_OVERLAPPED,
  222. 0,
  223. NULL);
  224. printf("handle %x gle %d\n", Handle, status);
  225. QfsCloseHandleIfValid(Handle);
  226. }
  227. #endif
  228. // Dir(PREFIX L"\\*");
  229. #if 0
  230. #if 0
  231. status = QfsCreateFile(
  232. &Handle,
  233. L"zTest2.txt",
  234. GENERIC_WRITE,
  235. FILE_SHARE_READ | FILE_SHARE_WRITE,
  236. NULL,
  237. CREATE_ALWAYS,
  238. 0,
  239. NULL);
  240. QfsWriteFile(
  241. Handle,
  242. msg,
  243. sizeof(msg),
  244. &dwSize,
  245. NULL);
  246. QfsFlushFileBuffers(Handle);
  247. QfsCloseHandle(Handle);
  248. #endif
  249. status = QfsCreateFile(
  250. &Handle,
  251. PREFIX L"zTest.txt",
  252. GENERIC_READ | GENERIC_WRITE,
  253. FILE_SHARE_READ | FILE_SHARE_WRITE,
  254. NULL,
  255. OPEN_EXISTING,
  256. 0,
  257. NULL);
  258. if (status == ERROR_SUCCESS) {
  259. dwSize = QfsGetFileSize(Handle, NULL);
  260. #if 1
  261. dwSize = 0;
  262. QfsReadFile(
  263. Handle,
  264. buf,
  265. sizeof(buf),
  266. &dwSize,
  267. NULL);
  268. printf("read => %d bytes, %s\n", dwSize, buf);
  269. #endif
  270. QfsSetEndOfFile(Handle, dwSize + 2);
  271. QfsCloseHandle(Handle);
  272. }
  273. QfsSetFileAttributes(PREFIX L"zTest.txt", FILE_ATTRIBUTE_NORMAL);
  274. QfsCopyFile(PREFIX L"zTest.txt", PREFIX L"A.txt", FALSE);
  275. #endif
  276. // QfsCopyFile(PREFIX L"A.txt", PREFIX L"B.txt", FALSE);
  277. #if 0
  278. QfsDeleteFile(L"zTest2.txt");
  279. QfsDeleteFile(L"zbuild.log");
  280. Dir(L"z\\*");
  281. Dir(L"z\\*.tx?");
  282. Dir(L"z\\1st\\*");
  283. QfsCreateDirectory(L"z1st", NULL);
  284. Dir(L"z\\1st\\*");
  285. #endif
  286. #if 0
  287. {
  288. ULARGE_INTEGER FreeBytesAvailable; // bytes available to caller
  289. ULARGE_INTEGER TotalNumberOfBytes; // bytes on disk
  290. ULARGE_INTEGER TotalNumberOfFreeBytes; // free bytes on disk
  291. if (QfsGetDiskFreeSpaceEx(L"z.", &FreeBytesAvailable, &TotalNumberOfBytes, &TotalNumberOfFreeBytes)) {
  292. printf("av %u total %u free %u\n", FreeBytesAvailable.LowPart, TotalNumberOfBytes.LowPart, TotalNumberOfFreeBytes.LowPart);
  293. }
  294. if (QfsGetDiskFreeSpaceEx(L"C:\\drop\\test\\root", &FreeBytesAvailable, &TotalNumberOfBytes, &TotalNumberOfFreeBytes)) {
  295. printf("av %u total %u free %u\n", FreeBytesAvailable.LowPart, TotalNumberOfBytes.LowPart, TotalNumberOfFreeBytes.LowPart);
  296. }
  297. // if (QfsGetDiskFreeSpaceEx(PREFIX L".", &FreeBytesAvailable, &TotalNumberOfBytes, &TotalNumberOfFreeBytes)) {
  298. // printf("av %u total %u free %u\n", FreeBytesAvailable.LowPart, TotalNumberOfBytes.LowPart, TotalNumberOfFreeBytes.LowPart);
  299. // }
  300. }
  301. #endif
  302. }