• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cinode / go / 3760981038

22 Dec 2022 08:35PM UTC coverage: 79.498%. First build
3760981038

push

github

Bartek
Basic test for the static_datastore binary

919 of 1156 relevant lines covered (79.5%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.4
/datastore/storage_filesystem.go
1
package datastore
2

3
import (
4
        "context"
5
        "io"
6
        "os"
7
        "path/filepath"
8

9
        "github.com/cinode/go/common"
10
)
11

12
const (
13
        fsSuffixCurrent = ".c"
14
        fsSuffixUpload  = ".u"
15
)
16

17
type fileSystem struct {
18
        path string
19
}
20

21
func newStorageFilesystem(path string) *fileSystem {
1✔
22
        return &fileSystem{
1✔
23
                path: path,
1✔
24
        }
1✔
25
}
1✔
26

27
func (fs *fileSystem) kind() string {
1✔
28
        return "FileSystem"
1✔
29
}
1✔
30
func (fs *fileSystem) openReadStream(ctx context.Context, name common.BlobName) (io.ReadCloser, error) {
1✔
31
        rc, err := os.Open(fs.getFileName(name, fsSuffixCurrent))
1✔
32
        if os.IsNotExist(err) {
2✔
33
                return nil, ErrNotFound
1✔
34
        }
1✔
35
        return rc, err
1✔
36
}
37

38
func (fs *fileSystem) createTemporaryWriteStream(name common.BlobName) (*os.File, error) {
1✔
39
        tempName := fs.getFileName(name, fsSuffixUpload)
1✔
40

1✔
41
        // Ensure dir exists
1✔
42
        err := os.MkdirAll(filepath.Dir(tempName), 0755)
1✔
43
        if err != nil {
2✔
44
                return nil, err
1✔
45
        }
1✔
46

47
        // Open file in exclusive mode, allow only a single upload at a time
48
        fh, err := os.OpenFile(
1✔
49
                tempName,
1✔
50
                os.O_CREATE|os.O_EXCL|os.O_APPEND|os.O_WRONLY,
1✔
51
                0644,
1✔
52
        )
1✔
53
        if os.IsExist(err) {
2✔
54
                return nil, ErrUploadInProgress
1✔
55
        }
1✔
56

57
        if err != nil {
2✔
58
                // Some OS error
1✔
59
                return nil, err
1✔
60
        }
1✔
61

62
        // Got temporary file handle
63
        return fh, nil
1✔
64
}
65

66
type fileSystemWriteCloser struct {
67
        fs       *os.File
68
        destName string
69
}
70

71
func (w *fileSystemWriteCloser) Write(b []byte) (int, error) {
1✔
72
        return w.fs.Write(b)
1✔
73
}
1✔
74

75
func (w *fileSystemWriteCloser) Cancel() {
1✔
76
        w.fs.Close()
1✔
77
        os.Remove(w.fs.Name())
1✔
78
}
1✔
79

80
func (w *fileSystemWriteCloser) Close() error {
1✔
81
        err := w.fs.Close()
1✔
82
        if err != nil {
1✔
83
                // This is not covered by tests, I have no idea how to trigger that
×
84
                os.Remove(w.fs.Name())
×
85
                return err
×
86
        }
×
87

88
        return os.Rename(w.fs.Name(), w.destName)
1✔
89
}
90

91
func (fs *fileSystem) openWriteStream(ctx context.Context, name common.BlobName) (WriteCloseCanceller, error) {
1✔
92

1✔
93
        fl, err := fs.createTemporaryWriteStream(name)
1✔
94
        if err != nil {
2✔
95
                return nil, err
1✔
96
        }
1✔
97

98
        return &fileSystemWriteCloser{
1✔
99
                fs:       fl,
1✔
100
                destName: fs.getFileName(name, fsSuffixCurrent),
1✔
101
        }, nil
1✔
102
}
103

104
func (fs *fileSystem) exists(ctx context.Context, name common.BlobName) (bool, error) {
1✔
105
        _, err := os.Stat(fs.getFileName(name, fsSuffixCurrent))
1✔
106
        if os.IsNotExist(err) {
2✔
107
                return false, nil
1✔
108
        }
1✔
109
        if err != nil {
2✔
110
                return false, err
1✔
111
        }
1✔
112
        return true, nil
1✔
113
}
114

115
func (fs *fileSystem) delete(ctx context.Context, name common.BlobName) error {
1✔
116
        err := os.Remove(fs.getFileName(name, fsSuffixCurrent))
1✔
117
        if os.IsNotExist(err) {
2✔
118
                return ErrNotFound
1✔
119
        }
1✔
120
        return err
1✔
121
}
122

123
func (fs *fileSystem) getFileName(name common.BlobName, suffix string) string {
1✔
124
        fNameParts := []string{fs.path}
1✔
125

1✔
126
        nameStr := name.String()
1✔
127
        for i := 0; i < 3; i++ {
2✔
128
                if len(nameStr) > 3 {
2✔
129
                        fNameParts = append(fNameParts, nameStr[:3])
1✔
130
                        nameStr = nameStr[3:]
1✔
131
                }
1✔
132
        }
133
        fNameParts = append(fNameParts, nameStr+suffix)
1✔
134

1✔
135
        return filepath.Join(fNameParts...)
1✔
136
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc