-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmmap_unix.go
More file actions
39 lines (34 loc) · 1.11 KB
/
Copy pathmmap_unix.go
File metadata and controls
39 lines (34 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//go:build unix
package potion
import (
"fmt"
"os"
"syscall"
)
// mmapFile maps the file at path into memory read-only and returns the
// mapped bytes with a function that releases the mapping. Because the
// mapping is backed by the OS page cache, every process that maps the same
// file shares one physical copy of it machine-wide, and no read of the file
// happens until the pages are first touched. Model files in the cache are
// only ever replaced by atomic rename (see downloadFile), so an existing
// mapping keeps its inode and can never observe a truncated file.
func mmapFile(path string) ([]byte, func() error, error) {
f, err := os.Open(path)
if err != nil {
return nil, nil, err
}
defer f.Close()
info, err := f.Stat()
if err != nil {
return nil, nil, err
}
size := info.Size()
if size <= 0 || int64(int(size)) != size {
return nil, nil, fmt.Errorf("cannot mmap %s: invalid size %d", path, size)
}
data, err := syscall.Mmap(int(f.Fd()), 0, int(size), syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
return nil, nil, err
}
return data, func() error { return syscall.Munmap(data) }, nil
}