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;
}
}
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;
#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"
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;
+}