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.

377 lines
7.2 KiB

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <direct.h>
  4. #include <assert.h>
  5. #include <tchar.h>
  6. #include <dbghelp.h>
  7. #include <strsafe.h>
  8. #include <str.h>
  9. typedef enum {
  10. cmdIni,
  11. cmdInfo,
  12. cmdClient,
  13. cmdHave,
  14. cmdSync,
  15. cmdMax
  16. };
  17. typedef BOOL (*CMDPROC)(TCHAR *dir);
  18. typedef struct _CMD {
  19. TCHAR *sz;
  20. CMDPROC cmdproc;
  21. PENUMDIRTREE_CALLBACK enumdirproc;
  22. } CMD, *PCMD;
  23. BOOL ini(TCHAR *path);
  24. BOOL info(TCHAR *dir);
  25. BOOL client(TCHAR *dir);
  26. BOOL have(TCHAR *dir);
  27. BOOL sync(TCHAR *dir);
  28. BOOL CALLBACK cbIni(LPCSTR filepath, void *data);
  29. BOOL CALLBACK cbInfo(LPCSTR filepath, void *data);
  30. BOOL CALLBACK cbClient(LPCSTR filepath, void *data);
  31. BOOL CALLBACK cbHave(LPCSTR filepath, void *data);
  32. BOOL CALLBACK cbSync(LPCSTR filepath, void *data);
  33. CMD gcmds[cmdMax] =
  34. {
  35. {TEXT("ini"), ini, cbIni},
  36. {TEXT("info"), info, cbInfo},
  37. {TEXT("client"), client, cbClient},
  38. {TEXT("have"), have, cbHave},
  39. {TEXT("sync"), sync, cbSync},
  40. };
  41. bool grecursive = false;
  42. int gcmd;
  43. TCHAR gdir[SZ_SIZE];
  44. bool init()
  45. {
  46. grecursive = false;
  47. gcmd = cmdMax;
  48. _tgetcwd(gdir, SZ_SIZE);
  49. return true;
  50. }
  51. bool parsecmd(int argc, TCHAR *argv[])
  52. {
  53. int i;
  54. for (i = 1; i < argc; i++)
  55. {
  56. if (!_tcscmp(argv[i], TEXT("...")))
  57. {
  58. grecursive = true;
  59. }
  60. else if (!_tcsicmp(argv[i], TEXT("ini")))
  61. {
  62. gcmd = cmdIni;
  63. }
  64. else if (!_tcsicmp(argv[i], TEXT("info")))
  65. {
  66. gcmd = cmdInfo;
  67. }
  68. else if (!_tcsicmp(argv[i], TEXT("client")))
  69. {
  70. gcmd = cmdClient;
  71. }
  72. else if (!_tcsicmp(argv[i], TEXT("have")))
  73. {
  74. gcmd = cmdHave;
  75. }
  76. else if (!_tcsicmp(argv[i], TEXT("sync")))
  77. {
  78. gcmd = cmdSync;
  79. }
  80. else
  81. {
  82. _tprintf(TEXT("SDP: \"%s\" is an unrecognized parameter.\n"), argv[i]);
  83. return false;
  84. }
  85. }
  86. return true;
  87. }
  88. BOOL
  89. dumpfile(
  90. const TCHAR *path
  91. )
  92. {
  93. BOOL rc;
  94. HANDLE hf;
  95. DWORD size;
  96. DWORD cb;
  97. LPSTR p;
  98. char buf[SZ_SIZE];
  99. assert(path && *path);
  100. fflush(stdout);
  101. rc = FALSE;
  102. hf = CreateFile(path,
  103. GENERIC_READ,
  104. FILE_SHARE_READ,
  105. NULL,
  106. OPEN_EXISTING,
  107. FILE_ATTRIBUTE_NORMAL,
  108. NULL);
  109. if (hf == INVALID_HANDLE_VALUE)
  110. return FALSE;
  111. // test validity of file pointer
  112. size = GetFileSize(hf, NULL);
  113. if (!size || size > SZ_SIZE)
  114. goto cleanup;
  115. // read it
  116. ZeroMemory(buf, SZ_SIZE * sizeof(buf[0]));
  117. if (!ReadFile(hf, buf, size, &cb, 0))
  118. goto cleanup;
  119. if (cb != size)
  120. goto cleanup;
  121. rc = TRUE;
  122. printf("%s\n", buf);
  123. cleanup:
  124. // done
  125. if (hf)
  126. CloseHandle(hf);
  127. fflush(stdout);
  128. return rc;
  129. }
  130. BOOL ini(TCHAR *path)
  131. {
  132. TCHAR file[SZ_SIZE];
  133. StringCchCopy(file, DIMA(file), path);
  134. if (!_tcsstr(file, TEXT("sd.ini")))
  135. {
  136. EnsureTrailingBackslash(file);
  137. StringCchCat(file, DIMA(file), TEXT("sd.ini"));
  138. }
  139. _tprintf(TEXT("------- SDP INI: %s -------\n"), path);
  140. dumpfile(file);
  141. return false;
  142. }
  143. BOOL CALLBACK cbIni(LPCSTR filepath, void *data)
  144. {
  145. TCHAR sz[SZ_SIZE];
  146. TCHAR path[SZ_SIZE];
  147. ansi2tchar(filepath, sz, SZ_SIZE);
  148. getpath(sz, path, DIMA(path));
  149. return ini(path);
  150. }
  151. BOOL process(TCHAR *dir, TCHAR *cmd)
  152. {
  153. BOOL rc;
  154. DWORD err;
  155. STARTUPINFO si;
  156. PROCESS_INFORMATION pi;
  157. TCHAR sz[SZ_SIZE];
  158. StringCchCopy(sz, DIMA(sz), cmd);
  159. fflush(stdout);
  160. GetStartupInfo(&si);
  161. ZeroMemory(&pi, sizeof(pi));
  162. rc = CreateProcess(NULL, // LPCWSTR lpszImageName,
  163. sz, // LPCWSTR lpszCmdLine,
  164. NULL, // LPSECURITY_ATTRIBUTES lpsaProcess,
  165. NULL, // LPSECURITY_ATTRIBUTES lpsaThread,
  166. true, // BOOL fInheritHandles,
  167. 0, // DWORD dwCreationFlags,
  168. NULL, // LPVOID lpvEnvironment,
  169. dir, // LPWSTR lpszCurDir,
  170. &si, // LPSTARTUPINFOW lpsiStartInfo,
  171. &pi // LPPROCESS_INFORMATION lppiProcInfo
  172. );
  173. if (!rc || !pi.hProcess)
  174. {
  175. err = GetLastError();
  176. goto cleanup;
  177. }
  178. // Wait for command to complete ... Give it 20 minutes
  179. err = WaitForSingleObject(pi.hProcess, 1200000);
  180. if (err != WAIT_OBJECT_0)
  181. {
  182. rc = false;
  183. goto cleanup;
  184. }
  185. // Get the process exit code
  186. GetExitCodeProcess(pi.hProcess, &err);
  187. rc = (err == ERROR_SUCCESS) ? true : false;
  188. cleanup:
  189. if (pi.hProcess)
  190. CloseHandle(pi.hProcess);
  191. fflush(stdout);
  192. return rc;
  193. }
  194. BOOL info(TCHAR *dir)
  195. {
  196. _tprintf(TEXT("------- SDP INFO: %s -------\n"), dir);
  197. return process(dir, TEXT("sd.exe info"));
  198. }
  199. BOOL CALLBACK cbInfo(LPCSTR filepath, void *data)
  200. {
  201. TCHAR sz[SZ_SIZE];
  202. TCHAR path[SZ_SIZE];
  203. ansi2tchar(filepath, sz, SZ_SIZE);
  204. getpath(sz, path, DIMA(path));
  205. info(path);
  206. return false;
  207. }
  208. BOOL client(TCHAR *dir)
  209. {
  210. _tprintf(TEXT("------- SDP CLIENT: %s -------\n"), dir);
  211. if (!process(dir, TEXT("sd.exe info")))
  212. return false;
  213. return process(dir, TEXT("sd.exe client -o"));
  214. }
  215. BOOL CALLBACK cbClient(LPCSTR filepath, void *data)
  216. {
  217. TCHAR sz[SZ_SIZE];
  218. TCHAR path[SZ_SIZE];
  219. ansi2tchar(filepath, sz, SZ_SIZE);
  220. getpath(sz, path, DIMA(path));
  221. // ini(sz);
  222. client(path);
  223. return false;
  224. }
  225. BOOL have(TCHAR *dir)
  226. {
  227. _tprintf(TEXT("------- SDP HAVE: %s -------\n"), dir);
  228. return process(dir, TEXT("sd.exe have"));
  229. }
  230. BOOL CALLBACK cbHave(LPCSTR filepath, void *data)
  231. {
  232. TCHAR sz[SZ_SIZE];
  233. TCHAR path[SZ_SIZE];
  234. ansi2tchar(filepath, sz, SZ_SIZE);
  235. getpath(sz, path, DIMA(path));
  236. have(path);
  237. return false;
  238. }
  239. BOOL sync(TCHAR *dir)
  240. {
  241. return process(dir, TEXT("sd.exe sync"));
  242. }
  243. BOOL CALLBACK cbSync(LPCSTR filepath, void *data)
  244. {
  245. TCHAR sz[SZ_SIZE];
  246. TCHAR path[SZ_SIZE];
  247. ansi2tchar(filepath, sz, SZ_SIZE);
  248. getpath(sz, path, DIMA(path));
  249. sync(path);
  250. fflush(stdout);
  251. return false;
  252. }
  253. bool cmd()
  254. {
  255. char sz[SZ_SIZE];
  256. if (gcmd == cmdMax)
  257. return false;
  258. if (grecursive)
  259. {
  260. tchar2ansi(gdir, sz, SZ_SIZE);
  261. EnumDirTree(INVALID_HANDLE_VALUE, sz, "sd.ini", NULL, gcmds[gcmd].enumdirproc, NULL);
  262. return true;
  263. }
  264. gcmds[gcmd].cmdproc(gdir);
  265. return true;
  266. }
  267. extern "C" int __cdecl _tmain(int argc, _TCHAR **argv, _TCHAR **envp)
  268. {
  269. if (!init())
  270. return -1;
  271. if (!parsecmd(argc, argv))
  272. return -1;
  273. if (!cmd())
  274. return -1;
  275. return 0;
  276. }