From e0eec632d0a7cca2fb09e026997fa9a39dcd7504 Mon Sep 17 00:00:00 2001 From: Ophestra Date: Thu, 23 Jul 2026 17:10:22 +0900 Subject: internal/pkg: optional executable files This enables a FileArtifact implementation to specify whether its resulting file should be executable. This change also implements shebang and ELF magic checks in the file artifact. Signed-off-by: Ophestra --- internal/pkg/file.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'internal/pkg/file.go') diff --git a/internal/pkg/file.go b/internal/pkg/file.go index 1b6b02f7..14467153 100644 --- a/internal/pkg/file.go +++ b/internal/pkg/file.go @@ -12,6 +12,7 @@ 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 { @@ -76,6 +77,20 @@ func (a *fileArtifact) Checksum() Checksum { 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 -- cgit v1.3.1