Esempio n. 1
0
int testRemoveCabeca() {
	int result;
	struct list_t *list;
	struct entry_t *e1, *e2, *e3, *entry;
	struct data_t *data;

	printf("Módulo list -> teste remover cabeça:");

	if ((list = list_create()) == NULL)
		error(1, errno, "  O teste não pode prosseguir");

	if ((data = data_create(5)) == NULL)
		error(1, errno, "  O teste não pode prosseguir");

	e1 = entry_create("abc", data);
	e2 = entry_create("def", data);
	e3 = entry_create("ghi", data);

	if (e1 == NULL || e2 == NULL || e3 == NULL)
		error(1, errno, "  O teste não pode prosseguir");

	memcpy(e1->value->data, "abc1", 5);
	memcpy(e2->value->data, "def1", 5);
	memcpy(e3->value->data, "ghi1", 5);

	list_add(list,e1);
	list_add(list,e2);
	list_add(list,e3);

	assert(list_remove(NULL, "abc") < 0);
	result = (list_remove(NULL, "abc") < 0);

	assert(list_remove(list, NULL) < 0);
	result = result && (list_remove(list, NULL) < 0);

	result = result &&
		 list_remove(list, "abc") == 0 &&
		 list_size(list) == 2;

	entry = list_get(list, "def");
	result = result &&
		 entry != e2 &&
		 strcmp(entry->key, e2->key) == 0;

	entry = list_get(list, "ghi");
	result = result &&
		 entry != e3 &&
		 strcmp(entry->key, e3->key) == 0;

	list_destroy(list);
	entry_destroy(e1);
	entry_destroy(e2);
	entry_destroy(e3);
	data_destroy(data);

	printf(" %s\n", result ? "passou" : "não passou");
	return result;
}
Esempio n. 2
0
void block_destroy(block_t *l)
{
	entry_t *e;

	while (l->_head->forward[0]) {
		e = l->_head->forward[0]->forward[0];
		entry_destroy(l->_head->forward[0]);
		l->_head->forward[0] = e;
	}
	entry_destroy(l->_head);
	block_free(l);
}
Esempio n. 3
0
/******************************************************
 Create a new character based on the specified template
 return the id of the newly created character
 the returned string MUST BE FREED by caller
 return nullptr if fails
 *******************************************************/
