diff --git a/src/obiarray.c b/src/obiarray.c index fd4015d..220277f 100644 --- a/src/obiarray.c +++ b/src/obiarray.c @@ -787,8 +787,16 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) return NULL; } + // Read the header size + if (read(array_data_file_descriptor, &header_size, sizeof(size_t)) < sizeof(size_t)) + { + obi_set_errno(OBI_ARRAY_ERROR); + obidebug(1, "\nError reading the header size to open a data array"); + close(array_data_file_descriptor); + return NULL; + } + // Fill the array data structure - header_size = get_array_data_header_size(); // TODO should be read? array_data->header = mmap(NULL, header_size, PROT_READ | PROT_WRITE, @@ -858,8 +866,16 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) return NULL; } + // Read the header size + if (read(array_file_descriptor, &header_size, sizeof(size_t)) < sizeof(size_t)) + { + obi_set_errno(OBI_ARRAY_ERROR); + obidebug(1, "\nError reading the header size to open an array"); + close(array_file_descriptor); + return NULL; + } + // Fill the array structure - header_size = get_array_header_size(); // TODO should be read? array->header = mmap(NULL, header_size, PROT_READ | PROT_WRITE, @@ -911,13 +927,6 @@ int obi_close_array(OBIDMS_array_p array) // TODO when do we call it? (as multip ret_val = close_array_data(array->data); - if (munmap(array->header, (array->header)->header_size) < 0) - { - obi_set_errno(OBI_ARRAY_ERROR); - obidebug(1, "\nError munmapping the header of an array file"); - ret_val = -1; - } - if (munmap(array->first, (((array->header)->nb_items_max) * sizeof(index_t))) < 0) { obi_set_errno(OBI_ARRAY_ERROR); @@ -925,6 +934,13 @@ int obi_close_array(OBIDMS_array_p array) // TODO when do we call it? (as multip ret_val = -1; } + if (munmap(array->header, (array->header)->header_size) < 0) + { + obi_set_errno(OBI_ARRAY_ERROR); + obidebug(1, "\nError munmapping the header of an array file"); + ret_val = -1; + } + free(array); return ret_val; diff --git a/src/obidmscolumn.c b/src/obidmscolumn.c index e4f7055..4ae2d4d 100644 --- a/src/obidmscolumn.c +++ b/src/obidmscolumn.c @@ -191,11 +191,10 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ obiversion_t new_version_number; char* version_file_name; int version_file_descriptor; - bool little_endian; int lock_mode; new_version_number = 0; - loc_size = sizeof(bool) + sizeof(obiversion_t); + loc_size = sizeof(obiversion_t); // Select the correct lockf operation according to the blocking mode if (block) @@ -252,24 +251,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ return -1; } - // Read the endianness of the file - if (read(version_file_descriptor, &little_endian, sizeof(bool)) < sizeof(bool)) - { - obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError reading the endianness of a version file"); - close(version_file_descriptor); - return -1; - } - - // Check if endianness is correct - if (little_endian != obi_is_little_endian()) - { - obi_set_errno(OBICOL_BAD_ENDIAN_ERROR); - obidebug(1, "\nEndianness of a version file is incorrect"); - close(version_file_descriptor); - return -1; - } - // Read the current version number if (read(version_file_descriptor, &new_version_number, sizeof(obiversion_t)) < sizeof(obiversion_t)) { @@ -282,14 +263,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ new_version_number++; // Write the new version number - if (lseek(version_file_descriptor, sizeof(bool), SEEK_SET) != sizeof(bool)) - { - obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError writing a new version number in a version file"); - close(version_file_descriptor); - return -1; - } - if (write(version_file_descriptor, &new_version_number, sizeof(obiversion_t)) < sizeof(obiversion_t)) { obi_set_errno(OBICOL_UNKNOWN_ERROR); @@ -328,9 +301,8 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo obiversion_t version_number; char* version_file_name; int version_file_descriptor; - bool little_endian; - loc_size = sizeof(bool) + sizeof(obiversion_t); + loc_size = sizeof(obiversion_t); version_number = 0; version_file_name = build_version_file_name(column_directory->column_name); @@ -376,17 +348,6 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo return -1; } - little_endian = obi_is_little_endian(); - - // Write endianness - if (write(version_file_descriptor, &little_endian, sizeof(bool)) < sizeof(bool)) - { - obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError writing endianness of a version file"); - close(version_file_descriptor); - return -1; - } - // Write version number if (write(version_file_descriptor, &version_number, sizeof(obiversion_t)) < sizeof(obiversion_t)) { @@ -444,9 +405,8 @@ obiversion_t obi_get_latest_version_number(OBIDMS_column_directory_p column_dire obiversion_t latest_version_number; char * version_file_name; int version_file_descriptor; - bool little_endian; - loc_size = sizeof(bool) + sizeof(obiversion_t); + loc_size = sizeof(obiversion_t); latest_version_number = 0; version_file_name = build_version_file_name(column_directory->column_name); @@ -483,24 +443,6 @@ obiversion_t obi_get_latest_version_number(OBIDMS_column_directory_p column_dire return -1; } - // Read the endianness - if (read(version_file_descriptor, &little_endian, sizeof(bool)) < sizeof(bool)) - { - obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError reading the endianness of a version file"); - close(version_file_descriptor); - return -1; - } - - // Verify the endianness - if (little_endian != obi_is_little_endian()) - { - obi_set_errno(OBICOL_BAD_ENDIAN_ERROR); - obidebug(1, "\nEndianness of a version file is incorrect"); - close(version_file_descriptor); - return -1; - } - // Read the latest version number if (read(version_file_descriptor, &latest_version_number, sizeof(obiversion_t)) < sizeof(obiversion_t)) { @@ -731,7 +673,6 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, new_column->writable = true; header = new_column->header; - header->little_endian = obi_is_little_endian(); header->header_size = header_size; header->data_size = data_size; header->line_count = nb_lines; @@ -793,9 +734,6 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, return NULL; } - // Calculate the header size - header_size = obi_get_platform_header_size(); - // Get the latest version number if it has the value -1 (not given by user) if (version_number == -1) { @@ -836,6 +774,16 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, return NULL; } + // Read the header size + if (read(column_file_descriptor, &header_size, sizeof(size_t)) < sizeof(size_t)) + { + obi_set_errno(OBICOL_UNKNOWN_ERROR); + obidebug(1, "\nError reading the header size to open a column"); + close(column_file_descriptor); + free(column); + return NULL; + } + // Fill the column structure column->dms = dms; column->column_directory = column_directory; @@ -1265,9 +1213,6 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* return NULL; } - // Calculate the header size - header_size = obi_get_platform_header_size(); // TODO read in header itself - // Get the latest version number version_number = obi_get_latest_version_number(column_directory); if (version_number < 0) @@ -1295,6 +1240,15 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* free(column_file_name); + // Read the header size + if (read(column_file_descriptor, &header_size, sizeof(size_t)) < sizeof(size_t)) + { + obi_set_errno(OBICOL_UNKNOWN_ERROR); + obidebug(1, "\nError reading the header size to read a header"); + close(column_file_descriptor); + return NULL; + } + // Fill the header structure header = mmap(NULL, header_size, diff --git a/src/obidmscolumn.h b/src/obidmscolumn.h index 399a171..bcf76bb 100644 --- a/src/obidmscolumn.h +++ b/src/obidmscolumn.h @@ -48,11 +48,6 @@ typedef int32_t obiversion_t; /**< Used to store the column version number * @brief OBIDMS column header structure. */ typedef struct OBIDMS_column_header { - bool little_endian; /**< Endianness of the column: - * - `true` on little endian platforms - * - `false` on big endian platforms - * @see obi_is_little_endian() - */ size_t header_size; /**< Size of the header in bytes. */ size_t data_size; /**< Size of the data in bytes. diff --git a/src/obierrno.h b/src/obierrno.h index 568c75f..5a0c478 100644 --- a/src/obierrno.h +++ b/src/obierrno.h @@ -65,25 +65,25 @@ extern int obi_errno; */ #define OBIDMS_ACCESS_ERROR (6) /**< Permission error trying to access the database */ - -#define OBICOLDIR_EXIST_ERROR (7) /**< Trying to create an OBIDMS column directory with a name - * that corresponds to an existing one - */ -#define OBICOLDIR_NOT_EXIST_ERROR (8) /**< Trying to open a non-existing OBIDMS column directory - */ -#define OBICOLDIR_LONG_NAME_ERROR (9) /**< The specified OBIDMS column directory name is too long - */ -#define OBICOLDIR_MEMORY_ERROR (10) /**< A memory error occurred during allocation while handling - * an OBIDMS column directory - */ -#define OBICOLDIR_UNKNOWN_ERROR (11) /**< Undetermined error while handling an OBIDMS column directory - */ -#define OBICOLDIR_ACCESS_ERROR (12) /**< Permission error trying to access an OBIDSM column directory - */ - -#define OBICOL_BAD_ENDIAN_ERROR (13) /**< The opened data structure does not corresponds +#define OBIDMS_BAD_ENDIAN_ERROR (7) /**< The opened database does not correspond * to the endianness of the platform. */ + +#define OBICOLDIR_EXIST_ERROR (8) /**< Trying to create an OBIDMS column directory with a name + * that corresponds to an existing one + */ +#define OBICOLDIR_NOT_EXIST_ERROR (9) /**< Trying to open a non-existing OBIDMS column directory + */ +#define OBICOLDIR_LONG_NAME_ERROR (10) /**< The specified OBIDMS column directory name is too long + */ +#define OBICOLDIR_MEMORY_ERROR (11) /**< A memory error occurred during allocation while handling + * an OBIDMS column directory + */ +#define OBICOLDIR_UNKNOWN_ERROR (12) /**< Undetermined error while handling an OBIDMS column directory + */ +#define OBICOLDIR_ACCESS_ERROR (13) /**< Permission error trying to access an OBIDSM column directory + */ + #define OBICOL_EXIST_ERROR (14) /**< Trying to create an OBIDMS column with a name * that corresponds to an existing one */