-
Notifications
You must be signed in to change notification settings - Fork 23
Add elfuse-oci pull into a local image store #341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
henrybear327
wants to merge
2
commits into
sysprog21:main
Choose a base branch
from
henrybear327:oci/pull
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -604,3 +604,4 @@ include mk/lint.mk | |
| include mk/verify.mk | ||
| include mk/format.mk | ||
| include mk/help.mk | ||
| include mk/oci.mk | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| // Copyright 2026 elfuse contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
| v1 "github.com/google/go-containerregistry/pkg/v1" | ||
| ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
| ) | ||
|
|
||
| var version = "dev" | ||
|
|
||
| var defaultPlatform = ocispec.Platform{OS: "linux", Architecture: "arm64"} | ||
|
|
||
| func parsePlatform(s string) (ocispec.Platform, error) { | ||
| parts := strings.Split(s, "/") | ||
| if len(parts) < 2 || len(parts) > 3 || parts[0] == "" || parts[1] == "" || len(parts) == 3 && parts[2] == "" { | ||
| return ocispec.Platform{}, fmt.Errorf("invalid --platform %q: expected os/arch[/variant]", s) | ||
| } | ||
| p, err := v1.ParsePlatform(s) | ||
| if err != nil { | ||
| return ocispec.Platform{}, fmt.Errorf("invalid --platform: %w", err) | ||
| } | ||
| if p.OS != "linux" { | ||
| return ocispec.Platform{}, fmt.Errorf("invalid --platform %q: OS must be linux", s) | ||
| } | ||
| switch p.Architecture { | ||
| case "aarch64": | ||
| p.Architecture = "arm64" | ||
| case "x86_64": | ||
| p.Architecture = "amd64" | ||
| } | ||
| if p.Architecture != "arm64" && p.Architecture != "amd64" { | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| return ocispec.Platform{}, fmt.Errorf("invalid --platform %q: architecture must be arm64 or amd64", s) | ||
| } | ||
| if p.Architecture == "arm64" && p.Variant == "v8" { | ||
| p.Variant = "" | ||
| } | ||
| if p.Variant != "" { | ||
| return ocispec.Platform{}, fmt.Errorf("invalid --platform %q: variants are not supported", s) | ||
| } | ||
| if p.OSVersion != "" || len(p.OSFeatures) != 0 || len(p.Features) != 0 { | ||
| return ocispec.Platform{}, fmt.Errorf("invalid --platform %q: expected os/arch[/variant]", s) | ||
| } | ||
| return ocispec.Platform{OS: p.OS, Architecture: p.Architecture, Variant: p.Variant}, nil | ||
| } | ||
|
|
||
| func platformString(p ocispec.Platform) string { | ||
| return v1.Platform{OS: p.OS, Architecture: p.Architecture, Variant: p.Variant}.String() | ||
| } | ||
|
|
||
| func defaultStore() (string, error) { | ||
| if s := os.Getenv("ELFUSE_OCI_STORE"); s != "" { | ||
| return s, nil | ||
| } | ||
| home, err := os.UserHomeDir() | ||
| if err != nil { | ||
| return "", fmt.Errorf("no --store given and HOME is unset: %w", err) | ||
| } | ||
| return filepath.Join(home, ".local", "share", "elfuse", "oci"), nil | ||
| } | ||
|
|
||
| type commonFlags struct { | ||
| Store string `help:"OCI store directory" env:"ELFUSE_OCI_STORE" type:"path"` | ||
| Platform string `help:"Target platform os/arch[/variant]" default:"linux/arm64"` | ||
| } | ||
|
|
||
| func (cf *commonFlags) values() (string, ocispec.Platform, error) { | ||
| root := cf.Store | ||
| if root == "" { | ||
| var err error | ||
| root, err = defaultStore() | ||
| if err != nil { | ||
| return "", ocispec.Platform{}, err | ||
| } | ||
| } | ||
| p, err := parsePlatform(cf.Platform) | ||
| if err != nil { | ||
| return "", ocispec.Platform{}, err | ||
| } | ||
| return root, p, nil | ||
| } | ||
|
|
||
| func (cf *commonFlags) openStore() (*store, ocispec.Platform, error) { | ||
| root, platform, err := cf.values() | ||
| if err != nil { | ||
| return nil, ocispec.Platform{}, err | ||
| } | ||
| s, err := openStore(root) | ||
| return s, platform, err | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| // Copyright 2026 elfuse contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestParsePlatform(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| in string | ||
| want string | ||
| ok bool | ||
| }{ | ||
| {"linux/arm64", "linux/arm64", true}, | ||
| {"linux/amd64", "linux/amd64", true}, | ||
| {"linux/arm64/v8", "linux/arm64", true}, | ||
| {"linux/aarch64", "linux/arm64", true}, | ||
| {"linux/x86_64", "linux/amd64", true}, | ||
| {"linux//", "", false}, | ||
| {"/arm64", "", false}, | ||
| {"linux/arm64/", "", false}, | ||
| {"linux/arm64/v8/extra", "", false}, | ||
| {"darwin/arm64", "", false}, | ||
| {"linux/riscv64", "", false}, | ||
| {"linux/arm/v7", "", false}, | ||
| {"linux/arm64/v9", "", false}, | ||
| {"linux/amd64/v3", "", false}, | ||
| } { | ||
| p, err := parsePlatform(tc.in) | ||
| if tc.ok != (err == nil) { | ||
| t.Errorf("parsePlatform(%q) err = %v, want ok=%v", tc.in, err, tc.ok) | ||
| continue | ||
| } | ||
| if tc.ok && platformString(p) != tc.want { | ||
| t.Errorf("parsePlatform(%q) = %q, want %q", tc.in, platformString(p), tc.want) | ||
| } | ||
| } | ||
| if _, err := parsePlatform("darwin/arm64"); err == nil || !strings.Contains(err.Error(), "must be linux") { | ||
| t.Errorf("non-linux error should explain the OS rule: %v", err) | ||
| } | ||
| if _, err := parsePlatform("linux/riscv64"); err == nil || !strings.Contains(err.Error(), "arm64 or amd64") { | ||
| t.Errorf("unsupported-arch error should name the two guests: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestDefaultStoreFromEnv(t *testing.T) { | ||
| t.Setenv("ELFUSE_OCI_STORE", "/x/y") | ||
| got, err := defaultStore() | ||
| if err != nil || got != "/x/y" { | ||
| t.Fatalf("defaultStore = %q, %v", got, err) | ||
| } | ||
| t.Setenv("ELFUSE_OCI_STORE", "") | ||
| got, err = defaultStore() | ||
| if err != nil || !strings.HasSuffix(got, "/.local/share/elfuse/oci") { | ||
| t.Fatalf("defaultStore fallback = %q, %v", got, err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| // Copyright 2026 elfuse contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "syscall" | ||
| "time" | ||
| ) | ||
|
|
||
| const flockPoll = 50 * time.Millisecond | ||
|
|
||
| type flockFile struct { | ||
| f *os.File | ||
| } | ||
|
|
||
| // Poll LOCK_NB so cancellation bounds the wait. | ||
| func acquireFlock(ctx context.Context, path string) (*flockFile, error) { | ||
| f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o600) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| for { | ||
| if err := ctx.Err(); err != nil { | ||
| f.Close() | ||
| return nil, fmt.Errorf("lock %s: %w", path, err) | ||
| } | ||
| err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) | ||
| if err == nil { | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| l := &flockFile{f: f} | ||
| // Do not return a lock acquired after cancellation. | ||
| if cerr := ctx.Err(); cerr != nil { | ||
| l.Close() | ||
| return nil, fmt.Errorf("lock %s: %w", path, cerr) | ||
| } | ||
| return l, nil | ||
| } | ||
| if errors.Is(err, syscall.EINTR) { | ||
| continue | ||
| } | ||
| if !errors.Is(err, syscall.EWOULDBLOCK) { | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
|
||
| f.Close() | ||
| return nil, fmt.Errorf("lock %s: %w", path, err) | ||
| } | ||
| select { | ||
| case <-ctx.Done(): | ||
| f.Close() | ||
| return nil, fmt.Errorf("lock %s: %w", path, ctx.Err()) | ||
| case <-time.After(flockPoll): | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (l *flockFile) Close() error { | ||
| _ = syscall.Flock(int(l.f.Fd()), syscall.LOCK_UN) | ||
| return l.f.Close() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.