-
Notifications
You must be signed in to change notification settings - Fork 0
/
open_map.go
111 lines (97 loc) · 3.01 KB
/
open_map.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package mmap
import (
"os"
)
// Open opens a file as a memory map using the given flags. It does not support the O_WRONLY or
// O_APPEND flags. It will create the file if it doesn't exist. If the file is empty or
// O_TRUNC is specified, the file will be resized to the size of a memory page as
// returned by os.Getpagesize() and the bytes will be zeroed out.
func Open(name string, flags int, mode os.FileMode) (*Map, error) {
switch {
case isSet(flags, os.O_WRONLY):
return nil, errors.New("Map does not support O_WRONLY flag").
Set("name", name).Set("flags", flags)
case isSet(flags, os.O_APPEND):
return nil, errors.New("Map does not support O_APPEND flag").
Set("name", name).Set("flags", flags)
}
var (
write = isSet(flags, ReadWrite)
creat = isSet(flags, Create)
excl = isSet(flags, Exclusive)
wsync = isSet(flags, Sync)
trunc = isSet(flags, Truncate)
)
switch {
case creat && !write:
return nil, errors.New("O_CREATE requires O_RDWR flag").
Set("name", name).Set("flags", flags)
case excl && !creat:
return nil, errors.New("O_EXCL requires O_CREATE flag").
Set("name", name).Set("flags", flags)
case wsync && !write:
return nil, errors.New("O_SYNC requires O_RDWR flag").
Set("name", name).Set("flags", flags)
case trunc && !write:
return nil, errors.New("O_TRUNC requires O_RDWR flag").
Set("name", name).Set("flags", flags)
}
file, err := os.OpenFile(name, flags, mode)
if err != nil {
err = errors.Wrap(err, "could not open file").Set("name", name)
return nil, err
}
defer file.Close()
info, err := file.Stat()
if err != nil {
err = errors.Wrap(err, "could not stat file").Set("name", name)
return nil, err
}
if info.Size() < 1 && !write {
return nil, errors.New("cannot mmap empty file").Set("name", name)
}
if write && (info.Size() < 1 || trunc) {
size := int64(os.Getpagesize())
err = file.Truncate(size)
if err != nil {
if trunc {
return nil, errors.New("could not truncate file").Set("name", name)
}
return nil, errors.New("could not resize new or empty file").Set("name", name)
}
info, err = file.Stat()
if err != nil {
return nil, errors.Wrap(err, "could not stat after resize").Set("name", name)
}
if info.Size() != size {
return nil, errors.New("incorrect size of resized file").
Set("name", name).Set("requested_size", size).Set("actual_size", info.Size())
}
}
size := int(info.Size())
if info.Size() != int64(size) {
return nil, errors.New("file too large for architecture").Set("name", name).Set("size", info.Size())
}
data, err := mmap(file.Fd(), size, write)
if err != nil {
return nil, errors.New("could not mmap file").Set("name", name)
}
if trunc {
for i := range data {
data[i] = 0
}
err := msync(data, true)
if err != nil {
return nil, errors.Wrap(err, "error syncing mmap after truncate")
}
}
return &Map{
name: name,
data: data,
write: write,
wsync: wsync,
direct: make(map[uintptr]Direct),
readers: make(map[int]*Reader),
writers: make(map[int]*Writer),
}, nil
}