Relative paths when creating or opening a DMS now work

This commit is contained in:
Celine Mercier
2016-04-29 17:46:36 +02:00
parent 31cf27d676
commit a32920e401
2 changed files with 73 additions and 35 deletions

View File

@ -18,6 +18,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <dirent.h> #include <dirent.h>
#include <unistd.h>
#include "obidms.h" #include "obidms.h"
#include "obierrno.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; struct stat buffer;
char* directory_name; char* directory_name;
int check_dir; int check_dir;
// Build and check the directory name // Build and check the directory name
directory_name = build_directory_name(dms_name); directory_name = build_directory_name(dms_path);
if (directory_name == NULL) if (directory_name == NULL)
return -1; 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; char* directory_name;
DIR* dms_dir; DIR* dms_dir;
int dms_file_descriptor; int dms_file_descriptor;
size_t i, j;
// Build and check the directory name // Build and check the directory name
directory_name = build_directory_name(dms_name); directory_name = build_directory_name(dms_path);
if (directory_name == NULL) if (directory_name == NULL)
return NULL; return NULL;
@ -278,22 +280,32 @@ OBIDMS_p obi_create_dms(const char* dms_name)
return NULL; 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 // 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 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; OBIDMS_p dms;
char* directory_name; char* relative_directory_path;
char* infos_file_name; char* infos_file_name;
int infos_file_descriptor; int infos_file_descriptor;
bool little_endian_dms; bool little_endian_dms;
bool little_endian_platform; bool little_endian_platform;
size_t i, j;
dms = NULL; dms = NULL;
@ -307,18 +319,37 @@ OBIDMS_p obi_open_dms(const char* dms_name)
} }
// Build and check the directory name // Build and check the directory name
directory_name = build_directory_name(dms_name); relative_directory_path = build_directory_name(dms_path);
if (directory_name == NULL) if (relative_directory_path == NULL)
{ {
free(dms); free(dms);
return NULL; return NULL;
} }
strncpy(dms->directory_name, directory_name, OBIDMS_MAX_NAME); // Build and store the absolute path to the DMS
free(directory_name); 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 // Try to open the directory
dms->directory = opendir(dms->directory_name); dms->directory = opendir(dms->directory_path);
if (dms->directory == NULL) if (dms->directory == NULL)
{ {
switch (errno) switch (errno)
@ -355,7 +386,7 @@ OBIDMS_p obi_open_dms(const char* dms_name)
} }
// Open informations file to check endianness // 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); infos_file_descriptor = openat(dms->dir_fd, infos_file_name, O_RDONLY, 0777);
if (infos_file_descriptor < 0) if (infos_file_descriptor < 0)
{ {
@ -602,15 +633,7 @@ char* obi_dms_get_dms_path(OBIDMS_p dms)
return NULL; return NULL;
} }
if (getcwd(full_path, MAX_PATH_LEN) == NULL) // TODO store when opening strcpy(full_path, dms->directory_path);
{
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);
return full_path; return full_path;
} }

View File

@ -78,9 +78,14 @@ typedef struct Opened_indexers_list {
* and opening of an OBITools Data Management System (DMS) * and opening of an OBITools Data Management System (DMS)
*/ */
typedef struct OBIDMS { typedef struct OBIDMS {
char dms_name[OBIDMS_MAX_NAME+1]; /** The name of the DMS.
*/
char directory_name[OBIDMS_MAX_NAME+1]; /**< The name of the directory char directory_name[OBIDMS_MAX_NAME+1]; /**< The name of the directory
* containing the DMS. * containing the DMS.
*/ */
char directory_path[MAX_PATH_LEN]; /**< The absolute path of the directory
* containing the DMS.
*/
DIR* directory; /**< A directory entry usable to DIR* directory; /**< A directory entry usable to
* refer and scan the database directory. * refer and scan the database directory.
*/ */
@ -105,7 +110,7 @@ typedef struct OBIDMS {
/** /**
* @brief Checks if an OBIDMS exists. * @brief Checks if an OBIDMS exists.
* *
* @param dms_name A pointer to a C string containing the name of the database. * @param dms_path A pointer to a C string containing the path to the database.
* *
* @returns An integer value indicating the status of the database. * @returns An integer value indicating the status of the database.
* @retval 1 if the database exists. * @retval 1 if the database exists.
@ -115,7 +120,7 @@ typedef struct OBIDMS {
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
int obi_dms_exists(const char* dms_name); int obi_dms_exists(const char* dms_path);
/** /**
@ -127,7 +132,7 @@ int obi_dms_exists(const char* dms_name);
* *
* A directory to store Obiblob indexers is also created. * A directory to store Obiblob indexers is also created.
* *
* @param dms_name A pointer to a C string containing the name of the database. * @param dms_path A pointer to a C string containing the path to the database.
* The actual directory name used to store the DMS will be * The actual directory name used to store the DMS will be
* `<dms_name>.obidms`. * `<dms_name>.obidms`.
* *
@ -144,7 +149,7 @@ OBIDMS_p obi_create_dms(const char* dms_name);
/** /**
* @brief Opens an existing OBITools Data Management instance (OBIDMS). * @brief Opens an existing OBITools Data Management instance (OBIDMS).
* *
* @param dms_name A pointer to a C string containing the name of the database. * @param dms_path A pointer to a C string containing the path to the database.
* *
* @returns A pointer to an OBIDMS structure describing the opened DMS. * @returns A pointer to an OBIDMS structure describing the opened DMS.
* @retval NULL if an error occurred. * @retval NULL if an error occurred.
@ -153,7 +158,7 @@ OBIDMS_p obi_create_dms(const char* dms_name);
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
OBIDMS_p obi_open_dms(const char* dms_name); OBIDMS_p obi_open_dms(const char* dms_path);
/** /**
@ -162,7 +167,7 @@ OBIDMS_p obi_open_dms(const char* dms_name);
* If the database already exists, this function opens it, otherwise it * If the database already exists, this function opens it, otherwise it
* creates a new database. * creates a new database.
* *
* @param dms_name A pointer to a C string containing the name of the database. * @param dms_path A pointer to a C string containing the path to the database.
* *
* @returns A pointer to an OBIDMS structure describing the OBIDMS. * @returns A pointer to an OBIDMS structure describing the OBIDMS.
* @retval NULL if an error occurred. * @retval NULL if an error occurred.
@ -171,7 +176,7 @@ OBIDMS_p obi_open_dms(const char* dms_name);
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
OBIDMS_p obi_dms(const char* dms_name); OBIDMS_p obi_dms(const char* dms_path);
/** /**
@ -295,9 +300,19 @@ int obi_dms_unlist_indexer(OBIDMS_p dms, Obi_indexer_p indexer);
/** /**
* Function meant to disappear soon * @brief Gets the full path to the DMS.
*
* @warning The returned pointer has to be freed by the caller.
*
* @param dms The DMS.
*
* @returns A pointer to the full path.
* @retval NULL if an error occurred.
*
* @since April 2016
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/ */
char* obi_dms_get_path(OBIDMS_p dms); char* obi_dms_get_dms_path(OBIDMS_p dms);
/** /**