From 5b2e370ffb25a968d98f6d946331d516a0e7a626 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Mon, 29 Aug 2016 17:30:31 +0200 Subject: [PATCH] Fixed a bug when using an absolute path for a DMS --- src/obidms.c | 57 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/src/obidms.c b/src/obidms.c index 6f7eb23..750e203 100644 --- a/src/obidms.c +++ b/src/obidms.c @@ -237,6 +237,8 @@ OBIDMS_p obi_create_dms(const char* dms_path) if (directory_name == NULL) return NULL; + fprintf(stderr, "\ndirectory created = %s\n", directory_name); + // Try to create the directory if (mkdir(directory_name, 00777) < 0) { @@ -308,12 +310,12 @@ OBIDMS_p obi_create_dms(const char* dms_path) OBIDMS_p obi_open_dms(const char* dms_path) { OBIDMS_p dms; - char* relative_directory_path; + char* directory_name; char* infos_file_name; int infos_file_descriptor; bool little_endian_dms; bool little_endian_platform; - size_t i, j; + size_t i, j, k; dms = NULL; @@ -326,26 +328,6 @@ OBIDMS_p obi_open_dms(const char* dms_path) return NULL; } - // Build and check the directory name - relative_directory_path = build_directory_name(dms_path); - if (relative_directory_path == NULL) - { - free(dms); - return NULL; - } - - // Build and store the absolute path to the DMS - if (getcwd(dms->directory_path, MAX_PATH_LEN) == NULL) - { - obi_set_errno(OBIDMS_UNKNOWN_ERROR); - obidebug(1, "\nError getting the absolute path to the current working directory"); - free(relative_directory_path); - return NULL; - } - strcat(dms->directory_path, "/"); - strcat(dms->directory_path, relative_directory_path); - free(relative_directory_path); - // Isolate and store the dms name j = 0; for (i=0; idms_name, dms_path+j); + // Build and check the directory name + directory_name = build_directory_name(dms->dms_name); + if (directory_name == NULL) + { + free(dms); + return NULL; + } + + // Build and store the absolute path to the DMS directory // TODO rework this, make function? potential problem with relative paths + if (dms_path[0] == '/') // The path is already absolute + { + strncpy(dms->directory_path, dms_path, j); + strcpy((dms->directory_path)+j, directory_name); + } + else // The path is relative + { + if (getcwd(dms->directory_path, MAX_PATH_LEN) == NULL) + { + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + obidebug(1, "\nError getting the absolute path to the current working directory"); + free(directory_name); + return NULL; + } + strcat(dms->directory_path, "/"); + k = strlen(dms->directory_path); + strncat(dms->directory_path, dms_path, j); + strcpy((dms->directory_path)+k+j, directory_name); + } + + free(directory_name); + // Try to open the directory dms->directory = opendir(dms->directory_path); if (dms->directory == NULL)