char * character_create_from_template(context_t * ctx, const char * my_template,
		const char * map, int layer, int x, int y)
{
	char * new_id;

	new_id = file_new(CHARACTER_TABLE, nullptr);
	if (file_copy(CHARACTER_TEMPLATE_TABLE, my_template, CHARACTER_TABLE,
			new_id) == false)
	{
		file_delete(CHARACTER_TABLE, new_id);
		return nullptr;
	}

	// Check if new character is allowed to be created here
	if (map_check_tile(ctx, new_id, map, layer, x, y) == 0)
	{
		entry_destroy(CHARACTER_TABLE, new_id);
		file_delete(CHARACTER_TABLE, new_id);
		free(new_id);
		return nullptr;
	}

	// Write position
	if (entry_write_string(CHARACTER_TABLE, new_id, map, CHARACTER_KEY_MAP,
			nullptr) == RET_NOK)
	{
		entry_destroy(CHARACTER_TABLE, new_id);
		file_delete(CHARACTER_TABLE, new_id);
		free(new_id);
		return nullptr;
	}

	if (entry_write_int(CHARACTER_TABLE, new_id, x, CHARACTER_KEY_POS_X,
			nullptr) == RET_NOK)
	{
		entry_destroy(CHARACTER_TABLE, new_id);
		file_delete(CHARACTER_TABLE, new_id);
		free(new_id);
		return nullptr;
	}

	if (entry_write_int(CHARACTER_TABLE, new_id, y, CHARACTER_KEY_POS_Y,
			nullptr) == RET_NOK)
	{
		entry_destroy(CHARACTER_TABLE, new_id);
		file_delete(CHARACTER_TABLE, new_id);
		free(new_id);
		return nullptr;
	}

	return new_id;
}
Esempio n. 4
0
int testGetKeys() {
	int result;
	struct list_t *list;
	struct entry_t *e1, *e2, *e3;
	struct data_t *data;
	char **keys;

	printf("Módulo list -> teste busca chaves:");

	if ((list = list_create()) == NULL)
		error(1, errno, "  O teste não pode prosseguir");

	if ((data = data_create(5)) == NULL)
		error(1, errno, "  O teste não pode prosseguir");

	assert(list_get_keys(NULL) == NULL);
	result = (list_get_keys(NULL) == NULL);

	e1 = entry_create("abc", data);
	e2 = entry_create("def", data);
	e3 = entry_create("ghi", data);

	if (e1 == NULL || e2 == NULL || e3 == NULL)
		error(1, errno, "  O teste não pode prosseguir");

	memcpy(e1->value->data, "abc1", 5);
	memcpy(e2->value->data, "def1", 5);
	memcpy(e3->value->data, "ghi1", 5);

	list_add(list,e1);
	list_add(list,e2);
	list_add(list,e3);

	if ((keys = list_get_keys(list)) == NULL)
		error(1, errno, "  O teste não pode prosseguir");

	result = strcmp(keys[0], e1->key) == 0 && keys[0] != e1->key &&
                 strcmp(keys[1], e2->key) == 0 && keys[1] != e2->key && 
                 strcmp(keys[2], e3->key) == 0 && keys[2] != e3->key && 
                 keys[3] == NULL;

	list_free_keys(keys);
	list_destroy(list);
	entry_destroy(e1);
	entry_destroy(e2);
	entry_destroy(e3);
	data_destroy(data);

	printf(" %s\n", result ? "passou" : "não passou");
	return result;
}
Esempio n. 5
0
static void bucket_erase_existed( bucket_t *bucket, const hashmap_key_t key, unsigned int * const existed )
{
	entry_t *prev;
	entry_t *iter;

	assert( NULL != bucket );
	assert( NULL != key );
	assert( NULL != existed );

	iter = bucket_find_entry( bucket, key, &prev );

	if( NULL != iter )
	{
		if( NULL == prev )
		{
			bucket->entries = iter->next;
		}
		else
		{
			prev->next = iter->next;
		}

		entry_destroy( iter );

		--bucket->stats.num_entries;
		*existed = 1;
	}
	else
	{
		*existed = 0;
	}
}
Esempio n. 6
0
int list_remove(struct list_t *list, char *key){
	// se estah vazia ou nao foi alocada
	if ( list == NULL || list->size == 0 || key == NULL)
		return -1;

	struct node_t *cur = list->head;
	struct node_t *auxPrev = NULL;		// aponta para o indice anterior ao actual

	while( cur != NULL )
	{
		if( strcmp(key, cur->entry->key) == 0 )
		{
			// se nao estah na 1a posicao
			if( auxPrev != NULL )
				auxPrev->next = cur->next;
			else
				list->head = cur->next;

			list->size--;
			entry_destroy(cur->entry);
			free(cur);

			return 0;
		}

		auxPrev = cur;
		cur = cur->next;

	}

	// se nao encontrou
	return -1;
}
Esempio n. 7
0
/*
 * Method that recursively frees all the memory space reserved.
 */
