fixed bugs and typos in the obidms and obidmscolumn source and header

files
This commit is contained in:
Celine Mercier
2015-06-17 16:51:16 +02:00
parent 9f9c20aa4a
commit ed662077b3
4 changed files with 342 additions and 316 deletions

View File

@ -5,7 +5,10 @@
* @Author: coissac * @Author: coissac
*/ */
#include <obidms.h> #include <stdlib.h>
#include <stdio.h>
#include "obidms.h"
/************************************************************************ /************************************************************************
@ -23,10 +26,10 @@
* *
* @warning The returned pointer has to be freed by the caller. * @warning The returned pointer has to be freed by the caller.
* *
* @param name the name of the OBIDMS * @param dms_name the name of the OBIDMS
* *
* @return a pointer to the directory name * @return a pointer to the directory name
* @retvalue <dirdbname> if everything is ok * @retvalue <directory_name> if everything is ok
* @retvalue NULL if an error occurs * @retvalue NULL if an error occurs
* *
* ###Error values * ###Error values
@ -36,27 +39,7 @@
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
static char *build_directory_name(const char *name) { static char *build_directory_name(const char *dms_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;
}
/*********************************************************************** /***********************************************************************
@ -65,67 +48,101 @@ static char *build_directory_name(const char *name) {
* *
***********************************************************************/ ***********************************************************************/
int obi_dms_exists(const char* name) { static char *build_directory_name(const char *dms_name)
{
char *directory_name;
// Build the database directory name
if (asprintf(&directory_name, "%s.obidms", dms_name) < 0)
{
obi_set_errno(OBIDMS_MEMORY_ERROR);
return NULL;
}
// Test if the database name is not too long
if (strlen(directory_name) >= OBIDMS_MAX_NAME)
{
obi_set_errno(OBIDMS_LONG_NAME_ERROR);
free(directory_name);
return NULL;
}
return 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_dms_exists(const char* dms_name)
{
struct stat buffer; struct stat buffer;
char *dirdbname; char *directory_name;
int exist; int exists;
// Build and check the directory name // Build and check the directory name
dirdbname = build_directory_name(name); directory_name = build_directory_name(dms_name);
if (dirdbname==NULL) if (directory_name == NULL)
return -1; return -1;
exist = stat(dirdbname,&buffer); exists = stat(directory_name, &buffer);
free(dirdbname); free(directory_name);
if(exist == 0) if(exists == 0)
return 1; return 1;
else // -1 else // -1
return 0; return 0;
} }
OBIDMS_p obi_create_dms(const char *name) { OBIDMS_p obi_create_dms(const char* dms_name)
char *dirdbname; {
char *directory_name;
// Build and check the directory name // Build and check the directory name
dirdbname = build_directory_name(name); directory_name = build_directory_name(dms_name);
if (dirdbname==NULL) if (directory_name == NULL)
return NULL; return NULL;
// Try to create the directory // Try to create the directory
if (mkdir(dirdbname,0x777) < 0) if (mkdir(directory_name, 0x777) < 0)
{ {
if (errno==EEXIST) if (errno == EEXIST)
obi_set_errno(OBIDMS_EXIST_ERROR); obi_set_errno(OBIDMS_EXIST_ERROR);
else else
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(dirdbname); free(directory_name);
return NULL; return NULL;
} }
free(dirdbname); free(directory_name);
return obi_open_dms(name); return obi_open_dms(dms_name);
} }
OBIDMS_p obi_open_dms(const char *name) { OBIDMS_p obi_open_dms(const char* dms_name)
OBIDMS_p dms=NULL; {
char *dirdbname; OBIDMS_p dms;
DIR *directory; char* directory_name;
DIR* directory;
dms = NULL;
// Build and check the directory name // Build and check the directory name
dirdbname = build_directory_name(name); directory_name = build_directory_name(dms_name);
if (dirdbname==NULL) if (directory_name == NULL)
return NULL; return NULL;
// try to open the directory // try to open the directory
directory = opendir(dirdbname); directory = opendir(directory_name);
if (directory==NULL) { if (directory == NULL) {
switch (errno) { switch (errno) {
case ENOENT: case ENOENT:
obi_set_errno(OBIDMS_NOT_EXIST_ERROR); obi_set_errno(OBIDMS_NOT_EXIST_ERROR);
@ -142,37 +159,38 @@ OBIDMS_p obi_open_dms(const char *name) {
default: default:
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
} }
free(dirdbname); free(directory_name);
return NULL; return NULL;
} }
// Allocate the data structure // Allocate the data structure
dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t)); dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t));
if (dms==NULL) if (dms == NULL)
{ {
obi_set_errno(OBIDMS_MEMORY_ERROR); obi_set_errno(OBIDMS_MEMORY_ERROR);
free(dirdbname); free(directory_name);
return NULL; return NULL;
} }
// Initialise the data structure // Initialize the data structure
strcpy(dms->dirname,dirdbname); strcpy(dms->directory_name, directory_name);
dms->directory=directory; dms->directory = directory;
free(dirdbname); free(directory_name);
return dms; return dms;
} }
OBIDMS_p obi_dms(const char *name) { OBIDMS_p obi_dms(const char* dms_name)
int exist = obi_dms_exists(name); {
int exists = obi_dms_exists(dms_name);
switch (exist) { switch (exists) {
case 0: case 0:
return obi_create_dms(name); return obi_create_dms(dms_name);
case 1: case 1:
return obi_open_dms(name); return obi_open_dms(dms_name);
}; };
return NULL; return NULL;
@ -180,8 +198,8 @@ OBIDMS_p obi_dms(const char *name) {
} }
int obi_close_dms(OBIDMS_p dms) { int obi_close_dms(OBIDMS_p dms)
{
if (dms != NULL) if (dms != NULL)
{ {
if (closedir(dms->directory) < 0) if (closedir(dms->directory) < 0)
@ -190,9 +208,7 @@ int obi_close_dms(OBIDMS_p dms) {
free(dms); free(dms);
return -1; return -1;
} }
free(dms); free(dms);
} }
return 0; return 0;

View File

@ -15,31 +15,31 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <obierrno.h> #include "obierrno.h"
#define OBIDMS_MAX_NAME (2048) /**< The maximum length of an OBIDMS name #define OBIDMS_MAX_NAME (2048) /**< The maximum length of an OBIDMS name
*/ */
/** @brief A structure decribing an OBIDMS instance /** @brief A structure describing an OBIDMS instance
* *
* A pointer to this structure is returned on creation * A pointer to this structure is returned on creation
* and opening of an OBITools Data Management System (DMS) * and opening of an OBITools Data Management System (DMS)
*/ */
typedef struct OBIDMS { typedef struct OBIDMS {
char dirname[OBIDMS_MAX_NAME+1]; /**< The name of the directory char directory_name[OBIDMS_MAX_NAME+1]; /**< The name of the directory
* containing the DMS * containing the DMS
*/ */
DIR *directory; /**< A directory entry usable to DIR* directory; /**< A directory entry usable to
* refer and scan the database directory * refer and scan the database directory
*/ */
} OBIDMS_t, *OBIDMS_p; } OBIDMS_t, *OBIDMS_p;
/*@ /*@
* @brief Checks if an OBIDMS exists * @brief Checks if an OBIDMS exists
* *
* @param name a pointer to a C string containing the name of the database. * @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 * The actual directory name used to store the DMS will be
* `<name>.obidms`. * `<name>.obidms`.
* *
@ -52,7 +52,7 @@ typedef struct OBIDMS {
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
int obi_dms_exists(const char* name); int obi_dms_exists(const char* dms_name);
/** /**
@ -62,7 +62,7 @@ int obi_dms_exists(const char* name);
* if a directory with this name does not already exist * if a directory with this name does not already exist
* before creating the new database. * before creating the new database.
* *
* @param name a pointer to a C string containing the name of the database. * @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 * The actual directory name used to store the DMS will be
* `<name>.obidms` * `<name>.obidms`
* *
@ -78,13 +78,13 @@ int obi_dms_exists(const char* name);
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
OBIDMS_p obi_create_dms(const char *name); OBIDMS_p obi_create_dms(const char *dms_name);
/** /**
* @brief Opens an existing OBITools Data Management instance (OBIDMS). * @brief Opens an existing OBITools Data Management instance (OBIDMS).
* *
* @param name a pointer to a C string containing the name of the database. * @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 * The actual directory name used to store the DMS will be
* `<name>.obidms` * `<name>.obidms`
* *
@ -100,7 +100,7 @@ OBIDMS_p obi_create_dms(const char *name);
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
OBIDMS_p obi_open_dms(const char *name); OBIDMS_p obi_open_dms(const char *dms_name);
/** /**
@ -109,7 +109,7 @@ OBIDMS_p obi_open_dms(const char *name);
* If the database already exists, this function opens it, otherwise it * If the database already exists, this function opens it, otherwise it
* creates a new database. * creates a new database.
* *
* @param name a pointer to a C string containing the name of the database. * @param dms_name a pointer to a C string containing the name of the database.
* The actual directory name used to store the DMS is * The actual directory name used to store the DMS is
* `<name>.obidms` * `<name>.obidms`
* *
@ -124,7 +124,7 @@ OBIDMS_p obi_open_dms(const char *name);
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
OBIDMS_p obi_dms(const char *name); OBIDMS_p obi_dms(const char *dms_name);
/** /**

View File

@ -10,11 +10,19 @@
*/ */
#include <obidmscolumn.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/mman.h> /* mmap() is defined in this header */ #include <sys/mman.h> /* mmap() is defined in this header */
#include "obidmscolumn.h"
#include "obidms.h"
#include "obitypes.h"
#include "obierrno.h"
#include "obilittlebigman.h"
#include "private_openat.h"
/************************************************************************* /*************************************************************************
* *
@ -30,9 +38,9 @@
* *
* @warning The returned pointer has to be freed by the caller. * @warning The returned pointer has to be freed by the caller.
* *
* @param name the name of the column of the OBIDMS * @param column_name the name of the OBIDMS column.
* *
* @return a pointer to the filename name * @return a pointer to the column file name
* @retvalue NULL if an error occurs * @retvalue NULL if an error occurs
* *
* ###Error values * ###Error values
@ -41,20 +49,20 @@
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
static char *build_column_name(const char *name, obiversion_t version); static char *build_column_file_name(const char *column_name, obiversion_t version_number);
/** /**
* @brief Internal function building the file name for a version column file. * @brief Internal function building the file name for a column version file.
* *
* The version column file indicate the latest version number for a column. * The column version file indicates the latest version number for a column.
* This function returns the name of the file storing this information * This function returns the name of the file storing this information.
* *
* @warning The returned pointer has to be freed by the caller. * @warning The returned pointer has to be freed by the caller.
* *
* @param name the name of the column of the OBIDMS * @param column_name the name of the OBIDMS column.
* *
* @return a pointer to the filename name * @return a pointer to the version file name
* @retvalue NULL if an error occurs * @retvalue NULL if an error occurs
* *
* ###Error values * ###Error values
@ -63,7 +71,7 @@ static char *build_column_name(const char *name, obiversion_t version);
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
static char *build_version_name(const char *name); static char *build_version_file_name(const char *column_name);
/** /**
@ -71,7 +79,7 @@ static char *build_version_name(const char *name);
* in the `dms` database * in the `dms` database
* *
* @param dms a pointer as returned by obi_create_dms() or obi_open_dms() * @param dms a pointer as returned by obi_create_dms() or obi_open_dms()
* @param name the name of the column * @param column_name the name of the column
* @param block is the call is blocking or not * @param block is the call is blocking or not
* - `true` the call is blocking * - `true` the call is blocking
* - `false` the call is not blocking * - `false` the call is not blocking
@ -82,7 +90,7 @@ static char *build_version_name(const char *name);
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
static obiversion_t obi_new_version(OBIDMS_p dms,char *name, bool block); static obiversion_t obi_get_new_version_number(OBIDMS_p dms, char *column_name, bool block);
/** /**
@ -92,15 +100,15 @@ static obiversion_t obi_new_version(OBIDMS_p dms,char *name, bool block);
* The new file is initialized with the minimum version number `0`. * The new file is initialized with the minimum version number `0`.
* *
* @param dms a pointer as returned by obi_create_dms() or obi_open_dms() * @param dms a pointer as returned by obi_create_dms() or obi_open_dms()
* @param name the name of the column * @param column_name the name of the column
* *
* @return the next usable version number for this comumn : `0` * @return the next usable version number for this column : `0`
* @retvalue -1 if the column does not exist * @retvalue -1 if the column does not exist
* *
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
static int create_version(OBIDMS_p dms,char *name); static int create_version_file(OBIDMS_p dms, char *column_name);
/************************************************************************ /************************************************************************
@ -109,12 +117,12 @@ static int create_version(OBIDMS_p dms,char *name);
* *
************************************************************************/ ************************************************************************/
static char *build_column_name(const char *name,obiversion_t version) { static char *build_column_file_name(const char *column_name, obiversion_t version_number)
{
char *filename; char *filename;
// Build the database directory name // Build the database directory name
if (asprintf(&filename,"%s@%d.odc",name,version) < 0) if (asprintf(&filename,"%s@%d.odc", column_name, version_number) < 0)
{ {
obi_set_errno(OBIDMS_MEMORY_ERROR); obi_set_errno(OBIDMS_MEMORY_ERROR);
return NULL; return NULL;
@ -124,12 +132,12 @@ static char *build_column_name(const char *name,obiversion_t version) {
} }
static char *build_version_name(const char *name) { static char *build_version_file_name(const char *column_name)
{
char *filename; char *filename;
// Build the database directory name // Build the database directory name
if (asprintf(&filename,"%s.odv",name) < 0) if (asprintf(&filename,"%s.odv", column_name) < 0)
{ {
obi_set_errno(OBIDMS_MEMORY_ERROR); obi_set_errno(OBIDMS_MEMORY_ERROR);
return NULL; return NULL;
@ -139,46 +147,49 @@ static char *build_version_name(const char *name) {
} }
static obiversion_t obi_new_version(OBIDMS_p dms,char *name, bool block) { static obiversion_t obi_get_new_version_number(OBIDMS_p dms, char *column_name, bool block)
{
off_t loc_size;
obiversion_t new_version_number;
char* version_file_name;
int directory_file_descriptor;
int version_file_descriptor;
bool little_endian;
int lock_mode;
off_t locsize=sizeof(bool) + sizeof(obiversion_t); new_version_number = 0;
obiversion_t newversion=0; loc_size = sizeof(bool) + sizeof(obiversion_t);
char * versionfile;
int directoryfd;
int versionfd;
bool little;
int lockmode;
// Select the correct lockf operation according to the blocking mode // Select the correct lockf operation according to the blocking mode
if (block) if (block)
lockmode=F_LOCK; lock_mode=F_LOCK;
else else
lockmode=F_TLOCK; lock_mode=F_TLOCK;
// build the version file name // build the version file name
versionfile = build_version_name(name); version_file_name = build_version_file_name(column_name);
if (versionfile==NULL) if (version_file_name == NULL)
return -1; return -1;
// Get the file descriptior associated to the database directory // Get the file descriptor associated to the database directory
directoryfd = dirfd(dms->directory); directory_file_descriptor = dirfd(dms->directory);
if (directoryfd < 0) { if (directory_file_descriptor < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
//open the version file //open the version file
//versionfd = openat(directoryfd, versionfile, O_RDWR); version_file_descriptor = private_openat(directory_file_descriptor, version_file_name, O_RDWR);
versionfd = open(versionfile, O_RDWR);
if (version_file_descriptor < 0)
if (versionfd < 0) { {
free(versionfile); free(version_file_name);
if (errno == ENOENT) if (errno == ENOENT)
{ {
return create_version(dms, name); return create_version_file(dms, column_name);
} }
else else
{ {
@ -188,283 +199,286 @@ static obiversion_t obi_new_version(OBIDMS_p dms,char *name, bool block) {
} }
// Test if the version file size is ok // Test if the version file size is ok
if (lseek(versionfd, SEEK_END, 0) < locsize) if (lseek(version_file_descriptor, 0, SEEK_END) < loc_size)
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
// prepare the file for locking // prepare the file for locking
if (lseek(versionfd, SEEK_SET, 0) != 0) if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
// Lock the file // Lock the file
if (lockf(versionfd, lockmode, locsize) < -1) if (lockf(version_file_descriptor, lock_mode, loc_size) < -1)
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
// read the endianess of the file // read the endianess of the file
if (read(versionfd, &little, sizeof(bool)) < sizeof(bool)) if (read(version_file_descriptor, &little_endian, sizeof(bool)) < sizeof(bool))
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
// check if endianess is correct // check if endianess is correct
if (little != obi_is_little_endian()) if (little_endian != obi_is_little_endian())
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR); obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
// read the current version number // read the current version number
if (read(versionfd, &newversion, sizeof(obiversion_t)) < sizeof(obiversion_t)) if (read(version_file_descriptor, &new_version_number, sizeof(obiversion_t)) < sizeof(obiversion_t))
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
newversion++; new_version_number++;
// write the new version number // write the new version number
if (lseek(versionfd,SEEK_SET,sizeof(bool)) != sizeof(bool)) if (lseek(version_file_descriptor, sizeof(bool), SEEK_SET) != sizeof(bool))
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
if (write(versionfd, &newversion, sizeof(obiversion_t)) < sizeof(obiversion_t)) if (write(version_file_descriptor, &new_version_number, sizeof(obiversion_t)) < sizeof(obiversion_t))
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
// prepare for the unlocking // prepare for the unlocking
if (lseek(versionfd,SEEK_SET,0) != 0) if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
// unlock the file // unlock the file
if (lockf(versionfd, F_ULOCK, locsize) < -1) if (lockf(version_file_descriptor, F_ULOCK, loc_size) < -1)
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
close(versionfd); close(version_file_descriptor);
free(versionfile); free(version_file_name);
return newversion; return new_version_number;
} }
static int create_version(OBIDMS_p dms,char *name) { static int create_version_file(OBIDMS_p dms, char *column_name)
off_t locsize=sizeof(bool) + sizeof(obiversion_t); {
obiversion_t version=0; off_t loc_size;
char * versionfile; obiversion_t version_number;
int directoryfd; char* version_file_name;
int versionfd; int directory_file_descriptor;
bool little; int version_file_descriptor;
bool little_endian;
versionfile = build_version_name(name); loc_size = sizeof(bool) + sizeof(obiversion_t);
version_number = 0;
if (versionfile==NULL) version_file_name = build_version_file_name(column_name);
if (version_file_name == NULL)
return -1; return -1;
directoryfd = dirfd(dms->directory); directory_file_descriptor = dirfd(dms->directory);
if (directoryfd < 0) { if (directory_file_descriptor < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
//versionfd = openat(directoryfd, version_file_descriptor = private_openat(directory_file_descriptor,
// versionfile, version_file_name,
// O_RDWR|O_CREAT|O_EXCL, O_RDWR | O_CREAT);
// 666);
versionfd = open(versionfile, if (version_file_descriptor < 0)
O_RDWR|O_CREAT|O_EXCL, {
666);
if (versionfd < 0) {
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
// Lock the file // Lock the file
if (lockf(versionfd, F_LOCK, locsize) < -1) if (lockf(version_file_descriptor, F_LOCK, loc_size) < -1)
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
if (ftruncate(versionfd,locsize) < 0) if (ftruncate(version_file_descriptor, loc_size) < 0)
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
if (lseek(versionfd,SEEK_SET,0) != 0) if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
little = obi_is_little_endian(); little_endian = obi_is_little_endian();
if (write(versionfd, &little, sizeof(bool)) < sizeof(bool)) if (write(version_file_descriptor, &little_endian, sizeof(bool)) < sizeof(bool))
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
if (write(versionfd, &version, sizeof(obiversion_t)) < sizeof(obiversion_t)) if (write(version_file_descriptor, &version_number, sizeof(obiversion_t)) < sizeof(obiversion_t))
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
// prepare for the unlocking // prepare for the unlocking
if (lseek(versionfd,SEEK_SET,0) != 0) if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
// unlock the file // unlock the file
if (lockf(versionfd, F_ULOCK, locsize) < -1) if (lockf(version_file_descriptor, F_ULOCK, loc_size) < -1)
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
close(versionfd); close(version_file_descriptor);
free(versionfile); free(version_file_name);
return version; return version_number;
} }
/*********************************************************************** /**********************************************************************
* *
* 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
* *
***********************************************************************/ **********************************************************************/
obiversion_t obi_latest_version(OBIDMS_p dms,char *name) { obiversion_t obi_get_latest_version_number(OBIDMS_p dms, char *column_name)
off_t locsize=sizeof(bool) + sizeof(obiversion_t); {
obiversion_t version=0; off_t loc_size;
char * versionfile; obiversion_t latest_version_number;
int directoryfd; char * version_file_name;
int versionfd; int directory_file_descriptor;
bool little; int version_file_descriptor;
bool little_endian;
versionfile = build_version_name(name); loc_size = sizeof(bool) + sizeof(obiversion_t);
if (versionfile==NULL) latest_version_number = 0;
version_file_name = build_version_file_name(column_name);
if (version_file_name==NULL)
return -1; return -1;
directoryfd = dirfd(dms->directory); directory_file_descriptor = dirfd(dms->directory);
if (directoryfd < 0) { if (directory_file_descriptor < 0) {
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
//versionfd = openat(directoryfd,versionfile,O_RDWR); version_file_descriptor = private_openat(directory_file_descriptor, version_file_name, O_RDONLY);
versionfd = open(versionfile,O_RDWR);
if (versionfd < 0) { if (version_file_descriptor < 0) {
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
if (lseek(versionfd,SEEK_END,0) < locsize) if (lseek(version_file_descriptor, 0, SEEK_END) < loc_size)
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
if (lseek(versionfd,SEEK_SET,0) != 0) if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
if (read(versionfd, &little, sizeof(bool)) < sizeof(bool)) if (read(version_file_descriptor, &little_endian, sizeof(bool)) < sizeof(bool))
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
if (little != obi_is_little_endian()) if (little_endian != obi_is_little_endian())
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR); obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
if (read(versionfd, &version, sizeof(obiversion_t)) < sizeof(obiversion_t)) if (read(version_file_descriptor, &latest_version_number, sizeof(obiversion_t)) < sizeof(obiversion_t))
{ {
close(versionfd); close(version_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(versionfile); free(version_file_name);
return -1; return -1;
} }
close(versionfd); close(version_file_descriptor);
free(versionfile); free(version_file_name);
return version; return latest_version_number;
} }
@ -475,120 +489,116 @@ size_t obi_get_platform_header_size()
OBIDMS_column_p obi_create_column(OBIDMS_p dms, OBIDMS_column_p obi_create_column(OBIDMS_p dms,
char *name, char *column_name,
OBIType_t type, OBIType_t type,
size_t nbelements) size_t nb_elements)
{ {
OBIDMS_column_p newcolumn=NULL; OBIDMS_column_p new_column;
OBIDMS_column_header_p header; OBIDMS_column_header_p header;
size_t filesize; size_t file_size;
obiversion_t version; obiversion_t version_number;
char * columnfile; char* column_file_name;
int columnfd; int column_file_descriptor;
int directoryfd; int directory_file_descriptor;
size_t hsize; size_t header_size;
size_t dsize; size_t data_size;
new_column = NULL;
directoryfd = dirfd(dms->directory); directory_file_descriptor = dirfd(dms->directory);
if (directoryfd < 0) { if (directory_file_descriptor < 0) {
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
return NULL; return NULL;
} }
hsize = obi_get_platform_header_size(); header_size = obi_get_platform_header_size();
dsize = obi_array_sizeof(type,nbelements); data_size = obi_array_sizeof(type, nb_elements);
filesize = hsize + dsize; file_size = header_size + data_size;
version = obi_new_version(dms,name,true); version_number = obi_get_new_version_number(dms, column_name, true);
if (version < 0) if (version_number < 0)
{ {
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
return NULL; return NULL;
} }
columnfile = build_column_name(name,version); column_file_name = build_column_file_name(column_name, version_number);
//columnfd = openat(directoryfd, column_file_descriptor = private_openat(directory_file_descriptor,
// columnfile, column_file_name,
// O_RDWR|O_CREAT|O_EXCL, O_RDWR | O_CREAT);
// 666);
columnfd = open(columnfile, if (ftruncate(column_file_descriptor, file_size) < 0)
O_RDWR|O_CREAT|O_EXCL,
666);
if (ftruncate(columnfd,filesize) < 0)
{ {
close(columnfd); close(column_file_descriptor);
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
free(columnfile); free(column_file_name);
return NULL; return NULL;
} }
newcolumn = (OBIDMS_column_p) malloc(sizeof(OBIDMS_column_t)); new_column = (OBIDMS_column_p) malloc(sizeof(OBIDMS_column_t));
if (newcolumn) if (new_column)
{ {
close(columnfd); close(column_file_descriptor);
obi_set_errno(OBIDMS_MEMORY_ERROR); obi_set_errno(OBIDMS_MEMORY_ERROR);
free(columnfile); free(column_file_name);
return NULL; return NULL;
} }
newcolumn->dms = dms; new_column->dms = dms;
newcolumn->header = mmap(NULL, new_column->header = mmap(NULL,
hsize, header_size,
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_SHARED, MAP_SHARED,
columnfd, column_file_descriptor,
0 0
); );
if (newcolumn->header == MAP_FAILED) if (new_column->header == MAP_FAILED)
{ {
close(columnfd); close(column_file_descriptor);
obi_set_errno(OBIDMS_MEMORY_ERROR); obi_set_errno(OBIDMS_MEMORY_ERROR);
free(columnfile); free(column_file_name);
free(newcolumn); free(new_column);
return NULL; return NULL;
} }
newcolumn->data = mmap(NULL, new_column->data = mmap(NULL,
dsize, data_size,
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_SHARED, MAP_SHARED,
columnfd, column_file_descriptor,
hsize header_size
); );
if (newcolumn->data == MAP_FAILED) if (new_column->data == MAP_FAILED)
{ {
munmap(newcolumn->header,hsize); munmap(new_column->header, header_size);
close(columnfd); close(column_file_descriptor);
obi_set_errno(OBIDMS_MEMORY_ERROR); obi_set_errno(OBIDMS_MEMORY_ERROR);
free(columnfile); free(column_file_name);
free(newcolumn); free(new_column);
return NULL; return NULL;
} }
newcolumn->writable= true; new_column->writable= true;
header = newcolumn->header; header = new_column->header;
header->little_endian = obi_is_little_endian(); header->little_endian = obi_is_little_endian();
header->header_size = hsize; header->header_size = header_size;
header->line_count = nbelements; header->line_count = nb_elements;
header->line_used = 0; header->lines_used = 0;
header->data_type = type; header->data_type = type;
header->creation_date = time(NULL); header->creation_date = time(NULL);
header->version = version; header->version = version_number;
header->comments[0] = 0x0; header->comments[0] = 0x0;
strncpy(header->name,name,OBIDMS_MAX_COLNAME); strncpy(header->name, column_name, OBIDMS_MAX_COLNAME);
free(columnfile); free(column_file_name);
return newcolumn; return new_column;
} }

View File

@ -18,10 +18,10 @@
#include <stdbool.h> #include <stdbool.h>
#include <time.h> #include <time.h>
#include <obidms.h> #include "obidms.h"
#include <obitypes.h> #include "obitypes.h"
#include <obierrno.h> #include "obierrno.h"
#include <obilittlebigman.h> #include "obilittlebigman.h"
#define OBIDMS_MAX_COLNAME (128) /**< The maximum length of an OBIDMS column name #define OBIDMS_MAX_COLNAME (128) /**< The maximum length of an OBIDMS column name
@ -43,7 +43,7 @@ typedef struct OBIDMS_column_header {
*/ */
int header_size; /**< size of the header in bytes */ int header_size; /**< size of the header in bytes */
size_t line_count; /**< number of lines of data */ size_t line_count; /**< number of lines of data */
size_t line_used; /**< number of lines of data used*/ size_t lines_used; /**< number of lines of data used*/
OBIType_t data_type; /**< type of the data */ OBIType_t data_type; /**< type of the data */
time_t creation_date; /**< date of creation of the file */ time_t creation_date; /**< date of creation of the file */
obiversion_t version; /**< version of the OBIColumn */ obiversion_t version; /**< version of the OBIColumn */
@ -89,8 +89,8 @@ typedef struct OBIDMS_column {
/** /**
* @brief Returns the header size in bytes of a column on this platform. * @brief Returns the header size in bytes of a column on this platform.
* *
* Header size is defined as a multiple of the memory page size. * The header size is defined as a multiple of the memory page size.
* Up to now the header size is defined as one time the page size * Up to now the header size is defined as one time the page size.
* *
* @return a `size_t` value corresponding to the header size in bytes * @return a `size_t` value corresponding to the header size in bytes
* *
@ -104,29 +104,29 @@ size_t obi_get_platform_header_size();
* @brief Creates a column. * @brief Creates a column.
* *
* @param dms a pointer as returned by obi_create_dms() or obi_open_dms() * @param dms a pointer as returned by obi_create_dms() or obi_open_dms()
* @param name the name of the new column * @param column_name the name of the new column
* @param type the OBIType code used to create the column * @param type the OBIType code used to create the column
* @param nbelements the number of elements to be stored * @param nb_elements the number of elements to be stored
* *
* @since May 2015 * @since May 2015
* @author Eric Coissac (eric.coissac@metabarcoding.org) * @author Eric Coissac (eric.coissac@metabarcoding.org)
*/ */
OBIDMS_column_p obi_create_column(OBIDMS_p dms, OBIDMS_column_p obi_create_column(OBIDMS_p dms,
char* name, char* column_name,
OBIType_t type, OBIType_t type,
size_t nbelements); size_t nb_elements);
/** /**
* @brief Returns the latest column version in the `dms` database * @brief Returns the latest column version in the `dms` database
* *
* @param dms a pointer as returned by obi_create_dms() or obi_open_dms() * @param dms a pointer as returned by obi_create_dms() or obi_open_dms()
* @param name the name of the column * @param column_name the name of the column
* *
* @return the bigger version number used for this column * @return the bigger version number used for this column
* @return -1 if the column does not exist * @return -1 if the column does not exist
*/ */
obiversion_t obi_latest_version(OBIDMS_p dms,char *name); obiversion_t obi_get_latest_version_number(OBIDMS_p dms, char *column_name);
#endif /* OBIDMSCOLUMN_H_ */ #endif /* OBIDMSCOLUMN_H_ */