smbstatus: add a basic byte-range locks dictionary
authorJule Anger <janger@samba.org>
Thu, 31 Mar 2022 08:30:30 +0000 (10:30 +0200)
committerJule Anger <janger@samba.org>
Mon, 8 Aug 2022 12:56:29 +0000 (12:56 +0000)
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 <janger@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
source3/utils/status.c
source3/utils/status_json.c
source3/utils/status_json.h
source3/utils/status_json_dummy.c

index 13ba3811736e0cd17a849f84c0f4cc4d5d69a863..2b2fab672579d481e3aafe995f968ec63f89eed5 100644 (file)
@@ -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);
index d100b10443916d857d33214760e632839ff3ce6e..a13eb47a53bcc24543ae44c9f1f6693328758165 100644 (file)
@@ -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;
+}
index 1f23dc1d62bbcfffd485c9976f094fb6c2e812ca..2a4f5335d652c134c89f65c1a4fe22c9972c75a1 100644 (file)
@@ -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
index c92fbb493726e5aed68b9fa6478f59b2ae67d39e..ad8e70c9c6feba3af2d201dacc72abaadf12b7cb 100644 (file)
@@ -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;
+}