]> git.ipfire.org Git - thirdparty/util-linux.git/commit
Generate valid JSON if partition table is empty
authorAdriaan de Groot <groot@kde.org>
Mon, 24 Aug 2020 21:30:35 +0000 (23:30 +0200)
committerKarel Zak <kzak@redhat.com>
Fri, 13 Nov 2020 10:53:31 +0000 (11:53 +0100)
commite911d9ff25ca3bccdef1ba825b539e88f83903fa
treeef167f4ec2444852713bb9e15ea5ba6776f28795
parentf5a00ea0dc54db80f9e947935a8f3dad3102c1bd
Generate valid JSON if partition table is empty

When the partition table is present **but** empty, the existing code would output fields followed by a `,` , on the assumption that the list of partitions would follow. But if the list of partitions is empty, it is skipped, leading to output like this:

```
{
  "partitiontable": {
      "label":"gpt",
      "id":"1F9E80D9-DD78-024F-94A3-B61EC82B18C8",
      "device":"/dev/sdb",
      "unit":"sectors",
      "firstlba":2048,
      "lastlba":30949342,
      "sectorsize":512,
  }
}
```

Note the `512,` on the *sectorsize* line.

This is invalid JSON for some parsers, which choke on it.

Avoid this, by checking when outputting the last separator: if there's no table, or there is a table but it is empty, then just put a newline, otherwise use the old path of comma-newline and assume there's going to be a list of partitions after.
libfdisk/src/script.c