2023-11-22 21:28:49 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Layer struct {
|
|
|
|
MediaType string `json:"mediaType"`
|
|
|
|
Digest string `json:"digest"`
|
|
|
|
Size int64 `json:"size"`
|
|
|
|
From string `json:"from,omitempty"`
|
2024-04-12 20:55:12 +00:00
|
|
|
status string
|
2023-11-22 21:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewLayer(r io.Reader, mediatype string) (*Layer, error) {
|
|
|
|
blobs, err := GetBlobsPath("")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-12 20:55:12 +00:00
|
|
|
temp, err := os.CreateTemp(blobs, "sha256-")
|
2023-11-22 21:28:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer temp.Close()
|
2024-04-12 20:55:12 +00:00
|
|
|
defer os.Remove(temp.Name())
|
2023-11-22 21:28:49 +00:00
|
|
|
|
|
|
|
sha256sum := sha256.New()
|
|
|
|
n, err := io.Copy(io.MultiWriter(temp, sha256sum), r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-12 20:55:12 +00:00
|
|
|
if err := temp.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
digest := fmt.Sprintf("sha256:%x", sha256sum.Sum(nil))
|
|
|
|
blob, err := GetBlobsPath(digest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
status := "using existing layer"
|
|
|
|
if _, err := os.Stat(blob); err != nil {
|
|
|
|
status = "creating new layer"
|
|
|
|
if err := os.Rename(temp.Name(), blob); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-22 21:28:49 +00:00
|
|
|
return &Layer{
|
2024-04-12 20:55:12 +00:00
|
|
|
MediaType: mediatype,
|
|
|
|
Digest: digest,
|
|
|
|
Size: n,
|
|
|
|
status: fmt.Sprintf("%s %s", status, digest),
|
2023-11-22 21:28:49 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLayerFromLayer(digest, mediatype, from string) (*Layer, error) {
|
|
|
|
blob, err := GetBlobsPath(digest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Stat(blob)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Layer{
|
|
|
|
MediaType: mediatype,
|
|
|
|
Digest: digest,
|
|
|
|
Size: fi.Size(),
|
|
|
|
From: from,
|
2024-04-12 20:55:12 +00:00
|
|
|
status: fmt.Sprintf("using existing layer %s", digest),
|
2023-11-22 21:28:49 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-05-10 22:48:41 +00:00
|
|
|
func (l *Layer) Open() (io.ReadSeekCloser, error) {
|
2023-11-22 21:28:49 +00:00
|
|
|
blob, err := GetBlobsPath(l.Digest)
|
|
|
|
if err != nil {
|
2024-04-12 20:55:12 +00:00
|
|
|
return nil, err
|
2023-11-22 21:28:49 +00:00
|
|
|
}
|
|
|
|
|
2024-04-12 20:55:12 +00:00
|
|
|
return os.Open(blob)
|
2023-11-22 21:28:49 +00:00
|
|
|
}
|
2024-04-18 00:23:19 +00:00
|
|
|
|
|
|
|
func (l *Layer) Remove() error {
|
|
|
|
ms, err := Manifests()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, m := range ms {
|
|
|
|
for _, layer := range append(m.Layers, m.Config) {
|
|
|
|
if layer.Digest == l.Digest {
|
|
|
|
// something is using this layer
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blob, err := GetBlobsPath(l.Digest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return os.Remove(blob)
|
|
|
|
}
|