-
Notifications
You must be signed in to change notification settings - Fork 124
/
sif.go
93 lines (78 loc) · 2.49 KB
/
sif.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
90
91
92
93
// Copyright (c) 2021 Apptainer a Series of LF Projects LLC
// For website terms of use, trademark policy, privacy policy and other
// project policies see https://lfprojects.org/policies
// Copyright (c) 2018-2021, 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 plugin
import (
"encoding/json"
"fmt"
"io"
"github.com/apptainer/apptainer/pkg/image"
pluginapi "github.com/apptainer/apptainer/pkg/plugin"
"github.com/apptainer/sif/v2/pkg/sif"
)
const (
// pluginBinaryName is the name of the plugin binary within the
// SIF file
pluginBinaryName = "plugin.so"
// pluginManifestName is the name of the plugin manifest within
// the SIF file
pluginManifestName = "plugin.manifest"
)
// isPluginFile checks if the image.Image contains the sections which
// make up a valid plugin. A plugin sif file should have the following
// format:
//
// DESCR[0]: Sifplugin
// - DataType: sif.DataPartition
// - FSType: sif.FsRaw
// - PartType: sif.PartData
// DESCR[1]: Sifmanifest
// - Datatype: sif.DataGenericJSON
func isPluginFile(img *image.Image) bool {
if img.Type != image.SIF {
return false
}
part, _ := img.GetAllPartitions()
if len(part) != 1 || len(img.Sections) != 1 {
return false
}
// check binary object
if part[0].Name != pluginBinaryName {
return false
} else if part[0].AllowedUsage&image.DataUsage == 0 {
return false
} else if part[0].Type != image.RAW {
return false
}
// check manifest
if img.Sections[0].Name != pluginManifestName {
return false
} else if img.Sections[0].AllowedUsage&image.DataUsage == 0 {
return false
} else if img.Sections[0].Type != uint32(sif.DataGenericJSON) {
return false
}
return true
}
// getManifest will extract the Manifest data from the input FileImage.
func getManifest(img *image.Image) (pluginapi.Manifest, error) {
var manifest pluginapi.Manifest
r, err := getManifestReader(img)
if err != nil {
return manifest, err
}
if err := json.NewDecoder(r).Decode(&manifest); err != nil {
return manifest, fmt.Errorf("while decoding JSON manifest: %s", err)
}
return manifest, nil
}
func getBinaryReader(img *image.Image) (io.Reader, error) {
return image.NewPartitionReader(img, pluginBinaryName, -1)
}
func getManifestReader(img *image.Image) (io.Reader, error) {
return image.NewSectionReader(img, pluginManifestName, -1)
}