Skip to content

fix: allow fetch-config --output to special files - #2227

Merged
TerryHowe merged 2 commits into
oras-project:mainfrom
havinash-007:fix/output-non-regular-files
Nov 2, 2026
Merged

TerryHowe merged 2 commits into
oras-project:mainfrom
havinash-007:fix/output-non-regular-files

Conversation

@havinash-007

Copy link
Copy Markdown
Contributor

Problem

Since #2222, oras manifest fetch-config --output <path> always writes to
os.CreateTemp(filepath.Dir(path)), chmods it to 0644 and renames it over the
target.

Repro (before the fix; OCI layout, no registry needed)

oras push --oci-layout lay:v1 --config cfg.json:application/vnd.test.config.v1+json
oras manifest fetch-config --oci-layout lay:v1 -o /dev/stdout | cat   # fails
ln -s t.json l.json; oras manifest fetch-config --oci-layout lay:v1 -o l.json  # l.json becomes a regular 0644 file
(umask 077; oras manifest fetch-config --oci-layout lay:v1 -o n.json)  # n.json is 0644

After the fix: stdout works, the symlink is kept and its target updated, n.json is 0600.

Fix

New writeOutputFile (cmd/oras/root/manifest/output_file.go):

  • Lstat the target. Symlink or existing non-regular file: open and write in
    place (no temp file, no rename).
  • Otherwise keep the atomic temp file + Sync + rename flow, but create the temp
    file in the target directory with 0666 (kernel applies umask) and Chmod it to
    the existing file's permission bits when overwriting.
  • Temp file is removed on any failure; digest/size verification still runs before
    the rename.

Tests

  • New unit tests: new file, overwrite keeps mode, failed write keeps original and
    leaves no temp file, umask honoured, symlink kept (target mode kept), FIFO,
    character device (/dev/null). Unix-only ones are in output_file_unix_test.go
    (!windows).
  • go test ./cmd/oras/... ./internal/... passes; go vet and gofmt -l clean;
    GOOS=windows go vet ./cmd/oras/root/manifest/ OK.
  • Manually compared old and new binaries on the repro above.

Not verified / caveats

  • golangci-lint is not installed locally, so CI lint was not run (gofmt/vet were).
  • Not run on macOS or Windows (only cross-vetted for Windows); e2e suite not run.
  • Symlinks and non-regular files are written in place, so they are not atomic: a
    failed digest verification can leave a truncated/partial target. In-place writes
    open with O_TRUNC, so -o /dev/stdout with stdout redirected by >> to a regular
    file truncates it (same as >; previous pre-fix: limit manifest config fetch size #2222 os.Create behaviour).
  • Replacing an existing regular file by rename does not preserve its owner or
    hard links (mode is preserved).
  • The - (stdout) path is unchanged. Other commands' --output handling was not touched.

AI assistance

This change was prepared with Claude Code (Claude Sonnet 5.5) for @havinash-007. Tests were run as listed above; the caveats list what was not verified.

Fixes #2224
Fixes #2226

🤖 Generated with Claude Code

`oras manifest fetch-config --output <path>` has written through a temp
file plus rename since oras-project#2222. This broke targets that cannot be
replaced, such as /dev/stdout, /dev/fd/N, FIFOs and devices (the temp
file was created in /dev). It also forced mode 0644 and replaced
symbolic links with regular files.

Add writeOutputFile and use it for `--output`:
- symbolic links and existing non-regular files are written through
  directly, so links, FIFOs and devices keep working and stay intact;
- otherwise the content is still written to a temp file in the same
  directory and renamed, keeping the write atomic;
- an existing regular file keeps its permission bits, and a new file
  gets 0666 filtered by the umask instead of a hard-coded 0644.

Fixes oras-project#2224
Fixes oras-project#2226

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Havinash Gangisetty <[email protected]>

@TerryHowe TerryHowe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the new writeOutputFile; findings inline.

Comment thread cmd/oras/root/manifest/output_file.go Resolved
Comment thread cmd/oras/root/manifest/output_file.go Resolved
Comment thread cmd/oras/root/manifest/output_file.go Resolved
Comment thread cmd/oras/root/manifest/output_file.go Resolved
- resolve symlinks with EvalSymlinks and keep the atomic rename for the
  target instead of truncating it in place before digest verification
- chmod the temp file before writing so content never sits in a wider mode
- name the temp file from the directory so long basenames do not exceed
  the name length limit, and fall back to an in-place write when no temp
  file can be created (e.g. read-only directory)
- drop the redundant symlink check in favour of IsRegular

Signed-off-by: Terry Howe <[email protected]>
@codecov

codecov Bot commented Nov 2, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.76923% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 73.33%. Comparing base (ab0ff3d) to head (1afbf7a).
⚠️ Report is 42 commits behind head on main.

Files with missing lines Patch % Lines
cmd/oras/root/manifest/output_file.go 70.68% 10 Missing and 7 partials ⚠️
cmd/oras/root/manifest/fetch_config.go 71.42% 1 Missing and 1 partial ⚠️

❌ Your patch check has failed because the patch coverage (70.76%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2227       +/-   ##
===========================================
+ Coverage   57.37%   73.33%   +15.96%     
===========================================
  Files         139      156       +17     
  Lines        6067     6526      +459     
===========================================
+ Hits         3481     4786     +1305     
+ Misses       2364     1452      -912     
- Partials      222      288       +66     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TerryHowe TerryHowe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@TerryHowe
TerryHowe merged commit 3bf31fc into oras-project:main Nov 2, 2026
9 of 10 checks passed
@TerryHowe TerryHowe mentioned this pull request Nov 17, 2026
4 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants