First minimal implementation of what should be the creation, opening and
closing of an OBIDMS
This commit is contained in:
149
src/obidms.c
Normal file
149
src/obidms.c
Normal file
@ -0,0 +1,149 @@
|
||||
/*
|
||||
* obidms.c
|
||||
*
|
||||
* @date 23 May 2015
|
||||
* @Author: coissac
|
||||
*/
|
||||
|
||||
#include <obidms.h>
|
||||
|
||||
/**
|
||||
* Internal function building the directory name from a OBIDMS name.
|
||||
*
|
||||
* The function build the directory name corresponding to a OBIDMS.
|
||||
* 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
|
||||
* - OBIDMS_MEMORY_ERROR : something wrong occurs during memory allocation.
|
||||
* - 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;
|
||||
}
|
||||
|
||||
OBIDMS_p obi_create_dms(const char *name) {
|
||||
OBIDMS_p dms=NULL;
|
||||
char *dirdbname;
|
||||
|
||||
// Build and check the directory name
|
||||
dirdbname = build_directory_name(name);
|
||||
if (dirdbname==NULL)
|
||||
return NULL;
|
||||
|
||||
|
||||
// Try to create the directory
|
||||
if (mkdir(dirdbname,) < 0)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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
|
||||
dms = <OBIDMS_p> malloc(sizeof(OBIDMS_t));
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user