Commit 4680d424 authored by Vít Kabele's avatar Vít Kabele

Fixed filesize resolution [POSIX only]

parent 26c4f23e
......@@ -9,6 +9,7 @@
#include <stdio.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <fcntl.h>
......@@ -22,7 +23,7 @@
* Send client message to socket.
*/
void
send_client_message(ClientMessage message, struct connection * 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!
......@@ -38,8 +39,8 @@ send_client_message(ClientMessage message, struct connection * conn) {
* Receive server message from opened socket.
*/
ServerMessage *
receive_server_message(struct connection * conn) {
void * buffer;
receive_server_message(struct connection * restrict conn) {
void * buffer;
ServerMessage * response;
size_t recv_size = message_recv(conn, &buffer); // FREE!
......@@ -56,7 +57,7 @@ struct inst_jar {
};
static size_t
substr_count(const char * string, const char * substr) {
substr_count(const char * string, const char * restrict substr) {
size_t count = 0;
for(const char * token = string; (token = strstr(token, substr)) != NULL; ++token)
++count;
......@@ -67,7 +68,7 @@ substr_count(const char * string, const char * substr) {
* Free the return value after use.
*/
static char **
split_string(char * string, const char * separator) {
split_string(char * string, const char * restrict separator) {
size_t tokens = substr_count(string, separator) + 1;
assert(tokens > 0);
char ** arr = malloc(tokens * sizeof(char *));
......@@ -83,11 +84,12 @@ split_string(char * string, const char * separator) {
* Get filesize
*/
static long
get_filesize(FILE * file){
assert(fseek(file, 0L, SEEK_END) == 0);
long size = ftell(file);
fseek(file, 0L, SEEK_SET);
return size;
get_filesize(const char * filename){
struct stat buffer;
int status;
status = stat(filename, &buffer);
warn("Size of file %s is %lli\n", filename, buffer.st_size);
return buffer.st_size;
}
/*
......@@ -95,11 +97,16 @@ get_filesize(FILE * file){
* Options should be field of paths pointing to a *.jar files separated by semicolon (:)
*/
InstrumentationDelivery
load_instrumentation(char * options){
load_instrumentation(char * restrict options){
InstrumentationDelivery delivery = INSTRUMENTATION_DELIVERY__INIT;
if(options == NULL)
return delivery;
size_t tokens = substr_count(options, ":") + 1;
struct inst_jar * jars = malloc(tokens * sizeof(struct inst_jar));
size_t total_size = 0;
InstrumentationDelivery delivery = INSTRUMENTATION_DELIVERY__INIT;
delivery.n_sizes = tokens;
delivery.sizes = malloc( tokens * sizeof(int32_t));
......@@ -112,16 +119,16 @@ load_instrumentation(char * options){
free(arr);
assert(tokens > 0);
// Silent fallback to backward compatibility.
if(tokens == 0) return delivery;
// 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].file);
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
warn("Size of instrumentation file %s is %li\n", jars[i].name, get_filesize(jars[i].file));
}
assert(total_size > 0);
......@@ -142,7 +149,7 @@ load_instrumentation(char * options){
}
int32_t
session_start(char * options) {
session_start(char * restrict options) {
struct connection * conn = network_acquire_connection();
int32_t sid = 0;
......@@ -175,6 +182,13 @@ session_start(char * options) {
message.session_id = sid;
message.instrumentation_delivery = &delivery;
send_client_message(message, conn);
ServerMessage * response = receive_server_message(conn);
assert(response->response_case == SERVER_MESSAGE__RESPONSE_INSTRUMENTATION_ACCEPT_CONFIRMATION);
assert(response->instrumentation_accept_confirmation->instrumentation_accepted);
server_message__free_unpacked(response, NULL);
}
network_release_connection(conn);
......
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