2021-07-26 13:09:12 +02:00
|
|
|
//go:build linux || darwin || freebsd || openbsd
|
2021-02-26 23:37:07 +01:00
|
|
|
|
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2021-02-27 00:01:47 +01:00
|
|
|
func mmap(fd int, length int) (data []byte, err error) {
|
2021-02-26 23:37:07 +01: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 07:00:20 +01:00
|
|
|
logger.Panicf("FATAL: cannot open file for fsync: %s", err)
|
2021-02-26 23:37:07 +01: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-27 00:01:47 +01:00
|
|
|
func createFlockFile(flockFile string) (*os.File, error) {
|
2021-02-26 23:37:07 +01: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-27 00:01:47 +01:00
|
|
|
|
|
|
|
func mustGetFreeSpace(path string) uint64 {
|
|
|
|
var stat unix.Statfs_t
|
2023-01-18 06:19:26 +01:00
|
|
|
if err := unix.Statfs(path, &stat); err != nil {
|
2021-02-27 00:01:47 +01:00
|
|
|
logger.Panicf("FATAL: cannot determine free disk space on %q: %s", path, err)
|
|
|
|
}
|
|
|
|
return freeSpace(stat)
|
|
|
|
}
|