• 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

77.46
/pkg/cinodefs/context.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 cinodefs
18

19
import (
20
        "bytes"
21
        "context"
22
        "errors"
23
        "fmt"
24
        "io"
25

26
        "github.com/cinode/go/pkg/blenc"
27
        "github.com/cinode/go/pkg/cinodefs/protobuf"
28
        "github.com/cinode/go/pkg/common"
29
        "google.golang.org/protobuf/proto"
30
)
31

32
var (
33
        ErrMissingKeyInfo    = errors.New("missing key info")
34
        ErrMissingWriterInfo = errors.New("missing writer info")
35
)
36

37
type graphContext struct {
38
        // blenc layer used in the graph
39
        be blenc.BE
40

41
        // known writer info data
42
        authInfos map[string]*common.AuthInfo
43
}
44

45
// Get symmetric encryption key for given entrypoint.
46
//
47
// Note: Currently the key will be stored inside entrypoint data,
48
// but more advanced methods of obtaining the key may be added
49
// through this function in the future.
50
func (c *graphContext) keyFromEntrypoint(
51
        ctx context.Context,
52
        ep *Entrypoint,
53
) (*common.BlobKey, error) {
1✔
54
        if ep.ep.KeyInfo == nil ||
1✔
55
                ep.ep.KeyInfo.Key == nil {
2✔
56
                return nil, ErrMissingKeyInfo
1✔
57
        }
1✔
58
        return common.BlobKeyFromBytes(ep.ep.GetKeyInfo().GetKey()), nil
1✔
59
}
60

61
// open io.ReadCloser for data behind given entrypoint
62
func (c *graphContext) getDataReader(
63
        ctx context.Context,
64
        ep *Entrypoint,
65
) (
66
        io.ReadCloser,
67
        error,
68
) {
1✔
69
        key, err := c.keyFromEntrypoint(ctx, ep)
1✔
70
        if err != nil {
2✔
71
                return nil, err
1✔
72
        }
1✔
73
        rc, err := c.be.Open(ctx, ep.BlobName(), key)
1✔
74
        if err != nil {
1✔
75
                return nil, fmt.Errorf("failed to open blob: %w", err)
×
76
        }
×
77
        return rc, nil
1✔
78
}
79

80
// return data behind entrypoint
81
func (c *graphContext) readProtobufMessage(
82
        ctx context.Context,
83
        ep *Entrypoint,
84
        msg proto.Message,
85
) error {
1✔
86
        rc, err := c.getDataReader(ctx, ep)
1✔
87
        if err != nil {
1✔
88
                return err
×
89
        }
×
90
        defer rc.Close()
1✔
91

1✔
92
        data, err := io.ReadAll(rc)
1✔
93
        if err != nil {
1✔
94
                return fmt.Errorf("failed to read blob: %w", err)
×
95
        }
×
96

97
        err = proto.Unmarshal(data, msg)
1✔
98
        if err != nil {
2✔
99
                return fmt.Errorf("malformed data: %w", err)
1✔
100
        }
1✔
101

102
        return nil
1✔
103
}
104

105
func (c *graphContext) createProtobufMessage(
106
        ctx context.Context,
107
        blobType common.BlobType,
108
        msg proto.Message,
109
) (
110
        *Entrypoint,
111
        error,
112
) {
1✔
113
        data, err := proto.Marshal(msg)
1✔
114
        if err != nil {
1✔
115
                return nil, fmt.Errorf("serialization failed: %w", err)
×
116
        }
×
117

118
        bn, key, ai, err := c.be.Create(ctx, blobType, bytes.NewReader(data))
1✔
119
        if err != nil {
2✔
120
                return nil, fmt.Errorf("write failed: %w", err)
1✔
121
        }
1✔
122

123
        if ai != nil {
1✔
124
                c.authInfos[bn.String()] = ai
×
125
        }
×
126

127
        return &Entrypoint{
1✔
128
                bn: bn,
1✔
129
                ep: protobuf.Entrypoint{
1✔
130
                        BlobName: bn.Bytes(),
1✔
131
                        KeyInfo: &protobuf.KeyInfo{
1✔
132
                                Key: key.Bytes(),
1✔
133
                        },
1✔
134
                },
1✔
135
        }, nil
1✔
136
}
137

138
func (c *graphContext) updateProtobufMessage(
139
        ctx context.Context,
140
        ep *Entrypoint,
141
        msg proto.Message,
142
) error {
1✔
143
        wi, found := c.authInfos[ep.BlobName().String()]
1✔
144
        if !found {
1✔
145
                return ErrMissingWriterInfo
×
146
        }
×
147

148
        key, err := c.keyFromEntrypoint(ctx, ep)
1✔
149
        if err != nil {
1✔
150
                return err
×
151
        }
×
152

153
        data, err := proto.Marshal(msg)
1✔
154
        if err != nil {
1✔
155
                return fmt.Errorf("serialization failed: %w", err)
×
156
        }
×
157

158
        err = c.be.Update(ctx, ep.BlobName(), wi, key, bytes.NewReader(data))
1✔
159
        if err != nil {
2✔
160
                return fmt.Errorf("write failed: %w", err)
1✔
161
        }
1✔
162

163
        return nil
1✔
164
}
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