MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / fileStats

Function fileStats

pkg/attestation/crafter/materials/materials.go:196–222  ·  view source on GitHub ↗

Returns the sha256 hash of the file, its size and an error

(filepath string)

Source from the content-addressed store, hash-verified

194
195// Returns the sha256 hash of the file, its size and an error
196func fileStats(filepath string) (*fileInfo, error) {
197 stat, err := os.Stat(filepath)
198 if err != nil {
199 return nil, err
200 }
201
202 f, err := os.Open(filepath)
203 if err != nil {
204 return nil, fmt.Errorf("can't open file to upload: %w", err)
205 }
206
207 hash, _, err := cr_v1.SHA256(f)
208 if err != nil {
209 f.Close()
210 return nil, fmt.Errorf("generating digest: %w", err)
211 }
212
213 // Since we have already iterated on the file to calculate the digest
214 // we need to rewind the file pointer
215 _, err = f.Seek(0, io.SeekStart)
216 if err != nil {
217 f.Close()
218 return nil, fmt.Errorf("rewinding file pointer: %w", err)
219 }
220
221 return &fileInfo{filename: stat.Name(), digest: hash.String(), size: stat.Size(), r: f}, nil
222}
223
224type Craftable interface {
225 Craft(ctx context.Context, value string) (*api.Attestation_Material, error)

Callers 1

uploadAndCraftFunction · 0.85

Calls 3

StringMethod · 0.65
CloseMethod · 0.45
NameMethod · 0.45

Tested by

no test coverage detected