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.

414 lines
9.7 KiB

  1. //--------------------------------------------------------------------------
  2. //
  3. // Copyright (C) 1999, Microsoft Corporation
  4. //
  5. // File: dfstarget.cxx
  6. //
  7. //--------------------------------------------------------------------------
  8. #ifndef __DFS_UTIL_TARGET__
  9. #define __DFS_UTIL_TARGET__
  10. #include "dfsheader.h"
  11. #include "dfsmisc.h"
  12. #include "dfsutil.hxx"
  13. #include "dfsStrings.hxx"
  14. #define TARGET_SERVER_ADDED 0x0001
  15. #define TARGET_FOLDER_ADDED 0x0002
  16. #define TARGET_STATE_ADDED 0x0004
  17. #define MARK_TARGET_DELETE 0x0010
  18. #define MARK_TARGET_ADD 0x0020
  19. #define UPDATE_TARGET_STATE 0x0040
  20. extern DWORD
  21. GetSites(LPWSTR Target,
  22. DfsString *pSite );
  23. class DfsTarget
  24. {
  25. private:
  26. ULONG _Flags;
  27. DfsString _Server;
  28. DfsString _Folder;
  29. DfsString _SiteName;
  30. ULONG _State;
  31. DfsTarget *_pNextTarget;
  32. ULONG _ChangeStatus;
  33. public:
  34. DfsTarget( )
  35. {
  36. _Flags = 0;
  37. _State = DFS_STORAGE_STATE_ONLINE;
  38. _pNextTarget = NULL;
  39. _ChangeStatus = 0;
  40. }
  41. ~DfsTarget( )
  42. {
  43. //
  44. // Take care to release all resources
  45. // here
  46. //
  47. }
  48. VOID
  49. ResetChangeStatus()
  50. {
  51. _ChangeStatus = 0;
  52. }
  53. VOID
  54. SetChangeStatus(ULONG State)
  55. {
  56. _ChangeStatus |= State;
  57. }
  58. DFSSTATUS
  59. ValidateTargetName( PUNICODE_STRING pTargetName,
  60. PUNICODE_STRING pServerName,
  61. PUNICODE_STRING pFolderName )
  62. {
  63. DFSSTATUS Status;
  64. Status = DfsGetFirstComponent( pTargetName,
  65. pServerName,
  66. pFolderName );
  67. if (pFolderName->Length == 0)
  68. {
  69. Status = ERROR_INVALID_PARAMETER;
  70. }
  71. return Status;
  72. }
  73. DFSSTATUS
  74. SetTargetUNCName( PUNICODE_STRING pTargetName, BOOLEAN SiteAware )
  75. {
  76. UNICODE_STRING ServerName;
  77. UNICODE_STRING FolderName;
  78. DFSSTATUS Status = ERROR_SUCCESS;
  79. Status = ValidateTargetName( pTargetName,
  80. &ServerName,
  81. &FolderName );
  82. if (Status == ERROR_SUCCESS)
  83. {
  84. Status = SetTargetServer(&ServerName, SiteAware);
  85. }
  86. if (Status == ERROR_SUCCESS)
  87. {
  88. Status = SetTargetFolder(&FolderName);
  89. }
  90. return Status;
  91. }
  92. DFSSTATUS
  93. SetTargetUNCName( LPWSTR Target, BOOLEAN SiteAware )
  94. {
  95. UNICODE_STRING TargetName;
  96. RtlInitUnicodeString( &TargetName, Target);
  97. return SetTargetUNCName(&TargetName, SiteAware);
  98. }
  99. DFSSTATUS
  100. SetTargetServer( PUNICODE_STRING pServerName,
  101. BOOLEAN SiteAware )
  102. {
  103. DFSSTATUS Status = ERROR_SUCCESS;
  104. if (_Flags & TARGET_SERVER_ADDED)
  105. {
  106. Status = ERROR_INVALID_PARAMETER;
  107. }
  108. if (Status == ERROR_SUCCESS)
  109. {
  110. Status = _Server.CreateString(pServerName);
  111. if (Status == ERROR_SUCCESS)
  112. {
  113. _Flags |= TARGET_SERVER_ADDED;
  114. }
  115. if (SiteAware)
  116. {
  117. if (Status == ERROR_SUCCESS)
  118. {
  119. GetSites( _Server.GetString(),
  120. &_SiteName);
  121. }
  122. }
  123. }
  124. return Status;
  125. }
  126. DFSSTATUS
  127. SetTargetServer( LPWSTR Server, BOOLEAN SiteAware )
  128. {
  129. UNICODE_STRING ServerName;
  130. RtlInitUnicodeString( &ServerName, Server);
  131. return SetTargetServer(&ServerName, SiteAware);
  132. }
  133. DFSSTATUS
  134. SetTargetFolder( PUNICODE_STRING pFolderName )
  135. {
  136. DFSSTATUS Status = ERROR_SUCCESS;
  137. if (_Flags & TARGET_FOLDER_ADDED)
  138. {
  139. Status = ERROR_INVALID_PARAMETER;
  140. }
  141. if (Status == ERROR_SUCCESS)
  142. {
  143. Status = _Folder.CreateString(pFolderName);
  144. if (Status == ERROR_SUCCESS)
  145. {
  146. _Flags |= TARGET_FOLDER_ADDED;
  147. }
  148. }
  149. return Status;
  150. }
  151. DFSSTATUS
  152. SetTargetFolder( LPWSTR Folder )
  153. {
  154. UNICODE_STRING FolderName;
  155. RtlInitUnicodeString( &FolderName, Folder);
  156. return SetTargetFolder(&FolderName);
  157. }
  158. BOOLEAN
  159. IsValidTarget()
  160. {
  161. return ((_Flags & (TARGET_FOLDER_ADDED | TARGET_SERVER_ADDED)) ==
  162. (TARGET_FOLDER_ADDED | TARGET_SERVER_ADDED));
  163. }
  164. VOID
  165. SetTargetState( ULONG State)
  166. {
  167. _Flags |= TARGET_STATE_ADDED;
  168. _State = State;
  169. }
  170. VOID
  171. AddNextTarget( DfsTarget *pTarget )
  172. {
  173. _pNextTarget = pTarget;
  174. }
  175. DfsTarget *
  176. GetNextTarget()
  177. {
  178. return _pNextTarget;
  179. }
  180. PUNICODE_STRING
  181. GetTargetServerCountedString()
  182. {
  183. return _Server.GetCountedString();
  184. }
  185. PUNICODE_STRING
  186. GetTargetFolderCountedString()
  187. {
  188. return _Folder.GetCountedString();
  189. }
  190. PUNICODE_STRING
  191. GetTargetSiteCountedString()
  192. {
  193. return _SiteName.GetCountedString();
  194. }
  195. LPWSTR
  196. GetTargetServerString()
  197. {
  198. return _Server.GetString();
  199. }
  200. LPWSTR
  201. GetTargetFolderString()
  202. {
  203. return _Folder.GetString();
  204. }
  205. LPWSTR
  206. GetTargetSiteString()
  207. {
  208. return _SiteName.GetString();
  209. }
  210. DWORD
  211. GetTargetState()
  212. {
  213. return _State;
  214. }
  215. DfsString *
  216. GetTargetServer()
  217. {
  218. return &_Server;
  219. }
  220. DfsString *
  221. GetTargetFolder()
  222. {
  223. return &_Folder;
  224. }
  225. BOOLEAN
  226. MarkedForDelete()
  227. {
  228. return ((_ChangeStatus & MARK_TARGET_DELETE) == MARK_TARGET_DELETE);
  229. }
  230. BOOLEAN
  231. MarkedForAddition()
  232. {
  233. return ((_ChangeStatus & MARK_TARGET_ADD) == MARK_TARGET_ADD);
  234. }
  235. BOOLEAN
  236. MarkedForStateUpdate()
  237. {
  238. return ((_ChangeStatus & UPDATE_TARGET_STATE) == UPDATE_TARGET_STATE);
  239. }
  240. DFSSTATUS
  241. ApplyApiChanges( DFS_API_MODE Mode,
  242. PUNICODE_STRING pLinkName,
  243. DfsString *pComment,
  244. ULONG Flags,
  245. PDFS_UPDATE_STATISTICS pStatistics )
  246. {
  247. DFSSTATUS Status = ERROR_SUCCESS;
  248. LPWSTR UseComment = NULL;
  249. if (pComment)
  250. {
  251. UseComment = pComment->GetString();
  252. }
  253. if (MarkedForDelete())
  254. {
  255. Status = DfsApiRemove(Mode,
  256. pLinkName->Buffer,
  257. GetTargetServerString(),
  258. GetTargetFolderString());
  259. if (Status != ERROR_SUCCESS)
  260. {
  261. ShowInformation((L"DfsRemove failed for %wS (%wS, %wS), Status %x\n",
  262. pLinkName->Buffer,
  263. GetTargetServerString(),
  264. GetTargetFolderString(),
  265. Status));
  266. }
  267. pStatistics->TargetDeleted++;
  268. pStatistics->ApiCount++;
  269. return Status;
  270. }
  271. if (MarkedForAddition())
  272. {
  273. Status = DfsApiAdd(Mode,
  274. pLinkName->Buffer,
  275. GetTargetServerString(),
  276. GetTargetFolderString(),
  277. UseComment,
  278. Flags );
  279. if (Status != ERROR_SUCCESS)
  280. {
  281. ShowInformation((L"DfsAdd failed for %wS (%wS, %wS), Status %x\n",
  282. pLinkName->Buffer,
  283. GetTargetServerString(),
  284. GetTargetFolderString(),
  285. Status));
  286. }
  287. pStatistics->TargetAdded++;
  288. pStatistics->ApiCount++;
  289. }
  290. if (MarkedForStateUpdate())
  291. {
  292. DFS_API_INFO ApiInfo, *pApiInfo;
  293. ApiInfo.Info101.State = GetTargetState();
  294. pApiInfo = &ApiInfo;
  295. Status = DfsApiSetInfo( Mode,
  296. pLinkName->Buffer,
  297. GetTargetServerString(),
  298. GetTargetFolderString(),
  299. 101,
  300. &pApiInfo);
  301. if (Status != ERROR_SUCCESS)
  302. {
  303. ShowInformation((L"DfsSetInfo State %d failed for %wS (%wS, %wS), Status %x\n",
  304. ApiInfo.Info101.State,
  305. pLinkName->Buffer,
  306. GetTargetServerString(),
  307. GetTargetFolderString(),
  308. Status));
  309. }
  310. pStatistics->TargetModified++;
  311. pStatistics->ApiCount++;
  312. }
  313. return Status;
  314. }
  315. BOOLEAN
  316. IsMatchingState( ULONG State)
  317. {
  318. return (_State == State);
  319. }
  320. BOOLEAN
  321. IsMatchingName( DfsString * pServer,
  322. DfsString *pFolder )
  323. {
  324. if ((_Server == pServer) &&
  325. (_Folder == pFolder))
  326. {
  327. return TRUE;
  328. }
  329. return FALSE;
  330. }
  331. VOID
  332. MarkForDelete()
  333. {
  334. _ChangeStatus = MARK_TARGET_DELETE;
  335. }
  336. VOID
  337. MarkForAddition()
  338. {
  339. _ChangeStatus = MARK_TARGET_ADD;
  340. }
  341. };
  342. #endif // __DFS_UTIL_TARGET__