-
Notifications
You must be signed in to change notification settings - Fork 2
/
fs_store.go
156 lines (127 loc) · 3.35 KB
/
fs_store.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
)
func fsStoreDefaultResolve(desc Description) string {
return filepath.Join(desc.Name, desc.Version, desc.Hash)
}
type fsStore struct {
root string
work string
resolve func(desc Description) string
}
type fsOption func(s *fsStore)
func WithWorkDir(p string) fsOption {
return func(s *fsStore) {
s.work = p
}
}
func WithPathResolve(resolve func(desc Description) string) fsOption {
return func(s *fsStore) {
s.resolve = resolve
}
}
func NewFsStore(root string, opts ...fsOption) (*fsStore, error) {
s := &fsStore{root: root}
for _, opt := range opts {
opt(s)
}
if s.work == "" {
s.work = filepath.Join(s.root, ".work")
}
if s.resolve == nil {
s.resolve = fsStoreDefaultResolve
}
if err := os.MkdirAll(s.root, 0744); err != nil {
return nil, fmt.Errorf("create store directory: %w", err)
}
if err := os.MkdirAll(s.work, 0744); err != nil {
return nil, fmt.Errorf("create work directory: %w", err)
}
if p, err := os.MkdirTemp(s.work, ""); err != nil {
return nil, fmt.Errorf("create work context at work directory: %w", err)
} else {
s.work = p
}
test_src := filepath.Join(s.work, ".test")
test_dst := filepath.Join(s.root, ".test")
err := func() error {
f, err := os.OpenFile(test_src, os.O_WRONLY|os.O_CREATE, 0700)
if err != nil {
return fmt.Errorf("create file at work directory: %w", err)
}
if _, err := f.Write([]byte("Royale with Cheese")); err != nil {
f.Close()
os.Remove(f.Name())
return fmt.Errorf("write to file at work directory: %w", err)
}
if err := f.Close(); err != nil {
return fmt.Errorf("close file at work directory: %w", err)
}
if err := os.Rename(test_src, test_dst); err != nil {
os.Remove(test_src)
return fmt.Errorf("rename file from work directory to store directory: %w", err)
}
if err := os.Remove(test_dst); err != nil {
return fmt.Errorf("remove file at store directory: %w", err)
}
return nil
}()
if err != nil {
return nil, fmt.Errorf("test fail: %w", err)
}
return s, nil
}
func (s *fsStore) Resolve(desc Description) string {
return filepath.Join(s.root, s.resolve(desc))
}
func (s *fsStore) Get(ctx context.Context, desc Description, w io.Writer) error {
tgt := s.Resolve(desc)
f, err := os.OpenFile(tgt, os.O_RDONLY, 0)
if err != nil {
return fmt.Errorf("open file: %w", err)
}
defer f.Close()
_, err = io.Copy(w, f)
return err
}
func (s *fsStore) Head(ctx context.Context, desc Description) (int, error) {
tgt := s.Resolve(desc)
info, err := os.Stat(tgt)
if err != nil {
return 0, err
}
return int(info.Size()), nil
}
func (s *fsStore) Put(ctx context.Context, desc Description, r io.Reader) error {
if err := os.MkdirAll(s.work, 0744); err != nil {
return fmt.Errorf("create work directory: %w", err)
}
tgt := s.Resolve(desc)
if _, err := os.Stat(tgt); err == nil {
return ErrExist
}
f, err := os.CreateTemp(s.work, "")
if err != nil {
return fmt.Errorf("create temp file: %w", err)
}
_, err = io.Copy(f, r)
f.Close()
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(tgt), 0744); err != nil {
return fmt.Errorf("create target directory: %w", err)
}
if err := os.Rename(f.Name(), tgt); err != nil {
return fmt.Errorf("move received file to storage: %w", err)
}
return nil
}
func (s *fsStore) Close() error {
return os.Remove(s.work)
}