Commit 152b34b5 by Celine Mercier

Closes #10 : column groups stored in directories

parent 61b6c3ce
......@@ -6,5 +6,5 @@
../../../src/obilittlebigman.c
../../../src/obitypes.h
../../../src/obitypes.c
../../../src/private_openat.h
../../../src/private_openat.c
../../../src/private_at_functions.h
../../../src/private_at_functions.c
......@@ -16,11 +16,11 @@ cdef extern from "obidms.h" nogil:
ctypedef OBIDMS_t* OBIDMS_p
OBIDMS_p obi_create_dms(const_char_p name)
OBIDMS_p obi_create_dms(const_char_p dms_name)
OBIDMS_p obi_open_dms(const_char_p name)
OBIDMS_p obi_open_dms(const_char_p dms_name)
OBIDMS_p obi_dms(const_char_p name)
OBIDMS_p obi_dms(const_char_p dms_name)
int obi_close_dms(OBIDMS_p dms)
......
../../../../src/obidmscolumn.h
../../../../src/obidmscolumn.c
../../../../src/obidmscolumngroup.h
../../../../src/obidmscolumngroup.c
../../../../src/obidms.h
../../../../src/obidms.c
../../../../src/obierrno.h
......@@ -8,5 +10,5 @@
../../../../src/obilittlebigman.c
../../../../src/obitypes.h
../../../../src/obitypes.c
../../../../src/private_openat.h
../../../../src/private_openat.c
../../../../src/private_at_functions.h
../../../../src/private_at_functions.c
......@@ -7,15 +7,20 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include "obidms.h"
#include "obierrno.h"
/************************************************************************
/**************************************************************************
*
* 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
* 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
*
************************************************************************/
**************************************************************************/
/**
......@@ -33,7 +38,7 @@
* @retvalue NULL if an error occurs
*
* ###Error values
* - OBIDMS_MEMORY_ERROR : something wrong occured during memory allocation.
* - OBIDMS_MEMORY_ERROR : something wrong occurred during memory allocation.
* - OBIDMS_LONG_NAME_ERROR : the database name exceeds the limit.
*
* @since May 2015
......@@ -42,11 +47,11 @@
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
* 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)
{
......@@ -71,31 +76,31 @@ 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 U B L I C F U N C T I O N S
* 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 exists;
int check_dir;
// Build and check the directory name
directory_name = build_directory_name(dms_name);
if (directory_name == NULL)
return -1;
exists = stat(directory_name, &buffer);
check_dir = stat(directory_name, &buffer);
free(directory_name);
if(exists == 0)
if (check_dir == 0)
return 1;
else // -1
return 0;
else
return 0;
}
......@@ -108,7 +113,6 @@ OBIDMS_p obi_create_dms(const char* dms_name)
if (directory_name == NULL)
return NULL;
// Try to create the directory
if (mkdir(directory_name, 0x777) < 0)
{
......@@ -140,24 +144,26 @@ OBIDMS_p obi_open_dms(const char* dms_name)
if (directory_name == NULL)
return NULL;
// try to open the directory
// Try to open the directory
directory = opendir(directory_name);
if (directory == NULL) {
switch (errno) {
case ENOENT:
obi_set_errno(OBIDMS_NOT_EXIST_ERROR);
break;
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 EACCES:
obi_set_errno(OBIDMS_ACCESS_ERROR);
break;
case ENOMEM:
obi_set_errno(OBIDMS_MEMORY_ERROR);
break;
case ENOMEM:
obi_set_errno(OBIDMS_MEMORY_ERROR);
break;
default:
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
default:
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
}
free(directory_name);
return NULL;
......@@ -165,7 +171,6 @@ OBIDMS_p obi_open_dms(const char* dms_name)
// Allocate the data structure
dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t));
if (dms == NULL)
{
obi_set_errno(OBIDMS_MEMORY_ERROR);
......@@ -178,15 +183,19 @@ OBIDMS_p obi_open_dms(const char* dms_name)
dms->directory = directory;
free(directory_name);
return dms;
}
OBIDMS_p obi_dms(const char* dms_name)
{
int exists = obi_dms_exists(dms_name);
int exists;
switch (exists) {
exists = obi_dms_exists(dms_name);
switch (exists)
{
case 0:
return obi_create_dms(dms_name);
case 1:
......@@ -194,7 +203,6 @@ OBIDMS_p obi_dms(const char* dms_name)
};
return NULL;
}
......
......@@ -41,12 +41,12 @@ typedef struct OBIDMS {
*
* @param dms_name a pointer to a C string containing the name of the database.
* The actual directory name used to store the DMS will be
* `<name>.obidms`.
* `<dms_name>.obidms`.
*
* @return an integer value indicating the status of the database
* @retvalue 1 the database exist
* @retvalue 0 the database does not exist
* @retvalue -1 an error occured
* @retvalue -1 an error occurred
*
* @see obi_close_dms()
* @since May 2015
......@@ -72,7 +72,7 @@ int obi_dms_exists(const char* dms_name);
* ###Error values
* - OBIDMS_EXIST_ERROR : a database with the same name already exists.
* - OBIDMS_LONG_NAME_ERROR : the database name exceeds the limit.
* - OBIDMS_MEMORY_ERROR : something wrong occured during memory allocation.
* - OBIDMS_MEMORY_ERROR : something wrong occurred during memory allocation.
*
* @see obi_close_dms()
* @since May 2015
......@@ -92,9 +92,8 @@ OBIDMS_p obi_create_dms(const char *dms_name);
* @retval NULL on error and the `obi_errno`variable is set.
*
* ###Error values
* - OBIDMS_EXIST_ERROR : a database with the same name already exists.
* - OBIDMS_LONG_NAME_ERROR : the database name exceeds the limit.
* - OBIDMS_MEMORY_ERROR : something wrong occured during memory allocation.
* - OBIDMS_MEMORY_ERROR : something wrong occurred during memory allocation.
*
* @see obi_close_dms()
* @since May 2015
......@@ -118,7 +117,7 @@ OBIDMS_p obi_open_dms(const char *dms_name);
*
* ###Error values
* - OBIDMS_LONG_NAME_ERROR : the database name exceeds the limit.
* - OBIDMS_MEMORY_ERROR : something wrong occured during memory allocation.
* - OBIDMS_MEMORY_ERROR : something wrong occurred during memory allocation.
*
* @see obi_close_dms()
* @since May 2015
......
......@@ -22,11 +22,9 @@
#include "obitypes.h"
#include "obierrno.h"
#include "obilittlebigman.h"
#include "obidmscolumngroup.h"
#define OBIDMS_MAX_COLNAME (128) /**< The maximum length of an OBIDMS column name
*/
typedef int32_t obiversion_t; /**< Used to store the column version number
*/
......@@ -35,7 +33,7 @@ typedef int32_t obiversion_t; /**< Used to store the column version number
*/
typedef struct OBIDMS_column_header {
bool little_endian; /**< endianess of the column:
bool little_endian; /**< endianness of the column:
- `true` on little endian platforms
- `false` on big endian platforms
......@@ -57,6 +55,7 @@ typedef struct OBIDMS_column_header {
*/
} OBIDMS_column_header_t, *OBIDMS_column_header_p;
/**
* @brief Structure describing a Column of the OBITools DMS
*
......@@ -64,25 +63,27 @@ typedef struct OBIDMS_column_header {
* creating, opening or cloning an OBIDMS_column.
*/
typedef struct OBIDMS_column {
OBIDMS_p dms ; /**< A pointer to a DMS instance
*/
OBIDMS_column_header_p header; /**< A pointer to the header of the column
*/
void* data; /**< A `void` pointer to the beginning of the
* data.
*
* @warning never use this member directly
* outside of the code of the
* low level functions
* of the OBITools DMS
*/
bool writable;/**< Indicates if the column is writable or not.
* - `true` the column is writable
* - `false` the column is read-only
*
* A column is writable only by its creator
* until it closes it.
*/
OBIDMS_p dms; /**< A pointer to a DMS instance
*/
OBIDMS_column_group_p column_group; /**< A pointer to an OBIDMS column group instance
*/
OBIDMS_column_header_p header; /**< A pointer to the header of the column
*/
void* data; /**< A `void` pointer to the beginning of the
* data.
*
* @warning never use this member directly
* outside of the code of the
* low level functions
* of the OBITools DMS
*/
bool writable; /**< Indicates if the column is writable or not.
* - `true` the column is writable
* - `false` the column is read-only
*
* A column is writable only by its creator
* until it closes it.
*/
} OBIDMS_column_t, *OBIDMS_column_p;
......@@ -103,7 +104,7 @@ size_t obi_get_platform_header_size();
/**
* @brief Creates a column.
*
* @param dms a pointer as returned by obi_create_dms() or obi_open_dms()
* @param dms a pointer on an OBIDMS column
* @param column_name the name of the new column
* @param type the OBIType code used to create the column
* @param nb_elements the number of elements to be stored
......@@ -112,9 +113,9 @@ size_t obi_get_platform_header_size();
* @author Eric Coissac (eric.coissac@metabarcoding.org)
*/
OBIDMS_column_p obi_create_column(OBIDMS_p dms,
char* column_name,
OBIType_t type,
size_t nb_elements);
const char* column_name,
OBIType_t type,
size_t nb_elements);
/**
......@@ -126,7 +127,7 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
* @return the bigger version number used for this column
* @return -1 if the column does not exist
*/
obiversion_t obi_get_latest_version_number(OBIDMS_p dms, char *column_name);
obiversion_t obi_get_latest_version_number(OBIDMS_column_group_p column_group);
#endif /* OBIDMSCOLUMN_H_ */
/****************************************************************************
* OBIDMS column groups functions *
****************************************************************************/
/**
* @file obidmscolumngroup.c
* @author Celine Mercier
* @date 18 June 2015
* @brief Functions for OBIDMS column groups.
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#include "obidmscolumngroup.h"
#include "obidms.h"
#include "private_at_functions.h"
#include "obierrno.h"
/**************************************************************************
*
* 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 group directory name from an OBIDMS column name.
*
* The function builds the directory name corresponding to an OBIDMS column group.
* It checks also that the name is not too long.
*
* @warning The returned pointer has to be freed by the caller.
*
* @param column_name the name of the OBIDMS column
*
* @return a pointer to the OBIDMS column directory name
* @retvalue <column_directory_name> if everything is ok
* @retvalue NULL if an error occurs
*
* ###Error values
* - OBIDMS_MEMORY_ERROR : something wrong occurred during memory allocation.
* - OBIDMS_LONG_NAME_ERROR : the database name exceeds the limit.
*
* @since June 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
static char* build_column_directory_name(const char* column_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_column_directory_name(const char* column_name)
{
char* column_directory_name;
// Build the database directory name
if (asprintf(&column_directory_name, "%s.obicol", column_name) < 0)
{
obi_set_errno(OBICOLDIR_MEMORY_ERROR);
return NULL;
}
// Test if the database name is not too long
if (strlen(column_directory_name) >= OBIDMS_COLUMN_DIR_MAX_NAME)
{
obi_set_errno(OBICOLDIR_LONG_NAME_ERROR);
free(column_directory_name);
return NULL;
}
return column_directory_name;
}
/**********************************************************************
*
* 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_column_group_exists(OBIDMS_p dms, const char* column_name)
{
struct stat buffer;
char* column_directory_name;
char* full_path;
int check_dir;
int dms_file_descriptor;
// Build and check the directory name
column_directory_name = build_column_directory_name(column_name);
if (column_directory_name == NULL)
{
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
return -1;
}
// Get the file descriptor for the dms
dms_file_descriptor = dirfd(dms->directory);
if (dms_file_descriptor < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(column_directory_name);
return -1;
}
// Get the full path for the column directory
full_path = get_full_path(dms_file_descriptor, column_directory_name);
if (full_path == NULL)
{
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
free(column_directory_name);
return -1;
}
check_dir = stat(full_path, &buffer);
free(column_directory_name);
free(full_path);
//close(dms_file_descriptor);
if(check_dir == 0)
return 1;
else
return 0;
}
OBIDMS_column_group_p obi_create_column_group(OBIDMS_p dms, const char* column_name)
{
char* column_directory_name;
int dms_file_descriptor;
// Build and check the directory name
column_directory_name = build_column_directory_name(column_name);
if (column_directory_name == NULL)
{
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
return NULL;
}
// Get the file descriptor for the dms
dms_file_descriptor = dirfd(dms->directory);
if (dms_file_descriptor < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(column_directory_name);
return NULL;
}
// Try to create the directory
if (private_mkdirat(dms_file_descriptor, column_directory_name, 0x777) < 0)
{
if (errno == EEXIST)
obi_set_errno(OBICOLDIR_EXIST_ERROR);
else
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
free(column_directory_name);
close(dms_file_descriptor);
return NULL;
}
free(column_directory_name);
//close(dms_file_descriptor);
return obi_open_column_group(dms, column_name);
}
OBIDMS_column_group_p obi_open_column_group(OBIDMS_p dms, const char* column_name)
{
OBIDMS_column_group_p column_group;
char* column_directory_name;
DIR* directory;
int dms_file_descriptor;
column_group = NULL;
// Build and check the directory name
column_directory_name = build_column_directory_name(column_name);
if (column_directory_name == NULL)
return NULL;
// Get the file descriptor for the dms
dms_file_descriptor = dirfd(dms->directory);
if (dms_file_descriptor < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(column_directory_name);
return NULL;
}
// Try to open the column directory
directory = private_opendirat(dms_file_descriptor, column_directory_name);
if (directory == NULL) {
switch (errno)
{
case ENOENT:
obi_set_errno(OBICOLDIR_NOT_EXIST_ERROR);
break;
case EACCES:
obi_set_errno(OBICOLDIR_ACCESS_ERROR);
break;
case ENOMEM:
obi_set_errno(OBICOLDIR_MEMORY_ERROR);
break;
default:
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
}
free(column_directory_name);
close(dms_file_descriptor);
return NULL;
}
// Allocate the column dir structure
column_group = (OBIDMS_column_group_p) malloc(sizeof(OBIDMS_column_group_t));
if (column_group == NULL)
{
obi_set_errno(OBICOLDIR_MEMORY_ERROR);
free(column_directory_name);
close(dms_file_descriptor);
return NULL;
}
// Initialize the data structure
strcpy(column_group->directory_name, column_directory_name);
strcpy(column_group->column_name, column_name);
column_group->directory = directory;
free(column_directory_name);
//close(dms_file_descriptor);
return column_group;
}
OBIDMS_column_group_p obi_column_group(OBIDMS_p dms, const char* column_name)
{
int exists;
exists = obi_column_group_exists(dms, column_name);
switch (exists)
{
case 0:
return obi_create_column_group(dms, column_name);
case 1:
return obi_open_column_group(dms, column_name);
};
return NULL;
}
int obi_close_column_group(OBIDMS_column_group_p column_group)
{
if (column_group != NULL)
{
// Close the column directory
if (closedir(column_group->directory) < 0)
{
obi_set_errno(OBICOLDIR_MEMORY_ERROR);
free(column_group);
return -1;
}
free(column_group);
}
return 0;
}
/****************************************************************************
* OBIDMS column directories header file *
****************************************************************************/
/**
* @file obidmscolumngroup.h
* @author Celine Mercier
* @date 18 June 2015
* @brief Header file for OBIDMS column groups.
*/
#ifndef OBIDMSCOLUMNGROUP_H_
#define OBIDMSCOLUMNGROUP_H_
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include "obidms.h"
#define OBIDMS_MAX_COLNAME (128) /**< The maximum length of an OBIDMS column name
*/
#define OBIDMS_COLUMN_DIR_MAX_NAME (2048) /**< The maximum length of an OBIDMS column directory name
*/
/** @brief A structure describing an OBIDMS column group instance
*
* A pointer to this structure is returned on creation
* and opening of an OBIDMS column group.
*/
typedef struct OBIDMS_column_group {
OBIDMS_p dms; /**< A pointer to a DMS instance.
*/
char column_name[OBIDMS_MAX_COLNAME+1]; /**< The name of the column group
* contained in the directory.
*/
char directory_name[OBIDMS_COLUMN_DIR_MAX_NAME+1]; /**< The name of the directory
* containing the column group.
*/
DIR* directory; /**< A directory entry usable to
* refer and scan the database directory.
*/
} OBIDMS_column_group_t, *OBIDMS_column_group_p;
/*@
* @brief Checks if an OBIDMS column group exists
*
* @param dms a pointer to an OBIDMS as returned by obi_create_dms() or obi_open_dms()
* @param column_name a pointer to a C string containing the name of the column.
* The actual directory name used to store the column is
* `<column_name>.obicol`.
*
* @return an integer value indicating the status of the column group
* @retvalue 1 the group exist
* @retvalue 0 the group does not exist
* @retvalue -1 an error occurred
*
* @see obi_close_column_group()
* @since June 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
int obi_column_group_exists(OBIDMS_p dms, const char* column_name);
/**
* @brief Creates a new OBIDMS column group instance.
*
* A new OBIDMS column group is created. This function checks
* if a directory with this name does not already exist
* before creating the new column group.
*
* @param dms a pointer to an OBIDMS as returned by obi_create_dms() or obi_open_dms()
* @param column_name a pointer to a C string containing the name of the column.
* The actual directory name used to store the column will be
* `<column_name>.obicol`.
*
* @return a pointer to an OBIDMS column group structure describing the newly created
* group
* @retval NULL on error and the `obi_errno` variable is set.
*
* ###Error values
* - OBIDMS_COL_DIR_EXIST_ERROR : xxxxx a database with the same name already exists.
*
* @see obi_close_column_group()
* @since June 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
OBIDMS_column_group_p obi_create_column_group(OBIDMS_p dms, const char* column_name);
/**
* @brief Opens an existing OBIDMS column group instance.
*
* @param dms a pointer to an OBIDMS as returned by obi_create_dms() or obi_open_dms()
* @param column_name a pointer to a C string containing the name of the column.
* The actual directory name used to store the column is
* `<column_name>.obicol`.
*
* @return a pointer to the OBIDMS column group structure describing the group
* @retval NULL on error and the `obi_errno`variable is set.
*
* ###Error values
* - OBIDMS_COL_DIR_ERROR : xxxxx a database with the same name already exists.
*
* @see obi_close_column_group()
* @since June 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
OBIDMS_column_group_p obi_open_column_group(OBIDMS_p dms, const char* column_name);
/**
* @brief Opens or creates a new OBIDMS column group instance.
*
* If the group already exists, this function opens it, otherwise it
* creates a new column group.
*
* @param dms a pointer to an OBIDMS as returned by obi_create_dms() or obi_open_dms()