/
storage_filesystem.go
169 lines (137 loc) · 3.57 KB
/
storage_filesystem.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
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
Copyright © 2023 Bartłomiej Święcki (byo)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package datastore
import (
"context"
"io"
"os"
"path/filepath"
"github.com/cinode/go/pkg/common"
)
const (
fsSuffixCurrent = ".c"
fsSuffixUpload = ".u"
)
type fileSystem struct {
path string
}
var _ storage = (*fileSystem)(nil)
func newStorageFilesystem(path string) (*fileSystem, error) {
err := os.MkdirAll(path, 0755)
if err != nil {
return nil, err
}
return &fileSystem{path: path}, nil
}
func (fs *fileSystem) kind() string {
return "FileSystem"
}
func (fs *fileSystem) address() string {
return filePrefix + fs.path
}
func (fs *fileSystem) openReadStream(ctx context.Context, name *common.BlobName) (io.ReadCloser, error) {
rc, err := os.Open(fs.getFileName(name, fsSuffixCurrent))
if os.IsNotExist(err) {
return nil, ErrNotFound
}
return rc, err
}
func (fs *fileSystem) createTemporaryWriteStream(name *common.BlobName) (*os.File, error) {
tempName := fs.getFileName(name, fsSuffixUpload)
// Ensure dir exists
err := os.MkdirAll(filepath.Dir(tempName), 0755)
if err != nil {
return nil, err
}
// Open file in exclusive mode, allow only a single upload at a time
fh, err := os.OpenFile(
tempName,
os.O_CREATE|os.O_EXCL|os.O_APPEND|os.O_WRONLY,
0644,
)
if os.IsExist(err) {
return nil, ErrUploadInProgress
}
if err != nil {
// Some OS error
return nil, err
}
// Got temporary file handle
return fh, nil
}
type fileSystemWriteCloser struct {
fs *os.File
destName string
}
func (w *fileSystemWriteCloser) Write(b []byte) (int, error) {
return w.fs.Write(b)
}
func (w *fileSystemWriteCloser) Cancel() {
if w.fs != nil {
w.fs.Close()
os.Remove(w.fs.Name())
}
}
func (w *fileSystemWriteCloser) Close() error {
err := w.fs.Close()
if err != nil {
// This is not covered by tests, I have no idea how to trigger that
os.Remove(w.fs.Name())
return err
}
err = os.Rename(w.fs.Name(), w.destName)
if err != nil {
return err
}
w.fs = nil
return nil
}
func (fs *fileSystem) openWriteStream(ctx context.Context, name *common.BlobName) (WriteCloseCanceller, error) {
fl, err := fs.createTemporaryWriteStream(name)
if err != nil {
return nil, err
}
return &fileSystemWriteCloser{
fs: fl,
destName: fs.getFileName(name, fsSuffixCurrent),
}, nil
}
func (fs *fileSystem) exists(ctx context.Context, name *common.BlobName) (bool, error) {
_, err := os.Stat(fs.getFileName(name, fsSuffixCurrent))
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, err
}
return true, nil
}
func (fs *fileSystem) delete(ctx context.Context, name *common.BlobName) error {
err := os.Remove(fs.getFileName(name, fsSuffixCurrent))
if os.IsNotExist(err) {
return ErrNotFound
}
return err
}
func (fs *fileSystem) getFileName(name *common.BlobName, suffix string) string {
fNameParts := []string{fs.path}
nameStr := name.String()
for i := 0; i < 3; i++ {
if len(nameStr) > 3 {
fNameParts = append(fNameParts, nameStr[:3])
nameStr = nameStr[3:]
}
}
fNameParts = append(fNameParts, nameStr+suffix)
return filepath.Join(fNameParts...)
}