Changeset 1873

Show
Ignore:
Timestamp:
3/1/2010 9:52:43 AM (5 months ago)
Author:
lowjoel
Message:

Don't double error-handle in VolumeInfo?: The OpenHandle? and Open functions check for a valid file handle each, which is redundant. Since OpenHandle? is supposed to be called internally from the assembly, do no error checking and assume the calling function knows what it is doing. This can be seen in NtfsApi? where an exception has been replaced with an if-statement.

Location:
trunk/eraser6/Eraser.Util
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/eraser6/Eraser.Util/NtfsApi.cs

    r1859 r1873  
    3838        public static long GetMftValidSize(VolumeInfo volume) 
    3939        { 
    40             return GetNtfsVolumeData(volume).MftValidDataLength; 
     40            NativeMethods.NTFS_VOLUME_DATA_BUFFER? volumeData = GetNtfsVolumeData(volume); 
     41            if (volumeData == null) 
     42                throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 
     43 
     44            return volumeData.Value.MftValidDataLength; 
    4145        } 
    4246 
     
    4852        public static long GetMftRecordSegmentSize(VolumeInfo volume) 
    4953        { 
    50             try 
    51             { 
    52                 return GetNtfsVolumeData(volume).BytesPerFileRecordSegment; 
    53             } 
    54             catch (UnauthorizedAccessException) 
    55             { 
     54            NativeMethods.NTFS_VOLUME_DATA_BUFFER? volumeData = GetNtfsVolumeData(volume); 
     55            if (volumeData == null) 
    5656                return Math.Min(volume.ClusterSize, 1024); 
    57             } 
     57 
     58            return volumeData.Value.BytesPerFileRecordSegment; 
    5859        } 
    5960 
     
    6465        /// <param name="volume">The volume to query.</param> 
    6566        /// <returns>The NTFS_VOLUME_DATA_BUFFER structure representing the data 
    66         /// file systme structures for the volume.</returns> 
    67         /// <exception cref="UnauthorizedAccessException">Thrown when the current user 
    68         /// does not have the permissions required to obtain the volume information.</exception> 
    69         internal static NativeMethods.NTFS_VOLUME_DATA_BUFFER GetNtfsVolumeData(VolumeInfo volume) 
     67        /// file system structures for the volume, or null if the data could not be 
     68        /// retrieved.</returns> 
     69        internal static NativeMethods.NTFS_VOLUME_DATA_BUFFER? GetNtfsVolumeData(VolumeInfo volume) 
    7070        { 
    7171            using (SafeFileHandle volumeHandle = volume.OpenHandle( 
    7272                FileAccess.Read, FileShare.ReadWrite, FileOptions.None)) 
    7373            { 
     74                if (volumeHandle.IsInvalid) 
     75                    return null; 
     76 
    7477                uint resultSize = 0; 
    7578                NativeMethods.NTFS_VOLUME_DATA_BUFFER volumeData = 
  • trunk/eraser6/Eraser.Util/VolumeInfo.cs

    r1867 r1873  
    620620            //Check that the handle is valid 
    621621            if (handle.IsInvalid) 
    622                 throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error()); 
     622            { 
     623                int errorCode = Marshal.GetLastWin32Error(); 
     624                handle.Close(); 
     625                throw Win32ErrorCode.GetExceptionForWin32Error(errorCode); 
     626            } 
    623627 
    624628            //Return the FileStream 
     
    660664            if (openPath.Length > 0 && openPath[openPath.Length - 1] == '\\') 
    661665                openPath = openPath.Remove(openPath.Length - 1); 
    662             SafeFileHandle result = NativeMethods.CreateFile(openPath, access, 
    663                 (uint)share, IntPtr.Zero, (uint)FileMode.Open, (uint)options, IntPtr.Zero); 
    664             if (result.IsInvalid) 
    665             { 
    666                 int errorCode = Marshal.GetLastWin32Error(); 
    667                 result.Close(); 
    668                 throw Win32ErrorCode.GetExceptionForWin32Error(errorCode); 
    669             } 
    670  
    671             return result; 
     666            return NativeMethods.CreateFile(openPath, access, (uint)share, IntPtr.Zero, 
     667                (uint)FileMode.Open, (uint)options, IntPtr.Zero); 
    672668        } 
    673669