Esempio n. 1
0
/*
 * UDF SMD get metadata items callback.
 */
static int udf_cask_get_metadata_cb(char *module, as_smd_item_list_t *items, void *udata)
{
	udf_get_data_t *p_get_data = (udf_get_data_t *) udata;
	cf_dyn_buf *out = p_get_data->db;

	unsigned char   hash[SHA_DIGEST_LENGTH];
	// hex string to be returned to the client
	unsigned char   sha1_hex_buff[CF_SHA_HEX_BUFF_LEN];
	// Currently just return directly for LUA
	uint8_t udf_type = AS_UDF_TYPE_LUA;

	for (int index = 0; index < items->num_items; index++) {
		as_smd_item_t *item = items->item[index];
		cf_debug(AS_UDF, "UDF metadata item[%d]:  module \"%s\" ; key \"%s\" ; value \"%s\" ; generation %u ; timestamp %lu",
				 index, item->module_name, item->key, item->value, item->generation, item->timestamp);
		cf_dyn_buf_append_string(out, "filename=");
		cf_dyn_buf_append_buf(out, (uint8_t *)item->key, strlen(item->key));
		cf_dyn_buf_append_string(out, ",");
		SHA1((uint8_t *)item->value, strlen(item->value), hash);

		// Convert to a hexadecimal string
		cf_convert_sha1_to_hex(hash, sha1_hex_buff);
		cf_dyn_buf_append_string(out, "hash=");
		cf_dyn_buf_append_buf(out, sha1_hex_buff, CF_SHA_HEX_BUFF_LEN);
		cf_dyn_buf_append_string(out, ",type=");
		cf_dyn_buf_append_string(out, as_udf_type_name[udf_type]);
		cf_dyn_buf_append_string(out, ";");
	}

	pthread_mutex_lock(p_get_data->mt);

	p_get_data->done = true;
	int retval = pthread_cond_signal(p_get_data->cv);
	if (retval) {
		cf_warning(AS_UDF, "pthread_cond_signal failed (rv %d)", retval);
	}

	pthread_mutex_unlock(p_get_data->mt);

	return retval;
}
Esempio n. 2
0
int udf_cask_info_list(char *name, cf_dyn_buf * out) {

	DIR 		  * dir	             = NULL;
	bool 		    not_empty        = false;
	struct dirent * entry            = NULL;
	int 		    count 	         = 0;
	uint8_t       * content          = NULL;
	size_t          content_len      = 0;
	unsigned char   content_gen[256] = {0};
	unsigned char   hash[SHA_DIGEST_LENGTH];
	// hex string to be returned to the client
	unsigned char   sha1_hex_buff[CF_SHA_HEX_BUFF_LEN];

	cf_debug(AS_INFO, "UDF CASK INFO LIST");

	// Currently just return directly for LUA
	uint8_t 		udf_type 	    = AS_UDF_TYPE_LUA;
	dir = opendir(g_config.mod_lua.user_path);
	if ( dir == 0 ) {
		cf_warning(AS_UDF, "could not open udf directory %s: %s", g_config.mod_lua.user_path, cf_strerror(errno));
		return -1;
	}

	while ( (entry = readdir(dir)) && entry->d_name ) {

		char * name = entry->d_name;
		size_t len = strlen(name);

		// if ( len < 4 ) continue;

		if ( name[0] == '.' ) continue;

		if ( not_empty ) {
			cf_dyn_buf_append_char(out, ';');
		}
		else {
			not_empty = true;
		}

		cf_dyn_buf_append_string(out, "filename=");
		cf_dyn_buf_append_buf(out, (uint8_t *) name, len);
		cf_dyn_buf_append_string(out, ",");
		mod_lua_rdlock(&mod_lua);
		if (file_read(name, &content, &content_len, content_gen) != 0) {
			cf_info(AS_UDF, "UDF-list : file not readable");
			cf_dyn_buf_append_string(out, "error=file_not_readable");
			mod_lua_unlock(&mod_lua);
			return 0;
		}
		mod_lua_unlock(&mod_lua);
		SHA1(content, content_len, hash);
		// Convert to a hexadecimal string
		cf_convert_sha1_to_hex(hash, sha1_hex_buff);
		cf_dyn_buf_append_string(out, "hash=");
		cf_dyn_buf_append_buf(out, sha1_hex_buff, CF_SHA_HEX_BUFF_LEN);
		cf_dyn_buf_append_string(out, ",type=");
		cf_dyn_buf_append_string(out, as_udf_type_name[udf_type]);
		count ++;
	}
	if (not_empty)
	{
		cf_dyn_buf_append_string(out, ";");
	}

	closedir(dir);

	return 0;
}
/**
 * @return AEROSPIKE_OK if successful. Otherwise an error occurred.
 */
as_status aerospike_udf_get(
	aerospike * as, as_error * err, const as_policy_info * policy, 
	const char * filename, as_udf_type type, as_udf_file * file)
{
	as_error_reset(err);
	
	if (! policy) {
		policy = &as->config.policies.info;
	}
		
	char command[512];
	snprintf(command, sizeof(command), "udf-get:filename=%s;", filename);
	
	char* response = 0;
	as_status status = aerospike_info_any(as, err, policy, command, &response);
	
	if (status) {
		return status;
	}
	
	// response := <command>\tgen=<string>;type=<string>;content=<string>;
	char* p = strchr(response, '\t');
	
	if (!p) {
		as_error_update(err, AEROSPIKE_ERR_PARAM, "Invalid udf-get response: %s", response);
		free(response);
		return AEROSPIKE_ERR_PARAM;
	}
	p++;
	
	p = strstr(p, "content=");
	
	if (!p) {
		as_error_update(err, AEROSPIKE_ERR_PARAM, "Invalid udf-get response: %s", response);
		free(response);
		return AEROSPIKE_ERR_PARAM;
	}
	p += 8;
	
	as_strncpy(file->name, filename, AS_UDF_FILE_NAME_SIZE);
	file->type = AS_UDF_TYPE_LUA;

	char* content = p;
	while (*p) {
		if (*p == ';') {
			*p = 0;
			break;
		}
		p++;
	}
	
	uint32_t len = (uint32_t)(p - content);
	uint32_t size;
	cf_b64_validate_and_decode_in_place((uint8_t*)content, len, &size);

	// Update file hash
	unsigned char hash[SHA_DIGEST_LENGTH];
#ifdef __APPLE__
	// Openssl is deprecated on mac, but the library is still included.
	// Save old settings and disable deprecated warnings.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
	SHA1((uint8_t*)content, size, hash);
#ifdef __APPLE__
	// Restore old settings.
#pragma GCC diagnostic pop
#endif
	cf_convert_sha1_to_hex(hash, file->hash);

	file->content._free = true;
	file->content.size = size;
	file->content.capacity = size;
	file->content.bytes = malloc(size);
	memcpy(file->content.bytes, content, size);
	
	free(response);
	return AEROSPIKE_OK;
}