2021-08-18 21:35:22 -04:00
|
|
|
//go:build windows
|
2015-10-17 19:45:42 -04:00
|
|
|
// +build windows
|
|
|
|
|
2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-10-17 19:45:42 -04:00
|
|
|
|
|
|
|
package disk
|
|
|
|
|
|
|
|
import (
|
2020-09-30 01:54:02 -04:00
|
|
|
"fmt"
|
2017-10-13 06:01:15 -04:00
|
|
|
"os"
|
2015-10-17 19:45:42 -04:00
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2020-10-01 23:24:34 -04:00
|
|
|
|
|
|
|
"golang.org/x/sys/windows"
|
2015-10-17 19:45:42 -04:00
|
|
|
)
|
|
|
|
|
2016-10-31 12:34:44 -04:00
|
|
|
var (
|
2020-10-01 23:24:34 -04:00
|
|
|
kernel32 = windows.NewLazySystemDLL("kernel32.dll")
|
2016-10-31 12:34:44 -04:00
|
|
|
|
|
|
|
// GetDiskFreeSpaceEx - https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx
|
|
|
|
// Retrieves information about the amount of space that is available on a disk volume,
|
|
|
|
// which is the total amount of space, the total amount of free space, and the total
|
|
|
|
// amount of free space available to the user that is associated with the calling thread.
|
|
|
|
GetDiskFreeSpaceEx = kernel32.NewProc("GetDiskFreeSpaceExW")
|
2020-10-01 23:24:34 -04:00
|
|
|
|
2016-10-31 12:34:44 -04:00
|
|
|
// GetDiskFreeSpace - https://msdn.microsoft.com/en-us/library/windows/desktop/aa364935(v=vs.85).aspx
|
|
|
|
// Retrieves information about the specified disk, including the amount of free space on the disk.
|
|
|
|
GetDiskFreeSpace = kernel32.NewProc("GetDiskFreeSpaceW")
|
|
|
|
)
|
|
|
|
|
2016-01-25 02:03:38 -05:00
|
|
|
// GetInfo returns total and free bytes available in a directory, e.g. `C:\`.
|
2015-10-17 19:45:42 -04:00
|
|
|
// It returns free space available to the user (including quota limitations)
|
|
|
|
//
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx
|
2016-01-25 02:03:38 -05:00
|
|
|
func GetInfo(path string) (info Info, err error) {
|
2016-06-07 21:15:04 -04:00
|
|
|
// Stat to know if the path exists.
|
2017-10-13 06:01:15 -04:00
|
|
|
if _, err = os.Stat(path); err != nil {
|
2016-06-07 21:15:04 -04:00
|
|
|
return Info{}, err
|
|
|
|
}
|
|
|
|
|
2015-10-17 19:45:42 -04:00
|
|
|
lpFreeBytesAvailable := int64(0)
|
|
|
|
lpTotalNumberOfBytes := int64(0)
|
|
|
|
lpTotalNumberOfFreeBytes := int64(0)
|
|
|
|
|
|
|
|
// Extract values safely
|
|
|
|
// BOOL WINAPI GetDiskFreeSpaceEx(
|
|
|
|
// _In_opt_ LPCTSTR lpDirectoryName,
|
|
|
|
// _Out_opt_ PULARGE_INTEGER lpFreeBytesAvailable,
|
|
|
|
// _Out_opt_ PULARGE_INTEGER lpTotalNumberOfBytes,
|
|
|
|
// _Out_opt_ PULARGE_INTEGER lpTotalNumberOfFreeBytes
|
|
|
|
// );
|
2015-10-17 22:09:43 -04:00
|
|
|
_, _, _ = GetDiskFreeSpaceEx.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
|
2015-10-17 19:45:42 -04:00
|
|
|
uintptr(unsafe.Pointer(&lpFreeBytesAvailable)),
|
|
|
|
uintptr(unsafe.Pointer(&lpTotalNumberOfBytes)),
|
2015-10-17 22:09:43 -04:00
|
|
|
uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes)))
|
2020-10-01 23:24:34 -04:00
|
|
|
|
|
|
|
if uint64(lpTotalNumberOfFreeBytes) > uint64(lpTotalNumberOfBytes) {
|
2022-08-04 19:10:08 -04:00
|
|
|
return info, fmt.Errorf("detected free space (%d) > total drive space (%d), fs corruption at (%s). please run 'fsck'",
|
2020-10-01 23:24:34 -04:00
|
|
|
uint64(lpTotalNumberOfFreeBytes), uint64(lpTotalNumberOfBytes), path)
|
|
|
|
}
|
|
|
|
|
|
|
|
info = Info{
|
|
|
|
Total: uint64(lpTotalNumberOfBytes),
|
|
|
|
Free: uint64(lpTotalNumberOfFreeBytes),
|
|
|
|
Used: uint64(lpTotalNumberOfBytes) - uint64(lpTotalNumberOfFreeBytes),
|
|
|
|
FSType: getFSType(path),
|
|
|
|
}
|
2016-07-04 01:34:45 -04:00
|
|
|
|
|
|
|
// Return values of GetDiskFreeSpace()
|
|
|
|
lpSectorsPerCluster := uint32(0)
|
|
|
|
lpBytesPerSector := uint32(0)
|
|
|
|
lpNumberOfFreeClusters := uint32(0)
|
|
|
|
lpTotalNumberOfClusters := uint32(0)
|
|
|
|
|
|
|
|
// Extract values safely
|
|
|
|
// BOOL WINAPI GetDiskFreeSpace(
|
|
|
|
// _In_ LPCTSTR lpRootPathName,
|
|
|
|
// _Out_ LPDWORD lpSectorsPerCluster,
|
|
|
|
// _Out_ LPDWORD lpBytesPerSector,
|
|
|
|
// _Out_ LPDWORD lpNumberOfFreeClusters,
|
|
|
|
// _Out_ LPDWORD lpTotalNumberOfClusters
|
|
|
|
// );
|
|
|
|
_, _, _ = GetDiskFreeSpace.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
|
|
|
|
uintptr(unsafe.Pointer(&lpSectorsPerCluster)),
|
|
|
|
uintptr(unsafe.Pointer(&lpBytesPerSector)),
|
|
|
|
uintptr(unsafe.Pointer(&lpNumberOfFreeClusters)),
|
|
|
|
uintptr(unsafe.Pointer(&lpTotalNumberOfClusters)))
|
|
|
|
|
2017-08-03 23:07:22 -04:00
|
|
|
info.Files = uint64(lpTotalNumberOfClusters)
|
|
|
|
info.Ffree = uint64(lpNumberOfFreeClusters)
|
2016-07-04 01:34:45 -04:00
|
|
|
|
2016-01-25 02:03:38 -05:00
|
|
|
return info, nil
|
2015-10-17 19:45:42 -04:00
|
|
|
}
|