<commit_before>/*
 * This file is part of buxton.
 *
 * Copyright (C) 2013 Intel Corporation
 *
 * buxton is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 */

#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif

#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <attr/xattr.h>

#include "daemon.h"
#include "log.h"
#include "smack.h"
#include "util.h"

bool parse_list(BuxtonControlMessage msg, size_t count, BuxtonData *list,
		       BuxtonString **key, BuxtonString **layer, BuxtonData **value)
{
	switch (msg) {
	case BUXTON_CONTROL_SET:
		if (count != 3)
			return false;
		if (list[0].type != STRING || list[1].type != STRING)
			return false;
		*layer = &(list[0].store.d_string);
		*key = &(list[1].store.d_string);
		*value = &(list[2]);
		break;
	case BUXTON_CONTROL_GET:
		if (count == 2) {
			if(list[0].type != STRING || list[1].type != STRING)
				return false;
			*layer = &(list[0].store.d_string);
			*key = &(list[1].store.d_string);
		} else if (count == 1) {
			if (list[0].type != STRING)
				return false;
			*key = &(list[0].store.d_string);
		} else {
			return false;
		}
		break;
	case BUXTON_CONTROL_UNSET:
		if (count != 2)
			return false;
		if (list[0].type != STRING || list[1].type != STRING)
			return false;
		*layer = &(list[0].store.d_string);
		*key = &(list[1].store.d_string);
		break;
	case BUXTON_CONTROL_NOTIFY:
		if (count != 1)
			return false;
		if (list[0].type != STRING)
			return false;
		*key = &(list[0].store.d_string);
		break;
	default:
		return false;
		break;
	}

	return true;
}

