2019-06-21 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public
|
|
|
|
* License v2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 021110-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "kerncompat.h"
|
|
|
|
#include <stdio.h>
|
2022-09-15 11:59:39 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2019-06-21 15:19:25 +00:00
|
|
|
#include <uuid/uuid.h>
|
2023-08-28 20:12:13 +00:00
|
|
|
#include "kernel-shared/uapi/btrfs.h"
|
|
|
|
#include "common/messages.h"
|
2019-06-21 15:19:25 +00:00
|
|
|
#include "common/format-output.h"
|
|
|
|
#include "common/utils.h"
|
2021-04-29 21:43:24 +00:00
|
|
|
#include "common/units.h"
|
2019-06-21 15:19:25 +00:00
|
|
|
#include "cmds/commands.h"
|
|
|
|
|
|
|
|
static void print_uuid(const u8 *uuid)
|
|
|
|
{
|
|
|
|
char uuidparse[BTRFS_UUID_UNPARSED_SIZE];
|
|
|
|
|
|
|
|
if (uuid_is_null(uuid)) {
|
2023-08-18 00:15:35 +00:00
|
|
|
printf("null");
|
2019-06-21 15:19:25 +00:00
|
|
|
} else {
|
|
|
|
uuid_unparse(uuid, uuidparse);
|
|
|
|
printf("%s", uuidparse);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 23:40:00 +00:00
|
|
|
static void print_escaped(const char *str)
|
|
|
|
{
|
|
|
|
while (*str) {
|
|
|
|
switch (*str) {
|
|
|
|
case '\b': /* 0x08 */
|
|
|
|
putchar('\\');
|
|
|
|
putchar('b');
|
|
|
|
break;
|
|
|
|
case '\t': /* 0x09 */
|
|
|
|
putchar('\\');
|
|
|
|
putchar('t');
|
|
|
|
break;
|
|
|
|
case '\n': /* 0x0a */
|
|
|
|
putchar('\\');
|
|
|
|
putchar('n');
|
|
|
|
break;
|
|
|
|
case '\f': /* 0x0c */
|
|
|
|
putchar('\\');
|
|
|
|
putchar('f');
|
|
|
|
break;
|
|
|
|
case '\r': /* 0x0d */
|
|
|
|
putchar('\\');
|
|
|
|
putchar('r');
|
|
|
|
break;
|
|
|
|
/* Other control characters from 0 .. 31 */
|
|
|
|
case '\v': /* 0x0b */
|
|
|
|
case 0x00 ... 0x07:
|
|
|
|
case 0x0e ... 0x1f:
|
|
|
|
printf("\\u%04x", *str);
|
|
|
|
break;
|
|
|
|
/* '/' (solidus) not escaped */
|
|
|
|
case '"':
|
|
|
|
case '\\':
|
|
|
|
putchar('\\');
|
2023-01-27 05:41:16 +00:00
|
|
|
fallthrough;
|
2022-12-15 23:40:00 +00:00
|
|
|
default:
|
|
|
|
putchar(*str);
|
|
|
|
}
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-21 15:19:25 +00:00
|
|
|
static void fmt_indent1(int indent)
|
|
|
|
{
|
|
|
|
while (indent--)
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fmt_indent2(int indent)
|
|
|
|
{
|
|
|
|
while (indent--) {
|
|
|
|
putchar(' ');
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fmt_error(struct format_ctx *fctx)
|
|
|
|
{
|
2022-09-27 22:52:00 +00:00
|
|
|
internal_error("formatting json: depth=%d", fctx->depth);
|
2019-06-21 15:19:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fmt_inc_depth(struct format_ctx *fctx)
|
|
|
|
{
|
|
|
|
if (fctx->depth >= JSON_NESTING_LIMIT - 1) {
|
2022-09-27 22:52:00 +00:00
|
|
|
internal_error("nesting too deep, limit %d", JSON_NESTING_LIMIT);
|
2019-06-21 15:19:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fctx->depth++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fmt_dec_depth(struct format_ctx *fctx)
|
|
|
|
{
|
|
|
|
if (fctx->depth < 1) {
|
2022-09-27 22:52:00 +00:00
|
|
|
internal_error("nesting below first level");
|
2019-06-21 15:19:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fctx->depth--;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void fmt_separator(struct format_ctx *fctx)
|
|
|
|
{
|
|
|
|
if (bconf.output_format == CMD_FORMAT_JSON) {
|
|
|
|
/* Check current depth */
|
|
|
|
if (fctx->memb[fctx->depth] == 0) {
|
|
|
|
/* First member, only indent */
|
|
|
|
putchar('\n');
|
|
|
|
fmt_indent2(fctx->depth);
|
|
|
|
fctx->memb[fctx->depth] = 1;
|
|
|
|
} else if (fctx->memb[fctx->depth] == 1) {
|
|
|
|
/* Something has been printed already */
|
|
|
|
printf(",\n");
|
|
|
|
fmt_indent2(fctx->depth);
|
|
|
|
fctx->memb[fctx->depth] = 2;
|
|
|
|
} else {
|
|
|
|
/* N-th member */
|
|
|
|
printf(",\n");
|
|
|
|
fmt_indent2(fctx->depth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 20:50:41 +00:00
|
|
|
/* Detect formats or values that must not be quoted (null, bool) */
|
2023-08-18 00:15:35 +00:00
|
|
|
static bool fmt_set_unquoted(struct format_ctx *fctx, const struct rowspec *row,
|
|
|
|
va_list args)
|
2023-08-17 20:50:41 +00:00
|
|
|
{
|
2023-08-17 23:24:03 +00:00
|
|
|
static const char *types[] = { "%llu", "bool" };
|
|
|
|
|
|
|
|
for (int i = 0; i < sizeof(types) / sizeof(types[0]); i++)
|
|
|
|
if (strcmp(types[i], row->fmt) == 0)
|
|
|
|
return true;
|
2023-08-18 00:15:35 +00:00
|
|
|
|
|
|
|
/* Null value */
|
|
|
|
if (strcmp("uuid", row->fmt) == 0) {
|
|
|
|
va_list tmpargs;
|
|
|
|
const u8 *uuid;
|
|
|
|
|
|
|
|
va_copy(tmpargs, args);
|
|
|
|
uuid = va_arg(tmpargs, const u8 *);
|
|
|
|
|
2024-04-03 22:35:40 +00:00
|
|
|
if (uuid_is_null(uuid)) {
|
|
|
|
va_end(tmpargs);
|
2023-08-18 00:15:35 +00:00
|
|
|
return true;
|
2024-04-03 22:35:40 +00:00
|
|
|
}
|
|
|
|
va_end(tmpargs);
|
2023-08-18 00:15:35 +00:00
|
|
|
}
|
2023-08-17 20:50:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-21 15:19:25 +00:00
|
|
|
void fmt_start(struct format_ctx *fctx, const struct rowspec *spec, int width,
|
|
|
|
int indent)
|
|
|
|
{
|
2020-12-16 17:41:31 +00:00
|
|
|
memset(fctx, 0, sizeof(*fctx));
|
2019-06-21 15:19:25 +00:00
|
|
|
fctx->width = width;
|
|
|
|
fctx->indent = indent;
|
|
|
|
fctx->rowspec = spec;
|
|
|
|
fctx->depth = 1;
|
|
|
|
|
|
|
|
if (bconf.output_format & CMD_FORMAT_JSON) {
|
|
|
|
putchar('{');
|
|
|
|
/* The top level is a map and is the first one */
|
|
|
|
fctx->jtype[fctx->depth] = JSON_TYPE_MAP;
|
|
|
|
fctx->memb[fctx->depth] = 0;
|
|
|
|
fmt_print_start_group(fctx, "__header", JSON_TYPE_MAP);
|
|
|
|
fmt_separator(fctx);
|
|
|
|
printf("\"version\": \"1\"");
|
|
|
|
fctx->memb[fctx->depth] = 1;
|
|
|
|
fmt_print_end_group(fctx, "__header");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fmt_end(struct format_ctx *fctx)
|
|
|
|
{
|
|
|
|
if (fctx->depth != 1)
|
|
|
|
fprintf(stderr, "WARNING: wrong nesting\n");
|
|
|
|
|
|
|
|
/* Close, no continuation to print */
|
2021-02-22 15:22:21 +00:00
|
|
|
if (bconf.output_format & CMD_FORMAT_JSON) {
|
2019-06-21 15:19:25 +00:00
|
|
|
fmt_dec_depth(fctx);
|
|
|
|
fmt_separator(fctx);
|
|
|
|
printf("}\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fmt_start_list_value(struct format_ctx *fctx)
|
|
|
|
{
|
|
|
|
if (bconf.output_format == CMD_FORMAT_TEXT) {
|
|
|
|
fmt_indent1(fctx->indent);
|
|
|
|
} else if (bconf.output_format == CMD_FORMAT_JSON) {
|
|
|
|
fmt_separator(fctx);
|
|
|
|
fmt_indent2(fctx->depth);
|
|
|
|
putchar('"');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fmt_end_list_value(struct format_ctx *fctx)
|
|
|
|
{
|
|
|
|
if (bconf.output_format == CMD_FORMAT_TEXT)
|
|
|
|
putchar('\n');
|
|
|
|
else if (bconf.output_format == CMD_FORMAT_JSON)
|
|
|
|
putchar('"');
|
|
|
|
}
|
|
|
|
|
|
|
|
void fmt_start_value(struct format_ctx *fctx, const struct rowspec *row)
|
|
|
|
{
|
|
|
|
if (bconf.output_format == CMD_FORMAT_TEXT) {
|
|
|
|
if (strcmp(row->fmt, "list") == 0)
|
|
|
|
putchar('\n');
|
|
|
|
else if (strcmp(row->fmt, "map") == 0)
|
|
|
|
putchar('\n');
|
|
|
|
} else if (bconf.output_format == CMD_FORMAT_JSON) {
|
|
|
|
if (strcmp(row->fmt, "list") == 0) {
|
|
|
|
} else if (strcmp(row->fmt, "map") == 0) {
|
2023-08-17 20:50:41 +00:00
|
|
|
} else if (fctx->unquoted) {
|
2019-06-21 15:19:25 +00:00
|
|
|
} else {
|
|
|
|
putchar('"');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 15:22:21 +00:00
|
|
|
/*
|
|
|
|
* Newline depends on format type:
|
|
|
|
* - json does delayed continuation "," in case there's a following object
|
|
|
|
* - plain text always ends with a newline
|
|
|
|
*/
|
2019-06-21 15:19:25 +00:00
|
|
|
void fmt_end_value(struct format_ctx *fctx, const struct rowspec *row)
|
|
|
|
{
|
2021-02-22 15:22:21 +00:00
|
|
|
if (bconf.output_format == CMD_FORMAT_TEXT)
|
|
|
|
putchar('\n');
|
2019-06-21 15:19:25 +00:00
|
|
|
if (bconf.output_format == CMD_FORMAT_JSON) {
|
|
|
|
if (strcmp(row->fmt, "list") == 0) {
|
|
|
|
} else if (strcmp(row->fmt, "map") == 0) {
|
2023-08-17 20:50:41 +00:00
|
|
|
} else if (fctx->unquoted) {
|
2019-06-21 15:19:25 +00:00
|
|
|
} else {
|
|
|
|
putchar('"');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fmt_print_start_group(struct format_ctx *fctx, const char *name,
|
|
|
|
enum json_type jtype)
|
|
|
|
{
|
|
|
|
if (bconf.output_format == CMD_FORMAT_JSON) {
|
|
|
|
fmt_separator(fctx);
|
|
|
|
fmt_inc_depth(fctx);
|
|
|
|
fctx->jtype[fctx->depth] = jtype;
|
|
|
|
fctx->memb[fctx->depth] = 0;
|
2020-12-11 16:48:11 +00:00
|
|
|
if (name)
|
|
|
|
printf("\"%s\": ", name);
|
2019-06-21 15:19:25 +00:00
|
|
|
if (jtype == JSON_TYPE_MAP)
|
2020-12-11 16:48:11 +00:00
|
|
|
putchar('{');
|
2019-06-21 15:19:25 +00:00
|
|
|
else if (jtype == JSON_TYPE_ARRAY)
|
2020-12-11 16:48:11 +00:00
|
|
|
putchar('[');
|
2019-06-21 15:19:25 +00:00
|
|
|
else
|
|
|
|
fmt_error(fctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fmt_print_end_group(struct format_ctx *fctx, const char *name)
|
|
|
|
{
|
|
|
|
if (bconf.output_format == CMD_FORMAT_JSON) {
|
|
|
|
/* Whatever was on previous line won't continue with "," */
|
|
|
|
const enum json_type jtype = fctx->jtype[fctx->depth];
|
|
|
|
|
|
|
|
fmt_dec_depth(fctx);
|
|
|
|
putchar('\n');
|
|
|
|
fmt_indent2(fctx->depth);
|
|
|
|
if (jtype == JSON_TYPE_MAP)
|
|
|
|
putchar('}');
|
|
|
|
else if (jtype == JSON_TYPE_ARRAY)
|
|
|
|
putchar(']');
|
|
|
|
else
|
|
|
|
fmt_error(fctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use rowspec to print according to currently set output format */
|
|
|
|
void fmt_print(struct format_ctx *fctx, const char* key, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
const struct rowspec *row;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
va_start(args, key);
|
|
|
|
row = &fctx->rowspec[0];
|
|
|
|
|
|
|
|
while (row->key) {
|
|
|
|
if (strcmp(key, row->key) == 0) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
row++;
|
|
|
|
}
|
|
|
|
if (!found) {
|
2022-09-27 22:52:00 +00:00
|
|
|
internal_error("unknown key: %s", key);
|
2019-06-21 15:19:25 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bconf.output_format == CMD_FORMAT_TEXT) {
|
|
|
|
const bool print_colon = row->out_text[0];
|
|
|
|
int len;
|
|
|
|
|
2021-02-22 15:22:21 +00:00
|
|
|
/* Print indented key name */
|
2019-06-21 15:19:25 +00:00
|
|
|
fmt_indent1(fctx->indent);
|
|
|
|
len = strlen(row->out_text);
|
|
|
|
|
|
|
|
printf("%s", row->out_text);
|
|
|
|
if (print_colon) {
|
|
|
|
putchar(':');
|
|
|
|
len++;
|
|
|
|
}
|
2021-02-22 15:22:21 +00:00
|
|
|
/* Align start for the value */
|
2019-06-21 15:19:25 +00:00
|
|
|
fmt_indent1(fctx->width - len);
|
|
|
|
} else if (bconf.output_format == CMD_FORMAT_JSON) {
|
|
|
|
if (strcmp(row->fmt, "list") == 0) {
|
|
|
|
fmt_print_start_group(fctx, row->out_json,
|
|
|
|
JSON_TYPE_ARRAY);
|
|
|
|
} else if (strcmp(row->fmt, "map") == 0) {
|
|
|
|
fmt_print_start_group(fctx, row->out_json,
|
|
|
|
JSON_TYPE_MAP);
|
|
|
|
} else {
|
|
|
|
/* Simple key/values */
|
|
|
|
fmt_separator(fctx);
|
btrfs-progs: qgroup show: add json output
Support json for 'qgroup show' with values printed by "btrfs qgroup
-pcre", the accounted size and the limits. It's implemented as a
separate call and not sharing the printing routines so any visible
changes need to by synchronized.
Formatter updates: don't print key name if .out_json is NULL.
Example output:
# btrfs --format json qgroup show /mnt/path
{
"__header": {
"version": "1"
},
"qgroup-show": [
{
"qgroupid": "0/5",
"referenced": "8831393792",
"max_referenced": "none",
"exclusive": "8224075776",
"max_exclusive": "none",
"path": "",
"parents": [
],
"children": [
]
},
{
"qgroupid": "0/361",
"referenced": "611459072",
"max_referenced": "none",
"exclusive": "65536",
"max_exclusive": "none",
"path": "subv1",
"parents": [
"1/1"
],
"children": [
]
},
{
"qgroupid": "0/362",
"referenced": "611459072",
"max_referenced": "none",
"exclusive": "65536",
"max_exclusive": "none",
"path": "snap1-r",
"parents": [
],
"children": [
]
},
{
"qgroupid": "1/1",
"referenced": "611459072",
"max_referenced": "none",
"exclusive": "65536",
"max_exclusive": "none",
"path": "",
"parents": [
],
"children": [
"0/361"
]
}
]
}
Issue: #555
Signed-off-by: David Sterba <dsterba@suse.com>
2022-12-07 17:20:50 +00:00
|
|
|
if (row->out_json)
|
|
|
|
printf("\"%s\": ", row->out_json);
|
2019-06-21 15:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 00:15:35 +00:00
|
|
|
fctx->unquoted = fmt_set_unquoted(fctx, row, args);
|
2019-06-21 15:19:25 +00:00
|
|
|
fmt_start_value(fctx, row);
|
|
|
|
|
|
|
|
if (row->fmt[0] == '%') {
|
|
|
|
vprintf(row->fmt, args);
|
2023-08-17 21:36:23 +00:00
|
|
|
} else if (strcmp(row->fmt, "bool") == 0) {
|
|
|
|
/* Bool is passed as int to varargs */
|
|
|
|
bool value = va_arg(args, int);
|
|
|
|
|
|
|
|
printf("%s", value ? "true" : "false");
|
2022-12-15 23:40:00 +00:00
|
|
|
} else if (strcmp(row->fmt, "str") == 0) {
|
|
|
|
const char *str = va_arg(args, const char *);
|
|
|
|
|
|
|
|
print_escaped(str);
|
2019-06-21 15:19:25 +00:00
|
|
|
} else if (strcmp(row->fmt, "uuid") == 0) {
|
|
|
|
const u8 *uuid = va_arg(args, const u8*);
|
|
|
|
|
|
|
|
print_uuid(uuid);
|
2023-08-17 23:39:32 +00:00
|
|
|
} else if (strcmp(row->fmt, "date-time") == 0) {
|
2019-06-21 15:19:25 +00:00
|
|
|
const time_t ts = va_arg(args, time_t);
|
|
|
|
|
|
|
|
if (ts) {
|
|
|
|
char tstr[256];
|
|
|
|
struct tm tm;
|
|
|
|
|
|
|
|
localtime_r(&ts, &tm);
|
|
|
|
strftime(tstr, 256, "%Y-%m-%d %X %z", &tm);
|
2019-07-13 23:14:54 +00:00
|
|
|
printf("%s", tstr);
|
2019-06-21 15:19:25 +00:00
|
|
|
} else {
|
|
|
|
putchar('-');
|
|
|
|
}
|
|
|
|
} else if (strcmp(row->fmt, "list") == 0) {
|
|
|
|
} else if (strcmp(row->fmt, "map") == 0) {
|
|
|
|
} else if (strcmp(row->fmt, "qgroupid") == 0) {
|
2023-03-14 16:21:52 +00:00
|
|
|
/*
|
|
|
|
* Level is u16 but promoted to int when it's a vararg, callers
|
|
|
|
* should add explicit cast.
|
|
|
|
*/
|
|
|
|
const int level = va_arg(args, int);
|
2019-06-21 15:19:25 +00:00
|
|
|
const u64 id = va_arg(args, u64);
|
|
|
|
|
2023-03-14 16:21:52 +00:00
|
|
|
printf("%hu/%llu", level, id);
|
2019-06-21 15:19:25 +00:00
|
|
|
} else if (strcmp(row->fmt, "size-or-none") == 0) {
|
|
|
|
const u64 size = va_arg(args, u64);
|
|
|
|
const unsigned int unit_mode = va_arg(args, unsigned int);
|
|
|
|
|
|
|
|
if (size)
|
|
|
|
printf("%s", pretty_size_mode(size, unit_mode));
|
|
|
|
else
|
|
|
|
putchar('-');
|
|
|
|
} else if (strcmp(row->fmt, "size") == 0) {
|
|
|
|
const u64 size = va_arg(args, u64);
|
|
|
|
const unsigned int unit_mode = va_arg(args, unsigned int);
|
|
|
|
|
|
|
|
printf("%s", pretty_size_mode(size, unit_mode));
|
|
|
|
} else {
|
2022-09-27 22:52:00 +00:00
|
|
|
internal_error("unknown format %s", row->fmt);
|
2019-06-21 15:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt_end_value(fctx, row);
|
|
|
|
va_end(args);
|
|
|
|
}
|