2015-05-23 16:29:06 +03:00
|
|
|
/*
|
|
|
|
* obidms.c
|
|
|
|
*
|
|
|
|
* @date 23 May 2015
|
|
|
|
* @Author: coissac
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
#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-08-03 15:10:39 +02:00
|
|
|
|
2015-07-31 18:03:48 +02:00
|
|
|
#define DEBUG_LEVEL 0
|
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-06-10 15:19:02 +02:00
|
|
|
* Internal function building the 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-05-23 16:29:06 +03:00
|
|
|
* It checks also that the name is not too long.
|
|
|
|
*
|
|
|
|
* @warning The returned pointer has to be freed by the caller.
|
|
|
|
*
|
2015-06-17 16:51:16 +02:00
|
|
|
* @param dms_name the name of the OBIDMS
|
2015-05-23 16:29:06 +03:00
|
|
|
*
|
|
|
|
* @return a pointer to the directory name
|
2015-06-17 16:51:16 +02:00
|
|
|
* @retvalue <directory_name> if everything is ok
|
2015-05-23 16:29:06 +03:00
|
|
|
* @retvalue NULL if an error occurs
|
|
|
|
*
|
|
|
|
* ###Error values
|
2015-06-23 18:35:34 +02:00
|
|
|
* - OBIDMS_MEMORY_ERROR : something wrong occurred during memory allocation.
|
2015-05-23 16:29:06 +03:00
|
|
|
* - OBIDMS_LONG_NAME_ERROR : the database name exceeds the limit.
|
|
|
|
*
|
|
|
|
* @since May 2015
|
|
|
|
* @author Eric Coissac (eric.coissac@metabarcoding.org)
|
|
|
|
*/
|
2015-06-17 16:51:16 +02:00
|
|
|
static char *build_directory_name(const char *dms_name);
|
|
|
|
|
|
|
|
|
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-06-17 16:51:16 +02:00
|
|
|
static char *build_directory_name(const char *dms_name)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
char *directory_name;
|
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-06-17 16:51:16 +02:00
|
|
|
if (mkdir(directory_name, 0x777) < 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-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;
|
|
|
|
|
|
|
|
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-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-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError closing an OBIDSM directory");
|
2015-05-23 16:29:06 +03:00
|
|
|
free(dms);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
free(dms);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|