aboutsummaryrefslogtreecommitdiffhomepage
path: root/pkg/file.go
blob: 14467153d81c15fbb7a6cc311e92658acceb18fe (plain)
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
94
95
96
97
98
99
100
package pkg

import (
	"bytes"
	"crypto/sha512"
	"fmt"
	"io"
)

// A fileArtifact is an [Artifact] that cures into data known ahead of time.
type fileArtifact []byte

var _ KnownChecksum = new(fileArtifact)
var _ CuresExempt = new(fileArtifact)
var _ RevisionArtifact = new(fileArtifact)

// fileArtifactNamed embeds fileArtifact alongside a caller-supplied name.
type fileArtifactNamed struct {
	fileArtifact
	// Caller-supplied user-facing reporting name.
	name string
}

var _ fmt.Stringer = new(fileArtifactNamed)
var _ KnownChecksum = new(fileArtifactNamed)

// String returns the caller-supplied reporting name.
func (a *fileArtifactNamed) String() string { return a.name }

// Params writes the caller-supplied reporting name and the file body.
func (a *fileArtifactNamed) Params(ctx *IContext) {
	ctx.WriteString(a.name)
	ctx.Write(a.fileArtifact)
}

// NewFile returns a [FileArtifact] that cures into a caller-supplied byte slice.
//
// Caller must not modify data after NewFile returns.
func NewFile(name string, data []byte) FileArtifact {
	f := fileArtifact(data)
	if name != "" {
		return &fileArtifactNamed{f, name}
	}
	return &f
}

// Kind returns the hardcoded [Kind] constant.
func (*fileArtifact) Kind() Kind { return KindFile }

// Params writes an empty string and the file body.
func (a *fileArtifact) Params(ctx *IContext) {
	ctx.WriteString("")
	ctx.Write(*a)
}

func init() {
	register(KindFile, func(r *IRReader) Artifact {
		name := r.ReadString()
		data := r.ReadStringBytes()
		if _, ok := r.Finalise(); !ok {
			panic(ErrExpectedChecksum)
		}
		return NewFile(name, data)
	})
}

// Inputs returns a nil slice.
func (*fileArtifact) Inputs() []Artifact { return nil }

// IsExclusive returns false: Cure returns a prepopulated buffer.
func (*fileArtifact) IsExclusive() bool { return false }

// Checksum computes and returns the checksum of caller-supplied data.
func (a *fileArtifact) Checksum() Checksum {
	h := sha512.New384()
	h.Write(*a)
	return Checksum(h.Sum(nil))
}

// Revision satisfies [RevisionArtifact] for incompatible IsExecutable behaviour.
func (*fileArtifact) Revision() uint64 { return 0 }

// IsExecutable returns whether the contents begin with shebang or the ELF
// magic number.
func (a *fileArtifact) IsExecutable() bool {
	return a != nil &&
		(bytes.HasPrefix(
			*a, []byte{'#', '!'},
		) || bytes.HasPrefix(
			*a, []byte{0x7f, 'E', 'L', 'F'},
		))
}

// Cure returns the caller-supplied data.
func (a *fileArtifact) Cure(*RContext) (io.ReadCloser, error) {
	return io.NopCloser(bytes.NewReader(*a)), nil
}

// CuresExempt exempts the cheap [KindFile] implementation.
func (*fileArtifact) CuresExempt() {}