bool bt_daemon_handle_message(BuxtonDaemon *self, client_list_item *client, size_t size)
{
	BuxtonControlMessage msg;
	BuxtonStatus response;
	BuxtonData *list = NULL;
	_cleanup_buxton_data_ BuxtonData *data = NULL;
	int i;
	size_t p_count;
	size_t response_len;
	BuxtonData response_data;
	BuxtonData *value = NULL;
	BuxtonString *key = NULL;
	BuxtonString *layer = NULL;
	_cleanup_free_ uint8_t *response_store = NULL;
	uid_t uid;
	bool ret = false;

	assert(self);
	assert(client);

	uid = self->buxton.client.uid;
	p_count = buxton_deserialize_message((uint8_t*)client->data, &msg, size, &list);
	if (p_count == 0) {
		/* Todo: terminate the client due to invalid message */
		buxton_debug("Failed to deserialize message\n");
		goto end;
	}

	/* Check valid range */
	if (msg < BUXTON_CONTROL_SET || msg > BUXTON_CONTROL_MAX)
		goto end;

	if (!parse_list(msg, p_count, list, &key, &layer, &value))
		goto end;

	/* use internal function from bt-daemon */
	switch (msg) {
	case BUXTON_CONTROL_SET:
		set_value(self, client, layer, key, value, &response);
		break;
	case BUXTON_CONTROL_GET:
		data = get_value(self, client, layer, key, &response);
		break;
	case BUXTON_CONTROL_UNSET:
		unset_value(self, client, layer, key, &response);
		break;
	case BUXTON_CONTROL_NOTIFY:
		register_notification(self, client, key, &response);
		break;
	default:
		goto end;
	}
	/* Set a response code */
	response_data.type = INT32;
	response_data.store.d_int32 = response;
	response_data.label = buxton_string_pack("dummy");

	/* Prepare a data response */
	if (data) {
		/* Get response */
		response_len = buxton_serialize_message(&response_store, BUXTON_CONTROL_STATUS, 2, &response_data, data);
		if (response_len == 0) {
			buxton_log("Failed to serialize 2 parameter response message\n");
			goto end;
		}
	} else {
		/* Set response */
		response_len = buxton_serialize_message(&response_store, BUXTON_CONTROL_STATUS, 1, &response_data);
		if (response_len == 0) {
			buxton_log("Failed to serialize single parameter response message\n");
			goto end;
		}
	}

	/* Now write the response */
	write(client->fd, response_store, response_len);
	ret = true;

end:
	/* Restore our own UID */
	self->buxton.client.uid = uid;
	if (list) {
		for (i=0; i < p_count; i++) {
			free(list[i].label.value);
			if (list[i].type == STRING)<commit_msg>core: Handle responses per message type<commit_after>/*
 * This file is part of buxton.
 *
 * Copyright (C) 2013 Intel Corporation
 *
 * buxton is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 */

#ifdef HAVE_CONFIG_H
    #include "config.h"
#endif

#include <assert.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <attr/xattr.h>

#include "daemon.h"
#include "log.h"
#include "smack.h"
#include "util.h"

bool parse_list(BuxtonControlMessage msg, size_t count, BuxtonData *list,
		       BuxtonString **key, BuxtonString **layer, BuxtonData **value)
{
	switch (msg) {
	case BUXTON_CONTROL_SET:
		if (count != 3)
			return false;
		if (list[0].type != STRING || list[1].type != STRING)
			return false;
		*layer = &(list[0].store.d_string);
		*key = &(list[1].store.d_string);
		*value = &(list[2]);
		break;
	case BUXTON_CONTROL_GET:
		if (count == 2) {
			if(list[0].type != STRING || list[1].type != STRING)
				return false;
			*layer = &(list[0].store.d_string);
			*key = &(list[1].store.d_string);
		} else if (count == 1) {
			if (list[0].type != STRING)
				return false;
			*key = &(list[0].store.d_string);
		} else {
			return false;
		}
		break;
	case BUXTON_CONTROL_UNSET:
		if (count != 2)
			return false;
		if (list[0].type != STRING || list[1].type != STRING)
			return false;
		*layer = &(list[0].store.d_string);
		*key = &(list[1].store.d_string);
		break;
	case BUXTON_CONTROL_NOTIFY:
		if (count != 1)
			return false;
		if (list[0].type != STRING)
			return false;
		*key = &(list[0].store.d_string);
		break;
	default:
		return false;
		break;
	}

	return true;
}

bool bt_daemon_handle_message(BuxtonDaemon *self, client_list_item *client, size_t size)
{
	BuxtonControlMessage msg;
	BuxtonStatus response;
	BuxtonData *list = NULL;
	_cleanup_buxton_data_ BuxtonData *data = NULL;
	int i;
	size_t p_count;
	size_t response_len;
	BuxtonData response_data;
	BuxtonData *value = NULL;
	BuxtonString *key = NULL;
	BuxtonString *layer = NULL;
	_cleanup_free_ uint8_t *response_store = NULL;
	uid_t uid;
	bool ret = false;

	assert(self);
	assert(client);

	uid = self->buxton.client.uid;
	p_count = buxton_deserialize_message((uint8_t*)client->data, &msg, size, &list);
	if (p_count == 0) {
		/* Todo: terminate the client due to invalid message */
		buxton_debug("Failed to deserialize message\n");
		goto end;
	}

	/* Check valid range */
	if (msg < BUXTON_CONTROL_SET || msg > BUXTON_CONTROL_MAX)
		goto end;

	if (!parse_list(msg, p_count, list, &key, &layer, &value))
		goto end;

	/* use internal function from bt-daemon */
	switch (msg) {
	case BUXTON_CONTROL_SET:
		set_value(self, client, layer, key, value, &response);
		break;
	case BUXTON_CONTROL_GET:
		data = get_value(self, client, layer, key, &response);
		break;
	case BUXTON_CONTROL_UNSET:
		unset_value(self, client, layer, key, &response);
		break;
	case BUXTON_CONTROL_NOTIFY:
		register_notification(self, client, key, &response);
		break;
	default:
		goto end;
	}
	/* Set a response code */
	response_data.type = INT32;
	response_data.store.d_int32 = response;
	response_data.label = buxton_string_pack("dummy");

	switch (msg) {
	case BUXTON_CONTROL_SET:
		response_len = buxton_serialize_message(&response_store, BUXTON_CONTROL_STATUS, 1, &response_data);
		if (response_len == 0) {
			buxton_log("Failed to serialize set response message\n");
			goto end;
		}
		break;
	case BUXTON_CONTROL_GET:
		response_len = buxton_serialize_message(&response_store, BUXTON_CONTROL_STATUS, 2, &response_data, data);
		if (response_len == 0) {
			buxton_log("Failed to serialize get response message\n");
			goto end;
		}
		break;
	case BUXTON_CONTROL_UNSET:
		response_len = buxton_serialize_message(&response_store, BUXTON_CONTROL_STATUS, 1, &response_data);
		if (response_len == 0) {
			buxton_log("Failed to serialize unset response message\n");
			goto end;
		}
		break;
	case BUXTON_CONTROL_NOTIFY:
		response_len = buxton_serialize_message(&response_store, BUXTON_CONTROL_STATUS, 1, &response_data);
		if (response_len == 0) {
			buxton_log("Failed to serialize notify response message\n");
			goto end;
		}
		break;
	default:
		goto end;
	}

	/* Now write the response */
	write(client->fd, response_store, response_len);
	ret = true;

end:
	/* Restore our own UID */
	self->buxton.client.uid = uid;
	if (list) {
		for (i=0; i < p_count; i++) {
			free(list[i].label.value);
			if (list[i].type == STRING)
{'fraction_non_alphanumeric': 0.13447208529175741, 'fraction_numerical': 0.00974435400664909, 'mean_word_length': 4.116715542521995, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1022416', 'n_tokens_mistral': 3490, 'n_tokens_neox': 3199, 'n_words': 834}