-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs.go
More file actions
54 lines (41 loc) · 1019 Bytes
/
fs.go
File metadata and controls
54 lines (41 loc) · 1019 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
51
52
53
54
package anvil
import (
"io"
"os"
"github.com/spf13/afero"
"github.com/yehan2002/errors"
)
// reader an interface that implements io.ReadAt and io.Closer
type reader interface {
io.ReaderAt
io.Closer
}
type noopReadAtCloser struct{ io.ReaderAt }
func (r *noopReadAtCloser) Close() error { return nil }
// writer a writer to modify an anvil file.
type writer interface {
io.WriterAt
Sync() error
Truncate(size int64) error
}
var _ writer = afero.File(nil)
func openFile(path string, settings Settings) (r reader, size int64, err error) {
var fileFlags int
if settings.ReadOnly {
fileFlags = os.O_RDONLY
} else {
fileFlags = os.O_RDWR | os.O_CREATE
}
if settings.Sync {
fileFlags |= os.O_SYNC
}
var f afero.File
if f, err = settings.fs.OpenFile(path, fileFlags, 0666); err != nil {
return nil, 0, errors.Wrap("anvil: unable to open file", err)
}
info, err := f.Stat()
if err != nil {
return nil, 0, errors.Wrap("anvil: unable to stat file", err)
}
return f, info.Size(), nil
}