File: sif.go

package info (click to toggle)
singularity-container 4.0.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 21,672 kB
  • sloc: asm: 3,857; sh: 2,125; ansic: 1,677; awk: 414; makefile: 110; python: 99
file content (90 lines) | stat: -rw-r--r-- 2,354 bytes parent folder | download | duplicates (2)
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
// Copyright (c) 2018-2022, 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/sylabs/sif/v2/pkg/sif"
	"github.com/sylabs/singularity/v4/pkg/image"
	pluginapi "github.com/sylabs/singularity/v4/pkg/plugin"
)

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)
}