2015-05-23 16:29:06 +03:00
|
|
|
/*
|
|
|
|
* obidms.c
|
|
|
|
*
|
|
|
|
* @date 23 May 2015
|
|
|
|
* @Author: coissac
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <obidms.h>
|
|
|
|
|
2015-05-26 21:36:55 +02:00
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
/************************************************************************
|
2015-05-26 21:36:55 +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-06-10 15:19:02 +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.
|
|
|
|
*
|
|
|
|
* @param name the name of the OBIDMS
|
|
|
|
*
|
|
|
|
* @return a pointer to the directory name
|
|
|
|
* @retvalue <dirdbname> if everything is ok
|
|
|
|
* @retvalue NULL if an error occurs
|
|
|
|
*
|
|
|
|
* ###Error values
|
2015-06-10 15:19:02 +02:00
|
|
|
* - OBIDMS_MEMORY_ERROR : something wrong occured 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)
|
|
|
|
*/
|
|
|
|
static char *build_directory_name(const char *name) {
|
|
|
|
|
|
|
|
char *dirdbname;
|
|
|
|
|
|
|
|
// Build the database directory name
|
|
|
|
if (asprintf(&dirdbname,"%s.obidms",name) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test if the database name is not too long
|
|
|
|
if (strlen(dirdbname) >= OBIDMS_MAX_NAME)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_LONG_NAME_ERROR);
|
|
|
|
free(dirdbname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dirdbname;
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-05-26 21:36:55 +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
|
|
|
|
*
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
int obi_dms_exists(const char* name) {
|
|
|
|
struct stat buffer;
|
|
|
|
char *dirdbname;
|
|
|
|
int exist;
|
|
|
|
|
|
|
|
// Build and check the directory name
|
|
|
|
dirdbname = build_directory_name(name);
|
|
|
|
if (dirdbname==NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
exist = stat(dirdbname,&buffer);
|
|
|
|
|
|
|
|
free(dirdbname);
|
|
|
|
|
|
|
|
if(exist == 0)
|
|
|
|
return 1;
|
|
|
|
else // -1
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-23 16:29:06 +03:00
|
|
|
OBIDMS_p obi_create_dms(const char *name) {
|
|
|
|
char *dirdbname;
|
|
|
|
|
|
|
|
// Build and check the directory name
|
|
|
|
dirdbname = build_directory_name(name);
|
|
|
|
if (dirdbname==NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
|
|
// Try to create the directory
|
2015-05-26 10:38:56 +02:00
|
|
|
if (mkdir(dirdbname,0x777) < 0)
|
2015-05-23 16:29:06 +03:00
|
|
|
{
|
|
|
|
if (errno==EEXIST)
|
|
|
|
obi_set_errno(OBIDMS_EXIST_ERROR);
|
|
|
|
else
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
|
|
|
|
free(dirdbname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(dirdbname);
|
|
|
|
|
|
|
|
return obi_open_dms(name);
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-05-23 16:29:06 +03:00
|
|
|
OBIDMS_p obi_open_dms(const char *name) {
|
|
|
|
OBIDMS_p dms=NULL;
|
|
|
|
char *dirdbname;
|
|
|
|
DIR *directory;
|
|
|
|
|
|
|
|
// Build and check the directory name
|
|
|
|
dirdbname = build_directory_name(name);
|
|
|
|
if (dirdbname==NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// try to open the directory
|
|
|
|
directory = opendir(dirdbname);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
free(dirdbname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the data structure
|
2015-05-26 10:38:56 +02:00
|
|
|
dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t));
|
2015-05-23 16:29:06 +03:00
|
|
|
|
|
|
|
if (dms==NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
|
|
|
free(dirdbname);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialise the data structure
|
|
|
|
strcpy(dms->dirname,dirdbname);
|
|
|
|
dms->directory=directory;
|
|
|
|
|
|
|
|
free(dirdbname);
|
|
|
|
return dms;
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-05-26 21:36:55 +02:00
|
|
|
OBIDMS_p obi_dms(const char *name) {
|
|
|
|
int exist = obi_dms_exists(name);
|
|
|
|
|
|
|
|
switch (exist) {
|
|
|
|
case 0:
|
|
|
|
return obi_create_dms(name);
|
|
|
|
case 1:
|
|
|
|
return obi_open_dms(name);
|
|
|
|
};
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-23 16:29:06 +03:00
|
|
|
|
|
|
|
int obi_close_dms(OBIDMS_p dms) {
|
|
|
|
|
|
|
|
if (dms != NULL)
|
|
|
|
{
|
|
|
|
if (closedir(dms->directory) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
|
|
|
free(dms);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(dms);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|