void erase_all_memory (struct node_t *node) {

    if (node != NULL) {
        erase_all_memory(node->next);
        entry_destroy(node->entry);
        free(node);
    }
}
Esempio n. 8
0
/******************************************************
Create a new character based on the specified template
return the id of the newly created character
the returned string MUST BE FREED by caller
return NULL if fails
*******************************************************/
char * character_create_from_template(context_t * ctx,const char * my_template,const char * map, int layer, int x, int y)
{
	char * new_id;
	char * templatename;
	char * fullname;

	new_id = file_new(CHARACTER_TABLE,NULL);

	templatename = strconcat(base_directory,"/",CHARACTER_TEMPLATE_TABLE,"/",my_template,NULL);
	fullname = strconcat(base_directory,"/",CHARACTER_TABLE,"/",new_id,NULL);
	file_copy(templatename,fullname);
	free(templatename);
	free(fullname);

	/* Check if new character is allowed to be created here */
	if(map_check_tile(ctx,new_id,map,layer,x,y) == 0) {
		entry_destroy(CHARACTER_TABLE,new_id);
		file_delete(CHARACTER_TABLE,new_id);
		free(new_id);
		return NULL;
	}

	/* Write position */
	if(entry_write_string(CHARACTER_TABLE,new_id,map,CHARACTER_KEY_MAP,NULL) == RET_NOK ) {
		entry_destroy(CHARACTER_TABLE,new_id);
		file_delete(CHARACTER_TABLE,new_id);
		free(new_id);
		return NULL;
	}

	if(entry_write_int(CHARACTER_TABLE,new_id,x,CHARACTER_KEY_POS_X,NULL) == RET_NOK ) {
		entry_destroy(CHARACTER_TABLE,new_id);
		file_delete(CHARACTER_TABLE,new_id);
		free(new_id);
		return NULL;
	}

	if(entry_write_int(CHARACTER_TABLE,new_id,y,CHARACTER_KEY_POS_Y,NULL) == RET_NOK ) {
		entry_destroy(CHARACTER_TABLE,new_id);
		file_delete(CHARACTER_TABLE,new_id);
		free(new_id);
		return NULL;
	}

	return new_id;
}
Esempio n. 9
0
int testDup() {
    char *key = strdup("123abc");
    struct data_t *value = data_create2(strlen("1234567890abc")+1,strdup("1234567890abc"));

    struct entry_t *entry = entry_create(key,value);

    struct entry_t *entry2 = entry_dup(entry);

    int result = (entry->key != entry2->key) &&
                 (strcmp(entry->key,entry2->key) == 0) &&
                 (entry->value != entry2->value) &&
                 (entry->value->datasize == entry2->value->datasize) &&
                 (memcmp(entry->value->data, entry2->value->data, entry->value->datasize) == 0);

    entry_destroy(entry);
    entry_destroy(entry2);

    printf("Modulo entry -> teste entry_dup: %s\n",result?"passou":"não passou");
    return result;
}
Esempio n. 10
0
/***********************************************************
 Create a new resource
 with the specified quantity
 return the id of the newly created resource
 the returned string must be freed by caller
 return NULL if fails
***********************************************************/
char * resource_new(const char * my_template, int quantity)
{
	char * new_id;

	new_id = item_create_empty();
	if( new_id == NULL ) {
		return NULL;
	}

	if(entry_write_string(ITEM_TABLE,new_id,my_template,ITEM_TEMPLATE, NULL) == RET_NOK ) {
		entry_destroy(ITEM_TABLE,new_id);
		return NULL;
	}

	if(entry_write_int(ITEM_TABLE,new_id,quantity,ITEM_QUANTITY, NULL) == RET_NOK ) {
		entry_destroy(ITEM_TABLE,new_id);
		return NULL;
	}

	return new_id;
}
Esempio n. 11
0
int schedule_entry_delete(struct usched_entry *entry) {
	int errsv = 0;

	if (!(entry = schedule_entry_disable(entry))) {
		errsv = errno;
		log_warn("schedule_entry_delete(): schedule_entry_disable(): %s\n", strerror(errno));
		errno = errsv;
		return -1;
	}

	entry_destroy(entry);

	return 0;
}
Esempio n. 12
0
int testCreate() {
    char *key = strdup("123abc");
    struct data_t *value = data_create2(strlen("1234567890abc")+1,strdup("1234567890abc"));

    struct entry_t *entry = entry_create(key,value);

    int result = (entry->key == key) &&
                 (entry->value == value);

    entry_destroy(entry);

    printf("Modulo entry -> teste entry_create: %s\n",result?"passou":"nao passou");
    return result;
}
Esempio n. 13
0
void list_destroy(struct list_t *list){
	if(list == NULL)
		return;

	struct node_t *cur = list->head;
	struct node_t *aux;

	while(cur != NULL){
		aux = cur;
		cur = cur->next;
		entry_destroy(aux->entry);
		free(aux);
	}

	free(list);
}
Esempio n. 14
0
static void bucket_clear( bucket_t *bucket )
{
	entry_t *iter;
	entry_t *next;

	assert( NULL != bucket );

	for( iter = bucket->entries; NULL != iter; iter = next )
	{
		next = iter->next;
		entry_destroy( iter );
	}

	bucket->entries = NULL;
	bucket->stats.num_entries = 0;
}
Esempio n. 15
0
int slap_destroy(void)
{
	int rc;

	Debug( LDAP_DEBUG_TRACE,
		"%s destroy: freeing system resources.\n",
		slap_name, 0, 0 );

	if ( default_referral ) {
		ber_bvarray_free( default_referral );
	}

	/* clear out any thread-keys for the main thread */
	ldap_pvt_thread_pool_context_reset( ldap_pvt_thread_pool_context());

	rc = backend_destroy();

	slap_sasl_destroy();

	/* rootdse destroy goes before entry_destroy()
	 * because it may use entry_free() */
	root_dse_destroy();
	entry_destroy();

	switch ( slapMode & SLAP_MODE ) {
	case SLAP_SERVER_MODE:
	case SLAP_TOOL_MODE:
		slap_counters_destroy( &slap_counters );
		break;

	default:
		Debug( LDAP_DEBUG_ANY,
			"slap_destroy(): undefined mode (%d).\n", slapMode, 0, 0 );

		rc = 1;
		break;

	}

	slap_op_destroy();

	ldap_pvt_thread_destroy();

	/* should destroy the above mutex */
	return rc;
}
Esempio n. 16
0
int testAddVarios() {
	int result,i,keysize;
	struct list_t *list;
	struct entry_t *entry[1024];
	struct entry_t *aux;
	struct data_t *data;
	char key[16];

	printf("Módulo list -> teste adicionar vários:");

	if ((list = list_create()) == NULL)
		error(1, errno, "  O teste não pode prosseguir");

	for(i=0; i<1024; i++) {
		sprintf(key, "keyabc-%d",i);
		keysize = strlen(key) + 1;

		if ((data = data_create(keysize)) == NULL)
			error(1, errno, "  O teste não pode prosseguir");

		memcpy(data->data, key, keysize);
		if ((entry[i] = entry_create(key, data)) == NULL)
			error(1, errno, "  O teste não pode prosseguir");

		data_destroy(data);
		list_add(list, entry[i]);
	}

	assert(list_size(list) == 1024);
	result = (list_size(list) == 1024);

	for(i=0; i<1024; i++) {
		assert(list_get(list, entry[i]->key) != entry[i]);
		aux = list_get(list, entry[i]->key);
		result = result &&
			 (aux != entry[i]) &&
			 strcmp(aux->key, entry[i]->key) == 0;
		entry_destroy(entry[i]);
	}

	list_destroy(list);

	printf(" %s\n", result ? "passou" : "não passou");
	return result;
}
Esempio n. 17
0
int testAddCabeca() {
	int result;
	struct list_t *list;

	struct entry_t *entry;
	struct entry_t *entry2;
	struct data_t *data;

	printf("Módulo list -> teste adicionar cabeça:");

	if ((list = list_create()) == NULL)
		error(1, errno, "  O teste não pode prosseguir");

	if ((data = data_create(5)) == NULL)
		error(1, errno, "  O teste não pode prosseguir");

	if ((entry = entry_create("abc", data)) == NULL)
		error(1, errno, "  O teste não pode prosseguir");


	memcpy(entry->value->data, "abc1", 5);

	assert(list_add(NULL, entry) < 0);
	result = (list_add(NULL, entry) < 0);

	assert(list_add(list, NULL) < 0);
	result = result && (list_add(list, NULL) < 0);

	result = result && (list_add(list, entry) == 0);

	entry2 = list_get(list, "abc");

	result = result &&
		 entry2 != entry && 
                 list_size(list) == 1 &&
		 strcmp(entry->key, entry2->key) == 0;

	entry_destroy(entry);
	data_destroy(data);
	list_destroy(list);

	printf(" %s\n",result ? "passou" : "não passou");
	return result;
}
Esempio n. 18
0
static gboolean
dir_forget_entry_if_useless(Dir* d, Entry* e)
{
  GConfValue* val;
  
  if (entry_get_schema_name(e) != NULL)
    return FALSE;
  
  val = entry_get_value(e, NULL, NULL);
  
  if (val != NULL)
    return FALSE; /* not useless */
      
  g_hash_table_remove(d->entry_cache, entry_get_name(e));

  entry_destroy(e);

  return TRUE;
}
Esempio n. 19
0
char *
test_entry_create_file()
{
    struct entry *file;

    file = entry_create_file("test", &test_user, PERM_RD | PERM_WR, PERM_RD);

    mu_assert("entry_create_file", file != NULL);
    mu_assert("file type", file->type == TYPE_FILE);
    mu_assert("file owner", file->owner_id == test_user.id);
    mu_assert("file owner perm", file->owner_perm == PERM_RD | PERM_WR);
    mu_assert("file other perm", file->other_perm == PERM_RD);

    mu_assert("file parent", file->parent == NULL);
    mu_assert("file content", file->content == NULL);
    
    entry_destroy(file);

    return 0;
}
Esempio n. 20
0
char *
test_entry_create_dir()
{
    struct entry *dir;

    dir = entry_create_dir("test", &test_user, PERM_RD | PERM_WR, PERM_RD);

    mu_assert("entry_create_dir", dir != NULL);
    mu_assert("dir type", dir->type == TYPE_DIR);
    mu_assert("dir owner", dir->owner_id == test_user.id);
    mu_assert("dir owner perm", dir->owner_perm == PERM_RD | PERM_WR);
    mu_assert("dir other perm", dir->other_perm == PERM_RD);

    mu_assert("dir parent", dir->parent == NULL);
    mu_assert("dir files", dir->count == 0);

    entry_destroy(dir);

    return 0;
}
Esempio n. 21
0
int conn_client_process(void) {
	int errsv = 0, ret = 0;
	struct usched_entry *cur = NULL;
	char *aaa_payload_data = NULL;

	while ((cur = runc.epool->pop(runc.epool))) {
		/* If this is a remote connection, the payload will be encrypted, so we need to
		 * inform the remote party of size of the encrypted payload and not the current
		 * (plain) size.
		 */
		if (conn_is_remote(runc.fd)) {
			/* TODO or FIXME: If we're going to encrypt this entry, why not do it right
			 * here instead of doing lots of calculations on entry->psize along this
			 * rountine?
			 */
			cur->psize += CRYPT_EXTRA_SIZE_CHACHA20POLY1305;
		}

		/* Convert endianness to network byte order */
		cur->id = htonll(cur->id);
		cur->flags = htonl(cur->flags);

		if (conn_is_remote(runc.fd)) {
			/* For remote connections, UID and GID must be set to 0xff */
			cur->uid = htonl(0xff);
			cur->gid = htonl(0xff);
		} else {
			cur->uid = htonl(cur->uid);
			cur->gid = htonl(cur->gid);
		}

		cur->trigger = htonl(cur->trigger);
		cur->step = htonl(cur->step);
		cur->expire = htonl(cur->expire);
		/* We can ignore pid, status, exec_time, latency, outdata_len and outdata here */
		cur->psize = htonl(cur->psize);

		/* Set username and session data if this is a remote connection */
		if (conn_is_remote(runc.fd)) {
			/* Set username */
			if (strlen(runc.opt.remote_username) >= sizeof(cur->username)) {
				log_crit("conn_client_process(): The requested username is too long to be processed: %s\n", runc.opt.remote_username);
				/* The payload size is in network byte order. We need to revert it
				 * before calling entry_destroy().
				 */
				cur->psize = ntohl(cur->psize) - CRYPT_EXTRA_SIZE_CHACHA20POLY1305;
				entry_destroy(cur);
				errno = EINVAL;
				return -1;
			}

			strcpy(cur->username, runc.opt.remote_username);

			/* Set the session data */
			if (entry_client_remote_session_create(cur, runc.opt.remote_password) < 0) {
				errsv = errno;
				log_crit("conn_client_process(): entry_client_remote_session_create(): %s\n", strerror(errno));
				/* The payload size is in network byte order. We need to revert it
				 * before calling entry_destroy().
				 */
				cur->psize = ntohl(cur->psize) - CRYPT_EXTRA_SIZE_CHACHA20POLY1305;
				entry_destroy(cur);
				errno = errsv;
				return -1;
			}
		}

		/* Send the first entry block */
		if (conn_write_blocking(runc.fd, cur, usched_entry_hdr_size()) != (ssize_t) usched_entry_hdr_size()) {
			errsv = errno;
			log_crit("conn_client_process(): conn_write_blocking() != %d: %s\n", usched_entry_hdr_size(), strerror(errno));
			/* The payload size is in network byte order. We need to revert it before
			 * calling entry_destroy().
			 */
			cur->psize = ntohl(cur->psize) - (conn_is_remote(runc.fd) ? CRYPT_EXTRA_SIZE_CHACHA20POLY1305 : 0);
			entry_destroy(cur);
			errno = errsv;
			return -1;
		}

		/* Convert endianness to host byte order */
		cur->id = ntohll(cur->id);
		cur->flags = ntohl(cur->flags);
		cur->uid = ntohl(cur->uid);
		cur->gid = ntohl(cur->gid);
		cur->trigger = ntohl(cur->trigger);
		cur->step = ntohl(cur->step);
		cur->expire = ntohl(cur->expire);
		cur->psize = ntohl(cur->psize) - (conn_is_remote(runc.fd) ? CRYPT_EXTRA_SIZE_CHACHA20POLY1305 : 0); /* Set the original payload size if the connection is remote. */

		/* Read the session token into the session field for further processing */
		if (conn_read_blocking(runc.fd, cur->session, sizeof(cur->session)) != (ssize_t) sizeof(cur->session)) {
			errsv = errno;
			log_crit("conn_client_process(): conn_read_blocking() != sizeof(cur->session): %s\n", strerror(errno));
			entry_destroy(cur);
			errno = errsv;
			return -1;
		}

		/* If this a remote connection, read the session field, which contains session data,
		 * and rewrite it with authentication information.
		 */
		if (conn_is_remote(runc.fd)) {
			/* Process remote session data */
			if (entry_client_remote_session_process(cur, runc.opt.remote_password) < 0) {
				errsv = errno;
				log_crit("conn_client_process(): entry_client_remote_session_process(): %s\n", strerror(errno));
				entry_destroy(cur);
				errno = errsv;
				return -1;
			}

			/* Encrypt payload */
			if (entry_payload_encrypt(cur, 0) < 0) {
				errsv = errno;	
				log_crit("conn_client_process(): entry_payload_encrypt(): %s\n", strerror(errno));
				entry_destroy(cur);
				errno = errsv;
				return -1;
			}
		}

		/* Craft the token and payload together */
		if (!(aaa_payload_data = mm_alloc(sizeof(cur->session) + cur->psize))) {
			errsv = errno;
			log_crit("conn_client_process(): mm_alloc(): %s\n", strerror(errno));
			entry_destroy(cur);
			errno = errsv;
			return -1;
		}

		/* Craft the session/authentication information along with the payload */
		memcpy(aaa_payload_data, cur->session, sizeof(cur->session));
		memcpy(aaa_payload_data + sizeof(cur->session), cur->payload, cur->psize);

		/* Send the authentication and authorization data along entry payload */
		if (conn_write_blocking(runc.fd, aaa_payload_data, sizeof(cur->session) + cur->psize) != (ssize_t) (sizeof(cur->session) + cur->psize)) {
			errsv = errno;
			log_crit("conn_client_process(): conn_write_blocking() != (sizeof(cur->session) + cur->psize): %s\n", strerror(errno));
			entry_destroy(cur);
			mm_free(aaa_payload_data);
			errno = errsv;
			return -1;
		}

		/* Reset and free aaa_payload_data */
		memset(aaa_payload_data, 0, sizeof(cur->session) + cur->psize);
		mm_free(aaa_payload_data);

		/* Process the response */
		if (entry_has_flag(cur, USCHED_ENTRY_FLAG_NEW)) {
			ret = process_client_recv_run(cur);
		} else if (entry_has_flag(cur, USCHED_ENTRY_FLAG_DEL)) {
			ret = process_client_recv_stop(cur);
		} else if (entry_has_flag(cur, USCHED_ENTRY_FLAG_GET)) {
			ret = process_client_recv_show(cur);
		} else if (entry_has_flag(cur, USCHED_ENTRY_FLAG_PAUSE)) {
			ret = process_client_recv_hold(cur);
		} else {
			log_warn("conn_client_process(): Unexpected value found in entry->flags\n");
			errno = EINVAL;
			ret = -1;
		}

		/* Check if a valid response was received */
		if (ret < 0) {
			errsv = errno;
			log_crit("conn_client_process(): %s\n", strerror(errno));
			entry_destroy(cur);
			errno = errsv;
			return -1;
		}

		entry_destroy(cur);
	}

	return 0;
}
Esempio n. 22
0
int list_add(struct list_t *list, struct entry_t *entry){
	if (list == NULL || entry == NULL)
		return -1;

	struct node_t *node = (struct node_t *) malloc(sizeof(struct node_t));

	if (node == NULL)
		return -1;

	// preparar node a ser inserido na lista
	node->entry = entry_dup(entry);
	node->next = NULL;

	struct node_t *current  = list->head;
	struct node_t *previous = current;
	int str_cmp;

	// lista vazia
	if (list->head == NULL) {
		list->head = node;
		list->size++;
	}
	// adicionar no ao inicio da lista se nao existe
	else if (strcmp(entry->key, list->head->entry->key) < 0) {
		node->next = list->head;
		list->head = node;
		list->size++;
	}
	else {
		str_cmp = strcmp(entry->key, current->entry->key);

		// encontrar posicao que a key excede
		while( str_cmp > 0 && current->next != NULL )
		{
			previous = current;
			current  = current->next;
			str_cmp  = strcmp( entry->key, current->entry->key );
		}

		// key jah presente na lista
		if ( str_cmp == 0 ){
			entry_destroy(node->entry);
			free(node);
			return -1;
		}
		// adicionar no antes do no encontrado
		else if (str_cmp < 0) {
			previous->next = node;
			node->next     = current;
			list->size++;
		}
		// adicionar no depois do no encontrado
		// str_cmp > 0
		else {
			// current nao eh no final
			if (current->next != NULL)
				node->next = current->next;

			current->next = node;
			list->size++;
		}
	}

	return 0;
}
Esempio n. 23
0
/*****************************
Remove an item file
return RET_NOK on error
*****************************/
ret_code_t item_destroy(const char * item_id)
{
	return entry_destroy(ITEM_TABLE,item_id);
}
Esempio n. 24
0
static void
entry_destroy_foreach(const gchar* name, Entry* e, gpointer data)
{
  entry_destroy (e);
}
Esempio n. 25
0
/* 
 * Executar uma função (indicada pelo opcode na msg) e retorna o resultado na
 * própria struct msg.
 * Retorna 0 (OK) ou -1 (erro, por exemplo, tabela nao inicializada).
 */
