Skip to content

Commit

Permalink
refactor disk space check to work on Linux and Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
tphakala committed Mar 17, 2024
1 parent 417086e commit 5325119
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
18 changes: 18 additions & 0 deletions disk_space_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build linux
// +build linux

package main

import (
"syscall"
)

func getFreeSpace(path string) (uint64, error) {
var stat syscall.Statfs_t
if err := syscall.Statfs(path, &stat); err != nil {
return 0, err
}

// Calculate free space available.
return stat.Bavail * uint64(stat.Bsize), nil
}
26 changes: 26 additions & 0 deletions disk_space_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//go:build windows
// +build windows

package main

import (
"syscall"

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

func getFreeSpace(path string) (uint64, error) {
var freeBytesAvailable, totalNumberOfBytes, totalNumberOfFreeBytes uint64

lpDirectoryName, err := syscall.UTF16PtrFromString(path)
if err != nil {
return 0, err
}

err = windows.GetDiskFreeSpaceEx(lpDirectoryName, &freeBytesAvailable, &totalNumberOfBytes, &totalNumberOfFreeBytes)
if err != nil {
return 0, err
}

return freeBytesAvailable, nil
}
11 changes: 4 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path/filepath"
"strings"
"syscall"
)

// FileOperationType defines the type of operation to perform on the audio files.
Expand All @@ -26,7 +25,7 @@ func main() {
targetDBPath string = "birdnet.db" // BirdNET-Go database.
sourceFilesDir string // BirdNET-Pi audio files directory.
targetFilesDir string = "clips" // BirdNET-Go audio files directory.
operationFlag string // copy or move audio clips
operationFlag string = "copy" // copy or move audio clips
skipAudioTransfer bool = false // skip copying audio files
)

Expand Down Expand Up @@ -124,12 +123,10 @@ func checkDiskSpace(sourceDir, targetDir string) (bool, error) {
return false, err
}

var stat syscall.Statfs_t
if err := syscall.Statfs(targetDir, &stat); err != nil {
freeSpace, err := getFreeSpace(targetDir)
if err != nil {
return false, err
}

// Calculate free space available in the target directory.
freeSpace := stat.Bavail * uint64(stat.Bsize)
return uint64(sourceSize) <= freeSpace, nil // Return whether there's enough space.
return uint64(sourceSize) <= freeSpace, nil
}

0 comments on commit 5325119

Please sign in to comment.