Closes #10 : column groups stored in directories

This commit is contained in:
Celine Mercier
2015-06-23 18:35:34 +02:00
parent 61b6c3ce83
commit 152b34b5f4
10 changed files with 745 additions and 223 deletions

View File

@ -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

View File

@ -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)

View File

@ -1,5 +1,7 @@
../../../../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

View File

@ -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
*
************************************************************************/
**************************************************************************/
/**
@ -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
*
***********************************************************************/
************************************************************************/
static char *build_directory_name(const char *dms_name)
{
@ -71,30 +76,30 @@ 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
*
*********************************************************************/
**********************************************************************/
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
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,10 +144,12 @@ 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) {
if (directory == NULL)
{
switch (errno)
{
case ENOENT:
obi_set_errno(OBIDMS_NOT_EXIST_ERROR);
break;
@ -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;
}

View File

@ -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

View File

@ -13,22 +13,25 @@
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdbool.h>
#include <sys/mman.h> /* mmap() is defined in this header */
#include "obidmscolumn.h"
#include "obidmscolumngroup.h"
#include "obidms.h"
#include "obitypes.h"
#include "obierrno.h"
#include "obilittlebigman.h"
#include "private_openat.h"
#include "private_at_functions.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
*
*************************************************************************/
**************************************************************************/
/**
@ -44,7 +47,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.
*
* @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org)
@ -90,7 +93,7 @@ static char *build_version_file_name(const char *column_name);
* @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org)
*/
static obiversion_t obi_get_new_version_number(OBIDMS_p dms, char *column_name, bool block);
static obiversion_t obi_get_new_version_number(OBIDMS_column_group_p column_group, bool block);
/**
@ -108,7 +111,7 @@ static obiversion_t obi_get_new_version_number(OBIDMS_p dms, char *column_name,
* @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org)
*/
static int create_version_file(OBIDMS_p dms, char *column_name);
static int create_version_file(OBIDMS_column_group_p column_group);
/************************************************************************
@ -124,7 +127,7 @@ static char *build_column_file_name(const char *column_name, obiversion_t versio
// Build the database directory name
if (asprintf(&filename,"%s@%d.odc", column_name, version_number) < 0)
{
obi_set_errno(OBIDMS_MEMORY_ERROR);
obi_set_errno(OBICOL_MEMORY_ERROR);
return NULL;
}
@ -139,7 +142,7 @@ static char *build_version_file_name(const char *column_name)
// Build the database directory name
if (asprintf(&filename,"%s.odv", column_name) < 0)
{
obi_set_errno(OBIDMS_MEMORY_ERROR);
obi_set_errno(OBICOL_MEMORY_ERROR);
return NULL;
}
@ -147,12 +150,12 @@ static char *build_version_file_name(const char *column_name)
}
static obiversion_t obi_get_new_version_number(OBIDMS_p dms, char *column_name, bool block)
static obiversion_t obi_get_new_version_number(OBIDMS_column_group_p column_group, bool block)
{
off_t loc_size;
obiversion_t new_version_number;
char* version_file_name;
int directory_file_descriptor;
int column_dir_file_descriptor;
int version_file_descriptor;
bool little_endian;
int lock_mode;
@ -166,34 +169,31 @@ static obiversion_t obi_get_new_version_number(OBIDMS_p dms, char *column_name,
else
lock_mode=F_TLOCK;
// build the version file name
version_file_name = build_version_file_name(column_name);
// Build the version file name
version_file_name = build_version_file_name(column_group->column_name);
if (version_file_name == NULL)
return -1;
// Get the file descriptor associated to the database directory
directory_file_descriptor = dirfd(dms->directory);
if (directory_file_descriptor < 0)
// Get the file descriptor associated to the column group directory
column_dir_file_descriptor = dirfd(column_group->directory);
if (column_dir_file_descriptor < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
return -1;
}
//open the version file
version_file_descriptor = private_openat(directory_file_descriptor, version_file_name, O_RDWR);
// Open the version file
version_file_descriptor = private_openat(column_dir_file_descriptor, version_file_name, O_RDWR);
if (version_file_descriptor < 0)
{
free(version_file_name);
//close(column_dir_file_descriptor);
if (errno == ENOENT)
{
return create_version_file(dms, column_name);
}
return create_version_file(column_group);
else
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
return -1;
}
}
@ -201,199 +201,223 @@ static obiversion_t obi_get_new_version_number(OBIDMS_p dms, char *column_name,
// Test if the version file size is ok
if (lseek(version_file_descriptor, 0, SEEK_END) < loc_size)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
close(column_dir_file_descriptor);
free(version_file_name);
return -1;
}
// prepare the file for locking
// Prepare the file for locking
if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
close(column_dir_file_descriptor);
free(version_file_name);
return -1;
}
// Lock the file
if (lockf(version_file_descriptor, lock_mode, loc_size) < -1)
if (lockf(version_file_descriptor, lock_mode, loc_size) < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
close(column_dir_file_descriptor);
free(version_file_name);
return -1;
}
// read the endianess of the file
// Read the endianness of the file
if (read(version_file_descriptor, &little_endian, sizeof(bool)) < sizeof(bool))
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
close(column_dir_file_descriptor);
free(version_file_name);
return -1;
}
// check if endianess is correct
// Check if endianness is correct
if (little_endian != obi_is_little_endian())
{
obi_set_errno(OBICOL_BAD_ENDIAN_ERROR);
close(version_file_descriptor);
obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR);
close(column_dir_file_descriptor);
free(version_file_name);
return -1;
}
// read the current version number
// Read the current version number
if (read(version_file_descriptor, &new_version_number, sizeof(obiversion_t)) < sizeof(obiversion_t))
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
close(column_dir_file_descriptor);
free(version_file_name);
return -1;
}
new_version_number++;
// write the new version number
// Write the new version number
if (lseek(version_file_descriptor, sizeof(bool), SEEK_SET) != sizeof(bool))
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
close(column_dir_file_descriptor);
free(version_file_name);
return -1;
}
if (write(version_file_descriptor, &new_version_number, sizeof(obiversion_t)) < sizeof(obiversion_t))
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
close(column_dir_file_descriptor);
free(version_file_name);
return -1;
}
// prepare for the unlocking
// Prepare for unlocking
if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
close(column_dir_file_descriptor);
free(version_file_name);
return -1;
}
// unlock the file
if (lockf(version_file_descriptor, F_ULOCK, loc_size) < -1)
// Unlock the file
if (lockf(version_file_descriptor, F_ULOCK, loc_size) < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
close(column_dir_file_descriptor);
free(version_file_name);
return -1;
}
close(version_file_descriptor);
//close(version_file_descriptor);
//close(column_dir_file_descriptor);
free(version_file_name);
return new_version_number;
}
static int create_version_file(OBIDMS_p dms, char *column_name)
static int create_version_file(OBIDMS_column_group_p column_group)
{
off_t loc_size;
obiversion_t version_number;
char* version_file_name;
int directory_file_descriptor;
int column_dir_file_descriptor;
int version_file_descriptor;
bool little_endian;
loc_size = sizeof(bool) + sizeof(obiversion_t);
version_number = 0;
version_file_name = build_version_file_name(column_name);
version_file_name = build_version_file_name(column_group->column_name);
if (version_file_name == NULL)
return -1;
directory_file_descriptor = dirfd(dms->directory);
if (directory_file_descriptor < 0)
// Get the file descriptor associated to the column group directory
column_dir_file_descriptor = dirfd(column_group->directory);
if (column_dir_file_descriptor < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
return -1;
}
version_file_descriptor = private_openat(directory_file_descriptor,
version_file_name,
O_RDWR | O_CREAT);
// Get the file descriptor associated to the version file
version_file_descriptor = private_openat(column_dir_file_descriptor, version_file_name, O_RDWR | O_CREAT);
if (version_file_descriptor < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(column_dir_file_descriptor);
return -1;
}
// Lock the file
if (lockf(version_file_descriptor, F_LOCK, loc_size) < -1)
if (lockf(version_file_descriptor, F_LOCK, loc_size) < 0)
{
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(version_file_descriptor);
close(column_dir_file_descriptor);
return -1;
}
// Truncate the version file to the right size
if (ftruncate(version_file_descriptor, loc_size) < 0)
{
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(version_file_descriptor);
close(column_dir_file_descriptor);
return -1;
}
// Position offset to 0 to prepare for writing
if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
{
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(version_file_descriptor);
close(column_dir_file_descriptor);
return -1;
}
little_endian = obi_is_little_endian();
// Write endianness
if (write(version_file_descriptor, &little_endian, sizeof(bool)) < sizeof(bool))
{
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(version_file_descriptor);
close(column_dir_file_descriptor);
return -1;
}
// Write version number
if (write(version_file_descriptor, &version_number, sizeof(obiversion_t)) < sizeof(obiversion_t))
{
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(version_file_descriptor);
close(column_dir_file_descriptor);
return -1;
}
// prepare for the unlocking
// Prepare for unlocking
if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
{
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(version_file_descriptor);
close(column_dir_file_descriptor);
return -1;
}
// unlock the file
if (lockf(version_file_descriptor, F_ULOCK, loc_size) < -1)
// Unlock the file
if (lockf(version_file_descriptor, F_ULOCK, loc_size) < 0)
{
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(version_file_descriptor);
close(column_dir_file_descriptor);
return -1;
}
close(version_file_descriptor);
//close(version_file_descriptor);
//close(column_dir_file_descriptor);
free(version_file_name);
return version_number;
}
@ -404,80 +428,95 @@ static int create_version_file(OBIDMS_p dms, char *column_name)
*
**********************************************************************/
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)
{
off_t loc_size;
obiversion_t latest_version_number;
char * version_file_name;
int directory_file_descriptor;
int column_dir_file_descriptor;
int version_file_descriptor;
bool little_endian;
loc_size = sizeof(bool) + sizeof(obiversion_t);
latest_version_number = 0;
version_file_name = build_version_file_name(column_name);
version_file_name = build_version_file_name(column_group->column_name);
if (version_file_name==NULL)
return -1;
directory_file_descriptor = dirfd(dms->directory);
if (directory_file_descriptor < 0) {
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
// Get the file descriptor associated to the column group directory
column_dir_file_descriptor = dirfd(column_group->directory);
if (column_dir_file_descriptor < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
return -1;
}
version_file_descriptor = private_openat(directory_file_descriptor, version_file_name, O_RDONLY);
if (version_file_descriptor < 0) {
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
// Get the file descriptor associated to the version file
version_file_descriptor = private_openat(column_dir_file_descriptor, version_file_name, O_RDONLY);
if (version_file_descriptor < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(column_dir_file_descriptor);
return -1;
}
// Check that the version file size is ok
if (lseek(version_file_descriptor, 0, SEEK_END) < loc_size)
{
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(version_file_descriptor);
close(column_dir_file_descriptor);
return -1;
}
// Set the offset to 0 in the version file
if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
{
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(version_file_descriptor);
close(column_dir_file_descriptor);
return -1;
}
// Read the endianness
if (read(version_file_descriptor, &little_endian, sizeof(bool)) < sizeof(bool))
{
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(version_file_descriptor);
close(column_dir_file_descriptor);
return -1;
}
// Verify the endianness
if (little_endian != obi_is_little_endian())
{
close(version_file_descriptor);
obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR);
obi_set_errno(OBICOL_BAD_ENDIAN_ERROR);
free(version_file_name);
close(version_file_descriptor);
close(column_dir_file_descriptor);
return -1;
}
// Read the latest version number
if (read(version_file_descriptor, &latest_version_number, sizeof(obiversion_t)) < sizeof(obiversion_t))
{
close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
free(version_file_name);
close(version_file_descriptor);
close(column_dir_file_descriptor);
return -1;
}
close(version_file_descriptor);
free(version_file_name);
//close(version_file_descriptor);
//close(column_dir_file_descriptor);
return latest_version_number;
}
@ -489,66 +528,97 @@ size_t obi_get_platform_header_size()
OBIDMS_column_p obi_create_column(OBIDMS_p dms,
char *column_name,
const char *column_name,
OBIType_t type,
size_t nb_elements)
{
OBIDMS_column_p new_column;
OBIDMS_column_group_p column_group;
OBIDMS_column_header_p header;
size_t file_size;
obiversion_t version_number;
char* column_file_name;
int column_file_descriptor;
int directory_file_descriptor;
int column_dir_file_descriptor;
size_t header_size;
size_t data_size;
new_column = NULL;
directory_file_descriptor = dirfd(dms->directory);
if (directory_file_descriptor < 0) {
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
// Get the column group structure associated to the column
column_group = obi_column_group(dms, column_name);
if (column_group == NULL)
return NULL;
// Get the file descriptor associated to the column group directory
column_dir_file_descriptor = dirfd(column_group->directory);
if (column_dir_file_descriptor < 0)
{
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
obi_close_column_group(column_group);
return NULL;
}
// Calculate the size needed
header_size = obi_get_platform_header_size();
data_size = obi_array_sizeof(type, nb_elements);
file_size = header_size + data_size;
version_number = obi_get_new_version_number(dms, column_name, true);
// Get the latest version number
version_number = obi_get_new_version_number(column_group, true);
if (version_number < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obi_close_column_group(column_group);
close(column_dir_file_descriptor);
return NULL;
}
// Get the column file name
column_file_name = build_column_file_name(column_name, version_number);
if (column_file_name == NULL)
{
obi_close_column_group(column_group);
close(column_dir_file_descriptor);
return NULL;
}
column_file_descriptor = private_openat(directory_file_descriptor,
column_file_name,
O_RDWR | O_CREAT);
// Open the column file
column_file_descriptor = private_openat(column_dir_file_descriptor, column_file_name, O_RDWR | O_CREAT);
if (column_file_descriptor < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obi_close_column_group(column_group);
close(column_dir_file_descriptor);
free(column_file_name);
return NULL;
}
// Truncate the column file to the right size
if (ftruncate(column_file_descriptor, file_size) < 0)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obi_close_column_group(column_group);
close(column_dir_file_descriptor);
close(column_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(column_file_name);
return NULL;
}
// Allocate the memory for the column structure
new_column = (OBIDMS_column_p) malloc(sizeof(OBIDMS_column_t));
if (new_column)
if (new_column == NULL)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obi_close_column_group(column_group);
close(column_dir_file_descriptor);
close(column_file_descriptor);
obi_set_errno(OBIDMS_MEMORY_ERROR);
free(column_file_name);
return NULL;
}
// Fill the column structure
new_column->dms = dms;
new_column->column_group = column_group;
new_column->header = mmap(NULL,
header_size,
PROT_READ | PROT_WRITE,
@ -559,8 +629,10 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
if (new_column->header == MAP_FAILED)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obi_close_column_group(column_group);
close(column_dir_file_descriptor);
close(column_file_descriptor);
obi_set_errno(OBIDMS_MEMORY_ERROR);
free(column_file_name);
free(new_column);
return NULL;
@ -577,8 +649,10 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
if (new_column->data == MAP_FAILED)
{
munmap(new_column->header, header_size);
obi_set_errno(OBICOL_UNKNOWN_ERROR);
obi_close_column_group(column_group);
close(column_dir_file_descriptor);
close(column_file_descriptor);
obi_set_errno(OBIDMS_MEMORY_ERROR);
free(column_file_name);
free(new_column);
return NULL;
@ -598,14 +672,9 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
strncpy(header->name, column_name, OBIDMS_MAX_COLNAME);
free(column_file_name);
//obi_close_column_group(column_group);
//close(column_dir_file_descriptor);
//close(column_file_descriptor);
return new_column;
}
// if( access( fname, F_OK ) != -1 ) {
// // file exists
// } else {
// // file doesn't exist
// }

View File

@ -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
*
@ -66,6 +65,8 @@ typedef struct OBIDMS_column_header {
typedef struct OBIDMS_column {
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
@ -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,7 +113,7 @@ 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,
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_ */

283
src/obidmscolumngroup.c Normal file
View File

@ -0,0 +1,283 @@
/****************************************************************************
* 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;
}

158
src/obidmscolumngroup.h Normal file
View File

@ -0,0 +1,158 @@
/****************************************************************************
* 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()
* @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_LONG_NAME_ERROR : the database name exceeds the limit.
* - OBIDMS_MEMORY_ERROR : something wrong occurred during memory allocation.
*
* @since June 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
OBIDMS_column_group_p obi_column_group(OBIDMS_p dms, const char* column_name);
/**
* @brief Closes an opened OBIDMS column group instance.
*
* @param column_group a pointer to an OBIDMS column group as returned by
* obi_create_column_group() or obi_open_column_group()
*
* @return an integer value indicating the success of the operation. Even on
* error, the `OBIDMS_column_group` structure is freed
* @retvalue 0 on success
* @retvalue -1 on failure and the `obi_errno` variable is set.
*
* @see obi_create_column_group()
* @see obi_open_column_group()
* @since June 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
int obi_close_column_group(OBIDMS_column_group_p column_group);
#endif /* OBIDMSCOLUMNDIR_H_ */

View File

@ -8,7 +8,8 @@
#include <obitypes.h>
size_t obi_sizeof(OBIType_t type) {
size_t obi_sizeof(OBIType_t type)
{
size_t size=0;
switch (type) {
@ -34,7 +35,8 @@ size_t obi_sizeof(OBIType_t type) {
}
size_t obi_array_sizeof(OBIType_t type, size_t nbelements) {
size_t obi_array_sizeof(OBIType_t type, size_t nbelements)
{
size_t size;
size_t rsize;
size_t psize;