Skip to content

Commit

Permalink
build diskfree on all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
gokcehan committed Apr 30, 2023
1 parent 2c86577 commit f04401b
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 27 deletions.
19 changes: 19 additions & 0 deletions df_openbsd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"log"

"golang.org/x/sys/unix"
)

func diskFree(wd string) string {
var stat unix.Statfs_t

if err := unix.Statfs(wd, &stat); err != nil {
log.Printf("diskfree: %s", err)
return ""
}

// Available blocks * size per block = available space in bytes
return "df: " + humanize(int64(uint64(stat.F_bavail)*uint64(stat.F_bsize)))
}
21 changes: 21 additions & 0 deletions df_statfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build darwin || dragonfly || freebsd || linux

package main

import (
"log"

"golang.org/x/sys/unix"
)

func diskFree(wd string) string {
var stat unix.Statfs_t

if err := unix.Statfs(wd, &stat); err != nil {
log.Printf("diskfree: %s", err)
return ""
}

// Available blocks * size per block = available space in bytes
return "df: " + humanize(int64(uint64(stat.Bavail)*uint64(stat.Bsize)))
}
21 changes: 21 additions & 0 deletions df_statvfs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//go:build illumos || netbsd || solaris

package main

import (
"log"

"golang.org/x/sys/unix"
)

func diskFree(wd string) string {
var stat unix.Statvfs_t

if err := unix.Statvfs(wd, &stat); err != nil {
log.Printf("diskfree: %s", err)
return ""
}

// Available blocks * size per block = available space in bytes
return "df: " + humanize(int64(uint64(stat.Bavail)*uint64(stat.Bsize)))
}
23 changes: 23 additions & 0 deletions df_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"log"

"golang.org/x/sys/windows"
)

func diskFree(wd string) string {
var free uint64

pathPtr, err := windows.UTF16PtrFromString(wd)
if err != nil {
log.Printf("diskfree: %s", err)
return ""
}
err = windows.GetDiskFreeSpaceEx(pathPtr, &free, nil, nil) // cwd, free, total, available
if err != nil {
log.Printf("diskfree: %s", err)
return ""
}
return "df: " + humanize(int64(free))
}
11 changes: 0 additions & 11 deletions os.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,3 @@ func exportFiles(f string, fs []string, pwd string) {
os.Setenv("fx", envFiles)
}
}
func diskFree(wd string) string {
var stat unix.Statfs_t

if err := unix.Statfs(wd, &stat); err != nil {
log.Printf("diskfree: %s", err)
return ""
}

// Available blocks * size per block = available space in bytes
return "df: " + humanize(int64(uint64(stat.Bavail)*uint64(stat.Bsize)))
}
16 changes: 0 additions & 16 deletions os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,19 +184,3 @@ func exportFiles(f string, fs []string, pwd string) {
os.Setenv("fx", envFiles)
}
}

func diskFree(wd string) string {
var free uint64

pathPtr, err := windows.UTF16PtrFromString(wd)
if err != nil {
log.Printf("diskfree: %s", err)
return ""
}
err = windows.GetDiskFreeSpaceEx(pathPtr, &free, nil, nil) // cwd, free, total, available
if err != nil {
log.Printf("diskfree: %s", err)
return ""
}
return "df: " + humanize(int64(free))
}

0 comments on commit f04401b

Please sign in to comment.