Skip to content

Commit

Permalink
Merge pull request #76 from gcash/data-dir-disk-checks
Browse files Browse the repository at this point in the history
[Fix] Check disk space at the level db store path
  • Loading branch information
zquestz authored Oct 27, 2018
2 parents 8c258d8 + d46a206 commit 021c797
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 42 deletions.
2 changes: 1 addition & 1 deletion database/ffldb/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1762,7 +1762,7 @@ func (db *db) begin(writable bool) (*transaction, error) {
// Make sure there is enough available disk space so we can inform the
// user of the problem instead of causing a db failure.
if writable {
freeSpace, err := getAvailableDiskSpace()
freeSpace, err := getAvailableDiskSpace(db.store.basePath)
if err != nil {
return nil, makeDbErr(database.ErrDriverSpecific,
"failed to inspect available disk space", err)
Expand Down
2 changes: 1 addition & 1 deletion database/ffldb/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ package ffldb
// 1 byte greater than the disk space that prevents writes.
// Unfortunately there is not a good way to get the current
// disk space on these platforms in Go at this time.
func getAvailableDiskSpace() (uint64, error) {
func getAvailableDiskSpace(path string) (uint64, error) {
return minAvailableSpaceUpdate + 1, nil
}
10 changes: 2 additions & 8 deletions database/ffldb/disk_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@
package ffldb

import (
"os"
"syscall"
)

// getAvailableDiskSpace returns the number of bytes of available disk space.
func getAvailableDiskSpace() (uint64, error) {
func getAvailableDiskSpace(path string) (uint64, error) {
var stat syscall.Statfs_t

wd, err := os.Getwd()
if err != nil {
return 0, err
}

syscall.Statfs(wd, &stat)
syscall.Statfs(path, &stat)

return stat.Bavail * uint64(stat.Bsize), nil
}
10 changes: 2 additions & 8 deletions database/ffldb/disk_dragonfly.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@
package ffldb

import (
"os"
"syscall"
)

// getAvailableDiskSpace returns the number of bytes of available disk space.
func getAvailableDiskSpace() (uint64, error) {
func getAvailableDiskSpace(path string) (uint64, error) {
var stat syscall.Statfs_t

wd, err := os.Getwd()
if err != nil {
return 0, err
}

syscall.Statfs(wd, &stat)
syscall.Statfs(path, &stat)

return uint64(stat.Bavail) * uint64(stat.Bsize), nil
}
10 changes: 2 additions & 8 deletions database/ffldb/disk_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@
package ffldb

import (
"os"
"syscall"
)

// getAvailableDiskSpace returns the number of bytes of available disk space.
func getAvailableDiskSpace() (uint64, error) {
func getAvailableDiskSpace(path string) (uint64, error) {
var stat syscall.Statfs_t

wd, err := os.Getwd()
if err != nil {
return 0, err
}

syscall.Statfs(wd, &stat)
syscall.Statfs(path, &stat)

return uint64(stat.Bavail) * uint64(stat.Bsize), nil
}
10 changes: 2 additions & 8 deletions database/ffldb/disk_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,14 @@
package ffldb

import (
"os"
"syscall"
)

// getAvailableDiskSpace returns the number of bytes of available disk space.
func getAvailableDiskSpace() (uint64, error) {
func getAvailableDiskSpace(path string) (uint64, error) {
var stat syscall.Statfs_t

wd, err := os.Getwd()
if err != nil {
return 0, err
}

syscall.Statfs(wd, &stat)
syscall.Statfs(path, &stat)

return stat.Bavail * uint64(stat.Bsize), nil
}
10 changes: 2 additions & 8 deletions database/ffldb/disk_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,17 @@
package ffldb

import (
"os"
"syscall"
"unsafe"
)

// getAvailableDiskSpace returns the number of bytes of available disk space.
func getAvailableDiskSpace() (uint64, error) {
wd, err := os.Getwd()
if err != nil {
return 0, err
}

func getAvailableDiskSpace(path string) (uint64, error) {
h := syscall.MustLoadDLL("kernel32.dll")
c := h.MustFindProc("GetDiskFreeSpaceExW")

var freeBytes, totalBytes, availBytes int64
_, _, err = c.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(wd))),
_, _, err := c.Call(uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(path))),
uintptr(unsafe.Pointer(&freeBytes)), uintptr(unsafe.Pointer(&totalBytes)), uintptr(unsafe.Pointer(&availBytes)))
if err != nil {
return 0, err
Expand Down

0 comments on commit 021c797

Please sign in to comment.