Source code of Windows XP (NT5)
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.

343 lines
8.3 KiB

  1. // Copyright (c) 1996-1999 Microsoft Corporation
  2. //+-------------------------------------------------------------------------
  3. //
  4. // Microsoft Windows
  5. //
  6. // File: fileops.hxx
  7. //
  8. // Contents: Definitions for OBJID and file operations
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. //
  15. //
  16. // History: 18-Nov-96 BillMo Created.
  17. //
  18. // Notes:
  19. //
  20. // Codework:
  21. //
  22. //--------------------------------------------------------------------------
  23. unsigned
  24. ConvertToNtPath(const TCHAR *ptszVolumePath, WCHAR *pwszNtPath, ULONG cwcBuf);
  25. //+----------------------------------------------------------------------------
  26. //
  27. // CVolumeDeviceName
  28. //
  29. // This represents a "volume device name", as defined by the mount manager.
  30. // Volume device names are of the form (but without the trailing whack). E.g.:
  31. //
  32. // "\\?\Volume{856d50da-d07e-11d2-9e72-806d6172696f}"
  33. //
  34. //+----------------------------------------------------------------------------
  35. class CVolumeDeviceName
  36. {
  37. public:
  38. // Create a volume device name from a zero-relative
  39. // drive letter index.
  40. CVolumeDeviceName( LONG iVol )
  41. {
  42. _stprintf( _tszPath, TEXT("\\\\.\\%c:"), iVol+TEXT('A') );
  43. }
  44. // Return the volume name..
  45. operator const TCHAR*() const
  46. {
  47. return( _tszPath );
  48. }
  49. // Validate a zero-relative drive letter index.
  50. static BOOL IsValid( LONG iVol )
  51. {
  52. if( 0 <= iVol && NUM_VOLUMES > iVol )
  53. return( TRUE );
  54. else
  55. return( FALSE );
  56. }
  57. private:
  58. TCHAR _tszPath[ MAX_PATH ];
  59. };
  60. //
  61. // IsLocalObjectVolume
  62. //
  63. // Returns true of the volume supports object IDs (NTFS5).
  64. //
  65. BOOL IsLocalObjectVolume( const TCHAR *ptszVolumeName );
  66. inline BOOL
  67. IsLocalObjectVolume( LONG iVol )
  68. {
  69. if( !CVolumeDeviceName::IsValid(iVol) )
  70. return( STATUS_OBJECT_PATH_NOT_FOUND );
  71. TCHAR tszVolumeName[ MAX_PATH+1 ];
  72. _tcscpy( tszVolumeName, CVolumeDeviceName(iVol) );
  73. _tcscat( tszVolumeName, TEXT("\\") );
  74. return( IsLocalObjectVolume( tszVolumeName ));
  75. }
  76. //
  77. // IsSystemVolumeInformation
  78. //
  79. // Returns true if the path is somewhere under "\System Volume Information"
  80. //
  81. BOOL IsSystemVolumeInformation( const TCHAR *ptszPath );
  82. HRESULT
  83. MapLocalPathToUNC( RPC_BINDING_HANDLE IDL_handle,
  84. const TCHAR *ptszLocalPath,
  85. TCHAR *ptszUNC );
  86. NTSTATUS
  87. OpenFileById( const TCHAR *ptszVolumeDeviceName,
  88. const CObjId &oid,
  89. ACCESS_MASK AccessMask,
  90. ULONG ShareAccess,
  91. ULONG AdditionalCreateOptions,
  92. HANDLE *ph);
  93. inline NTSTATUS
  94. OpenFileById( LONG iVol,
  95. const CObjId &oid,
  96. ACCESS_MASK AccessMask,
  97. ULONG ShareAccess,
  98. ULONG AdditionalCreateOptions,
  99. HANDLE *ph)
  100. {
  101. if( !CVolumeDeviceName::IsValid(iVol) )
  102. return( STATUS_OBJECT_PATH_NOT_FOUND );
  103. return( OpenFileById( CVolumeDeviceName(iVol), oid,
  104. AccessMask, ShareAccess, AdditionalCreateOptions, ph ));
  105. }
  106. NTSTATUS
  107. OpenVolume( const TCHAR *ptszVolumeDeviceName, HANDLE * ph );
  108. inline NTSTATUS
  109. OpenVolume( LONG iVol, HANDLE * phVolume )
  110. {
  111. if( !CVolumeDeviceName::IsValid(iVol) )
  112. return( STATUS_OBJECT_PATH_NOT_FOUND );
  113. return( OpenVolume( CVolumeDeviceName(iVol), phVolume ));
  114. }
  115. NTSTATUS
  116. CheckVolumeWriteProtection( const TCHAR *ptszVolumeDeviceName, BOOL *pfWriteProtected );
  117. #define CCH_MAX_VOLUME_NAME 50 // \\?\Volume{96765fc3-9c72-11d1-b93d-000000000000}\
  118. LONG
  119. MapVolumeDeviceNameToIndex( TCHAR *ptszVolumeDeviceName );
  120. NTSTATUS
  121. SetVolId( const TCHAR *ptszVolumeDeviceName, const CVolumeId &volid );
  122. inline NTSTATUS
  123. SetVolId( LONG iVol, const CVolumeId &volid )
  124. {
  125. if( !CVolumeDeviceName::IsValid(iVol) )
  126. return( STATUS_OBJECT_PATH_NOT_FOUND );
  127. return( SetVolId( CVolumeDeviceName(iVol), volid ));
  128. }
  129. NTSTATUS
  130. TrkCreateFile( const WCHAR *pwszCompleteDosPath,
  131. ACCESS_MASK AccessMask,
  132. ULONG Attributes,
  133. ULONG ShareAccess,
  134. ULONG CreationDisposition, // e.g. FILE_OPEN, FILE_OPEN_IF, etc
  135. ULONG CreateOptions, // e.g. FILE_WRITE_THROUGH, FILE_OPEN_FOR_BACKUP_INTENT
  136. LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  137. HANDLE *ph);
  138. NTSTATUS
  139. FindLocalPath( IN const TCHAR *ptszVolumeDeviceName,
  140. IN const CObjId &objid,
  141. OUT CDomainRelativeObjId *pdroidBirth,
  142. OUT TCHAR *ptszLocalPath );
  143. inline NTSTATUS
  144. FindLocalPath( IN ULONG iVol,
  145. IN const CObjId &objid,
  146. OUT CDomainRelativeObjId *pdroidBirth,
  147. OUT TCHAR *ptszLocalPath )
  148. {
  149. if( !CVolumeDeviceName::IsValid(iVol) )
  150. return( STATUS_OBJECT_PATH_NOT_FOUND );
  151. return( FindLocalPath( CVolumeDeviceName(iVol), objid, pdroidBirth, ptszLocalPath ));
  152. }
  153. NTSTATUS
  154. GetDroids( HANDLE hFile,
  155. CDomainRelativeObjId *pdroidCurrent,
  156. CDomainRelativeObjId *pdroidBirth,
  157. RGO_ENUM rgoEnum );
  158. NTSTATUS
  159. GetDroids( const TCHAR *ptszFile,
  160. CDomainRelativeObjId *pdroidCurrent,
  161. CDomainRelativeObjId *pdroidBirth,
  162. RGO_ENUM rgoEnum );
  163. NTSTATUS
  164. SetObjId( const HANDLE hFile,
  165. CObjId objid,
  166. const CDomainRelativeObjId &droidBirth );
  167. NTSTATUS
  168. SetObjId( const TCHAR *ptszFile,
  169. CObjId objid,
  170. const CDomainRelativeObjId &droidBirth );
  171. NTSTATUS
  172. MakeObjIdReborn(HANDLE hFile);
  173. NTSTATUS
  174. MakeObjIdReborn(const TCHAR *ptszVolumeDeviceName, const CObjId &objid);
  175. inline NTSTATUS
  176. MakeObjIdReborn(LONG iVol, const CObjId &objid)
  177. {
  178. if( !CVolumeDeviceName::IsValid(iVol) )
  179. return( STATUS_OBJECT_PATH_NOT_FOUND );
  180. return( MakeObjIdReborn( CVolumeDeviceName(iVol), objid ));
  181. }
  182. NTSTATUS
  183. SetBirthId( HANDLE hFile,
  184. const CDomainRelativeObjId &droidBirth );
  185. NTSTATUS
  186. SetBirthId( const TCHAR *ptszFile,
  187. const CDomainRelativeObjId &droidBirth );
  188. NTSTATUS
  189. GetBirthId( IN HANDLE hFile, OUT CDomainRelativeObjId *pdroidBirth );
  190. // This routine is inline because it doesn't get used in the production code,
  191. // only in tests.
  192. inline NTSTATUS
  193. DelObjId(HANDLE hFile)
  194. {
  195. // -----------------
  196. // Delete the Object ID
  197. // -----------------
  198. // Send the FSCTL
  199. IO_STATUS_BLOCK IoStatus;
  200. return NtFsControlFile(
  201. hFile,
  202. NULL,
  203. NULL,
  204. NULL,
  205. &IoStatus,
  206. FSCTL_DELETE_OBJECT_ID,
  207. NULL, // in buffer
  208. 0, // in buffer size
  209. NULL, // Out buffer
  210. 0); // Out buffer size
  211. }
  212. // This routine is inline because it doesn't get used in the production code,
  213. // only in tests.
  214. inline NTSTATUS
  215. DelObjId(const TCHAR *ptszVolumeDeviceName, const CObjId &objid)
  216. {
  217. // --------------
  218. // Initialization
  219. // --------------
  220. NTSTATUS status = STATUS_SUCCESS;
  221. HANDLE hFile = NULL;
  222. // -------------
  223. // Open the file
  224. // -------------
  225. EnableRestorePrivilege();
  226. status = OpenFileById(ptszVolumeDeviceName,
  227. objid,
  228. SYNCHRONIZE | FILE_WRITE_ATTRIBUTES,
  229. FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
  230. FILE_OPEN_FOR_BACKUP_INTENT, // for FSCTL_DELETE_OBJECT_ID
  231. &hFile);
  232. if( !NT_SUCCESS(status) )
  233. {
  234. hFile = NULL;
  235. TrkLog(( TRKDBG_ERROR, TEXT("Couldn't del objid, failed open (%08x)"),
  236. status ));
  237. goto Exit;
  238. }
  239. status = DelObjId( hFile );
  240. if( !NT_SUCCESS(status) && STATUS_OBJECT_NAME_NOT_FOUND != status )
  241. {
  242. TrkLog(( TRKDBG_ERROR, TEXT("Couldn't delete file Object ID, failed delete (%08x)"),
  243. status ));
  244. goto Exit;
  245. }
  246. // ----
  247. // Exit
  248. // ----
  249. Exit:
  250. if( NULL != hFile )
  251. NtClose( hFile );
  252. return( status );
  253. }
  254. inline NTSTATUS
  255. DelObjId(LONG iVol, const CObjId &objid)
  256. {
  257. if( !CVolumeDeviceName::IsValid(iVol) )
  258. return( STATUS_OBJECT_PATH_NOT_FOUND );
  259. return( DelObjId( CVolumeDeviceName(iVol), objid ));
  260. }