summaryrefslogtreecommitdiff
path: root/win/ntfssect.c
blob: 8c2bccaa9b5748b7140ea912ef1bbe65486dc5c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/* -------------------------------------------------------------------------- *
 *
 *   Copyright 2011 Shao Miller - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
 *   Boston MA 02111-1307, USA; either version 2 of the License, or
 *   (at your option) any later version; incorporated herein by reference.
 *
 * ------------------------------------------------------------------------- */

/****
 * ntfssect.c
 *
 * Fetch NTFS file cluster & sector information via Windows
 *
 * With special thanks to Mark Roddy for his article:
 *   http://www.wd-3.com/archive/luserland.htm
 */

#include <windows.h>
#include <winioctl.h>
#include <stddef.h>
#include <string.h>

#include "ntfssect.h"

/*** Macros */
#define M_ERR(msg) (NtfsSectLastErrorMessage = (msg))

/*** Function declarations */
static DWORD NtfsSectGetVolumeHandle(
    CHAR * VolumeName,
    S_NTFSSECT_VOLINFO * VolumeInfo
  );
static DWORD NtfsSectGetVolumePartitionLba(S_NTFSSECT_VOLINFO * VolumeInfo);

/*** Objects */
CHAR * NtfsSectLastErrorMessage;

/*** Function definitions */
DWORD M_NTFSSECT_API NtfsSectGetFileVcnExtent(
    HANDLE File,
    LARGE_INTEGER * Vcn,
    S_NTFSSECT_EXTENT * Extent
  ) {
    BOOL bad, ok;
    DWORD output_size, rc;
    STARTING_VCN_INPUT_BUFFER input;
    RETRIEVAL_POINTERS_BUFFER output;

    bad = (
        File == INVALID_HANDLE_VALUE ||
        !Vcn ||
        Vcn->QuadPart < 0 ||
        !Extent
      );
    if (bad)
      return ERROR_INVALID_PARAMETER;

    input.StartingVcn = *Vcn;
    ok = DeviceIoControl(
        File,
        FSCTL_GET_RETRIEVAL_POINTERS,
        &input,
        sizeof input,
        &output,
        sizeof output,
        &output_size,
        NULL
      );
    rc = GetLastError();
    switch (rc) {
        case NO_ERROR:
        case ERROR_MORE_DATA:
          Extent->FirstVcn = output.StartingVcn;
          Extent->NextVcn = output.Extents[0].NextVcn;
          Extent->FirstLcn = output.Extents[0].Lcn;
          return ERROR_SUCCESS;

        case ERROR_HANDLE_EOF:
          break;

        default:
          M_ERR("NtfsSectGetFileVcnExtent(): Unknown status!");
      }

    return rc;
  }

/* Internal use only */
static DWORD NtfsSectGetVolumeHandle(
    CHAR * VolumeName,
    S_NTFSSECT_VOLINFO * VolumeInfo
  ) {
    #define M_VOL_PREFIX "\\\\.\\"
    CHAR volname[sizeof M_VOL_PREFIX - 1 + MAX_PATH + 1] = M_VOL_PREFIX;
    CHAR * const volname_short = volname + sizeof M_VOL_PREFIX - 1;
    CHAR * c;
    DWORD rc;

    /* Prefix "\\.\" onto the passed volume name */
    strcpy(volname + sizeof M_VOL_PREFIX - 1, VolumeName);

    /* Find the last non-null character */
    for (c = volname_short; *c; ++c)
      ;

    /* Remove trailing back-slash */
    if (c[-1] == '\\')
      c[-1] = 0;

    /* Open the volume */
    VolumeInfo->Handle = CreateFile(
        volname,
        GENERIC_READ,
        FILE_SHARE_READ | FILE_SHARE_WRITE,
        NULL,
        OPEN_EXISTING,
        0,
        NULL
      );
    rc = GetLastError();
    if (VolumeInfo->Handle == INVALID_HANDLE_VALUE) {
        M_ERR("Unable to open volume handle!");
        goto err_handle;
      }

    return ERROR_SUCCESS;

    CloseHandle(VolumeInfo->Handle);
    err_handle:

    return rc;
  }

DWORD M_NTFSSECT_API NtfsSectGetVolumeInfo(
    CHAR * VolumeName,
    S_NTFSSECT_VOLINFO * VolumeInfo
  ) {
    S_NTFSSECT_XPFUNCS xp_funcs;
    DWORD rc, free_clusts, total_clusts;
    BOOL ok;

    if (!VolumeName || !VolumeInfo)
      return ERROR_INVALID_PARAMETER;

    rc = NtfsSectGetVolumeHandle(VolumeName, VolumeInfo);
    if (rc != ERROR_SUCCESS)
      goto err_handle;

    rc = NtfsSectLoadXpFuncs(&xp_funcs);
    if (rc != ERROR_SUCCESS)
      goto err_xp_funcs;

    ok = xp_funcs.GetDiskFreeSpace(
        VolumeName,
        &VolumeInfo->SectorsPerCluster,
        &VolumeInfo->BytesPerSector,
        &free_clusts,
        &total_clusts
      );
    rc = GetLastError();
    if (!ok) {
        M_ERR("GetDiskFreeSpace() failed!");
        goto err_freespace;
      }

    rc = NtfsSectGetVolumePartitionLba(VolumeInfo);
    if (rc != ERROR_SUCCESS)
      goto err_lba;

    VolumeInfo->Size = sizeof *VolumeInfo;
    rc = ERROR_SUCCESS;

    err_lba:

    err_freespace:

    NtfsSectUnloadXpFuncs(&xp_funcs);
    err_xp_funcs:

    if (rc != ERROR_SUCCESS) {
        CloseHandle(VolumeInfo->Handle);
        VolumeInfo->Handle = INVALID_HANDLE_VALUE;
      }
    err_handle:

    return rc;
  }

