Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b76936c
commit be58a93
Showing
10 changed files
with
155 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Package disk provides cross platform disk usage information | ||
package disk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package disk | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestUsage(t *testing.T) { | ||
dir, err := os.UserHomeDir() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
free, total, err := Usage(dir) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
t.Logf("%s - %d/%d bytes", dir, free, total) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//go:build !windows | ||
|
||
package disk | ||
|
||
import "syscall" | ||
|
||
// Usage returns the free and total bytes on the filesystem containing the | ||
// specified path. | ||
func Usage(p string) (free, total uint64, err error) { | ||
var stat syscall.Statfs_t | ||
if err := syscall.Statfs(p, &stat); err != nil { | ||
return 0, 0, err | ||
} | ||
return stat.Bfree * uint64(stat.Bsize), stat.Blocks * uint64(stat.Bsize), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//go:build windows | ||
|
||
package disk | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
// Usage returns the free and totreal bytes on the filesystem containing the | ||
// specified path. | ||
func Usage(p string) (free, total uint64, err error) { | ||
if _, err = os.Stat(p); err != nil { | ||
return 0, 0, fmt.Errorf("failed to stat %v: %v", p, err) | ||
} | ||
|
||
ptr, err := windows.UTF16PtrFromString(p) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = windows.GetDiskFreeSpaceEx(ptr, &free, &total, nil) | ||
return | ||
} |