int invoke(struct message_t *msg) {

    int retVal = 0;
    char *key;
    struct entry_t *entry;

    if(sharedPtable && msg) {
        switch (msg->opcode) {
            case OP_RT_GET:
                // table_get: (struct table_t* char*) -> (struct data_t*)
                if(msg->content.key) {
                    key = msg->content.key;
                    if(!(msg->content.value = ptable_get(sharedPtable, key))) {
                        if(!(msg->content.value = data_create(0))) {
                            msg->opcode = OP_RT_ERROR;
                            msg->c_type = CT_RESULT;
                            msg->content.result = -1;
                        }
                    }
                    if(msg->content.value) {
                        msg->opcode ++; // Incrementa para dizer que esta mensagem contem o resultado
                        msg->c_type = CT_VALUE;
                        free(key);
                    }
                }
                else {
                    msg->opcode = OP_RT_ERROR;
                    msg->c_type = CT_RESULT;
                    msg->content.result = -1;
                }
            break;

            case OP_RT_PUT:
                // table_put: (struct table_t* char* struct data_t*) -> (int)
                if(msg->content.entry) {
                    entry = msg->content.entry;
                    if((retVal = ptable_put(sharedPtable, entry->key, entry->value)) != -1) {
                        msg->content.result = retVal;
                        msg->opcode ++;
                        msg->c_type = CT_RESULT;
                    }
                    else {
						// Deu erro pq ficamos sem espaço de log.
						//cria um ficheiro temporário com o estado da tabela
						if(pmanager_store_table(sharedPtable->pmanager, sharedPtable->table) < 0) {
							ERROR("persistent_table: pmanager_store_table");
							msg->opcode = OP_RT_ERROR;
							msg->c_type = CT_RESULT;
							msg->content.result = -1;
						}
						
						//cria um ficheiro temporário com o estado da tabela
						if(pmanager_rotate_log(sharedPtable->pmanager) != 0) {
							ERROR("persistent_table: pmanager_rotate_log");
							msg->opcode = OP_RT_ERROR;
							msg->c_type = CT_RESULT;
							msg->content.result = -1;
						}
						if((retVal = ptable_put(sharedPtable, entry->key, entry->value)) != -1) {
							msg->content.result = retVal;
							msg->opcode ++;
							msg->c_type = CT_RESULT;
						} else {
							msg->opcode = OP_RT_ERROR;
							msg->c_type = CT_RESULT;
							msg->content.result = -1;
						}
                    }
                    entry_destroy(entry);
                }
                else {
                    msg->opcode = OP_RT_ERROR;
                    msg->c_type = CT_RESULT;
                    msg->content.result = -1;
                }
            break;

            case OP_RT_SIZE:
                // table_size: (struct table_t*) -> (int)
                msg->content.result = ptable_size(sharedPtable);
                msg->opcode ++;
                msg->c_type = CT_RESULT;
                // Nao temos nada para libertar...
            break;
	
            case OP_RT_DEL:
                // table_del: (struct table_t* char*) -> (int)
                if(msg->content.key) {
                    key = msg->content.key;
                    msg->content.key = NULL;
                    retVal = ptable_del(sharedPtable, key);
                    msg->content.result = retVal;
                    msg->opcode ++;
                    msg->c_type = CT_RESULT;
                    free(key);
                }
                else {
                    msg->opcode = OP_RT_ERROR;
                    msg->c_type = CT_RESULT;
                    msg->content.result = -1;
                }
            break;

            case OP_RT_GETKEYS:
                // table_get_keys: (struct table_t*) -> (char**)
                if((msg->content.keys = ptable_get_keys(sharedPtable))) {
                    msg->opcode ++;
                    msg->c_type = CT_KEYS;
                }
                else {
                    msg->opcode = OP_RT_ERROR;
                    msg->c_type = CT_RESULT;
                    msg->content.result = -1;
                }
                // Não temos nada para libertar...
            break;

            case OP_RT_GETTS:
                // table_get: (struct table_t* char*) -> (int)
                if(msg->content.key) {
                    key = strdup(msg->content.key);
					free(msg->content.key);

                    // Verifica a validade da resposta
                    if((msg->content.timestamp = ptable_get_ts(sharedPtable, key)) < 0) {
                            msg->opcode = OP_RT_ERROR;
							msg->c_type = CT_RESULT;
                            msg->content.result = -1;
                    } else if(msg->content.timestamp >= 0) {
                        msg->opcode ++; // Incrementa para dizer que esta mensagem contem o resultado
                        msg->c_type = CT_TIMESTAMP;
                    }
                }
                else {
                    msg->opcode = OP_RT_ERROR;
                    msg->c_type = CT_RESULT;
                    msg->content.result = -1;
                }
				free(key);
            break;

            default:
                ERROR("opcode");
                msg->opcode = OP_RT_ERROR;
                msg->c_type = CT_RESULT;
                msg->content.result = -1;
            break;
        }
    }
    else {
        ERROR("NULL sharedPtable");
        retVal = -1;
    }
    return retVal;
}