Skip to content

Export cleans up file in case of errors #2067

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,10 @@ jobs:
./app/crane export - - < ubuntu.tar > filesystem.tar
ls -la *.tar

- name: crane export leaves filesystem clean in case of error
shell: bash
run: |
set -euxo pipefail

./app/crane export this-image-does-not-exist export.tar
[[ -f export.tar ]] && echo "did not expect export.tar to exist" && exit 1
9 changes: 7 additions & 2 deletions cmd/crane/cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func NewCmdExport(options *[]crane.Option) *cobra.Command {
# Read image from stdin
crane export - ubuntu.tar`,
Args: cobra.RangeArgs(1, 2),
RunE: func(_ *cobra.Command, args []string) error {
RunE: func(_ *cobra.Command, args []string) (err error) {
src, dst := args[0], "-"
if len(args) > 1 {
dst = args[1]
Expand All @@ -50,7 +50,12 @@ func NewCmdExport(options *[]crane.Option) *cobra.Command {
if err != nil {
return fmt.Errorf("failed to open %s: %w", dst, err)
}
defer f.Close()
defer func() {
f.Close()
if err != nil {
os.Remove(f.Name())
}
}()

var img v1.Image
if src == "-" {
Expand Down