From: Sam Moore Date: Fri, 16 Aug 2013 00:28:37 +0000 (+0800) Subject: Fixed binary file access X-Git-Url: https://git.ucc.asn.au/?a=commitdiff_plain;h=321bf62fcb0943a140dbc55bff5350e11c0eb946;p=matches%2FMCTX3420.git Fixed binary file access Really dumb mistakes. If we can open the file for reading and writing, and remove the need to open/close in both threads each time it is accessed, the efficiency might be better. We'd still have to close the file to save changes to disk though, might be a problem if the program crashes. --- diff --git a/server/Makefile b/server/Makefile index 95cd03e..009128b 100644 --- a/server/Makefile +++ b/server/Makefile @@ -2,7 +2,7 @@ CXX = gcc FLAGS = -std=c99 -Wall -Werror -pedantic -g LIB = -lpthread -OBJ = log.o sensor.o test_request.o main.o +OBJ = log.o sensor.o query.o main.o RM = rm -f BIN = server diff --git a/server/main.c b/server/main.c index 524f550..5e2c6c8 100644 --- a/server/main.c +++ b/server/main.c @@ -9,7 +9,7 @@ #include // for signal handling // --- Custom headers --- // - +#include "query.h" #include "log.h" #include "options.h" #include "sensor.h" @@ -74,7 +74,7 @@ int main(int argc, char ** argv) } // run request thread in the main thread - Query_Request(NULL); //TODO: Replace with FastCGI code + Query_Main(NULL); //TODO: Replace with FastCGI code return 0; } diff --git a/server/query.c b/server/query.c index 7a9ab16..8499e37 100644 --- a/server/query.c +++ b/server/query.c @@ -6,7 +6,7 @@ - +#include "query.h" #include "sensor.h" #include "log.h" @@ -32,8 +32,7 @@ void QuerySensor(int id) //TODO: This code will form the SensorHandler FastCGI f } else { - fseek(file, 0, SEEK_SET); - rewind(file); + fseek(file, (s->read_offset)*sizeof(DataPoint), SEEK_SET); amount_read = fread(&buffer, sizeof(DataPoint), QUERY_BUFSIZ, file); s->read_offset += amount_read; Log(LOGDEBUG, "Read %d data points; offset now at %d", amount_read, s->read_offset); diff --git a/server/sensor.c b/server/sensor.c index f1ec5dc..a3d3f0b 100644 --- a/server/sensor.c +++ b/server/sensor.c @@ -105,13 +105,12 @@ void * Sensor_Main(void * arg) // CRITICAL SECTION (no threads should be able to read/write the file at the same time) pthread_mutex_lock(&(s->mutex)); - // Open binary file and dump buffer into it - FILE * file = fopen(s->filename, "wb"); + // Open binary file in append mode and dump buffer into it + FILE * file = fopen(s->filename, "ab"); if (file == NULL) { - Fatal("Couldn't open file \"%s\" mode wb - %s", s->filename, strerror(errno)); + Fatal("Couldn't open file \"%s\" mode ab - %s", s->filename, strerror(errno)); } - fseek(file, 0, SEEK_END); int amount_written = fwrite(s->buffer, sizeof(DataPoint), SENSOR_DATABUFSIZ, file); if (amount_written != SENSOR_DATABUFSIZ) {