sessions.c: JAR loading extracted to standalone method

parent 8b709571
Pipeline #3524 passed with stages
in 3 minutes and 13 seconds
......@@ -23,7 +23,10 @@
* Send client message to socket.
*/
void
send_client_message(const ClientMessage message, struct connection * restrict conn) {
send_client_message(
const ClientMessage message,
struct connection * restrict conn
) {
void * buffer;
size_t send_size = client_message__get_packed_size(&message);
buffer = malloc(send_size); // FREE!
......@@ -50,57 +53,74 @@ receive_server_message(struct connection * restrict conn) {
return (response);
}
/*
* Instrumentation jar structure
*/
struct inst_jar {
char * name;
FILE * file;
long filesize;
char * name;
void * buffer;
size_t filesize;
};
/*
* Get filesize
*/
static long
get_filesize(const char * filename){
get_filesize(const char * filename) {
struct stat buffer;
int status;
status = stat(filename, &buffer);
return buffer.st_size;
return (buffer.st_size);
}
/**
/*
* Load the files to the buffers.
* @return
*/
static void
load_files_to_buffers(struct inst_jar * jars, const size_t count) {
FILE * file;
// Open the files and ensure that all of them exists
for (size_t i = 0; i < count; ++i) {
file = fopen(jars[i].name, "r");
assert(file != NULL);
jars[i].filesize = (size_t)get_filesize(jars[i].name);
jars[i].buffer = malloc(jars[i].filesize);
fread(jars[i].buffer, 1, jars[i].filesize, file);
fclose(file);
}
}
/*
* Instrumentation delivery message build
* @param config
* @return
*/
static InstrumentationDelivery
instrumentationDeliveryInit(struct config * config){
instrumentationDeliveryInit(struct config * config) {
InstrumentationDelivery delivery = INSTRUMENTATION_DELIVERY__INIT;
size_t tokens = config->jar_count;
char ** arr = config->instrumentation_jars;
struct inst_jar * jars = malloc(tokens * sizeof(struct inst_jar));
size_t jarCount = config->jar_count;
size_t total_size = 0;
delivery.n_sizes = tokens;
delivery.sizes = malloc( tokens * sizeof(int32_t));
struct inst_jar * jars = malloc(jarCount * sizeof (struct inst_jar));
for(size_t i = 0; i < tokens; ++i){
jars[i].name = arr[i];
}
delivery.n_sizes = jarCount;
delivery.sizes = malloc(jarCount * sizeof (int32_t));
free(arr);
for (size_t i = 0; i < jarCount; ++i) {
jars[i].name = config->instrumentation_jars[i];
}
// Silent fallback to backward compatibility.
if(tokens == 0) return delivery;
load_files_to_buffers(jars, jarCount);
// Open the files and ensure that all of them exists
for(size_t i = 0; i < tokens; ++i){
jars[i].file = fopen(jars[i].name,"r");
assert(jars[i].file != NULL);
jars[i].filesize = get_filesize(jars[i].name);
total_size += jars[i].filesize;
delivery.sizes[i] = (int32_t) jars[i].filesize; // Crop conversion size_t -> int32_t
for (size_t i = 0; i < jarCount; ++i) {
delivery.sizes[i] = (int32_t) jars[i].filesize;
total_size += delivery.sizes[i];
}
assert(total_size > 0);
......@@ -109,10 +129,10 @@ instrumentationDeliveryInit(struct config * config){
delivery.instrumentation.data = malloc(total_size);
void * buffer = delivery.instrumentation.data;
for(size_t i = 0; i < tokens; ++i){
fread(buffer, 1, (size_t)jars[i].filesize, jars[i].file);
for (size_t i = 0; i < jarCount; ++i) {
memcpy(buffer, jars[i].buffer, jars[i].filesize);
buffer += jars[i].filesize;
fclose(jars[i].file);
free(jars[i].buffer);
}
free(jars);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment