-
Notifications
You must be signed in to change notification settings - Fork 19
/
file_disk.go
52 lines (42 loc) · 977 Bytes
/
file_disk.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
package fs
import (
"errors"
"os"
)
// A FileDisk is an implementation of a BlockDevice that uses a
// *os.File as its backing store.
type FileDisk struct {
f *os.File
size int64
}
// NewFileDisk creates a new FileDisk from the given *os.File. The
// file must already be created and set the to the proper size.
func NewFileDisk(f *os.File) (*FileDisk, error) {
fi, err := f.Stat()
if err != nil {
return nil, err
}
if fi.IsDir() {
return nil, errors.New("file is a directory")
}
return &FileDisk{
f: f,
size: fi.Size(),
}, nil
}
func (f *FileDisk) Close() error {
return f.f.Close()
}
func (f *FileDisk) Len() int64 {
return f.size
}
func (f *FileDisk) ReadAt(p []byte, off int64) (int, error) {
return f.f.ReadAt(p, off)
}
func (f *FileDisk) SectorSize() int {
// Hardcoded for now, one day we may want to make this customizable
return 512
}
func (f *FileDisk) WriteAt(p []byte, off int64) (int, error) {
return f.f.WriteAt(p, off)
}