mirror of https://github.com/prometheus/prometheus
62 lines
1.3 KiB
Go
62 lines
1.3 KiB
Go
|
// Copyright 2018 The Prometheus Authors
|
||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
// you may not use this file except in compliance with the License.
|
||
|
// You may obtain a copy of the License at
|
||
|
//
|
||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||
|
//
|
||
|
// Unless required by applicable law or agreed to in writing, software
|
||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
// See the License for the specific language governing permissions and
|
||
|
// limitations under the License.
|
||
|
|
||
|
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
|
||
|
}
|