* [wip] port to stereoscope
* revert wip stereoscope port
- the v1/v2 split behind `dev.use-stereoscope` is being replaced by an in-place port onto stereoscope
- reusable pieces (progress handlers, bubbletea setup) get pulled back from f14b994 as needed
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* add docker-free image fixture builder
- `internal/imagefixture` writes deterministic image tarballs from handwritten layer specs: legacy `docker save`, docker 25+ OCI `docker save` (uncompressed, gzip or zstd layers) and plain oci-archive
- named fixture set covering whiteouts (file, dir, opaque, missing target), links, history edge cases, mode/uid only changes and duplicated files, plus the checked in `.data` save tarballs
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* record export and CI parity goldens from the current parser
- `internal/imagefixture/testdata/golden/<fixture>.export.json` and `<fixture>.ci.json` capture what the pre-stereoscope code produces for every fixture, so the stereoscope port can be checked against them without the old code
- regenerate with `UPDATE_GOLDEN=true go test ./cmd/dive/cli/internal/command/...`
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* add stereoscope v0.3.4 and wire its bus and logger
- stereoscope publishes on dive's partybus and logs through dive's logger (nested under `from-lib=stereoscope`)
- `go` directive moves to 1.26.8 (stereoscope's minimum), CI bootstraps go 1.26.x to match
- `docker/cli` moves to v29.8.2 (via stereoscope), `docker/docker` to v28.5.2 so the existing resolver still builds against the newer `go-connections`
- `scylladb/go-set` stays on the pseudo version stereoscope pins (386 fix, never tagged); a `replace` back to v1.0.2 would break `go install ...@latest`
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* build linux releases with containers-storage support
- linux builds get `-tags containers_image_openpgp,exclude_graphdriver_btrfs` (same as syft), which compiles in stereoscope's containers-storage provider for podman/buildah stores
- still `CGO_ENABLED=0` and static; darwin/windows are unchanged since there's no local store there
- linux/amd64 binary grows about 7MB (21MB to 28MB)
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* add stereoscope based image analysis
- `dive.Analyze(ctx, ref, img)` derives layer info (diff ID, history command, size) and efficiency stats from a
stereoscope image, exposing the image itself on `Analysis`
- efficiency walks each layer's own tree and sizes whiteouts from the squashed tree of the layers below, no
restacking
- a whiteout of a path no lower layer has is skipped instead of cutting the layer's walk short
- inefficiencies come sorted largest first with ties ordered by path
- `imagefixture.Load` reads a fixture through stereoscope
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* cover stereoscope efficiency on fixture images
duplicates, removed files and dirs, opaque dirs, whiteouts of missing paths and hardlinks
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* add JSON export from the stereoscope analysis
`export.NewExportFromAnalysis` sits alongside `NewExport` with the same JSON shape:
- `layer[].id` and `layer[].digestId` are both the diff ID
- file lists walk each layer's own tree using the tar header as written, so hardlinks stay links
- implicit parent dirs (no tar header) are left out instead of showing up as empty entries
- `FileInfo` is now export's own type rather than `dive/filetree`'s
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* evaluate CI rules from a stats value
rules and the report read `ci.Stats` (efficiency, wasted bytes, user wasted percent, inefficient files) instead of
`*image.Analysis`, so both analyses can feed the evaluator. `ci.NewStats` reads the stereoscope analysis,
`ci.NewStatsFromImageAnalysis` the old one. Rules and report text are unchanged
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* check export and CI goldens against the stereoscope path
the golden tests now load every fixture through stereoscope and run `dive.Analyze`, `NewExportFromAnalysis` and
the CI evaluator. The old parser versions of these tests are gone (git history has their goldens).
every difference from the old goldens is intentional:
- all 17 `*.export.json`: `layer[].id` is now the diff ID (was the legacy layer dir hash, or `"blobs"` for OCI saves)
- `history-fewer-than-layers.export.json`, `no-history.export.json`: `digestId` is the diff ID even without a
history entry (was `""`)
- `implicit-parent-dirs.export.json`: implicit parent dirs no longer show up as empty `path: ""` entries
- `duplicate-files-across-layers.{export,ci}.json`, `test-docker-image.{export,ci}.json`,
`test-oci-docker-image.{export,ci}.json`: equal size inefficient files are ordered by path (was `sort.Sort` order)
- `whiteout-missing-target.{export,ci}.json`: a whiteout of a path no lower layer has no longer aborts the layer's
efficiency walk, so `/opt/a` counts as 250 wasted bytes, efficiency goes 0.99 -> 0.5 and both rule sets now fail
`lowestEfficiency` and `highestUserWastedPercent`
- `oci-archive-gzip`: layer order comes from the index now, but the fixture has one layer so only `id` changes
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* add stereoscope based image source loading
- `source.Load` resolves a reference to a stereoscope image, caller owns `img.Cleanup()`
- accepts dive's `docker://` style and stereoscope's `docker:` style schemes
- new sources: oci-archive, oci-dir, registry (`oci-registry`), containerd, containers-storage, auto
- docker client keeps dive's host resolution (`DOCKER_HOST`, `DOCKER_CONTEXT`, config context, ssh hosts)
- podman reads containers-storage first on linux, then the podman socket
- `source.Build` wraps `docker build` / `podman build` and returns a loadable reference
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* accept the new image sources and add --platform
- `--source` / `container-engine` accept every `source.Sources` value plus the `docker-tar` and `oci-registry` aliases
- new `--platform` / `platform` option
- `Analysis.Reference` keeps the image argument as given (scheme included) for `source.Load`
- the old resolver path keeps working off `Analysis.Source` and `Analysis.Image`
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* add bubbletea progress handlers copied from syft
- depend on `github.com/anchore/bubbly` v0.2.2 (brings in bubbletea v1.3.10)
- handlers for stereoscope's pull docker, pull containerd, fetch image and read image events, copied from syft v1.55.0 without the cataloger/SBOM parts
- per layer rows from the new `ReadLayer` event, nested under the read image row and dropped once each layer is indexed
- dive's `TaskStarted` events render as one task tree; tasks that reuse an ID (dive passes the image name for several tasks) still get their own row
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* add a bubbletea progress UI that hands off to the TUI
- `ui.New` picks the bubbletea progress UI on an interactive terminal and the existing line based UI otherwise (no TTY, piped stdin, `--quiet`, `-v`, `NO_TTY`, or `CI` set)
- on `ExploreAnalysis` the progress program quits and restores the terminal before the gocui TUI starts; both UIs share one handoff function
- reports and notifications are held until the progress UI exits, the report text on stdout matches the line based UI
- ctrl-c / esc while the progress UI owns the terminal publishes an interrupt exit event
- console log output (dive's and stereoscope's, which share a logger) is muted while a UI draws, file logging is left alone
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* add a UI-only file tree built from stereoscope trees
- ported the view parts of `dive/filetree` (nodes, view info, render, sort, diff marks) into `ui/v1/filetree`
- the bottom of a layer range is the layer's precomputed `SquashedTree`, each top layer's own tree is compared in turn, same marks as before
- compare trees are built on demand per range and cached, instead of eagerly for every range
- "modified" content checks hash lazily through the image file catalog, only for files present on both sides with equal metadata, cached per file reference
- file metadata comes from the original tar header so hardlinks, sizes and modes look the same as before
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* run the TUI and extract on the stereoscope analysis
- `ExploreAnalysis` now carries a `*dive.Analysis` and blocks the publisher until the TUI is done with it, so the caller can clean up the image afterwards (the payload type sits in the `event` package for now, `payload` importing `dive` is an import cycle until the old resolvers go)
- layer views derive the id, short id and "FROM <id>" from the layer diff ID; image details list inefficiencies in their new (largest first) order
- extract reads the selected layer's files under the selected path from the loaded image instead of re-saving it, and writes them relative to the cwd as before, refusing absolute or `..` tar names, paths through symlinks that leave the cwd, and existing symlinks at a file's own path. It now matches the selected path exactly instead of as a string prefix
- temporary bridge: the old command path loads the image again through `dive/source` for the TUI
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* run dive and dive build on the stereoscope path
- both commands now go options -> `source.Load` -> `dive.Analyze` -> `--json` export, `--ci` evaluation or the TUI, the temporary bridge and the old resolver/adapter calls are gone
- `dive build` runs `source.Build` and loads the built image by its ID; `auto` builds with docker. It keeps the line UI since the build subprocess writes to the terminal
- `dive` uses `ui.New` (bubbletea progress UI where the terminal allows it)
- same task rows as before (Loading, Analyzing, Exporting, Evaluating, Building), and a failed task now stops its spinner
- every `source.Load` temp dir hangs off one generator and `source.Cleanup()` runs after `app.Run()`, so an interrupt (which skips the command's own cleanup) no longer leaves the image copy behind
- the "this can take a while" notification is dropped
- `--json` truncates an existing file instead of writing over its start
- `options.Analysis` keeps only `Image` (the raw reference), `V1UI` is now `LineUI`
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* delete the old image parser and file tree
- `dive/filetree`, `dive/image` (docker and podman resolvers included), `dive.GetImageResolver` and `command/adapter` are gone, stereoscope is the only image model now
- `export.NewExportFromAnalysis` is now `export.NewExport`, `ci.NewStatsFromImageAnalysis` is gone
- the `ExploreAnalysis` payload moves back to `internal/bus/event/payload`
- `docker/docker` is no longer a dependency
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* move the TUI from ui/v1 to ui/tui
- package `v1` is now `tui`, the line UI lives in `ui/line_ui.go`, no behavior change
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* deprecate --ignore-errors
- path errors no longer block the TUI, so the flag gates nothing. It is still accepted (flag and `ignore-errors` config) and warns when set
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* make the CLI tests run without docker
- tests that need docker or podman skip with the reason when the engine (or dive's docker config) isn't reachable
- new docker-free tests run the real command against docker-archive and oci-archive fixtures for `--json` (checked against the export goldens) and `--ci` pass/fail
- the default CI config test now runs `--ci` on the archive instead of relying on a load failure
- snapshots no longer embed the repo path; layer `id` in the JSON snapshot is now the diff ID, and inefficient file ties are ordered by path
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* move to golangci-lint v2
- v1.64.8 is built with go1.24 and refuses the go 1.26 module, binny now pins v2.14.0
- `.golangci.yaml` migrated with `golangci-lint migrate` (same funlen limits, default linters)
- fixes the staticcheck findings v2 reports
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* document the new image sources
- sources table with the new values, both scheme forms, `auto` and `--platform`
- podman reads containers-storage on linux (build tag, on in linux release builds) and the socket elsewhere
- pulls go through the docker API, layer `id` in the JSON is the diff ID, `ignore-errors` is deprecated
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* address review feedback on the stereoscope port
- `dive/source` moves to `internal/source`, and `dive.WalkTree` / `dive.TarHeader` are replaced by an internal `layertree` walker, so the only public surface left is `dive.Analyze` (which now honors ctx)
- docker client is built from the context endpoint (context TLS and ssh kept), `DOCKER_TLS` / `DOCKER_TLS_VERIFY` honored for env hosts
- podman client is built in dive and refuses ssh destinations, since stereoscope's fallback skipped host key checks
- registry http fallback limited to `localhost` and loopback literals, User-Agent no longer carries the binary path
- ctx reaches the TUI, cleanup waits for in-flight loads, and a second ctrl-c during cleanup no longer kills dive
- notifications always print (quiet aside), are escaped, show above the progress frame, and are drained on teardown
- extract writes with `O_EXCL` inside an `os.Root` and reports problems after the TUI exits instead of ending it
- TUI marks opaque whiteouts and their ancestors, implicit dirs no longer render Modified, failed hash reads aren't cached
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* keep go.uber.org/atomic v1.11.0 from main
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* allow CC0-1.0 licensed dependencies
`github.com/therootcompany/xz` (pulled in through stereoscope) is CC0-1.0, a public domain dedication like the already allowed `Unlicense`.
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* escape untrusted text in the final command error
Load errors carry text a registry or daemon controls (response bodies, pull messages), and clio prints the command's error raw, so a registry could drive the terminal (e.g. OSC 52 to set the clipboard).
- root and `build` errors now go through the same escaping as notifications, wrapped so `errors.Is` / `errors.As` still work
- the shared filter also escapes Unicode format characters (bidi overrides, zero width), so a path can't display as something else
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* find the podman machine socket on macOS
Since the ssh fallback was dropped, `--source podman` (and `dive build` with podman) on macOS found nothing unless `CONTAINER_HOST` was set, because podman machine only records ssh destinations in its config.
When no other socket is found, dive now asks `podman machine inspect` for the default machine's API socket and uses it if it exists.
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* check the snapshot cleanup error in the CLI tests
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* point the acceptance tests at the renamed snapshot build dirs
Splitting the goreleaser builds into `linux-build` and `build` changed the snapshot output dirs (`linux-build_linux_amd64_v1`, `build_darwin_amd64_v1`, `build_windows_amd64_v1`), so the acceptance jobs couldn't find the binaries.
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
* read OCI archives through docker-archive too
main's parser accepted an OCI archive under `--source docker-archive`, stereoscope's docker-archive provider needs a `manifest.json`. `docker-archive` now falls back to the oci-archive provider, so existing invocations keep working.
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
---------
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
371 lines
9.9 KiB
Go
371 lines
9.9 KiB
Go
// Package imagefixture writes small, deterministic container image tarballs from handwritten layer specs, so tests
|
|
// can exercise image parsing and analysis without a docker daemon.
|
|
package imagefixture
|
|
|
|
import (
|
|
"archive/tar"
|
|
"bytes"
|
|
"compress/gzip"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"sort"
|
|
"time"
|
|
|
|
"github.com/klauspost/compress/zstd"
|
|
)
|
|
|
|
// Format is the on-disk layout of the image tarball.
|
|
type Format int
|
|
|
|
const (
|
|
// LegacySave is the `docker save` layout before docker 25: manifest.json, <id>/layer.tar, <hex>.json config.
|
|
LegacySave Format = iota
|
|
// OCISave is the `docker save` layout from docker 25 on: an OCI layout (index.json, oci-layout, blobs/sha256/...)
|
|
// plus docker's manifest.json and repositories files.
|
|
OCISave
|
|
// OCIArchive is a plain OCI layout tarball (no manifest.json), as written by `buildx --output type=oci`.
|
|
OCIArchive
|
|
)
|
|
|
|
// Compression applies to layer blobs of the OCI formats. LegacySave layers are always uncompressed.
|
|
type Compression int
|
|
|
|
const (
|
|
Uncompressed Compression = iota
|
|
Gzip
|
|
Zstd
|
|
)
|
|
|
|
// Entry is a single tar entry within a layer.
|
|
type Entry struct {
|
|
Name string
|
|
Type byte // tar typeflag, zero means tar.TypeReg
|
|
Mode int64
|
|
UID, GID int
|
|
Linkname string
|
|
Content string
|
|
}
|
|
|
|
// History is one image config history entry.
|
|
type History struct {
|
|
CreatedBy string
|
|
EmptyLayer bool
|
|
}
|
|
|
|
// Spec describes a whole image.
|
|
type Spec struct {
|
|
Format Format
|
|
Compression Compression
|
|
Layers [][]Entry
|
|
// nil means the config has no history key at all
|
|
History []History
|
|
}
|
|
|
|
// fixedTime is used for every mtime and timestamp so output is byte for byte stable.
|
|
var fixedTime = time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
|
|
|
|
const repoTag = "dive-fixture:latest"
|
|
|
|
type blob struct {
|
|
digest string // "sha256:<hex>"
|
|
data []byte
|
|
}
|
|
|
|
func newBlob(data []byte) blob {
|
|
sum := sha256.Sum256(data)
|
|
return blob{digest: "sha256:" + hex.EncodeToString(sum[:]), data: data}
|
|
}
|
|
|
|
func (b blob) hex() string { return b.digest[len("sha256:"):] }
|
|
|
|
type descriptor struct {
|
|
MediaType string `json:"mediaType"`
|
|
Digest string `json:"digest"`
|
|
Size int `json:"size"`
|
|
Annotations map[string]string `json:"annotations,omitempty"`
|
|
}
|
|
|
|
type dockerManifest struct {
|
|
Config string `json:"Config"`
|
|
RepoTags []string `json:"RepoTags"`
|
|
Layers []string `json:"Layers"`
|
|
}
|
|
|
|
// Write writes the image tarball for the spec to w.
|
|
func (s Spec) Write(w io.Writer) error {
|
|
var diffIDs []string
|
|
var layerTars [][]byte
|
|
for i, entries := range s.Layers {
|
|
data, err := layerTar(entries)
|
|
if err != nil {
|
|
return fmt.Errorf("layer %d: %w", i, err)
|
|
}
|
|
layerTars = append(layerTars, data)
|
|
diffIDs = append(diffIDs, newBlob(data).digest)
|
|
}
|
|
|
|
config, err := s.config(diffIDs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
out := tar.NewWriter(w)
|
|
switch s.Format {
|
|
case LegacySave:
|
|
err = writeLegacy(out, config, layerTars, diffIDs)
|
|
case OCISave, OCIArchive:
|
|
err = s.writeOCI(out, config, layerTars)
|
|
default:
|
|
err = fmt.Errorf("unknown format %d", s.Format)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return out.Close()
|
|
}
|
|
|
|
func (s Spec) config(diffIDs []string) (blob, error) {
|
|
type historyJSON struct {
|
|
Created string `json:"created"`
|
|
CreatedBy string `json:"created_by,omitempty"`
|
|
EmptyLayer bool `json:"empty_layer,omitempty"`
|
|
}
|
|
type rootfs struct {
|
|
Type string `json:"type"`
|
|
DiffIDs []string `json:"diff_ids"`
|
|
}
|
|
cfg := struct {
|
|
Architecture string `json:"architecture"`
|
|
OS string `json:"os"`
|
|
Created string `json:"created"`
|
|
Config map[string]any `json:"config"`
|
|
History []historyJSON `json:"history,omitempty"`
|
|
RootFS rootfs `json:"rootfs"`
|
|
}{
|
|
Architecture: "amd64",
|
|
OS: "linux",
|
|
Created: fixedTime.Format(time.RFC3339),
|
|
Config: map[string]any{"Env": []string{"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"}},
|
|
RootFS: rootfs{Type: "layers", DiffIDs: diffIDs},
|
|
}
|
|
for _, h := range s.History {
|
|
cfg.History = append(cfg.History, historyJSON{
|
|
Created: fixedTime.Format(time.RFC3339),
|
|
CreatedBy: h.CreatedBy,
|
|
EmptyLayer: h.EmptyLayer,
|
|
})
|
|
}
|
|
data, err := json.Marshal(cfg)
|
|
if err != nil {
|
|
return blob{}, err
|
|
}
|
|
return newBlob(data), nil
|
|
}
|
|
|
|
func writeLegacy(out *tar.Writer, config blob, layerTars [][]byte, diffIDs []string) error {
|
|
files := map[string][]byte{}
|
|
var layerPaths []string
|
|
parent := ""
|
|
for i, data := range layerTars {
|
|
// ponytail: v1 layer ids are just a stable hash of parent + diff id, docker's real chain math isn't needed
|
|
sum := sha256.Sum256([]byte(parent + " " + diffIDs[i]))
|
|
id := hex.EncodeToString(sum[:])
|
|
v1, err := json.Marshal(map[string]any{"id": id, "parent": parent, "created": fixedTime.Format(time.RFC3339)})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
files[id+"/"] = nil
|
|
files[id+"/VERSION"] = []byte("1.0")
|
|
files[id+"/json"] = v1
|
|
files[id+"/layer.tar"] = data
|
|
layerPaths = append(layerPaths, id+"/layer.tar")
|
|
parent = id
|
|
}
|
|
configName := config.hex() + ".json"
|
|
files[configName] = config.data
|
|
|
|
manifest, err := json.Marshal([]dockerManifest{{Config: configName, RepoTags: []string{repoTag}, Layers: layerPaths}})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
files["manifest.json"] = manifest
|
|
if parent != "" {
|
|
files["repositories"] = []byte(fmt.Sprintf(`{"dive-fixture":{"latest":%q}}`, parent))
|
|
}
|
|
return writeSorted(out, files)
|
|
}
|
|
|
|
func (s Spec) writeOCI(out *tar.Writer, config blob, layerTars [][]byte) error {
|
|
mediaType := "application/vnd.oci.image.layer.v1.tar"
|
|
switch s.Compression {
|
|
case Gzip:
|
|
mediaType += "+gzip"
|
|
case Zstd:
|
|
mediaType += "+zstd"
|
|
}
|
|
|
|
files := map[string][]byte{"blobs/": nil, "blobs/sha256/": nil}
|
|
add := func(b blob) string {
|
|
name := "blobs/sha256/" + b.hex()
|
|
files[name] = b.data
|
|
return name
|
|
}
|
|
|
|
var layerDescs []descriptor
|
|
var layerPaths []string
|
|
for _, data := range layerTars {
|
|
compressed, err := compress(data, s.Compression)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
b := newBlob(compressed)
|
|
layerPaths = append(layerPaths, add(b))
|
|
layerDescs = append(layerDescs, descriptor{MediaType: mediaType, Digest: b.digest, Size: len(b.data)})
|
|
}
|
|
configPath := add(config)
|
|
|
|
manifest, err := json.Marshal(struct {
|
|
SchemaVersion int `json:"schemaVersion"`
|
|
MediaType string `json:"mediaType"`
|
|
Config descriptor `json:"config"`
|
|
Layers []descriptor `json:"layers"`
|
|
}{
|
|
SchemaVersion: 2,
|
|
MediaType: "application/vnd.oci.image.manifest.v1+json",
|
|
Config: descriptor{MediaType: "application/vnd.oci.image.config.v1+json", Digest: config.digest, Size: len(config.data)},
|
|
Layers: layerDescs,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
manifestBlob := newBlob(manifest)
|
|
add(manifestBlob)
|
|
|
|
index, err := json.Marshal(map[string]any{
|
|
"schemaVersion": 2,
|
|
"mediaType": "application/vnd.oci.image.index.v1+json",
|
|
"manifests": []descriptor{{
|
|
MediaType: "application/vnd.oci.image.manifest.v1+json",
|
|
Digest: manifestBlob.digest,
|
|
Size: len(manifestBlob.data),
|
|
Annotations: map[string]string{
|
|
"io.containerd.image.name": "docker.io/library/" + repoTag,
|
|
"org.opencontainers.image.ref.name": "latest",
|
|
},
|
|
}},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
files["index.json"] = index
|
|
files["oci-layout"] = []byte(`{"imageLayoutVersion":"1.0.0"}`)
|
|
|
|
if s.Format == OCISave {
|
|
dm, err := json.Marshal([]dockerManifest{{Config: configPath, RepoTags: []string{repoTag}, Layers: layerPaths}})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
files["manifest.json"] = dm
|
|
files["repositories"] = []byte(fmt.Sprintf(`{"dive-fixture":{"latest":%q}}`, config.hex()))
|
|
}
|
|
return writeSorted(out, files)
|
|
}
|
|
|
|
// writeSorted writes files in name order; a nil value is written as a directory.
|
|
func writeSorted(out *tar.Writer, files map[string][]byte) error {
|
|
names := make([]string, 0, len(files))
|
|
for name := range files {
|
|
names = append(names, name)
|
|
}
|
|
sort.Strings(names)
|
|
for _, name := range names {
|
|
data := files[name]
|
|
hdr := &tar.Header{Name: name, Typeflag: tar.TypeReg, Mode: 0o644, Size: int64(len(data)), ModTime: fixedTime}
|
|
if data == nil {
|
|
hdr.Typeflag, hdr.Mode, hdr.Size = tar.TypeDir, 0o755, 0
|
|
}
|
|
if err := out.WriteHeader(hdr); err != nil {
|
|
return err
|
|
}
|
|
if _, err := out.Write(data); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// layerTar writes the entries in the given order (order is meaningful within a layer, e.g. hardlink targets).
|
|
func layerTar(entries []Entry) ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
tw := tar.NewWriter(&buf)
|
|
for _, e := range entries {
|
|
hdr := &tar.Header{
|
|
Name: e.Name,
|
|
Typeflag: e.Type,
|
|
Mode: e.Mode,
|
|
Uid: e.UID,
|
|
Gid: e.GID,
|
|
Linkname: e.Linkname,
|
|
ModTime: fixedTime,
|
|
Format: tar.FormatGNU,
|
|
}
|
|
if hdr.Typeflag == 0 {
|
|
hdr.Typeflag = tar.TypeReg
|
|
}
|
|
if hdr.Typeflag == tar.TypeReg {
|
|
hdr.Size = int64(len(e.Content))
|
|
}
|
|
if hdr.Mode == 0 {
|
|
switch hdr.Typeflag {
|
|
case tar.TypeDir:
|
|
hdr.Mode = 0o755
|
|
case tar.TypeSymlink:
|
|
hdr.Mode = 0o777
|
|
default:
|
|
hdr.Mode = 0o644
|
|
}
|
|
}
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
return nil, fmt.Errorf("entry %q: %w", e.Name, err)
|
|
}
|
|
if hdr.Size > 0 {
|
|
if _, err := io.WriteString(tw, e.Content); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
}
|
|
if err := tw.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|
|
|
|
func compress(data []byte, c Compression) ([]byte, error) {
|
|
var buf bytes.Buffer
|
|
var w io.WriteCloser
|
|
var err error
|
|
switch c {
|
|
case Uncompressed:
|
|
return data, nil
|
|
case Gzip:
|
|
// zero header mtime and no name keep gzip output stable
|
|
w = gzip.NewWriter(&buf)
|
|
case Zstd:
|
|
w, err = zstd.NewWriter(&buf, zstd.WithEncoderConcurrency(1))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
default:
|
|
return nil, fmt.Errorf("unknown compression %d", c)
|
|
}
|
|
if _, err := w.Write(data); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := w.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
return buf.Bytes(), nil
|
|
}
|