DWORD M_NTFSSECT_API NtfsSectGetVolumeInfoFromFileName(
    CHAR * FileName,
    S_NTFSSECT_VOLINFO * VolumeInfo
  ) {
    S_NTFSSECT_XPFUNCS xp_funcs;
    DWORD rc;
    CHAR volname[MAX_PATH + 1];
    BOOL ok;

    if (!FileName || !VolumeInfo)
      return ERROR_INVALID_PARAMETER;

    rc = NtfsSectLoadXpFuncs(&xp_funcs);
    if (rc != ERROR_SUCCESS) {
        goto err_xp_funcs;
      }

    ok = xp_funcs.GetVolumePathName(
        FileName,
        volname,
        sizeof volname
      );
    rc = GetLastError();
    if (!ok) {
        M_ERR("GetVolumePathName() failed!");
        goto err_volname;
      }

    rc = NtfsSectGetVolumeInfo(volname, VolumeInfo);

    err_volname:

    NtfsSectUnloadXpFuncs(&xp_funcs);
    err_xp_funcs:

    return rc;
  }

/* Internal use only */
static DWORD NtfsSectGetVolumePartitionLba(S_NTFSSECT_VOLINFO * VolumeInfo) {
    BOOL ok;
    VOLUME_DISK_EXTENTS vol_disk_extents;
    DWORD output_size, rc;

    ok = DeviceIoControl(
        VolumeInfo->Handle,
        IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,
        NULL,
        0,
        &vol_disk_extents,
        sizeof vol_disk_extents,
        &output_size,
        NULL
      );
    rc = GetLastError();
    if (!ok) {
        M_ERR("Couldn't fetch volume disk extent(s)!");
        goto err_vol_disk_extents;
      }

    if (vol_disk_extents.NumberOfDiskExtents != 1) {
        M_ERR("Unsupported number of volume disk extents!");
        goto err_num_of_extents;
      }

    VolumeInfo->PartitionLba.QuadPart = (
        vol_disk_extents.Extents[0].StartingOffset.QuadPart /
        VolumeInfo->BytesPerSector
      );

    return ERROR_SUCCESS;

    err_num_of_extents:

    err_vol_disk_extents:

    return rc;
  }

DWORD M_NTFSSECT_API NtfsSectLcnToLba(
    const S_NTFSSECT_VOLINFO * VolumeInfo,
    const LARGE_INTEGER * Lcn,
    LARGE_INTEGER * Lba
  ) {
    BOOL bad;
    bad = (
        !VolumeInfo ||
        !VolumeInfo->BytesPerSector ||
        !VolumeInfo->SectorsPerCluster ||
        !Lcn ||
        Lcn->QuadPart < 0 ||
        !Lba
      );
    if (bad)
      return ERROR_INVALID_PARAMETER;

    Lba->QuadPart = (
        VolumeInfo->PartitionLba.QuadPart +
        Lcn->QuadPart *
        VolumeInfo->SectorsPerCluster
      );
    return ERROR_SUCCESS;
  }

DWORD M_NTFSSECT_API NtfsSectLoadXpFuncs(S_NTFSSECT_XPFUNCS * XpFuncs) {
    DWORD rc;

    if (!XpFuncs)
      return ERROR_INVALID_PARAMETER;

    XpFuncs->Size = sizeof *XpFuncs;

    XpFuncs->Kernel32 = LoadLibrary("kernel32.dll");
    rc = GetLastError();
    if (!XpFuncs->Kernel32) {
        M_ERR("KERNEL32.DLL not found!");
        goto err;
      }

    XpFuncs->GetVolumePathName = (F_KERNEL32_GETVOLUMEPATHNAME *) (
        GetProcAddress(
            XpFuncs->Kernel32,
            "GetVolumePathNameA"
          )
      );
    rc = GetLastError();
    if (!XpFuncs->GetVolumePathName) {
        M_ERR("GetVolumePathName() not found in KERNEL32.DLL!");
        goto err;
      }

    XpFuncs->GetDiskFreeSpace = (F_KERNEL32_GETDISKFREESPACE *) (
        GetProcAddress(
            XpFuncs->Kernel32,
            "GetDiskFreeSpaceA"
          )
      );
    rc = GetLastError();
    if (!XpFuncs->GetDiskFreeSpace) {
        M_ERR("GetDiskFreeSpace() not found in KERNEL32.DLL!");
        goto err;
      }

    return ERROR_SUCCESS;

    err:
    NtfsSectUnloadXpFuncs(XpFuncs);
    return rc;
  }

VOID M_NTFSSECT_API NtfsSectUnloadXpFuncs(S_NTFSSECT_XPFUNCS * XpFuncs) {
    if (!XpFuncs)
      return;

    XpFuncs->GetDiskFreeSpace = NULL;
    XpFuncs->GetVolumePathName = NULL;
    if (XpFuncs->Kernel32)
      FreeLibrary(XpFuncs->Kernel32);
    XpFuncs->Kernel32 = NULL;
    XpFuncs->Size = 0;
    return;
  }