mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-09 23:24:05 -08:00
49 lines
704 B
Go
49 lines
704 B
Go
package fileutil
|
|
|
|
import (
|
|
"os"
|
|
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
type MmapFile struct {
|
|
f *os.File
|
|
b []byte
|
|
}
|
|
|
|
func OpenMmapFile(path string) (*MmapFile, error) {
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "try lock file")
|
|
}
|
|
info, err := f.Stat()
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "stat")
|
|
}
|
|
|
|
b, err := mmap(f, int(info.Size()))
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "mmap")
|
|
}
|
|
|
|
return &MmapFile{f: f, b: b}, nil
|
|
}
|
|
|
|
func (f *MmapFile) Close() error {
|
|
err0 := munmap(f.b)
|
|
err1 := f.f.Close()
|
|
|
|
if err0 != nil {
|
|
return err0
|
|
}
|
|
return err1
|
|
}
|
|
|
|
func (f *MmapFile) File() *os.File {
|
|
return f.f
|
|
}
|
|
|
|
func (f *MmapFile) Bytes() []byte {
|
|
return f.b
|
|
}
|