1 | /* $Id: RTFileQueryFsSizes-nt.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTFileQueryFsSizes, Native NT.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #define LOG_GROUP RTLOGGROUP_FILE
|
---|
32 | #include "internal-r3-nt.h"
|
---|
33 |
|
---|
34 | #include <iprt/file.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 |
|
---|
37 |
|
---|
38 |
|
---|
39 | RTR3DECL(int) RTFileQueryFsSizes(RTFILE hFile, PRTFOFF pcbTotal, RTFOFF *pcbFree,
|
---|
40 | uint32_t *pcbBlock, uint32_t *pcbSector)
|
---|
41 | {
|
---|
42 | int rc;
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * Get the volume information.
|
---|
46 | */
|
---|
47 | FILE_FS_SIZE_INFORMATION FsSizeInfo;
|
---|
48 | IO_STATUS_BLOCK Ios = RTNT_IO_STATUS_BLOCK_INITIALIZER;
|
---|
49 | NTSTATUS rcNt = NtQueryVolumeInformationFile((HANDLE)RTFileToNative(hFile), &Ios,
|
---|
50 | &FsSizeInfo, sizeof(FsSizeInfo), FileFsSizeInformation);
|
---|
51 | if (NT_SUCCESS(rcNt))
|
---|
52 | {
|
---|
53 | /*
|
---|
54 | * Calculate the return values.
|
---|
55 | */
|
---|
56 | if (pcbTotal)
|
---|
57 | {
|
---|
58 | *pcbTotal = FsSizeInfo.TotalAllocationUnits.QuadPart
|
---|
59 | * FsSizeInfo.SectorsPerAllocationUnit
|
---|
60 | * FsSizeInfo.BytesPerSector;
|
---|
61 | if ( *pcbTotal / FsSizeInfo.SectorsPerAllocationUnit / FsSizeInfo.BytesPerSector
|
---|
62 | != FsSizeInfo.TotalAllocationUnits.QuadPart)
|
---|
63 | *pcbTotal = UINT64_MAX;
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (pcbFree)
|
---|
67 | {
|
---|
68 | *pcbFree = FsSizeInfo.AvailableAllocationUnits.QuadPart
|
---|
69 | * FsSizeInfo.SectorsPerAllocationUnit
|
---|
70 | * FsSizeInfo.BytesPerSector;
|
---|
71 | if ( *pcbFree / FsSizeInfo.SectorsPerAllocationUnit / FsSizeInfo.BytesPerSector
|
---|
72 | != FsSizeInfo.AvailableAllocationUnits.QuadPart)
|
---|
73 | *pcbFree = UINT64_MAX;
|
---|
74 | }
|
---|
75 |
|
---|
76 | rc = VINF_SUCCESS;
|
---|
77 | if (pcbBlock)
|
---|
78 | {
|
---|
79 | *pcbBlock = FsSizeInfo.SectorsPerAllocationUnit * FsSizeInfo.BytesPerSector;
|
---|
80 | if (*pcbBlock / FsSizeInfo.BytesPerSector != FsSizeInfo.SectorsPerAllocationUnit)
|
---|
81 | rc = VERR_OUT_OF_RANGE;
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (pcbSector)
|
---|
85 | *pcbSector = FsSizeInfo.BytesPerSector;
|
---|
86 | }
|
---|
87 | else
|
---|
88 | rc = RTErrConvertFromNtStatus(rcNt);
|
---|
89 |
|
---|
90 | return rc;
|
---|
91 | }
|
---|
92 |
|
---|