Skip to content

Truncate JSON output file #502

Description

@Foxlik

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreportnon-TUI interactions (stdout reports)

    Projects

    • Status
      Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions