Relative paths when creating or opening a DMS now work
This commit is contained in:
75
src/obidms.c
75
src/obidms.c
@ -18,6 +18,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "obidms.h"
|
||||
#include "obierrno.h"
|
||||
@ -202,14 +203,14 @@ int create_dms_infos_file(int dms_file_descriptor, const char* dms_name)
|
||||
*
|
||||
**********************************************************************/
|
||||
|
||||
int obi_dms_exists(const char* dms_name)
|
||||
int obi_dms_exists(const char* dms_path)
|
||||
{
|
||||
struct stat buffer;
|
||||
char* directory_name;
|
||||
int check_dir;
|
||||
|
||||
// Build and check the directory name
|
||||
directory_name = build_directory_name(dms_name);
|
||||
directory_name = build_directory_name(dms_path);
|
||||
if (directory_name == NULL)
|
||||
return -1;
|
||||
|
||||
@ -224,14 +225,15 @@ int obi_dms_exists(const char* dms_name)
|
||||
}
|
||||
|
||||
|
||||
OBIDMS_p obi_create_dms(const char* dms_name)
|
||||
OBIDMS_p obi_create_dms(const char* dms_path)
|
||||
{
|
||||
char* directory_name;
|
||||
DIR* dms_dir;
|
||||
int dms_file_descriptor;
|
||||
char* directory_name;
|
||||
DIR* dms_dir;
|
||||
int dms_file_descriptor;
|
||||
size_t i, j;
|
||||
|
||||
// Build and check the directory name
|
||||
directory_name = build_directory_name(dms_name);
|
||||
directory_name = build_directory_name(dms_path);
|
||||
if (directory_name == NULL)
|
||||
return NULL;
|
||||
|
||||
@ -278,22 +280,32 @@ OBIDMS_p obi_create_dms(const char* dms_name)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Isolate the dms name
|
||||
j = 0;
|
||||
for (i=0; i<strlen(dms_path); i++)
|
||||
{
|
||||
if (dms_path[i] == '/')
|
||||
j = i+1;
|
||||
i++;
|
||||
}
|
||||
|
||||
// Create the informations file
|
||||
if (create_dms_infos_file(dms_file_descriptor, dms_name) < 0)
|
||||
if (create_dms_infos_file(dms_file_descriptor, dms_path+j) < 0)
|
||||
return NULL;
|
||||
|
||||
return obi_open_dms(dms_name);
|
||||
return obi_open_dms(dms_path);
|
||||
}
|
||||
|
||||
|
||||
OBIDMS_p obi_open_dms(const char* dms_name)
|
||||
OBIDMS_p obi_open_dms(const char* dms_path)
|
||||
{
|
||||
OBIDMS_p dms;
|
||||
char* directory_name;
|
||||
char* relative_directory_path;
|
||||
char* infos_file_name;
|
||||
int infos_file_descriptor;
|
||||
bool little_endian_dms;
|
||||
bool little_endian_platform;
|
||||
size_t i, j;
|
||||
|
||||
dms = NULL;
|
||||
|
||||
@ -307,18 +319,37 @@ OBIDMS_p obi_open_dms(const char* dms_name)
|
||||
}
|
||||
|
||||
// Build and check the directory name
|
||||
directory_name = build_directory_name(dms_name);
|
||||
if (directory_name == NULL)
|
||||
relative_directory_path = build_directory_name(dms_path);
|
||||
if (relative_directory_path == NULL)
|
||||
{
|
||||
free(dms);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strncpy(dms->directory_name, directory_name, OBIDMS_MAX_NAME);
|
||||
free(directory_name);
|
||||
// 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; i<strlen(dms_path); i++)
|
||||
{
|
||||
if (dms_path[i] == '/')
|
||||
j = i+1;
|
||||
i++;
|
||||
}
|
||||
strcpy(dms->dms_name, dms_path+j);
|
||||
|
||||
// Try to open the directory
|
||||
dms->directory = opendir(dms->directory_name);
|
||||
dms->directory = opendir(dms->directory_path);
|
||||
if (dms->directory == NULL)
|
||||
{
|
||||
switch (errno)
|
||||
@ -355,7 +386,7 @@ OBIDMS_p obi_open_dms(const char* dms_name)
|
||||
}
|
||||
|
||||
// Open informations file to check endianness
|
||||
infos_file_name = build_infos_file_name(dms_name);
|
||||
infos_file_name = build_infos_file_name(dms->dms_name);
|
||||
infos_file_descriptor = openat(dms->dir_fd, infos_file_name, O_RDONLY, 0777);
|
||||
if (infos_file_descriptor < 0)
|
||||
{
|
||||
@ -602,15 +633,7 @@ char* obi_dms_get_dms_path(OBIDMS_p dms)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (getcwd(full_path, MAX_PATH_LEN) == NULL) // TODO store when opening
|
||||
{
|
||||
obi_set_errno(OBI_UTILS_ERROR);
|
||||
obidebug(1, "\nError getting the path to a file or directory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcat(full_path, "/");
|
||||
strcat(full_path, dms->directory_name);
|
||||
strcpy(full_path, dms->directory_path);
|
||||
|
||||
return full_path;
|
||||
}
|
||||
|
Reference in New Issue
Block a user