2015-09-30 12:03:46 +02:00
|
|
|
/********************************************************************
|
|
|
|
* OBIDMS functions *
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file obidms.c
|
|
|
|
* @author Eric Coissac (eric.coissac@metabarcoding.org)
|
|
|
|
* @date 23 May 2015
|
|
|
|
* @brief OBIDMS functions.
|
2015-05-23 16:29:06 +03:00
|
|
|
*/
|
|
|
|
|
2015-09-30 12:03:46 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2015-06-23 18:35:34 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/stat.h>
|
2015-11-03 14:22:00 +01:00
|
|
|
#include <fcntl.h>
|
2015-06-23 18:35:34 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
2015-06-17 16:51:16 +02:00
|
|
|
|
|
|
|
#include "obidms.h"
|
2015-06-23 18:35:34 +02:00
|
|
|
#include "obierrno.h"
|
2015-07-31 18:03:48 +02:00
|
|
|
#include "obidebug.h"
|
|
|
|
#include "obidmscolumn.h"
|
2015-11-03 14:22:00 +01:00
|
|
|
#include "private_at_functions.h"
|
2015-07-31 18:03:48 +02:00
|
|
|
|
2015-08-03 15:10:39 +02:00
|
|
|
|
2015-09-30 12:03:46 +02:00
|
|
|
#define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?)
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-05-26 21:36:55 +02:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
/**************************************************************************
|
2015-05-26 21:36:55 +02:00
|
|
|
*
|
2015-06-23 18:35:34 +02:00
|
|
|
* D E C L A R A T I O N O F T H E P R I V A T E F U N C T I O N S
|
2015-05-26 21:36:55 +02:00
|
|
|
*
|
2015-06-23 18:35:34 +02:00
|
|
|
**************************************************************************/
|
2015-05-26 21:36:55 +02:00
|
|
|
|
|
|
|
|
2015-05-23 16:29:06 +03:00
|
|
|
/**
|
2015-09-30 12:03:46 +02:00
|
|
|
* Internal function building the OBIDMS directory name from an OBIDMS name.
|
2015-05-23 16:29:06 +03:00
|
|
|
*
|
2015-06-10 15:19:02 +02:00
|
|
|
* The function builds the directory name corresponding to an OBIDMS.
|
2015-09-30 12:03:46 +02:00
|
|
|
* It also checks that the name is not too long.
|
2015-05-23 16:29:06 +03:00
|
|
|
*
|
|
|
|
* @warning The returned pointer has to be freed by the caller.
|
|
|
|
*
|
2015-09-30 12:03:46 +02:00
|
|
|
* @param dms_name The name of the OBIDMS.
|
2015-05-23 16:29:06 +03:00
|
|
|
*
|
2015-09-30 12:03:46 +02:00
|
|
|
* @returns A pointer to the directory name.
|
|
|
|
* @retval NULL if an error occurred.
|
2015-05-23 16:29:06 +03:00
|
|
|
*
|
|
|
|
* @since May 2015
|
|
|
|
* @author Eric Coissac (eric.coissac@metabarcoding.org)
|
|
|
|
*/
|
2015-09-30 12:03:46 +02:00
|
|
|
static char* build_directory_name(const char* dms_name);
|
2015-06-17 16:51:16 +02:00
|
|
|
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
/************************************************************************
|
2015-06-17 16:51:16 +02:00
|
|
|
*
|
2015-06-23 18:35:34 +02:00
|
|
|
* D E F I N I T I O N O F T H E P R I V A T E F U N C T I O N S
|
2015-06-17 16:51:16 +02:00
|
|
|
*
|
2015-06-23 18:35:34 +02:00
|
|
|
************************************************************************/
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-09-30 12:03:46 +02:00
|
|
|
static char* build_directory_name(const char* dms_name)
|
2015-06-17 16:51:16 +02:00
|
|
|
{
|
2015-09-30 12:03:46 +02:00
|
|
|
char* directory_name;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
|
|
|
// Build the database directory name
|
2015-06-17 16:51:16 +02:00
|
|
|
if (asprintf(&directory_name, "%s.obidms", dms_name) < 0)
|
2015-05-23 16:29:06 +03:00
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nProblem building an OBIDMS directory name");
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test if the database name is not too long
|
2015-06-17 16:51:16 +02:00
|
|
|
if (strlen(directory_name) >= OBIDMS_MAX_NAME)
|
2015-05-23 16:29:06 +03:00
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_LONG_NAME_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nProblem building an OBIDMS directory name");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(directory_name);
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
return directory_name;
|
2015-05-23 16:29:06 +03:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
/**********************************************************************
|
2015-05-26 21:36:55 +02:00
|
|
|
*
|
2015-06-23 18:35:34 +02:00
|
|
|
* D E F I N I T I O N O F T H E P U B L I C F U N C T I O N S
|
2015-05-26 21:36:55 +02:00
|
|
|
*
|
2015-06-23 18:35:34 +02:00
|
|
|
**********************************************************************/
|
2015-05-26 21:36:55 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
int obi_dms_exists(const char* dms_name)
|
|
|
|
{
|
2015-05-26 21:36:55 +02:00
|
|
|
struct stat buffer;
|
2015-06-17 16:51:16 +02:00
|
|
|
char *directory_name;
|
2015-06-23 18:35:34 +02:00
|
|
|
int check_dir;
|
2015-05-26 21:36:55 +02:00
|
|
|
|
|
|
|
// Build and check the directory name
|
2015-06-17 16:51:16 +02:00
|
|
|
directory_name = build_directory_name(dms_name);
|
|
|
|
if (directory_name == NULL)
|
2015-05-26 21:36:55 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
check_dir = stat(directory_name, &buffer);
|
2015-05-26 21:36:55 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
free(directory_name);
|
2015-05-26 21:36:55 +02:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
if (check_dir == 0)
|
2015-05-26 21:36:55 +02:00
|
|
|
return 1;
|
2015-06-23 18:35:34 +02:00
|
|
|
else
|
|
|
|
return 0;
|
2015-05-26 21:36:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
OBIDMS_p obi_create_dms(const char* dms_name)
|
|
|
|
{
|
2015-11-03 14:22:00 +01:00
|
|
|
char* directory_name;
|
|
|
|
DIR* dms_dir;
|
|
|
|
int dms_file_descriptor;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
|
|
|
// Build and check the directory name
|
2015-06-17 16:51:16 +02:00
|
|
|
directory_name = build_directory_name(dms_name);
|
|
|
|
if (directory_name == NULL)
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Try to create the directory
|
2015-09-21 15:48:02 +02:00
|
|
|
if (mkdir(directory_name, 00777) < 0)
|
2015-05-23 16:29:06 +03:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
if (errno == EEXIST)
|
2015-05-23 16:29:06 +03:00
|
|
|
obi_set_errno(OBIDMS_EXIST_ERROR);
|
|
|
|
else
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nProblem creating an OBIDMS directory");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(directory_name);
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-03 14:22:00 +01:00
|
|
|
// Get file descriptor of DMS directory to create the arrays directory
|
|
|
|
dms_dir = opendir(directory_name);
|
|
|
|
if (dms_dir == NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nProblem opening a newly created OBIDMS directory");
|
|
|
|
free(directory_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
dms_file_descriptor = dirfd(dms_dir);
|
|
|
|
if (dms_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nProblem getting the file descriptor of a newly created OBIDMS directory");
|
|
|
|
free(directory_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the arrays directory
|
|
|
|
if (mkdirat(dms_file_descriptor, ARRAY_DIR_NAME, 00777) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBI_ARRAY_ERROR);
|
|
|
|
obidebug(1, "\nProblem creating an arrays directory");
|
|
|
|
free(directory_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
free(directory_name);
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
return obi_open_dms(dms_name);
|
2015-05-23 16:29:06 +03:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
OBIDMS_p obi_open_dms(const char* dms_name)
|
|
|
|
{
|
|
|
|
OBIDMS_p dms;
|
|
|
|
char* directory_name;
|
|
|
|
DIR* directory;
|
2015-11-03 14:22:00 +01:00
|
|
|
int dms_file_descriptor;
|
2015-06-17 16:51:16 +02:00
|
|
|
|
|
|
|
dms = NULL;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
|
|
|
// Build and check the directory name
|
2015-06-17 16:51:16 +02:00
|
|
|
directory_name = build_directory_name(dms_name);
|
|
|
|
if (directory_name == NULL)
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Try to open the directory
|
2015-06-17 16:51:16 +02:00
|
|
|
directory = opendir(directory_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (directory == NULL)
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case ENOENT:
|
|
|
|
obi_set_errno(OBIDMS_NOT_EXIST_ERROR);
|
|
|
|
break;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
case EACCES:
|
|
|
|
obi_set_errno(OBIDMS_ACCESS_ERROR);
|
|
|
|
break;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
case ENOMEM:
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
|
|
|
break;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
default:
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-05-23 16:29:06 +03:00
|
|
|
}
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nCan't open OBIDMS directory");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(directory_name);
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the data structure
|
2015-05-26 10:38:56 +02:00
|
|
|
dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t));
|
2015-06-17 16:51:16 +02:00
|
|
|
if (dms == NULL)
|
2015-05-23 16:29:06 +03:00
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError allocating the memory for the OBIDMS structure");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(directory_name);
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
// Initialize the data structure
|
|
|
|
strcpy(dms->directory_name, directory_name);
|
|
|
|
dms->directory = directory;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-11-03 14:22:00 +01:00
|
|
|
// Get file descriptor of DMS directory to open the arrays directory
|
|
|
|
dms_file_descriptor = dirfd(directory);
|
|
|
|
if (dms_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError getting the file descriptor for a newly created OBIDMS directory");
|
|
|
|
free(directory_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the arrays directory
|
|
|
|
dms->array_directory = private_opendirat(dms_file_descriptor, ARRAY_DIR_NAME);
|
|
|
|
if (dms->array_directory == NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError opening the arrays directory");
|
|
|
|
free(directory_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
free(directory_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
|
2015-05-23 16:29:06 +03:00
|
|
|
return dms;
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
OBIDMS_p obi_dms(const char* dms_name)
|
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
int exists;
|
2015-05-26 21:36:55 +02:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
exists = obi_dms_exists(dms_name);
|
|
|
|
|
|
|
|
switch (exists)
|
|
|
|
{
|
2015-05-26 21:36:55 +02:00
|
|
|
case 0:
|
2015-06-17 16:51:16 +02:00
|
|
|
return obi_create_dms(dms_name);
|
2015-05-26 21:36:55 +02:00
|
|
|
case 1:
|
2015-06-17 16:51:16 +02:00
|
|
|
return obi_open_dms(dms_name);
|
2015-05-26 21:36:55 +02:00
|
|
|
};
|
|
|
|
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError checking if an OBIDMS directory exists");
|
2015-05-26 21:36:55 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
int obi_close_dms(OBIDMS_p dms)
|
|
|
|
{
|
2015-05-23 16:29:06 +03:00
|
|
|
if (dms != NULL)
|
|
|
|
{
|
|
|
|
if (closedir(dms->directory) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
2015-11-03 14:22:00 +01:00
|
|
|
obidebug(1, "\nError closing an OBIDMS directory");
|
|
|
|
free(dms);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (closedir(dms->array_directory) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBI_ARRAY_ERROR);
|
|
|
|
obidebug(1, "\nError closing an array directory");
|
2015-05-23 16:29:06 +03:00
|
|
|
free(dms);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
free(dms);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|