-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfd.go
More file actions
50 lines (39 loc) · 774 Bytes
/
fd.go
File metadata and controls
50 lines (39 loc) · 774 Bytes
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
package manager
import (
"errors"
"runtime"
"strconv"
"golang.org/x/sys/unix"
)
// errClosedFd - Use of closed file descriptor error
var errClosedFd = errors.New("use of closed file descriptor")
// fd - File descriptor
type fd struct {
raw int64
}
// newFD - returns a new file descriptor
func newFD(value uint32) *fd {
f := &fd{int64(value)}
runtime.SetFinalizer(f, func(f *fd) {
_ = f.Close()
})
return f
}
func (fd *fd) String() string {
return strconv.FormatInt(fd.raw, 10)
}
func (fd *fd) Value() (uint32, error) {
if fd.raw < 0 {
return 0, errClosedFd
}
return uint32(fd.raw), nil
}
func (fd *fd) Close() error {
if fd.raw < 0 {
return nil
}
value := int(fd.raw)
fd.raw = -1
runtime.SetFinalizer(fd, nil)
return unix.Close(value)
}