mirror of
https://github.com/ente-io/ente.git
synced 2025-05-29 05:51:19 +00:00
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package file
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"syscall"
|
|
|
|
"github.com/ente-io/stacktrace"
|
|
)
|
|
|
|
func MakeDirectoryIfNotExists(path string) error {
|
|
if _, err := os.Stat(path); os.IsNotExist(err) {
|
|
return os.MkdirAll(path, os.ModeDir|0755)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func DeleteAllFilesInDirectory(path string) error {
|
|
_, err := os.Stat(path)
|
|
if err != nil {
|
|
// os.Stat throwing error would mean, file path does not exist
|
|
return nil
|
|
}
|
|
err = os.RemoveAll(path)
|
|
return stacktrace.Propagate(err, "")
|
|
}
|
|
|
|
// FreeSpace returns the free space in bytes on the disk where path is mounted.
|
|
func FreeSpace(path string) (uint64, error) {
|
|
var fs syscall.Statfs_t
|
|
err := syscall.Statfs(path, &fs)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return fs.Bfree * uint64(fs.Bsize), nil
|
|
}
|
|
|
|
// EnsureSufficientSpace Return an error if we risk running out of disk space if we try to download
|
|
// and write a file of size.
|
|
// This function keeps a buffer of 2 GB free space in its calculations.
|
|
func EnsureSufficientSpace(size int64) error {
|
|
free, err := FreeSpace("/")
|
|
if err != nil {
|
|
return stacktrace.Propagate(err, "Failed to fetch free space")
|
|
}
|
|
|
|
gb := uint64(1024) * 1024 * 1024
|
|
need := uint64(size) + (2 * gb)
|
|
if free < need {
|
|
return fmt.Errorf("insufficient space on disk (need %d bytes, free %d bytes)", size, free)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetLockNameForObject(objectKey string) string {
|
|
return fmt.Sprintf("Object:%s", objectKey)
|
|
}
|