Files
obitools3/src/obidms.c

295 lines
6.5 KiB
C
Raw Normal View History

/********************************************************************
* OBIDMS functions *
********************************************************************/
/**
* @file obidms.c
* @author Eric Coissac (eric.coissac@metabarcoding.org)
* @date 23 May 2015
* @brief OBIDMS functions.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <dirent.h>
#include "obidms.h"
#include "obierrno.h"
#include "obidebug.h"
#include "obidmscolumn.h"
#include "private_at_functions.h"
#define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?)
/**************************************************************************
*
* 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
*
**************************************************************************/
/**
* Internal function building the OBIDMS directory name from an OBIDMS name.
*
2015-06-10 15:19:02 +02:00
* The function builds the directory name corresponding to an OBIDMS.
* It also checks that the name is not too long.
*
* @warning The returned pointer has to be freed by the caller.
*
* @param dms_name The name of the OBIDMS.
*
* @returns A pointer to the directory name.
* @retval NULL if an error occurred.
*
* @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org)
*/
static char* build_directory_name(const char* dms_name);
/************************************************************************
*
* 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
*
************************************************************************/
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)
{
obi_set_errno(OBIDMS_MEMORY_ERROR);
obidebug(1, "\nProblem building an OBIDMS directory name");
return NULL;
}
// Test if the database name is not too long
if (strlen(directory_name) >= OBIDMS_MAX_NAME)
{
obi_set_errno(OBIDMS_LONG_NAME_ERROR);
obidebug(1, "\nProblem building an OBIDMS directory name");
free(directory_name);
return NULL;
}
return directory_name;
}
2015-06-10 15:19:02 +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
*
**********************************************************************/
int obi_dms_exists(const char* dms_name)
{
struct stat buffer;
char *directory_name;
int check_dir;
// Build and check the directory name
directory_name = build_directory_name(dms_name);
if (directory_name == NULL)
return -1;
check_dir = stat(directory_name, &buffer);
free(directory_name);
if (check_dir == 0)
return 1;
else
return 0;
}
OBIDMS_p obi_create_dms(const char* dms_name)
{
char* directory_name;
DIR* dms_dir;
int dms_file_descriptor;
// Build and check the directory name
directory_name = build_directory_name(dms_name);
if (directory_name == NULL)
return NULL;
// Try to create the directory
if (mkdir(directory_name, 00777) < 0)
{
if (errno == EEXIST)
obi_set_errno(OBIDMS_EXIST_ERROR);
else
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obidebug(1, "\nProblem creating an OBIDMS directory");
free(directory_name);
return NULL;
}
// 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;
}
free(directory_name);
return obi_open_dms(dms_name);
}
2015-06-10 15:19:02 +02:00
OBIDMS_p obi_open_dms(const char* dms_name)
{
OBIDMS_p dms;
char* directory_name;
DIR* directory;
int dms_file_descriptor;
dms = NULL;
// Build and check the directory name
directory_name = build_directory_name(dms_name);
if (directory_name == NULL)
return NULL;
// Try to open the directory
directory = opendir(directory_name);
if (directory == NULL)
{
switch (errno)
{
case ENOENT:
obi_set_errno(OBIDMS_NOT_EXIST_ERROR);
break;
case EACCES:
obi_set_errno(OBIDMS_ACCESS_ERROR);
break;
case ENOMEM:
obi_set_errno(OBIDMS_MEMORY_ERROR);
break;
default:
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
}
obidebug(1, "\nCan't open OBIDMS directory");
free(directory_name);
return NULL;
}
// Allocate the data structure
2015-05-26 10:38:56 +02:00
dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t));
if (dms == NULL)
{
obi_set_errno(OBIDMS_MEMORY_ERROR);
obidebug(1, "\nError allocating the memory for the OBIDMS structure");
free(directory_name);
return NULL;
}
// Initialize the data structure
strcpy(dms->directory_name, directory_name);
dms->directory = directory;
// 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;
}
free(directory_name);
return dms;
}
2015-06-10 15:19:02 +02:00
OBIDMS_p obi_dms(const char* dms_name)
{
int exists;
exists = obi_dms_exists(dms_name);
switch (exists)
{
case 0:
return obi_create_dms(dms_name);
case 1:
return obi_open_dms(dms_name);
};
obidebug(1, "\nError checking if an OBIDMS directory exists");
return NULL;
}
int obi_close_dms(OBIDMS_p dms)
{
if (dms != NULL)
{
if (closedir(dms->directory) < 0)
{
obi_set_errno(OBIDMS_MEMORY_ERROR);
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");
free(dms);
return -1;
}
free(dms);
}
return 0;
}