2021-05-31 06:21:23 +00:00
|
|
|
package fs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-05-03 08:47:02 +00:00
|
|
|
"sync/atomic"
|
2021-05-31 06:21:23 +00:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2023-05-03 08:47:02 +00:00
|
|
|
func mustRemoveDirAtomic(dir string) {
|
|
|
|
n := atomic.AddUint64(&atomicDirRemoveCounter, 1)
|
|
|
|
tmpDir := fmt.Sprintf("%s.must-remove.%d", dir, n)
|
|
|
|
if err := os.Rename(dir, tmpDir); err != nil {
|
|
|
|
logger.Panicf("FATAL: cannot move %s to %s: %s", dir, tmpDir, err)
|
|
|
|
}
|
|
|
|
MustRemoveAll(tmpDir)
|
|
|
|
}
|
|
|
|
|
2021-05-31 06:21:23 +00:00
|
|
|
func mmap(fd int, length int) (data []byte, err error) {
|
|
|
|
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-05-31 06:21:23 +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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func createFlockFile(flockFile string) (*os.File, error) {
|
|
|
|
flockF, err := os.Create(flockFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot create lock file %q: %w", flockFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
flock := unix.Flock_t{
|
|
|
|
Type: unix.F_WRLCK,
|
|
|
|
Start: 0,
|
|
|
|
Len: 0,
|
|
|
|
Whence: 0,
|
|
|
|
}
|
|
|
|
if err := unix.FcntlFlock(flockF.Fd(), unix.F_SETLK, &flock); err != nil {
|
|
|
|
return nil, fmt.Errorf("cannot acquire lock on file %q: %w", flockFile, err)
|
|
|
|
}
|
|
|
|
return flockF, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mustGetFreeSpace(path string) uint64 {
|
|
|
|
var stat unix.Statvfs_t
|
2023-01-18 05:19:26 +00:00
|
|
|
if err := unix.Statvfs(path, &stat); err != nil {
|
2021-05-31 06:21:23 +00:00
|
|
|
logger.Panicf("FATAL: cannot determine free disk space on %q: %s", path, err)
|
|
|
|
}
|
|
|
|
return freeSpace(stat)
|
|
|
|
}
|
|
|
|
|
|
|
|
func freeSpace(stat unix.Statvfs_t) uint64 {
|
|
|
|
return uint64(stat.Bavail) * uint64(stat.Bsize)
|
|
|
|
}
|