From b37bd8f21c61ee3ace5f6b6454591a44bd21b87b Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Mon, 9 Nov 2015 15:06:02 +0100 Subject: [PATCH] File descriptors for dms, column and array directories are now stored in structures. --- src/obiarray.c | 153 ++++++++++-------------------------------- src/obiarray.h | 3 + src/obidms.c | 62 ++++++++++------- src/obidms.h | 6 ++ src/obidmscolumn.c | 152 +++++++++-------------------------------- src/obidmscolumndir.c | 48 ++++--------- src/obidmscolumndir.h | 17 +++-- 7 files changed, 134 insertions(+), 307 deletions(-) diff --git a/src/obiarray.c b/src/obiarray.c index 628670c..fd4015d 100644 --- a/src/obiarray.c +++ b/src/obiarray.c @@ -282,38 +282,24 @@ int close_array_data(OBIDMS_array_data_p array_data) int grow_array(OBIDMS_array_p array) // TODO Lock when needed { - size_t file_size; - size_t old_data_size; - size_t new_data_size; - size_t header_size; - int array_dir_file_descriptor; - int array_file_descriptor; - char* array_file_name; - - // Get the file descriptor associated to the array directory - array_dir_file_descriptor = dirfd(array->directory); - if (array_dir_file_descriptor < 0) - { - obi_set_errno(OBI_ARRAY_ERROR); - obidebug(1, "\nError getting the file descriptor for an array directory"); - return -1; - } + size_t file_size; + size_t old_data_size; + size_t new_data_size; + size_t header_size; + int array_file_descriptor; + char* array_file_name; // Get the array file name array_file_name = build_array_file_name((array->header)->array_name); if (array_file_name == NULL) - { - close(array_dir_file_descriptor); return -1; - } // Open the array file - array_file_descriptor = openat(array_dir_file_descriptor, array_file_name, O_RDWR); + array_file_descriptor = openat(array->dir_fd, array_file_name, O_RDWR); if (array_file_descriptor < 0) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError opening an array file"); - close(array_dir_file_descriptor); free(array_file_name); return -1; } @@ -330,7 +316,6 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError enlarging an array file"); - close(array_dir_file_descriptor); close(array_file_descriptor); return -1; } @@ -341,7 +326,6 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError munmapping the array of an array file before enlarging"); - close(array_dir_file_descriptor); close(array_file_descriptor); return -1; } @@ -358,7 +342,6 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError re-mmapping the array of an array file after enlarging the file"); - close(array_dir_file_descriptor); close(array_file_descriptor); return -1; } @@ -380,38 +363,24 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed { - size_t file_size; + size_t file_size; index_t old_data_size; index_t new_data_size; - size_t header_size; - int array_dir_file_descriptor; - int array_data_file_descriptor; - char* array_data_file_name; - - // Get the file descriptor associated to the array directory - array_dir_file_descriptor = dirfd(array->directory); - if (array_dir_file_descriptor < 0) - { - obi_set_errno(OBI_ARRAY_ERROR); - obidebug(1, "\nError getting the file descriptor for an array directory"); - return -1; - } + size_t header_size; + int array_data_file_descriptor; + char* array_data_file_name; // Get the array data file name array_data_file_name = build_array_data_file_name((array->header)->array_name); if (array_data_file_name == NULL) - { - close(array_dir_file_descriptor); return -1; - } // Open the array data file - array_data_file_descriptor = openat(array_dir_file_descriptor, array_data_file_name, O_RDWR); + array_data_file_descriptor = openat(array->dir_fd, array_data_file_name, O_RDWR); if (array_data_file_descriptor < 0) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError opening an array data file"); - close(array_dir_file_descriptor); free(array_data_file_name); return -1; } @@ -428,7 +397,6 @@ int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError enlarging an array data file"); - close(array_dir_file_descriptor); close(array_data_file_descriptor); return -1; } @@ -439,7 +407,6 @@ int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError munmapping the data of an array data file before enlarging"); - close(array_dir_file_descriptor); close(array_data_file_descriptor); return -1; } @@ -456,7 +423,6 @@ int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError re-mmapping the data of an array data file after enlarging the file"); - close(array_dir_file_descriptor); close(array_data_file_descriptor); return -1; } @@ -475,7 +441,7 @@ int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed int array_compare(byte_t* value_1, byte_t* value_2) { - int comp; + int comp; uint8_t size_1; uint8_t size_2; int32_t len_1; @@ -521,31 +487,18 @@ size_t array_sizeof(byte_t* value) int obi_array_exists(OBIDMS_p dms, const char* array_name) { - struct stat buffer; - char* array_file_path; - char* array_file_name; - int check_dir; - int array_dir_file_descriptor; - - // Get the file descriptor of the array directory - array_dir_file_descriptor = dirfd(dms->array_directory); - if (array_dir_file_descriptor < 0) - { - obi_set_errno(OBI_ARRAY_ERROR); - obidebug(1, "\nError getting the file descriptor for an array directory"); - return -1; - } + struct stat buffer; + char* array_file_path; + char* array_file_name; + int check_dir; // Build file name array_file_name = build_array_file_name(array_name); if (array_file_name == NULL) - { - close(array_dir_file_descriptor); return -1; - } // Build the array file path - array_file_path = get_full_path(array_dir_file_descriptor, array_file_name); + array_file_path = get_full_path(dms->array_dir_fd, array_file_name); if (array_file_path == NULL) { obidebug(1, "\nError getting the file path for an array file"); @@ -585,8 +538,8 @@ OBIDMS_array_p obi_array(OBIDMS_p dms, const char* array_name) OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) { - char* array_file_name; - char* array_data_file_name; + char* array_file_name; + char* array_data_file_name; size_t header_size; size_t data_size; size_t file_size; @@ -598,22 +551,13 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) // Create the data file - // Get the file descriptor of the array directory - array_dir_file_descriptor = dirfd(dms->array_directory); - if (array_dir_file_descriptor < 0) - { - obi_set_errno(OBI_ARRAY_ERROR); - obidebug(1, "\nError getting the file descriptor for an array directory"); - return NULL; - } - // Build file name array_data_file_name = build_array_data_file_name(array_name); if (array_data_file_name == NULL) - { - close(array_dir_file_descriptor); return NULL; - } + + // Get the file descriptor of the array directory + array_dir_file_descriptor = dms->array_dir_fd; // Create file array_data_file_descriptor = openat(array_dir_file_descriptor, array_data_file_name, O_RDWR | O_CREAT | O_EXCL, 0777); @@ -621,7 +565,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError creating an array data file"); - close(array_dir_file_descriptor); free(array_data_file_name); return NULL; } @@ -637,7 +580,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError truncating an array data file to the right size"); - close(array_dir_file_descriptor); close(array_data_file_descriptor); return NULL; } @@ -648,7 +590,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError allocating the memory for the array data structure"); - close(array_dir_file_descriptor); close(array_data_file_descriptor); return NULL; } @@ -665,7 +606,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError mmapping the header of an array data file"); - close(array_dir_file_descriptor); close(array_data_file_descriptor); free(array_data); return NULL; @@ -683,7 +623,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError mmapping the data of an array data file"); munmap(array_data->header, header_size); - close(array_dir_file_descriptor); close(array_data_file_descriptor); free(array_data); return NULL; @@ -708,7 +647,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) array_file_name = build_array_file_name(array_name); if (array_file_name == NULL) { - close(array_dir_file_descriptor); close_array_data(array_data); return NULL; } @@ -724,7 +662,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError creating an array file"); - close(array_dir_file_descriptor); close_array_data(array_data); free(array_file_name); return NULL; @@ -736,7 +673,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError truncating an array file to the right size"); - close(array_dir_file_descriptor); close_array_data(array_data); close(array_file_descriptor); return NULL; @@ -748,7 +684,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError allocating the memory for the array structure"); - close(array_dir_file_descriptor); close_array_data(array_data); close(array_file_descriptor); return NULL; @@ -766,7 +701,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError mmapping the header of an array file"); - close(array_dir_file_descriptor); close_array_data(array_data); close(array_file_descriptor); free(array); @@ -784,7 +718,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError mmapping the data of an array file"); - close(array_dir_file_descriptor); close_array_data(array_data); munmap(array->header, header_size); close(array_file_descriptor); @@ -794,6 +727,7 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) array->data = array_data; array->directory = dms->array_directory; + array->dir_fd = array_dir_file_descriptor; (array->header)->header_size = header_size; (array->header)->array_size = data_size; @@ -825,21 +759,12 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) // Open the data file // Get the file descriptor of the array directory - array_dir_file_descriptor = dirfd(dms->array_directory); - if (array_dir_file_descriptor < 0) - { - obi_set_errno(OBI_ARRAY_ERROR); - obidebug(1, "\nError getting the file descriptor for an array directory"); - return NULL; - } + array_dir_file_descriptor = dms->array_dir_fd; // Build file name array_data_file_name = build_array_data_file_name(array_name); if (array_data_file_name == NULL) - { - close(array_dir_file_descriptor); return NULL; - } // Open file array_data_file_descriptor = openat(array_dir_file_descriptor, array_data_file_name, O_RDWR, 0777); @@ -847,7 +772,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError opening an array data file"); - close(array_dir_file_descriptor); free(array_data_file_name); return NULL; } @@ -859,7 +783,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError allocating the memory for the array data structure"); - close(array_dir_file_descriptor); close(array_data_file_descriptor); return NULL; } @@ -877,7 +800,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError mmapping the header of an array data file"); - close(array_dir_file_descriptor); close(array_data_file_descriptor); free(array_data); return NULL; @@ -894,7 +816,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError mmapping the data of an array data file"); - close(array_dir_file_descriptor); munmap(array_data->header, header_size); close(array_data_file_descriptor); free(array_data); @@ -910,7 +831,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) array_file_name = build_array_file_name(array_name); if (array_file_name == NULL) { - close(array_dir_file_descriptor); close_array_data(array_data); return NULL; } @@ -921,7 +841,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError opening an array file"); - close(array_dir_file_descriptor); close_array_data(array_data); free(array_file_name); return NULL; @@ -934,7 +853,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError allocating the memory for the array structure"); - close(array_dir_file_descriptor); close_array_data(array_data); close(array_file_descriptor); return NULL; @@ -953,7 +871,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError mmapping the header of an array file"); - close(array_dir_file_descriptor); close_array_data(array_data); close(array_file_descriptor); free(array); @@ -971,7 +888,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) { obi_set_errno(OBI_ARRAY_ERROR); obidebug(1, "\nError mmapping the data of an array file"); - close(array_dir_file_descriptor); close_array_data(array_data); munmap(array->header, header_size); close(array_file_descriptor); @@ -981,6 +897,7 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name) array->data = array_data; array->directory = dms->array_directory; + array->dir_fd = array_dir_file_descriptor; close(array_file_descriptor); @@ -1090,15 +1007,15 @@ byte_t* obi_array_get(OBIDMS_array_p array, index_t idx) index_t obi_array_search(OBIDMS_array_p array, byte_t* value) { - index_t idx_min; - index_t idx_max; - index_t idx_mid; - index_t idx; - index_t* a; - byte_t* data; - byte_t* to_compare; - index_t nb_items; - int comp; + index_t idx_min; + index_t idx_max; + index_t idx_mid; + index_t idx; + index_t* a; + byte_t* data; + byte_t* to_compare; + index_t nb_items; + int comp; nb_items = (array->header)->nb_items; data = (array->data)->data; diff --git a/src/obiarray.h b/src/obiarray.h index 26c2226..df7997f 100644 --- a/src/obiarray.h +++ b/src/obiarray.h @@ -101,6 +101,9 @@ typedef struct OBIDMS_array { DIR* directory; /**< A directory entry usable to * refer and scan the array directory. */ + int dir_fd; /**< The file descriptor of the directory entry + * usable to refer and scan the array directory. + */ } OBIDMS_array_t, *OBIDMS_array_p; diff --git a/src/obidms.c b/src/obidms.c index 20e69c8..2162394 100644 --- a/src/obidms.c +++ b/src/obidms.c @@ -164,7 +164,6 @@ OBIDMS_p obi_create_dms(const char* dms_name) return NULL; } - return obi_open_dms(dms_name); } @@ -173,19 +172,32 @@ OBIDMS_p obi_open_dms(const char* dms_name) { OBIDMS_p dms; char* directory_name; - DIR* directory; - int dms_file_descriptor; dms = NULL; + // Allocate the data structure + dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t)); + if (dms == NULL) + { + obi_set_errno(OBIDMS_MEMORY_ERROR); + obidebug(1, "\nError allocating the memory for the OBIDMS structure"); + return NULL; + } + // Build and check the directory name directory_name = build_directory_name(dms_name); if (directory_name == NULL) + { + free(dms); return NULL; + } + + strncpy(dms->directory_name, directory_name, OBIDMS_MAX_NAME); + free(directory_name); // Try to open the directory - directory = opendir(directory_name); - if (directory == NULL) + dms->directory = opendir(dms->directory_name); + if (dms->directory == NULL) { switch (errno) { @@ -205,45 +217,43 @@ OBIDMS_p obi_open_dms(const char* dms_name) obi_set_errno(OBIDMS_UNKNOWN_ERROR); } obidebug(1, "\nCan't open OBIDMS directory"); - free(directory_name); + free(dms); return NULL; } - // Allocate the data structure - dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t)); - if (dms == NULL) - { - obi_set_errno(OBIDMS_MEMORY_ERROR); - obidebug(1, "\nError allocating the memory for the OBIDMS structure"); - free(directory_name); - return NULL; - } - - // Initialize the data structure - strcpy(dms->directory_name, directory_name); - dms->directory = directory; - // Get file descriptor of DMS directory to open the arrays directory - dms_file_descriptor = dirfd(directory); - if (dms_file_descriptor < 0) + dms->dir_fd = dirfd(dms->directory); + if (dms->dir_fd < 0) { obi_set_errno(OBIDMS_UNKNOWN_ERROR); obidebug(1, "\nError getting the file descriptor for a newly created OBIDMS directory"); - free(directory_name); + closedir(dms->directory); + free(dms); return NULL; } // Open the arrays directory - dms->array_directory = private_opendirat(dms_file_descriptor, ARRAY_DIR_NAME); + dms->array_directory = private_opendirat(dms->dir_fd, ARRAY_DIR_NAME); if (dms->array_directory == NULL) { obi_set_errno(OBIDMS_UNKNOWN_ERROR); obidebug(1, "\nError opening the arrays directory"); - free(directory_name); + closedir(dms->directory); + free(dms); return NULL; } - free(directory_name); + // Store the array directory's file descriptor + dms->array_dir_fd = dirfd(dms->array_directory); + if (dms->array_dir_fd < 0) + { + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + obidebug(1, "\nError opening the arrays directory"); + closedir(dms->directory); + closedir(dms->array_directory); + free(dms); + return NULL; + } return dms; } diff --git a/src/obidms.h b/src/obidms.h index f26cf7f..8e371fb 100644 --- a/src/obidms.h +++ b/src/obidms.h @@ -43,9 +43,15 @@ typedef struct OBIDMS { DIR* directory; /**< A directory entry usable to * refer and scan the database directory. */ + int dir_fd; /**< The file descriptor of the directory entry + * usable to refer and scan the database directory. + */ DIR* array_directory; /**< A directory entry usable to * refer and scan the array directory. */ + int array_dir_fd; /**< The file descriptor of the directory entry + * usable to refer and scan the array directory. + */ } OBIDMS_t, *OBIDMS_p; diff --git a/src/obidmscolumn.c b/src/obidmscolumn.c index 216fb9c..9f66419 100644 --- a/src/obidmscolumn.c +++ b/src/obidmscolumn.c @@ -190,7 +190,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ off_t loc_size; obiversion_t new_version_number; char* version_file_name; - int column_dir_file_descriptor; int version_file_descriptor; bool little_endian; int lock_mode; @@ -209,18 +208,8 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_ if (version_file_name == NULL) return -1; - // Get the file descriptor associated to the column directory - column_dir_file_descriptor = dirfd(column_directory->directory); - if (column_dir_file_descriptor < 0) - { - obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError getting the file descriptor for a column file directory"); - free(version_file_name); - return -1; - } - // Open the version file - version_file_descriptor = openat(column_dir_file_descriptor, version_file_name, O_RDWR); + version_file_descriptor = openat(column_directory->dir_fd, version_file_name, O_RDWR); if (version_file_descriptor < 0) { if (errno == ENOENT) @@ -338,7 +327,6 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo off_t loc_size; obiversion_t version_number; char* version_file_name; - int column_dir_file_descriptor; int version_file_descriptor; bool little_endian; @@ -349,18 +337,8 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo if (version_file_name == NULL) return -1; - // Get the file descriptor associated to the column directory - column_dir_file_descriptor = dirfd(column_directory->directory); - if (column_dir_file_descriptor < 0) - { - obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError getting the file descriptor for a column directory"); - free(version_file_name); - return -1; - } - // Get the file descriptor associated to the version file - version_file_descriptor = openat(column_dir_file_descriptor, version_file_name, O_RDWR | O_CREAT, 0777); + version_file_descriptor = openat(column_directory->dir_fd, version_file_name, O_RDWR | O_CREAT, 0777); if (version_file_descriptor < 0) { obi_set_errno(OBICOL_UNKNOWN_ERROR); @@ -465,7 +443,6 @@ obiversion_t obi_get_latest_version_number(OBIDMS_column_directory_p column_dire off_t loc_size; obiversion_t latest_version_number; char * version_file_name; - int column_dir_file_descriptor; int version_file_descriptor; bool little_endian; @@ -476,18 +453,8 @@ obiversion_t obi_get_latest_version_number(OBIDMS_column_directory_p column_dire if (version_file_name==NULL) return -1; - // Get the file descriptor associated to the column directory - column_dir_file_descriptor = dirfd(column_directory->directory); - if (column_dir_file_descriptor < 0) - { - obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError getting the file descriptor for a column directory"); - free(version_file_name); - return -1; - } - // Get the file descriptor associated to the version file - version_file_descriptor = openat(column_dir_file_descriptor, version_file_name, O_RDONLY); + version_file_descriptor = openat(column_directory->dir_fd, version_file_name, O_RDONLY); if (version_file_descriptor < 0) { obi_set_errno(OBICOL_UNKNOWN_ERROR); @@ -596,7 +563,6 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, obiversion_t version_number; char* column_file_name; int column_file_descriptor; - int column_dir_file_descriptor; size_t header_size; size_t data_size; index_t minimum_line_count; @@ -674,15 +640,6 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, return NULL; } - // Get the file descriptor associated to the column directory - column_dir_file_descriptor = dirfd(column_directory->directory); - if (column_dir_file_descriptor < 0) - { - obi_set_errno(OBICOLDIR_UNKNOWN_ERROR); - obidebug(1, "\nError getting the file descriptor for a column directory"); - return NULL; - } - // Calculate the size needed header_size = obi_get_platform_header_size(); data_size = obi_array_sizeof(data_type, nb_lines, nb_elements_per_line); @@ -703,7 +660,7 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms, } // Open the column file - column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR | O_CREAT, 0777); + column_file_descriptor = openat(column_directory->dir_fd, column_file_name, O_RDWR | O_CREAT, 0777); if (column_file_descriptor < 0) { obi_set_errno(OBICOL_UNKNOWN_ERROR); @@ -823,7 +780,6 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, OBIDMS_array_p array; char* column_file_name; int column_file_descriptor; - int column_dir_file_descriptor; size_t header_size; size_t data_size; @@ -837,15 +793,6 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, return NULL; } - // Get the file descriptor associated to the column directory - column_dir_file_descriptor = dirfd(column_directory->directory); - if (column_dir_file_descriptor < 0) - { - obi_set_errno(OBICOLDIR_UNKNOWN_ERROR); - obidebug(1, "\nError getting the file descriptor for a column directory"); - return NULL; - } - // Calculate the header size header_size = obi_get_platform_header_size(); @@ -868,7 +815,7 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, } // Open the column file, ALWAYS READ-ONLY - column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDONLY); + column_file_descriptor = openat(column_directory->dir_fd, column_file_name, O_RDONLY); if (column_file_descriptor < 0) { obi_set_errno(OBICOL_UNKNOWN_ERROR); @@ -909,8 +856,6 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms, return NULL; } - // TODO Check endianness? - // Compute data size from the informations in the header data_size = obi_array_sizeof((column->header)->data_type, (column->header)->line_count, (column->header)->nb_elements_per_line); @@ -1045,7 +990,7 @@ int obi_close_column(OBIDMS_column_p column) return -1; } - obi_close_column_directory(column->column_directory); + obi_close_column_directory(column->column_directory); // TODO or not free(column); @@ -1055,13 +1000,12 @@ int obi_close_column(OBIDMS_column_p column) int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it necessary to unmap/remap? { - size_t file_size; - size_t data_size; + size_t file_size; + size_t data_size; index_t new_line_count; - double multiple; - int column_dir_file_descriptor; - int column_file_descriptor; - char* column_file_name; + double multiple; + int column_file_descriptor; + char* column_file_name; // Compute the new line count = the number of lines used rounded to the nearest greater multiple of page size greater than 0 multiple = ceil((double) (ONE_IF_ZERO((column->header)->lines_used) * (column->header)->nb_elements_per_line * obi_sizeof((column->header)->data_type)) / (double) getpagesize()); @@ -1071,15 +1015,6 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece if ((column->header)->line_count == new_line_count) return 0; - // Get the file descriptor associated to the column directory - column_dir_file_descriptor = dirfd((column->column_directory)->directory); - if (column_dir_file_descriptor < 0) - { - obi_set_errno(OBICOLDIR_UNKNOWN_ERROR); - obidebug(1, "\nError getting the file descriptor for a column directory"); - return -1; - } - // Get the column file name column_file_name = build_column_file_name((column->header)->name, (column->header)->version); if (column_file_name == NULL) @@ -1088,7 +1023,7 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece } // Open the column file - column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR); + column_file_descriptor = openat((column->column_directory)->dir_fd, column_file_name, O_RDWR); if (column_file_descriptor < 0) { obi_set_errno(OBICOL_UNKNOWN_ERROR); @@ -1121,13 +1056,13 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece } // Remap the data - column->data = mmap(NULL, - data_size, - PROT_READ | PROT_WRITE, - MAP_SHARED, - column_file_descriptor, - (column->header)->header_size - ); + column->data = mmap(NULL, + data_size, + PROT_READ | PROT_WRITE, + MAP_SHARED, + column_file_descriptor, + (column->header)->header_size + ); if (column->data == MAP_FAILED) { @@ -1148,25 +1083,15 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece int obi_enlarge_column(OBIDMS_column_p column) { - size_t file_size; - size_t old_data_size; - size_t new_data_size; - size_t header_size; + size_t file_size; + size_t old_data_size; + size_t new_data_size; + size_t header_size; index_t old_line_count; index_t new_line_count; - int column_dir_file_descriptor; - int column_file_descriptor; - char* column_file_name; - void* new_data; - - // Get the file descriptor associated to the column directory - column_dir_file_descriptor = dirfd((column->column_directory)->directory); - if (column_dir_file_descriptor < 0) - { - obi_set_errno(OBICOLDIR_UNKNOWN_ERROR); - obidebug(1, "\nError getting the file descriptor for a column directory"); - return -1; - } + int column_file_descriptor; + char* column_file_name; + void* new_data; // Get the column file name column_file_name = build_column_file_name((column->header)->name, (column->header)->version); @@ -1176,7 +1101,7 @@ int obi_enlarge_column(OBIDMS_column_p column) } // Open the column file - column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR); + column_file_descriptor = openat((column->column_directory)->dir_fd, column_file_name, O_RDWR); if (column_file_descriptor < 0) { obi_set_errno(OBICOL_UNKNOWN_ERROR); @@ -1336,7 +1261,6 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* OBIDMS_column_directory_p column_directory; char* column_file_name; int column_file_descriptor; - int column_dir_file_descriptor; size_t header_size; obiversion_t version_number; @@ -1348,16 +1272,6 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* return NULL; } - // Get the file descriptor associated to the column directory - column_dir_file_descriptor = dirfd(column_directory->directory); - if (column_dir_file_descriptor < 0) - { - obi_set_errno(OBICOLDIR_UNKNOWN_ERROR); - obidebug(1, "\nError getting the file descriptor of a column directory"); - obi_close_column_directory(column_directory); - return NULL; - } - // Calculate the header size header_size = obi_get_platform_header_size(); // TODO read in header itself @@ -1377,7 +1291,7 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* } // Open the column file (READ-ONLY) - column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDONLY); + column_file_descriptor = openat(column_directory->dir_fd, column_file_name, O_RDONLY); if (column_file_descriptor < 0) { obidebug(1, "\nError opening a column file"); @@ -1405,8 +1319,6 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* return NULL; } - // TODO Check endianness? - close(column_file_descriptor); return header; @@ -1428,8 +1340,8 @@ int obi_unmap_header(OBIDMS_column_header_p header) // TODO to be rewritten in an optimized and safe way index_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char* element_name) { - char* elements_names; - char* name; + char* elements_names; + char* name; index_t element_index; elements_names = strdup((column->header)->elements_names); @@ -1468,8 +1380,8 @@ index_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const cha char* obi_column_format_date(time_t date) { - char* formatted_time; - struct tm* tmp; + char* formatted_time; + struct tm* tmp; formatted_time = (char*) malloc(FORMATTED_TIME_LENGTH*sizeof(char)); diff --git a/src/obidmscolumndir.c b/src/obidmscolumndir.c index 850320b..1ffb642 100644 --- a/src/obidmscolumndir.c +++ b/src/obidmscolumndir.c @@ -97,25 +97,14 @@ int obi_column_directory_exists(OBIDMS_p dms, const char* column_name) char* column_directory_name; char* full_path; int check_dir; - int dms_file_descriptor; // Build and check the directory name column_directory_name = build_column_directory_name(column_name); if (column_directory_name == NULL) return -1; - // Get the file descriptor for the dms - dms_file_descriptor = dirfd(dms->directory); - if (dms_file_descriptor < 0) - { - obi_set_errno(OBIDMS_UNKNOWN_ERROR); - obidebug(1, "\nError getting a file descriptor for an OBIDMS directory"); - free(column_directory_name); - return -1; - } - // Get the full path for the column directory - full_path = get_full_path(dms_file_descriptor, column_directory_name); + full_path = get_full_path(dms->dir_fd, column_directory_name); if (full_path == NULL) { obi_set_errno(OBICOLDIR_UNKNOWN_ERROR); @@ -139,7 +128,6 @@ int obi_column_directory_exists(OBIDMS_p dms, const char* column_name) OBIDMS_column_directory_p obi_create_column_directory(OBIDMS_p dms, const char* column_name) { char* column_directory_name; - int dms_file_descriptor; // Build and check the directory name column_directory_name = build_column_directory_name(column_name); @@ -149,18 +137,8 @@ OBIDMS_column_directory_p obi_create_column_directory(OBIDMS_p dms, const char* return NULL; } - // Get the file descriptor for the dms - dms_file_descriptor = dirfd(dms->directory); - if (dms_file_descriptor < 0) - { - obi_set_errno(OBIDMS_UNKNOWN_ERROR); - obidebug(1, "\nError getting a file descriptor for an OBIDMS directory"); - free(column_directory_name); - return NULL; - } - // Try to create the directory - if (mkdirat(dms_file_descriptor, column_directory_name, 00777) < 0) + if (mkdirat(dms->dir_fd, column_directory_name, 00777) < 0) { if (errno == EEXIST) obi_set_errno(OBICOLDIR_EXIST_ERROR); @@ -182,7 +160,6 @@ OBIDMS_column_directory_p obi_open_column_directory(OBIDMS_p dms, const char* co OBIDMS_column_directory_p column_directory; char* column_directory_name; DIR* directory; - int dms_file_descriptor; column_directory = NULL; @@ -191,18 +168,8 @@ OBIDMS_column_directory_p obi_open_column_directory(OBIDMS_p dms, const char* co if (column_directory_name == NULL) return NULL; - // Get the file descriptor for the dms - dms_file_descriptor = dirfd(dms->directory); - if (dms_file_descriptor < 0) - { - obi_set_errno(OBIDMS_UNKNOWN_ERROR); - obidebug(1, "\nError getting a file descriptor for an OBIDMS directory"); - free(column_directory_name); - return NULL; - } - // Try to open the column directory - directory = private_opendirat(dms_file_descriptor, column_directory_name); + directory = private_opendirat(dms->dir_fd, column_directory_name); if (directory == NULL) { switch (errno) { @@ -241,6 +208,15 @@ OBIDMS_column_directory_p obi_open_column_directory(OBIDMS_p dms, const char* co strcpy(column_directory->column_name, column_name); column_directory->directory = directory; + column_directory->dir_fd = dirfd(directory); + if (column_directory->dir_fd < 0) + { + obi_set_errno(OBICOLDIR_MEMORY_ERROR); + obidebug(1, "\nError allocating the memory for an OBIDMS column directory structure"); + free(column_directory_name); + free(column_directory); + } + free(column_directory_name); return column_directory; diff --git a/src/obidmscolumndir.h b/src/obidmscolumndir.h index 8ed5045..2ec8a4f 100644 --- a/src/obidmscolumndir.h +++ b/src/obidmscolumndir.h @@ -33,16 +33,19 @@ */ typedef struct OBIDMS_column_directory { OBIDMS_p dms; /**< A pointer to a DMS instance. - */ + */ char column_name[OBIDMS_COLUMN_MAX_NAME+1]; /**< The name of the column - * contained in the directory. - */ + * contained in the directory. + */ char directory_name[OBIDMS_COLUMN_MAX_NAME+1]; /**< The name of the directory - * containing the column. - */ + * containing the column. + */ DIR* directory; /**< A directory entry usable to - * refer and scan the database directory. - */ + * refer and scan the column directory. + */ + int dir_fd; /**< The file descriptor of the directory entry + * usable to refer and scan the column directory. + */ } OBIDMS_column_directory_t, *OBIDMS_column_directory_p;