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

cinode / go / 15633973488

13 Jun 2025 11:57AM UTC coverage: 95.318%. Remained the same
15633973488

Pull #45

github

byo
fixup
Pull Request #45: Fix github action issues

3013 of 3161 relevant lines covered (95.32%)

1.07 hits per line

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

75.0
/pkg/datastore/datastore_dynamic_link.go
1
/*
2
Copyright © 2023 Bartłomiej Święcki (byo)
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package datastore
18

19
import (
20
        "context"
21
        "errors"
22
        "io"
23

24
        "github.com/cinode/go/pkg/common"
25
        "github.com/cinode/go/pkg/internal/blobtypes/dynamiclink"
26
)
27

28
func (ds *datastore) openDynamicLink(ctx context.Context, name *common.BlobName) (io.ReadCloser, error) {
1✔
29
        rc, err := ds.s.openReadStream(ctx, name)
1✔
30
        if err != nil {
2✔
31
                return nil, err
1✔
32
        }
1✔
33

34
        dl, err := dynamiclink.FromPublicData(name, rc)
1✔
35
        if err != nil {
1✔
36
                rc.Close()
×
37
                return nil, err
×
38
        }
×
39

40
        return struct {
1✔
41
                io.Reader
1✔
42
                io.Closer
1✔
43
        }{
1✔
44
                Reader: dl.GetPublicDataReader(),
1✔
45
                Closer: rc,
1✔
46
        }, nil
1✔
47
}
48

49
// loadCurrentDynamicLink returns existing dynamic link data if present (nil if not found). That link is not meant to be
50
// read from - only for comparison
51
func (ds *datastore) newLinkGreaterThanCurrent(
52
        ctx context.Context,
53
        name *common.BlobName,
54
        newLink *dynamiclink.PublicReader,
55
) (
56
        bool, error,
57
) {
1✔
58
        rc, err := ds.s.openReadStream(ctx, name)
1✔
59
        if errors.Is(err, ErrNotFound) {
2✔
60
                return true, nil
1✔
61
        }
1✔
62
        if err != nil {
1✔
63
                return false, err
×
64
        }
×
65
        defer rc.Close()
1✔
66

1✔
67
        dl, err := dynamiclink.FromPublicData(name, rc)
1✔
68
        if err != nil {
1✔
69
                return false, err
×
70
        }
×
71

72
        return newLink.GreaterThan(dl), nil
1✔
73
}
74

75
func (ds *datastore) updateDynamicLink(ctx context.Context, name *common.BlobName, updateStream io.Reader) error {
1✔
76
        ws, err := ds.s.openWriteStream(ctx, name)
1✔
77
        if err != nil {
1✔
78
                return err
×
79
        }
×
80
        defer ws.Cancel()
1✔
81

1✔
82
        // Start parsing the update link - it will raise an error if link can not be validated
1✔
83
        updatedLink, err := dynamiclink.FromPublicData(name, updateStream)
1✔
84
        if err != nil {
2✔
85
                return err
1✔
86
        }
1✔
87

88
        greater, err := ds.newLinkGreaterThanCurrent(ctx, name, updatedLink)
1✔
89
        if err != nil {
1✔
90
                return err
×
91
        }
×
92

93
        // Update the link if the new one turns out to be a better choice
94
        if greater {
2✔
95
                // Copy the data to local storage, this will also additionally
1✔
96
                // validate the data from the link
1✔
97
                _, err := io.Copy(ws, updatedLink.GetPublicDataReader())
1✔
98
                if err != nil {
1✔
99
                        return err
×
100
                }
×
101

102
                // Only now confirm the update - a successful close replaces
103
                // the current link with the updated one
104
                err = ws.Close()
1✔
105
                if err != nil {
1✔
106
                        return err
×
107
                }
×
108
        }
109

110
        return nil
1✔
111
}
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