Improve disk code to return back disk StatFS{} structure

```
StatFS {
Total int64
Free int64
FSType string
}
```

Provides more information in a cross platform way.
This commit is contained in:
Harshavardhana 2015-10-17 19:09:43 -07:00
parent 4b3961e1df
commit a8a935f5fd
8 changed files with 116 additions and 59 deletions

View File

@ -1,13 +1,4 @@
language: go language: go
before_install:
- git clone https://github.com/yasm/yasm
- cd yasm
- git checkout v1.2.0
- "./autogen.sh"
- "./configure"
- make
- export PATH=$PATH:`pwd`
- cd ..
script: script:
- make test - make test
- make test GOFLAGS="-race" - make test GOFLAGS="-race"

11
pkg/disk/disk.go Normal file
View File

@ -0,0 +1,11 @@
package disk
// StatFS stat fs struct is container which holds following values
// Total - total size of the volume / disk
// Free - free size of the volume / disk
// FSType - file system type string
type StatFS struct {
Total int64
Free int64
FSType string
}

View File

@ -34,8 +34,11 @@ var _ = Suite(&MySuite{})
func (s *MySuite) TestFree(c *C) { func (s *MySuite) TestFree(c *C) {
path, err := ioutil.TempDir(os.TempDir(), "minio-") path, err := ioutil.TempDir(os.TempDir(), "minio-")
c.Check(err, IsNil) c.Assert(err, IsNil)
_, _, err = disk.Stat(path) statfs, err := disk.Stat(path)
c.Check(err, IsNil) c.Assert(err, IsNil)
c.Assert(statfs.Total, Not(Equals), 0)
c.Assert(statfs.Free, Not(Equals), 0)
c.Assert(statfs.FSType, Not(Equals), "UNKNOWN")
} }

View File

@ -23,13 +23,18 @@ import (
) )
// Stat returns total and free bytes available in a directory, e.g. `/`. // Stat returns total and free bytes available in a directory, e.g. `/`.
func Stat(path string) (total, free int64, err error) { func Stat(path string) (statfs StatFS, err error) {
s := syscall.Statfs_t{} s := syscall.Statfs_t{}
err = syscall.Statfs(path, &s) err = syscall.Statfs(path, &s)
if err != nil { if err != nil {
return return StatFS{}, err
} }
total = int64(s.Bsize) * int64(s.Blocks) statfs = StatFS{}
free = int64(s.Bsize) * int64(s.Bfree) statfs.Total = int64(s.Bsize) * int64(s.Blocks)
return statfs.Free = int64(s.Bsize) * int64(s.Bfree)
statfs.FSType, err = getFSType(path)
if err != nil {
return StatFS{}, err
}
return statfs, nil
} }

View File

@ -27,20 +27,14 @@ import (
// It returns free space available to the user (including quota limitations) // 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 // https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx
func Stat(path string) (total, free int64, err error) { func Stat(path string) (statfs StatFS, err error) {
kernel32, err := syscall.LoadLibrary("Kernel32.dll") dll := syscall.MustLoadDLL("kernel32.dll")
if err != nil {
return 0, 0, err
}
defer syscall.FreeLibrary(kernel32)
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa364937(v=vs.85).aspx // 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, // 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 // 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. // amount of free space available to the user that is associated with the calling thread.
GetDiskFreeSpaceEx, err := syscall.GetProcAddress(syscall.Handle(kernel32), "GetDiskFreeSpaceExW") GetDiskFreeSpaceEx := dll.MustFindProc("GetDiskFreeSpaceExW")
if err != nil {
return 0, 0, err
}
lpFreeBytesAvailable := int64(0) lpFreeBytesAvailable := int64(0)
lpTotalNumberOfBytes := int64(0) lpTotalNumberOfBytes := int64(0)
lpTotalNumberOfFreeBytes := int64(0) lpTotalNumberOfFreeBytes := int64(0)
@ -52,18 +46,13 @@ func Stat(path string) (total, free int64, err error) {
// _Out_opt_ PULARGE_INTEGER lpTotalNumberOfBytes, // _Out_opt_ PULARGE_INTEGER lpTotalNumberOfBytes,
// _Out_opt_ PULARGE_INTEGER lpTotalNumberOfFreeBytes // _Out_opt_ PULARGE_INTEGER lpTotalNumberOfFreeBytes
// ); // );
r1, _, e1 := syscall.Syscall6(uintptr(GetDiskFreeSpaceEx), 4, _, _, _ = GetDiskFreeSpaceEx.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
uintptr(unsafe.Pointer(&lpFreeBytesAvailable)), uintptr(unsafe.Pointer(&lpFreeBytesAvailable)),
uintptr(unsafe.Pointer(&lpTotalNumberOfBytes)), uintptr(unsafe.Pointer(&lpTotalNumberOfBytes)),
uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes)), 0, 0) uintptr(unsafe.Pointer(&lpTotalNumberOfFreeBytes)))
if e1 != 0 { statfs = StatFS{}
return 0, 0, error(e1) statfs.Total = int64(lpTotalNumberOfBytes)
} statfs.Free = int64(lpFreeBytesAvailable)
if r1 == 0 { statfs.FSType = getFSType(path)
return 0, 0, syscall.EINVAL return statfs, nil
}
total = int64(lpTotalNumberOfBytes)
free = int64(lpFreeBytesAvailable)
return total, free, nil
} }

View File

@ -28,22 +28,17 @@ var fsType2StringMap = map[string]string{
"11": "HFS", "11": "HFS",
} }
// FSType returns the filesystem type of the underlying mounted filesystem // getFSType returns the filesystem type of the underlying mounted filesystem
func FSType(path string) (string, error) { func getFSType(path string) (string, error) {
s := syscall.Statfs_t{} s := syscall.Statfs_t{}
err := syscall.Statfs(path, &s) err := syscall.Statfs(path, &s)
if err != nil { if err != nil {
return "", err return "", err
} }
return getFSType(s.Type), nil fsTypeHex := strconv.FormatUint(uint64(s.Type), 16)
}
// getFSType - get filesystem type
func getFSType(fsType uint32) string {
fsTypeHex := strconv.FormatUint(uint64(fsType), 16)
fsTypeString, ok := fsType2StringMap[fsTypeHex] fsTypeString, ok := fsType2StringMap[fsTypeHex]
if ok == false { if ok == false {
return "UNKNOWN" return "UNKNOWN", nil
} }
return fsTypeString return fsTypeString, nil
} }

View File

@ -39,22 +39,17 @@ var fsType2StringMap = map[string]string{
"f15f": "ecryptfs", "f15f": "ecryptfs",
} }
// FSType returns the filesystem type of the underlying mounted filesystem // getFSType returns the filesystem type of the underlying mounted filesystem
func FSType(path string) (string, error) { func getFSType(path string) (string, error) {
s := syscall.Statfs_t{} s := syscall.Statfs_t{}
err := syscall.Statfs(path, &s) err := syscall.Statfs(path, &s)
if err != nil { if err != nil {
return "", err return "", err
} }
return getFSType(s.Type), nil fsTypeHex := strconv.FormatInt(s.Type, 16)
}
// getFSType - get filesystem type
func getFSType(fsType int64) string {
fsTypeHex := strconv.FormatInt(fsType, 16)
fsTypeString, ok := fsType2StringMap[fsTypeHex] fsTypeString, ok := fsType2StringMap[fsTypeHex]
if ok == false { if ok == false {
return "UNKNOWN" return "UNKNOWN", nil
} }
return fsTypeString return fsTypeString, nil
} }

68
pkg/disk/type_windows.go Normal file
View File

@ -0,0 +1,68 @@
// +build windows
/*
* Minio Cloud Storage, (C) 2015 Minio, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package disk
import (
"path/filepath"
"syscall"
"unsafe"
)
// getFSType returns the filesystem type of the underlying mounted filesystem
func getFSType(path string) string {
dll := syscall.MustLoadDLL("kernel32.dll")
GetVolumeInformation := dll.MustFindProc("GetVolumeInformationW")
var volumeNameSize uint32 = 260
var nFileSystemNameSize, lpVolumeSerialNumber uint32
var lpFileSystemFlags, lpMaximumComponentLength uint32
var lpFileSystemNameBuffer, volumeName [260]byte
var ps = syscall.StringToUTF16Ptr(filepath.VolumeName(path))
// Extract values safely
// BOOL WINAPI GetVolumeInformation(
// _In_opt_ LPCTSTR lpRootPathName,
// _Out_opt_ LPTSTR lpVolumeNameBuffer,
// _In_ DWORD nVolumeNameSize,
// _Out_opt_ LPDWORD lpVolumeSerialNumber,
// _Out_opt_ LPDWORD lpMaximumComponentLength,
// _Out_opt_ LPDWORD lpFileSystemFlags,
// _Out_opt_ LPTSTR lpFileSystemNameBuffer,
// _In_ DWORD nFileSystemNameSize
// );
_, _, _ = GetVolumeInformation.Call(uintptr(unsafe.Pointer(ps)),
uintptr(unsafe.Pointer(&volumeName)),
uintptr(volumeNameSize),
uintptr(unsafe.Pointer(&lpVolumeSerialNumber)),
uintptr(unsafe.Pointer(&lpMaximumComponentLength)),
uintptr(unsafe.Pointer(&lpFileSystemFlags)),
uintptr(unsafe.Pointer(&lpFileSystemNameBuffer)),
uintptr(unsafe.Pointer(&nFileSystemNameSize)), 0)
var bytes []byte
if lpFileSystemNameBuffer[6] == 0 {
bytes = []byte{lpFileSystemNameBuffer[0], lpFileSystemNameBuffer[2],
lpFileSystemNameBuffer[4]}
} else {
bytes = []byte{lpFileSystemNameBuffer[0], lpFileSystemNameBuffer[2],
lpFileSystemNameBuffer[4], lpFileSystemNameBuffer[6]}
}
return string(bytes)
}