From dc3b10cda6839f6ff21bec2c39518c0dd2c83efe Mon Sep 17 00:00:00 2001 From: Jule Anger Date: Thu, 31 Mar 2022 10:30:30 +0200 Subject: [PATCH] smbstatus: add a basic byte-range locks dictionary Adds an empty json dictionary under the key "byte_range_locks" and adds foreach locked file a dictionary with information (path and filename) to the byte-range locks dictionary. Only print to stdout, if json_output is not set. Signed-off-by: Jule Anger Reviewed-by: Ralph Boehme --- source3/utils/status.c | 32 +++++++++++------ source3/utils/status_json.c | 59 +++++++++++++++++++++++++++++++ source3/utils/status_json.h | 4 +++ source3/utils/status_json_dummy.c | 7 ++++ 4 files changed, 91 insertions(+), 11 deletions(-) diff --git a/source3/utils/status.c b/source3/utils/status.c index 13ba3811736..2b2fab67257 100644 --- a/source3/utils/status.c +++ b/source3/utils/status.c @@ -349,9 +349,12 @@ static void print_brl_stdout(struct traverse_state *state, static int prepare_brl(struct traverse_state *state) { - /* only print header line if there are locked files */ - state->first = true; - + if (!state->json_output) { + /* only print header line if there are locked files */ + state->first = true; + } else { + add_section_to_json(state, "byte_range_locks"); + } return 0; } @@ -397,14 +400,21 @@ static void print_brl(struct file_id id, } } - print_brl_stdout(state, - server_id_str_buf(pid, &tmp), - file_id_str_buf(id, &ftmp), - desc, - (intmax_t)start, - (intmax_t)size, - sharepath, - fname); + if (!state->json_output) { + print_brl_stdout(state, + server_id_str_buf(pid, &tmp), + file_id_str_buf(id, &ftmp), + desc, + (intmax_t)start, + (intmax_t)size, + sharepath, + fname); + } else { + print_brl_json(state, + sharepath, + fname); + + } TALLOC_FREE(fname); TALLOC_FREE(share_mode); diff --git a/source3/utils/status_json.c b/source3/utils/status_json.c index d100b104439..a13eb47a53b 100644 --- a/source3/utils/status_json.c +++ b/source3/utils/status_json.c @@ -964,3 +964,62 @@ failure: TALLOC_FREE(tmp_ctx); return -1; } + +int print_brl_json(struct traverse_state *state, + const char *sharepath, + const char *filename) +{ + struct json_object file_json; + struct json_object brl_json; + int result = 0; + char *key; + + TALLOC_CTX *tmp_ctx = talloc_stackframe(); + if (tmp_ctx == NULL) { + return -1; + } + + if (sharepath[strlen(sharepath)-1] == '/') { + key = talloc_asprintf(tmp_ctx, "%s%s", sharepath, filename); + } else { + key = talloc_asprintf(tmp_ctx, "%s/%s", sharepath, filename); + } + if (key == NULL) { + goto failure; + } + + brl_json = json_get_object(&state->root_json, "byte_range_locks"); + if (json_is_invalid(&brl_json)) { + goto failure; + } + file_json = json_get_object(&brl_json, key); + if (json_is_invalid(&file_json)) { + goto failure; + } + + result = json_add_string(&file_json, "file_name", filename); + if (result < 0) { + goto failure; + } + result = json_add_string(&file_json, "share_path", sharepath); + if (result < 0) { + goto failure; + } + + result = json_add_object(&brl_json, key, &file_json); + if (result < 0) { + goto failure; + } + result = json_update_object(&state->root_json, "byte_range_locks", &brl_json); + if (result < 0) { + goto failure; + } + + TALLOC_FREE(tmp_ctx); + return 0; +failure: + json_free(&file_json); + json_free(&brl_json); + TALLOC_FREE(tmp_ctx); + return -1; +} diff --git a/source3/utils/status_json.h b/source3/utils/status_json.h index 1f23dc1d62b..2a4f5335d65 100644 --- a/source3/utils/status_json.h +++ b/source3/utils/status_json.h @@ -53,4 +53,8 @@ int print_share_mode_json(struct traverse_state *state, uint32_t lease_type, const char *filename); +int print_brl_json(struct traverse_state *state, + const char *sharepath, + const char *filename); + #endif diff --git a/source3/utils/status_json_dummy.c b/source3/utils/status_json_dummy.c index c92fbb49372..ad8e70c9c6f 100644 --- a/source3/utils/status_json_dummy.c +++ b/source3/utils/status_json_dummy.c @@ -69,3 +69,10 @@ int print_share_mode_json(struct traverse_state *state, { return 0; } + +int print_brl_json(struct traverse_state *state, + const char *sharepath, + const char *filename) +{ + return 0; +} -- 2.47.3