source: trunk/eraser6/Eraser.Util/NtfsApi.cs @ 1802

Revision 1802, 2.7 KB checked in by lowjoel, 5 years ago (diff)

Merged the CodeReview? Branch back to trunk. (Finally!) Closes #275: Code Review.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008-2010 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using Microsoft.Win32.SafeHandles;
26using System.Runtime.InteropServices;
27
28namespace Eraser.Util
29{
30    public static class NtfsApi
31    {
32        /// <summary>
33        /// Gets the actual size of the MFT.
34        /// </summary>
35        /// <param name="volume">The volume to query.</param>
36        /// <returns>The size of the MFT.</returns>
37        public static long GetMftValidSize(VolumeInfo volume)
38        {
39            return GetNtfsVolumeData(volume).MftValidDataLength;
40        }
41
42        /// <summary>
43        /// Gets the size of one MFT record segment.
44        /// </summary>
45        /// <param name="volume">The volume to query.</param>
46        /// <returns>The size of one MFT record segment.</returns>
47        public static long GetMftRecordSegmentSize(VolumeInfo volume)
48        {
49            return GetNtfsVolumeData(volume).BytesPerFileRecordSegment;
50        }
51
52        /// <summary>
53        /// Sends the FSCTL_GET_NTFS_VOLUME_DATA control code, returning the resuling
54        /// NTFS_VOLUME_DATA_BUFFER.
55        /// </summary>
56        /// <param name="volume">The volume to query.</param>
57        /// <returns>The NTFS_VOLUME_DATA_BUFFER structure representing the data
58        /// file systme structures for the volume.</returns>
59        internal static NativeMethods.NTFS_VOLUME_DATA_BUFFER GetNtfsVolumeData(VolumeInfo volume)
60        {
61            using (SafeFileHandle volumeHandle = NativeMethods.CreateFile(
62                volume.VolumeId.Remove(volume.VolumeId.Length - 1),
63                NativeMethods.GENERIC_READ, NativeMethods.FILE_SHARE_READ |
64                NativeMethods.FILE_SHARE_WRITE, IntPtr.Zero, NativeMethods.OPEN_EXISTING,
65                0, IntPtr.Zero))
66            {
67                uint resultSize = 0;
68                NativeMethods.NTFS_VOLUME_DATA_BUFFER volumeData =
69                    new NativeMethods.NTFS_VOLUME_DATA_BUFFER();
70                if (NativeMethods.DeviceIoControl(volumeHandle,
71                    NativeMethods.FSCTL_GET_NTFS_VOLUME_DATA, IntPtr.Zero, 0, out volumeData,
72                    (uint)Marshal.SizeOf(volumeData), out resultSize, IntPtr.Zero))
73                {
74                    return volumeData;
75                }
76
77                throw Win32ErrorCode.GetExceptionForWin32Error(Marshal.GetLastWin32Error());
78            }
79        }
80    }
81}
Note: See TracBrowser for help on using the repository browser.