/
layer.go
89 lines (71 loc) · 2.15 KB
/
layer.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
// Copyright (c) 2023, Sylabs Inc. All rights reserved.
// This software is licensed under a 3-clause BSD license. Please consult the
// LICENSE.md file distributed with the sources of this project regarding your
// rights to use or distribute this software.
package oras
import (
"io"
"os"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/types"
)
const (
// SifLayerMediaTypeV1 is the mediaType for the "layer" which contains the actual SIF file
SifLayerMediaTypeV1 = "application/vnd.sylabs.sif.layer.v1.sif"
// SifLayerMediaTypeProto is the mediaType from prototyping and Singularity
// <3.7 which unfortunately includes a typo and doesn't have a version suffix
// See: https://github.com/hpcng/singularity/issues/4437
SifLayerMediaTypeProto = "appliciation/vnd.sylabs.sif.layer.tar"
)
// SifLayer implements a go-containerregistry v1.Layer backed by a SIF file, for
// ORAS / OCI artifact usage.
type SifLayer struct {
size int64
rc io.ReadCloser
hash v1.Hash
mediaType types.MediaType
}
var _ = v1.Layer(&SifLayer{})
func (sl *SifLayer) Digest() (v1.Hash, error) {
return sl.hash, nil
}
func (sl *SifLayer) DiffID() (v1.Hash, error) {
return sl.hash, nil
}
func (sl *SifLayer) Compressed() (io.ReadCloser, error) {
return sl.rc, nil
}
func (sl *SifLayer) Uncompressed() (io.ReadCloser, error) {
return sl.rc, nil
}
func (sl *SifLayer) Size() (int64, error) {
return sl.size, nil
}
func (sl *SifLayer) MediaType() (types.MediaType, error) {
return sl.mediaType, nil
}
// NewLayerFromSIF creates a new layer, backed by file, with mt as the
// MediaType. The MediaType should always be set SifLayerMediaTypeV1 in
// production. It is configurable so that we can implement tests which use the
// old prototype media value.
func NewLayerFromSIF(file string, mt types.MediaType) (*SifLayer, error) {
sl := SifLayer{
mediaType: mt,
}
fi, err := os.Stat(file)
if err != nil {
return nil, err
}
sl.size = fi.Size()
hash, err := ImageHash(file)
if err != nil {
return nil, err
}
sl.hash = hash
rc, err := os.Open(file)
if err != nil {
return nil, err
}
sl.rc = rc
return &sl, nil
}