Skip to content
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

Truncate JSON output file #502

Open
Foxlik opened this issue Feb 1, 2024 · 0 comments
Open

Truncate JSON output file #502

Foxlik opened this issue Feb 1, 2024 · 0 comments

Comments

@Foxlik
Copy link

Foxlik commented Feb 1, 2024

The JSON output file is not truncated at the moment. So in case where I write to an existing JSON file I get a broken JSON if the new contents is shorter than the previous.

$ dive --json dive.json image_a:latest
Image Source: image_a:latest
Fetching image... (this can take a while for large images)
Analyzing image...
Exporting image to 'dive.json'...
$ jq . dive.json > /dev/null
$ dive --json dive.json image_b:latest
Image Source: image_b:latest
Fetching image... (this can take a while for large images)
Analyzing image...
Exporting image to 'dive.json'...
$ jq . dive.json > /dev/null
parse error: Invalid numeric literal at line 161, column 6
$ cat dive.json | nl
     1	{
     2	  "layer": [
     3	    {
... TRUNCATED ...
   154	      {
   155	        "count": 2,
   156	        "sizeBytes": 0,
   157	        "file": "/tmp"
   158	      }
   159	    ]
   160	  }
   161	}ve_${DIVE_VERSION}_linux_amd64.tar.gz       | tar -zxf /dev/stdin -C /dive -T/dive.list     \u0026\u0026 rm -f /dive.list \u0026\u0026 echo \"${DIVE_SHA256} /dive/dive\" | sha256sum -c"
   162	    }
   163	  ],
   164	  "image": {
   165	    "sizeBytes": 106007465,
   166	    "inefficientBytes": 0,
   167	    "efficiencyScore": 1,
   168	    "fileReference": []
   169	  }
   170	}
$ rm dive.json
$ dive --json dive.json image_b:latest
Image Source: image_b:latest
Fetching image... (this can take a while for large images)
Analyzing image...
Exporting image to 'dive.json'...
$ jq . dive.json > /dev/null
$

I think the issue is the JSON output file should be opened with O_TRUNC as well.

file, err := filesystem.OpenFile(options.ExportFile, os.O_RDWR|os.O_CREATE, 0644)

Or am I missing something?

I can submit a PR if it makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant