]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
smbstatus: add a basic dictionary with open files
authorJule Anger <janger@samba.org>
Wed, 30 Mar 2022 13:11:11 +0000 (15:11 +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 "open_files" and adds foreach
locked file a dictionary with information (path, filename and pending
deletes) to the locked files dictionary. Uses path and filename as key.

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 007f97bf7bb2543ae0a28c25e49f4c509593451f..1b9729e562a782f32496e1e67d98f9ea311171b4 100644 (file)
@@ -155,9 +155,12 @@ static int print_share_mode_stdout(struct traverse_state *state,
 
 static int prepare_share_mode(struct traverse_state *state)
 {
-       /* only print header line if there are open files */
-       state->first = true;
-
+       if (!state->json_output) {
+               /* only print header line if there are open files */
+               state->first = true;
+       } else {
+               add_section_to_json(state, "open_files");
+       }
        return 0;
 }
 
@@ -297,16 +300,23 @@ static int print_share_mode(struct file_id fid,
                }
 
                timestr = time_to_asc((time_t)e->time.tv_sec);
-               print_share_mode_stdout(state,
-                                       pid,
-                                       user_str,
-                                       denymode,
-                                       (unsigned int)e->access_mask,
-                                       rw,
-                                       oplock,
-                                       d->servicepath,
-                                       filename,
-                                       timestr);
+
+               if (!state->json_output) {
+                       print_share_mode_stdout(state,
+                                               pid,
+                                               user_str,
+                                               denymode,
+                                               (unsigned int)e->access_mask,
+                                               rw,
+                                               oplock,
+                                               d->servicepath,
+                                               filename,
+                                               timestr);
+               } else {
+                       print_share_mode_json(state,
+                                             d,
+                                             filename);
+               }
        }
        TALLOC_FREE(tmp_ctx);
        return 0;
index 5ad91094b378097392d4adf9efa4a8cf793be1f0..a11024e7d4b32c8a1025c2639435dacc301baffc 100644 (file)
@@ -22,6 +22,7 @@
 #include "lib/util/time_basic.h"
 #include "conn_tdb.h"
 #include "session.h"
+#include "librpc/gen_ndr/open_files.h"
 #include "status_json.h"
 #include "../libcli/security/security.h"
 #include "status.h"
@@ -358,3 +359,63 @@ failure:
        TALLOC_FREE(tmp_ctx);
        return -1;
 }
+
+int print_share_mode_json(struct traverse_state *state,
+                         const struct share_mode_data *d,
+                         const char *filename)
+{
+       struct json_object locks_json;
+       struct json_object file_json;
+       char *key = NULL;
+       int result = 0;
+
+       TALLOC_CTX *tmp_ctx = talloc_stackframe();
+       if (tmp_ctx == NULL) {
+               return -1;
+       }
+
+       if (d->servicepath[strlen(d->servicepath)-1] == '/') {
+               key = talloc_asprintf(tmp_ctx, "%s%s", d->servicepath, filename);
+       } else {
+               key = talloc_asprintf(tmp_ctx, "%s/%s", d->servicepath, filename);
+       }
+
+       locks_json = json_get_object(&state->root_json, "open_files");
+       if (json_is_invalid(&locks_json)) {
+               goto failure;
+       }
+       file_json = json_get_object(&locks_json, key);
+       if (json_is_invalid(&file_json)) {
+               goto failure;
+       }
+
+       result = json_add_string(&file_json, "service_path", d->servicepath);
+       if (result < 0) {
+               goto failure;
+       }
+       result = json_add_string(&file_json, "filename", filename);
+       if (result < 0) {
+               goto failure;
+       }
+       result = json_add_int(&file_json, "num_pending_deletes", d->num_delete_tokens);
+       if (result < 0) {
+               goto failure;
+       }
+
+       result = json_update_object(&locks_json, key, &file_json);
+       if (result < 0) {
+               goto failure;
+       }
+       result = json_update_object(&state->root_json, "open_files", &locks_json);
+       if (result < 0) {
+               goto failure;
+       }
+
+       TALLOC_FREE(tmp_ctx);
+       return 0;
+failure:
+       json_free(&file_json);
+       json_free(&locks_json);
+       TALLOC_FREE(tmp_ctx);
+       return -1;
+}
index 72fa9f98fd06c7935059637f70c3ee0407c7c1a5..44b339914ac6870ef10e0a3db8ebe8e474b0369c 100644 (file)
@@ -44,4 +44,8 @@ int traverse_sessionid_json(struct traverse_state *state,
                            enum crypto_degree signing_degree,
                            const char *connection_dialect);
 
+int print_share_mode_json(struct traverse_state *state,
+                         const struct share_mode_data *d,
+                         const char *filename);
+
 #endif
index 6530b2f8f079b703b3d3fba082744b4dd036ad24..96b6bc6b569277589b701daed36a5d569907087c 100644 (file)
@@ -57,3 +57,10 @@ int traverse_sessionid_json(struct traverse_state *state,
 {
        return 0;
 }
+
+int print_share_mode_json(struct traverse_state *state,
+                         const struct share_mode_data *d,
+                         const char *filename)
+{
+       return 0;
+}