From 6248160eeb411c484c5e2371cb91e634ab92cb85 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Tue, 26 May 2015 10:38:56 +0200 Subject: [PATCH] C Syntax error patches --- src/obidms.c | 5 +- src/obidms.h | 7 +- src/obidmscolumn.c | 570 ++++++++++++++++++++++++++++++++++++++++++++- src/obidmscolumn.h | 110 ++++++++- src/obitypes.c | 2 +- src/obitypes.h | 1 + 6 files changed, 679 insertions(+), 16 deletions(-) diff --git a/src/obidms.c b/src/obidms.c index d694911..78c34a6 100644 --- a/src/obidms.c +++ b/src/obidms.c @@ -51,7 +51,6 @@ static char *build_directory_name(const char *name) { } OBIDMS_p obi_create_dms(const char *name) { - OBIDMS_p dms=NULL; char *dirdbname; // Build and check the directory name @@ -61,7 +60,7 @@ OBIDMS_p obi_create_dms(const char *name) { // Try to create the directory - if (mkdir(dirdbname,) < 0) + if (mkdir(dirdbname,0x777) < 0) { if (errno==EEXIST) obi_set_errno(OBIDMS_EXIST_ERROR); @@ -111,7 +110,7 @@ OBIDMS_p obi_open_dms(const char *name) { } // Allocate the data structure - dms = malloc(sizeof(OBIDMS_t)); + dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t)); if (dms==NULL) { diff --git a/src/obidms.h b/src/obidms.h index 6e80d3c..37ed312 100644 --- a/src/obidms.h +++ b/src/obidms.h @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include @@ -28,7 +30,7 @@ typedef struct OBIDMS { char dirname[OBIDMS_MAX_NAME+1]; /**< The name of the directory * containing the DMS */ - DIR *directory /**< A directory entry usable to + DIR *directory; /**< A directory entry usable to * refer and scan the database directory */ } OBIDMS_t, *OBIDMS_p; @@ -54,6 +56,9 @@ typedef struct OBIDMS { */ #define OBIDMS_ACCESS_ERROR (6) /**< Permission error for accessing to the database */ +#define OBIDMS_BAD_ENDIAN_ERROR (7) /**< The opened data structure does not corresponds + * to the endianess of the platform. + */ /**@}*/ diff --git a/src/obidmscolumn.c b/src/obidmscolumn.c index 5254ac7..8e81d1f 100644 --- a/src/obidmscolumn.c +++ b/src/obidmscolumn.c @@ -10,11 +10,579 @@ */ -#include +#include #include +#include #include /* mmap() is defined in this header */ +/*********************************************************************** + * + * 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 + * + ***********************************************************************/ + + +/** + * @brief Internal function building the file name for a column. + * + * The function build the file name corresponding to a column of an OBIDMS. + * + * @warning The returned pointer has to be freed by the caller. + * + * @param name the name of the column of the OBIDMS + * + * @return a pointer to the filename name + * @retvalue NULL if an error occurs + * + * ###Error values + * - OBIDMS_MEMORY_ERROR : something wrong occurs during memory allocation. + * + * @since May 2015 + * @author Eric Coissac (eric.coissac@metabarcoding.org) + */ +static char *build_column_name(const char *name,obiversion_t version); + +/** + * @brief Internal function building the file name for a version column file. + * + * The version column file indicate the latest version number for a column. + * This function returns the name of the file storing this information + * + * @warning The returned pointer has to be freed by the caller. + * + * @param name the name of the column of the OBIDMS + * + * @return a pointer to the filename name + * @retvalue NULL if an error occurs + * + * ###Error values + * - OBIDMS_MEMORY_ERROR : something wrong occurs during memory allocation. + * + * @since May 2015 + * @author Eric Coissac (eric.coissac@metabarcoding.org) + */ +static char *build_version_name(const char *name); + +/** + * @brief Internal function returning a new column version number + * in the `dms` database + * + * @param dms a pointer as returned by obi_create_dms() or obi_open_dms() + * @param name the name of the column + * @param block is the call is blocking or not + * - `true` the call is blocking + * - `false` the call is not blocking + * + * @return the bigger version number used for this column + * @retvalue -1 if the column does not exist + * + * @since May 2015 + * @author Eric Coissac (eric.coissac@metabarcoding.org) + */ +static obiversion_t obi_new_version(OBIDMS_p dms,char *name, bool block); + +/** + * @brief Internal function creating a new column version file + * in the `dms` database + * + * 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 name the name of the column + * + * @return the next usable version number for this comumn : `0` + * @retvalue -1 if the column does not exist + * + * @since May 2015 + * @author Eric Coissac (eric.coissac@metabarcoding.org) + */ +static int create_version(OBIDMS_p dms,char *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_name(const char *name,obiversion_t version) { + + char *filename; + + // Build the database directory name + if (asprintf(&filename,"%s@%d.odc",name,version) < 0) + { + obi_set_errno(OBIDMS_MEMORY_ERROR); + return NULL; + } + + return filename; +} + + +static char *build_version_name(const char *name) { + + char *filename; + + // Build the database directory name + if (asprintf(&filename,"%s.odv",name) < 0) + { + obi_set_errno(OBIDMS_MEMORY_ERROR); + return NULL; + } + + return filename; +} + +static obiversion_t obi_new_version(OBIDMS_p dms,char *name, bool block) { + + off_t locsize=sizeof(bool) + sizeof(obiversion_t); + obiversion_t newversion=0; + char * versionfile; + int directoryfd; + int versionfd; + bool little; + int lockmode; + + // Select the correct lockf operation according to the blocking mode + if (block) + lockmode=F_LOCK; + else + lockmode=F_TLOCK; + + // build the version file name + versionfile = build_version_name(name); + if (versionfile==NULL) + return -1; + + // Get the file descriptior associated to the database directory + directoryfd = dirfd(dms->directory); + if (directoryfd < 0) { + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + //open the version file + versionfd = openat(directoryfd,versionfile,O_RDWR); + if (versionfd < 0) { + free(versionfile); + + if (errno == ENOENT) + { + return create_version(dms,name); + } + else + { + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + return -1; + } + } + + // Test if the version file size is ok + if (lseek(versionfd,SEEK_END,0) < locsize) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + // prepare the file for locking + if (lseek(versionfd,SEEK_SET,0) != 0) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + // Lock the file + if (lockf(versionfd, lockmode, locsize) < -1) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + // read the endianess of the file + if (read(versionfd, &little, sizeof(bool)) < sizeof(bool)) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + // check if endianess is correct + if (little != obi_is_little_end()) + { + close(versionfd); + obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR); + free(versionfile); + return -1; + } + + // read the current version number + if (read(versionfd, &newversion, sizeof(obiversion_t)) < sizeof(obiversion_t)) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + newversion++; + + // write the new version number + if (lseek(versionfd,SEEK_SET,sizeof(bool)) != sizeof(bool)) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + if (write(versionfd, &newversion, sizeof(obiversion_t)) < sizeof(obiversion_t)) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + // prepare for the unlocking + if (lseek(versionfd,SEEK_SET,0) != 0) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + // unlock the file + if (lockf(versionfd, F_ULOCK, locsize) < -1) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + close(versionfd); + free(versionfile); + return newversion; +} + +static int create_version(OBIDMS_p dms,char *name) { + off_t locsize=sizeof(bool) + sizeof(obiversion_t); + obiversion_t version=0; + char * versionfile; + int directoryfd; + int versionfd; + bool little; + + versionfile = build_version_name(name); + + if (versionfile==NULL) + return -1; + + directoryfd = dirfd(dms->directory); + if (directoryfd < 0) { + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + versionfd = openat(directoryfd, + versionfile, + O_RDWR|O_CREAT|O_EXCL, + 666); + + if (versionfd < 0) { + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + // Lock the file + if (lockf(versionfd, F_LOCK, locsize) < -1) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + if (ftruncate(versionfd,locsize) < 0) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + if (lseek(versionfd,SEEK_SET,0) != 0) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + little = obi_is_little_end(); + + if (write(versionfd, &little, sizeof(bool)) < sizeof(bool)) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + if (write(versionfd, &version, sizeof(obiversion_t)) < sizeof(obiversion_t)) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + // prepare for the unlocking + if (lseek(versionfd,SEEK_SET,0) != 0) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + // unlock the file + if (lockf(versionfd, F_ULOCK, locsize) < -1) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + close(versionfd); + free(versionfile); + return version; +} + +/*********************************************************************** + * + * 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) { + off_t locsize=sizeof(bool) + sizeof(obiversion_t); + obiversion_t version=0; + char * versionfile; + int directoryfd; + int versionfd; + bool little; + + versionfile = build_version_name(name); + if (versionfile==NULL) + return -1; + + directoryfd = dirfd(dms->directory); + if (directoryfd < 0) { + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + versionfd = openat(directoryfd,versionfile,O_RDWR); + if (versionfd < 0) { + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + if (lseek(versionfd,SEEK_END,0) < locsize) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + if (lseek(versionfd,SEEK_SET,0) != 0) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + if (read(versionfd, &little, sizeof(bool)) < sizeof(bool)) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + if (little != obi_is_little_end()) + { + close(versionfd); + obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR); + free(versionfile); + return -1; + } + + if (read(versionfd, &version, sizeof(obiversion_t)) < sizeof(obiversion_t)) + { + close(versionfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(versionfile); + return -1; + } + + + close(versionfd); + free(versionfile); + return version; +} + +size_t obi_get_platform_header_size() +{ + return getpagesize() * 1; +} + + + +OBIDMSColumn_p obi_create_column(OBIDMS_p dms, + char *name, + OBIType_t type, + size_t nbelement) +{ + OBIDMSColumn_p newcolumn=NULL; + OBIDMSColumnHeader_p header; + size_t filesize; + obiversion_t version; + char * columnfile; + int columnfd; + int directoryfd; + size_t hsize; + size_t dsize; + + + directoryfd = dirfd(dms->directory); + if (directoryfd < 0) { + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + return NULL; + } + + hsize = obi_get_platform_header_size(); + dsize = obi_array_sizeof(type,nbelement); + + filesize = hsize + dsize; + + version = obi_new_version(dms,name,true); + + if (version < 0) + { + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + return NULL; + } + + columnfile = build_column_name(name,version); + + columnfd = openat(directoryfd, + columnfile, + O_RDWR|O_CREAT|O_EXCL, + 666); + + if (ftruncate(columnfd,filesize) < 0) + { + close(columnfd); + obi_set_errno(OBIDMS_UNKNOWN_ERROR); + free(columnfile); + return NULL; + } + + newcolumn = (OBIDMSColumn_p) malloc(sizeof(OBIDMSColumn_t)); + + if (newcolumn) + { + close(columnfd); + obi_set_errno(OBIDMS_MEMORY_ERROR); + free(columnfile); + return NULL; + } + + newcolumn->dms = dms; + newcolumn->header = mmap(NULL, + hsize, + PROT_READ | PROT_WRITE, + MAP_SHARED, + columnfd, + 0 + ); + + if (newcolumn->header == MAP_FAILED) + { + close(columnfd); + obi_set_errno(OBIDMS_MEMORY_ERROR); + free(columnfile); + free(newcolumn); + return NULL; + } + + newcolumn->data = mmap(NULL, + dsize, + PROT_READ | PROT_WRITE, + MAP_SHARED, + columnfd, + hsize + ); + + if (newcolumn->data == MAP_FAILED) + { + munmap(newcolumn->header,hsize); + close(columnfd); + obi_set_errno(OBIDMS_MEMORY_ERROR); + free(columnfile); + free(newcolumn); + return NULL; + } + + newcolumn->writable= true; + + header = newcolumn->header; + header->little_endian = obi_is_little_end(); + header->header_size = hsize; + header->line_count = nbelement; + header->line_used = 0; + header->data_type = type; + header->creation_date = time(NULL); + header->version = version; + header->comments[0] = 0x0; + strncpy(header->name,name,OBIDMS_MAX_COLNAME); + + free(columnfile); + return newcolumn; +} + + + +// if( access( fname, F_OK ) != -1 ) { +// // file exists +// } else { +// // file doesn't exist +// } + + +/************************************************************************ + ************************************************************************ + ************************************************************************/ + /** * @brief Reads a memory block from a file. * diff --git a/src/obidmscolumn.h b/src/obidmscolumn.h index af97765..61bd157 100644 --- a/src/obidmscolumn.h +++ b/src/obidmscolumn.h @@ -16,8 +16,12 @@ #include #include #include +#include -#include "obitypes.h" +#include +#include +#include +#include /** * @brief Value separator in OBIDMSColumn files. @@ -36,24 +40,110 @@ static const int HEADER_SIZE = 4096; */ static const int BUFFER_SIZE = 4; +#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 + */ /** * @brief OBIColumnHeader structure. */ typedef struct OBIDMSColumnHeader { - bool endyan_byte_order; /**< endyan byte order */ - int header_size_value; /**< size of the header: a multiple of PAGESIZE */ - bool file_status; /**< file status : 0 is Open and 1 is Closed */ - pid_t PID; /**< PID of the process that created the file and is - the only one allowed to modify it if it is open */ - int line_count; /**< number of lines of data */ + bool little_endian; /**< endianess of the column: + - `true` on little endian platform + - `false` on big endian platform + + @see obi_is_little_end() + */ + int header_size; /**< size of the header in bytes */ + size_t line_count; /**< number of lines of data */ + size_t line_used; /**< number of lines of data used*/ OBIType_t data_type; /**< type of the data */ - char* creation_date; /**< date of creation of the file */ - int version; /**< version of the OBIColumn */ - char* comments; /**< comments */ + time_t creation_date; /**< date of creation of the file */ + obiversion_t version; /**< version of the OBIColumn */ + char name[OBIDMS_MAX_COLNAME]; /**< The column name as a NULL + * terminated string. + */ + char comments[1]; /**< comments stored as a classical + zero end C string. T + The size of the comment is just limited + by the header size + */ } OBIDMSColumnHeader_t, *OBIDMSColumnHeader_p; +/** + * @brief Structure describing a Column of the OBITools DMS + * + * A data structure of this type is returned by the functions + * creating, opening or cloning an OBIDMSColumn. + */ +typedef struct OBIDMSColumn { + OBIDMS_p dms ; /**< A pointer to a DMS instance + */ + OBIDMSColumnHeader_p header; /**< A pointer to the header of the column + */ + void* data; /**< A `void` pointer to the beginning of the + * data. + * + * @attention 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 he closes it. + */ +} OBIDMSColumn_t, *OBIDMSColumn_p; + +/** + * @brief Return the header size in bytes of a column on this platform. + * + * 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 + * + * @return a `size_t` value corresponding to the header size in bytes + * + * @since May 2015 + * @author Eric Coissac (eric.coissac@metabarcoding.org) + */ +size_t obi_get_platform_header_size(); + + + +/** + * @brief Create a column. + * + * @param dms a pointer as returned by obi_create_dms() or obi_open_dms() + * @param name the name of the new column + * @param type the OBIType code used to create the column + * @param nbelement the number of element to be stored + * + * @since May 2015 + * @author Eric Coissac (eric.coissac@metabarcoding.org) + */ + +OBIDMSColumn_p obi_create_column(OBIDMS_p dms, + char* name, + OBIType_t type, + size_t nbelement); + + +/** + * @brief Return the latest column version in the `dms` database + * + * @param dms a pointer as returned by obi_create_dms() or obi_open_dms() + * @param name the name of the column + * + * @return the bigger version number used for this column + * @return -1 if the column does not exist + */ +obiversion_t obi_latest_version(OBIDMS_p dms,char *name); char* obi_map_read_only(int file, int start, int size); diff --git a/src/obitypes.c b/src/obitypes.c index 06c9120..c04ec63 100644 --- a/src/obitypes.c +++ b/src/obitypes.c @@ -8,7 +8,7 @@ #include size_t obi_sizeof(OBIType_t type) { - size_t size=0 + size_t size=0; switch (type) { case OBI_VOID: size = 1; diff --git a/src/obitypes.h b/src/obitypes.h index 0d32191..593f403 100644 --- a/src/obitypes.h +++ b/src/obitypes.h @@ -10,6 +10,7 @@ #include #include +#include #define OBIInt_NA (INT32_MIN) #define OBIFloat_NA (NAN)