From b04b4b5902b566da7a14ab0bef134e9f21128287 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Mon, 21 Mar 2016 11:33:06 +0100 Subject: [PATCH] made POSIX compliant --- src/obiavl.c | 20 ++++++++++++++------ src/obidms.c | 8 +++++--- src/obidms_taxonomy.c | 5 +++-- src/obidmscolumn.c | 8 ++++++-- src/obidmscolumndir.c | 7 ++++--- src/obiview.c | 5 +++-- src/private_at_functions.c | 16 ++++++++++------ src/private_at_functions.h | 5 +++-- 8 files changed, 48 insertions(+), 26 deletions(-) diff --git a/src/obiavl.c b/src/obiavl.c index 6f79fe3..3eb7450 100644 --- a/src/obiavl.c +++ b/src/obiavl.c @@ -354,7 +354,8 @@ static char* build_avl_file_name(const char* avl_name) char* file_name; // Build the file name - if (asprintf(&file_name,"%s.oda", avl_name) < 0) + file_name = (char*) malloc((strlen(avl_name) + 5)*sizeof(char)); + if (sprintf(file_name,"%s.oda", avl_name) < 0) { obi_set_errno(OBI_AVL_ERROR); obidebug(1, "\nError building an AVL tree file name"); @@ -379,7 +380,8 @@ static char* build_avl_data_file_name(const char* avl_name) char* file_name; // Build the file name - if (asprintf(&file_name,"%s.odd", avl_name) < 0) + file_name = (char*) malloc((strlen(avl_name) + 5)*sizeof(char)); + if (sprintf(file_name,"%s.odd", avl_name) < 0) { obi_set_errno(OBI_AVL_ERROR); obidebug(1, "\nError building an AVL tree data file name"); @@ -942,15 +944,20 @@ int obi_avl_exists(OBIDMS_p dms, const char* avl_name) struct stat buffer; char* avl_file_path; char* avl_file_name; + char* avl_file_relative_path; + int relative_path_size; int check_dir; - // Build file name + // Build the AVL tree file path avl_file_name = build_avl_file_name(avl_name); if (avl_file_name == NULL) return -1; - - // Build the AVL tree file path - avl_file_path = get_full_path(dms->avl_dir_fd, avl_file_name); + relative_path_size = strlen(avl_file_name) + strlen(AVL_TREES_DIR_NAME) + 2; + avl_file_relative_path = (char*) malloc(relative_path_size*sizeof(char)); + strcpy(avl_file_relative_path, AVL_TREES_DIR_NAME); + strcat(avl_file_relative_path, "/"); + strcat(avl_file_relative_path, avl_file_name); + avl_file_path = get_full_path(dms, avl_file_relative_path); if (avl_file_path == NULL) { obidebug(1, "\nError getting the file path for an AVL tree file"); @@ -961,6 +968,7 @@ int obi_avl_exists(OBIDMS_p dms, const char* avl_name) free(avl_file_path); free(avl_file_name); + free(avl_file_relative_path); if (check_dir == 0) return 1; diff --git a/src/obidms.c b/src/obidms.c index 68a27c3..4d3a646 100644 --- a/src/obidms.c +++ b/src/obidms.c @@ -105,7 +105,8 @@ static char* build_directory_name(const char* dms_name) char* directory_name; // Build the database directory name - if (asprintf(&directory_name, "%s.obidms", dms_name) < 0) + directory_name = (char*) malloc((strlen(dms_name) + 8)*sizeof(char)); + if (sprintf(directory_name, "%s.obidms", dms_name) < 0) { obi_set_errno(OBIDMS_MEMORY_ERROR); obidebug(1, "\nProblem building an OBIDMS directory name"); @@ -130,7 +131,8 @@ static char* build_infos_file_name(const char* dms_name) char* file_name; // Build file name - if (asprintf(&file_name, "%s_infos", dms_name) < 0) + file_name = (char*) malloc((strlen(dms_name) + 7)*sizeof(char)); + if (sprintf(file_name, "%s_infos", dms_name) < 0) { obi_set_errno(OBIDMS_MEMORY_ERROR); obidebug(1, "\nProblem building an informations file name"); @@ -391,7 +393,7 @@ OBIDMS_p obi_open_dms(const char* dms_name) dms->little_endian = little_endian_dms; // Open the AVL trees directory - dms->avl_directory = private_opendirat(dms->dir_fd, AVL_TREES_DIR_NAME); + dms->avl_directory = opendir_in_dms(dms, AVL_TREES_DIR_NAME); if (dms->avl_directory == NULL) { obi_set_errno(OBIDMS_UNKNOWN_ERROR); diff --git a/src/obidms_taxonomy.c b/src/obidms_taxonomy.c index f280cf2..4701215 100644 --- a/src/obidms_taxonomy.c +++ b/src/obidms_taxonomy.c @@ -379,8 +379,9 @@ OBIDMS_taxonomy_p obi_read_taxonomy(OBIDMS_p dms, const char* taxonomy_name, boo buffer_size = 2048; // TODO - main_taxonomy_dir_path = get_full_path(dms->dir_fd, TAXONOMY_DIR_NAME); - if (asprintf(&taxonomy_path, "%s/%s/%s", main_taxonomy_dir_path, taxonomy_name, taxonomy_name) < 0) + main_taxonomy_dir_path = get_full_path(dms, TAXONOMY_DIR_NAME); + taxonomy_path = (char*) malloc((strlen(main_taxonomy_dir_path) + strlen(taxonomy_name) + strlen(taxonomy_name) + 3)*sizeof(char)); + if (sprintf(taxonomy_path, "%s/%s/%s", main_taxonomy_dir_path, taxonomy_name, taxonomy_name) < 0) { free(main_taxonomy_dir_path); obi_close_taxonomy(tax); diff --git a/src/obidmscolumn.c b/src/obidmscolumn.c index 4f5f4bc..4eaf0f3 100644 --- a/src/obidmscolumn.c +++ b/src/obidmscolumn.c @@ -156,9 +156,12 @@ static index_t get_line_count_per_page(OBIType_t data_type, index_t nb_elements_ static char* build_column_file_name(const char* column_name, obiversion_t version_number) { char* file_name; + int version_number_length; // Build the file name - if (asprintf(&file_name,"%s@%d.odc", column_name, version_number) < 0) + version_number_length = (version_number == 0 ? 1 : (int)(log10(version_number)+1)); + file_name = (char*) malloc((strlen(column_name) + version_number_length + 6)*sizeof(char)); // TODO check the mallocs... + if (sprintf(file_name,"%s@%d.odc", column_name, version_number) < 0) { obi_set_errno(OBICOL_MEMORY_ERROR); obidebug(1, "\nError building a column file name"); @@ -174,7 +177,8 @@ static char* build_version_file_name(const char* column_name) char* file_name; // Build the file name - if (asprintf(&file_name,"%s.odv", column_name) < 0) + file_name = (char*) malloc((strlen(column_name) + 5)*sizeof(char)); + if (sprintf(file_name,"%s.odv", column_name) < 0) { obi_set_errno(OBICOL_MEMORY_ERROR); obidebug(1, "\nError building a version file name"); diff --git a/src/obidmscolumndir.c b/src/obidmscolumndir.c index 1ffb642..bfe6d1e 100644 --- a/src/obidmscolumndir.c +++ b/src/obidmscolumndir.c @@ -65,7 +65,8 @@ static char* build_column_directory_name(const char* column_name) char* column_directory_name; // Build the database directory name - if (asprintf(&column_directory_name, "%s.obicol", column_name) < 0) + column_directory_name = (char*) malloc((strlen(column_name) + 8)*sizeof(char)); + if (sprintf(column_directory_name, "%s.obicol", column_name) < 0) { obi_set_errno(OBICOLDIR_MEMORY_ERROR); obidebug(1, "\nError building a column directory name"); @@ -104,7 +105,7 @@ int obi_column_directory_exists(OBIDMS_p dms, const char* column_name) return -1; // Get the full path for the column directory - full_path = get_full_path(dms->dir_fd, column_directory_name); + full_path = get_full_path(dms, column_directory_name); if (full_path == NULL) { obi_set_errno(OBICOLDIR_UNKNOWN_ERROR); @@ -169,7 +170,7 @@ OBIDMS_column_directory_p obi_open_column_directory(OBIDMS_p dms, const char* co return NULL; // Try to open the column directory - directory = private_opendirat(dms->dir_fd, column_directory_name); + directory = opendir_in_dms(dms, column_directory_name); if (directory == NULL) { switch (errno) { diff --git a/src/obiview.c b/src/obiview.c index c700646..73da4be 100644 --- a/src/obiview.c +++ b/src/obiview.c @@ -84,7 +84,8 @@ static char* build_obiview_file_name() char* file_name; // Build file name - if (asprintf(&file_name, OBIVIEW_FILE_NAME) < 0) + file_name = (char*) malloc((strlen(OBIVIEW_FILE_NAME) + 1)*sizeof(char)); + if (sprintf(file_name, OBIVIEW_FILE_NAME) < 0) { obi_set_errno(OBIVIEW_ERROR); obidebug(1, "\nProblem building an obiview file name"); @@ -984,7 +985,7 @@ int obi_save_view(Obiview_p view) return -1; // Get the full path for the column directory - full_path = get_full_path((view->dms)->dir_fd, view_file_name); + full_path = get_full_path(view->dms, view_file_name); if (full_path == NULL) { obi_set_errno(OBIVIEW_ERROR); diff --git a/src/private_at_functions.c b/src/private_at_functions.c index debacab..3207fd7 100644 --- a/src/private_at_functions.c +++ b/src/private_at_functions.c @@ -17,16 +17,18 @@ #include #include #include +#include #include "private_at_functions.h" #include "obidebug.h" #include "obierrno.h" +#include "obidms.h" #define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?) -char* get_full_path(int directory_file_descriptor, const char* path_name) +char* get_full_path(OBIDMS_p dms, const char* path_name) { char* full_path; @@ -37,26 +39,28 @@ char* get_full_path(int directory_file_descriptor, const char* path_name) return NULL; } - if (fcntl(directory_file_descriptor, F_GETPATH, full_path) < 0) + if (getcwd(full_path, MAX_PATH_LEN) == NULL) { obidebug(1, "\nError getting the path to a file or directory"); return NULL; } // TODO check errors? - strlcat(full_path, "/", MAX_PATH_LEN); - strlcat(full_path, path_name, MAX_PATH_LEN); + strcat(full_path, "/"); + strcat(full_path, dms->directory_name); + strcat(full_path, "/"); + strcat(full_path, path_name); return full_path; } -DIR* private_opendirat(int directory_file_descriptor, const char* path_name) +DIR* opendir_in_dms(OBIDMS_p dms, const char* path_name) { char* full_path; DIR* directory; - full_path = get_full_path(directory_file_descriptor, path_name); + full_path = get_full_path(dms, path_name); if (full_path == NULL) return NULL; diff --git a/src/private_at_functions.h b/src/private_at_functions.h index 51e47a4..668f80a 100644 --- a/src/private_at_functions.h +++ b/src/private_at_functions.h @@ -16,6 +16,7 @@ #include +#include "obidms.h" #define MAX_PATH_LEN 4096 /**< Maximum length for the character string defining a file or directory path */ @@ -37,7 +38,7 @@ * @since June 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -char* get_full_path(int directory_file_descriptor, const char* path_name); +char* get_full_path(OBIDMS_p dms, const char* path_name); /** @@ -52,7 +53,7 @@ char* get_full_path(int directory_file_descriptor, const char* path_name); * @since June 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -DIR* private_opendirat(int directory_file_descriptor, const char* path_name); +DIR* opendir_in_dms(OBIDMS_p dms, const char* path_name); #endif /* PRIVATEOPENAT_H_ */