2021-07-26 11:09:12 +00:00
|
|
|
//go:build linux || darwin || freebsd || openbsd
|
|
|
|
// +build linux darwin freebsd openbsd
|
2021-02-26 22:37:07 +00:00
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2021-02-26 23:01:47 +00:00
|
|
|
func mmap(fd int, length int) (data []byte, err error) {
|
2021-02-26 22:37:07 +00:00
|
|
|
return unix.Mmap(fd, 0, length, unix.PROT_READ, unix.MAP_SHARED)
|
|
|
|
|
|
|
|
}
|
|
|
|
func mUnmap(data []byte) error {
|
|
|
|
return unix.Munmap(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustSyncPath(path string) {
|
|
|
|
d, err := os.Open(path)
|
|
|
|
if err != nil {
|
2022-12-04 06:00:20 +00:00
|
|
|
logger.Panicf("FATAL: cannot open file for fsync: %s", err)
|
2021-02-26 22:37:07 +00:00
|
|
|
}
|
|
|
|
if err := d.Sync(); err != nil {
|
|
|
|
_ = d.Close()
|
|
|
|
logger.Panicf("FATAL: cannot flush %q to storage: %s", path, err)
|
|
|
|
}
|
|
|
|
if err := d.Close(); err != nil {
|
|
|
|
logger.Panicf("FATAL: cannot close %q: %s", path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-26 23:01:47 +00:00
|
|
|
func createFlockFile(flockFile string) (*os.File, error) {
|
2021-02-26 22:37:07 +00:00
|
|
|
flockF, err := os.Create(flockFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create lock file %q: %w", flockFile, err)
|
|
|
|
}
|
|
|
|
if err := unix.Flock(int(flockF.Fd()), unix.LOCK_EX|unix.LOCK_NB); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot acquire lock on file %q: %w", flockFile, err)
|
|
|
|
}
|
|
|
|
return flockF, nil
|
|
|
|
}
|
2021-02-26 23:01:47 +00:00
|
|
|
|
|
|
|
func mustGetFreeSpace(path string) uint64 {
|
|
|
|
var stat unix.Statfs_t
|
2023-01-18 05:19:26 +00:00
|
|
|
if err := unix.Statfs(path, &stat); err != nil {
|
2021-02-26 23:01:47 +00:00
|
|
|
logger.Panicf("FATAL: cannot determine free disk space on %q: %s", path, err)
|
|
|
|
}
|
|
|
|
return freeSpace(stat)
|
|
|
|
}
|