2015-05-22 17:54:34 +02:00
|
|
|
/****************************************************************************
|
2015-06-10 15:19:02 +02:00
|
|
|
* OBIDMS_column functions *
|
2015-05-22 17:54:34 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2015-06-10 15:19:02 +02:00
|
|
|
* @file OBIDMS_column.h
|
2015-05-22 17:54:34 +02:00
|
|
|
* @author Celine Mercier
|
|
|
|
* @date 22 May 2015
|
|
|
|
* @brief Functions for the shared elements of all the OBIColumn structures.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2015-06-17 16:51:16 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2015-05-26 10:38:56 +02:00
|
|
|
#include <fcntl.h>
|
2015-05-22 17:54:34 +02:00
|
|
|
#include <sys/mman.h> /* mmap() is defined in this header */
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
#include "obidmscolumn.h"
|
|
|
|
#include "obidms.h"
|
|
|
|
#include "obitypes.h"
|
|
|
|
#include "obierrno.h"
|
|
|
|
#include "obilittlebigman.h"
|
|
|
|
#include "private_openat.h"
|
2015-05-22 17:54:34 +02:00
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
/*************************************************************************
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
2015-06-10 15:19:02 +02:00
|
|
|
* 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
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
2015-06-10 15:19:02 +02:00
|
|
|
*************************************************************************/
|
2015-05-26 10:38:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Internal function building the file name for a column.
|
|
|
|
*
|
2015-06-10 15:19:02 +02:00
|
|
|
* The function builds the file name corresponding to a column of an OBIDMS.
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
|
|
|
* @warning The returned pointer has to be freed by the caller.
|
|
|
|
*
|
2015-06-17 16:51:16 +02:00
|
|
|
* @param column_name the name of the OBIDMS column.
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
2015-06-17 16:51:16 +02:00
|
|
|
* @return a pointer to the column file name
|
2015-05-26 10:38:56 +02:00
|
|
|
* @retvalue NULL if an error occurs
|
|
|
|
*
|
|
|
|
* ###Error values
|
2015-06-10 15:19:02 +02:00
|
|
|
* - OBIDMS_MEMORY_ERROR : something wrong occured during memory allocation.
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
|
|
|
* @since May 2015
|
|
|
|
* @author Eric Coissac (eric.coissac@metabarcoding.org)
|
|
|
|
*/
|
2015-06-17 16:51:16 +02:00
|
|
|
static char *build_column_file_name(const char *column_name, obiversion_t version_number);
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-05-26 10:38:56 +02:00
|
|
|
|
|
|
|
/**
|
2015-06-17 16:51:16 +02:00
|
|
|
* @brief Internal function building the file name for a column version file.
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
2015-06-17 16:51:16 +02:00
|
|
|
* The column version file indicates the latest version number for a column.
|
|
|
|
* This function returns the name of the file storing this information.
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
|
|
|
* @warning The returned pointer has to be freed by the caller.
|
|
|
|
*
|
2015-06-17 16:51:16 +02:00
|
|
|
* @param column_name the name of the OBIDMS column.
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
2015-06-17 16:51:16 +02:00
|
|
|
* @return a pointer to the version file name
|
2015-05-26 10:38:56 +02:00
|
|
|
* @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)
|
|
|
|
*/
|
2015-06-17 16:51:16 +02:00
|
|
|
static char *build_version_file_name(const char *column_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-05-26 10:38:56 +02:00
|
|
|
/**
|
|
|
|
* @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()
|
2015-06-17 16:51:16 +02:00
|
|
|
* @param column_name the name of the column
|
2015-05-26 10:38:56 +02:00
|
|
|
* @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)
|
|
|
|
*/
|
2015-06-17 16:51:16 +02:00
|
|
|
static obiversion_t obi_get_new_version_number(OBIDMS_p dms, char *column_name, bool block);
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-05-26 10:38:56 +02:00
|
|
|
/**
|
|
|
|
* @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()
|
2015-06-17 16:51:16 +02:00
|
|
|
* @param column_name the name of the column
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
2015-06-17 16:51:16 +02:00
|
|
|
* @return the next usable version number for this column : `0`
|
2015-05-26 10:38:56 +02:00
|
|
|
* @retvalue -1 if the column does not exist
|
|
|
|
*
|
|
|
|
* @since May 2015
|
|
|
|
* @author Eric Coissac (eric.coissac@metabarcoding.org)
|
|
|
|
*/
|
2015-06-17 16:51:16 +02:00
|
|
|
static int create_version_file(OBIDMS_p dms, char *column_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
|
|
|
/************************************************************************
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
2015-06-10 15:19:02 +02:00
|
|
|
* 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
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
2015-06-10 15:19:02 +02:00
|
|
|
************************************************************************/
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
static char *build_column_file_name(const char *column_name, obiversion_t version_number)
|
|
|
|
{
|
2015-05-26 10:38:56 +02:00
|
|
|
char *filename;
|
|
|
|
|
|
|
|
// Build the database directory name
|
2015-06-17 16:51:16 +02:00
|
|
|
if (asprintf(&filename,"%s@%d.odc", column_name, version_number) < 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
static char *build_version_file_name(const char *column_name)
|
|
|
|
{
|
2015-05-26 10:38:56 +02:00
|
|
|
char *filename;
|
|
|
|
|
|
|
|
// Build the database directory name
|
2015-06-17 16:51:16 +02:00
|
|
|
if (asprintf(&filename,"%s.odv", column_name) < 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
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;
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
new_version_number = 0;
|
|
|
|
loc_size = sizeof(bool) + sizeof(obiversion_t);
|
2015-05-26 10:38:56 +02:00
|
|
|
|
|
|
|
// Select the correct lockf operation according to the blocking mode
|
|
|
|
if (block)
|
2015-06-17 16:51:16 +02:00
|
|
|
lock_mode=F_LOCK;
|
2015-05-26 10:38:56 +02:00
|
|
|
else
|
2015-06-17 16:51:16 +02:00
|
|
|
lock_mode=F_TLOCK;
|
2015-05-26 10:38:56 +02:00
|
|
|
|
|
|
|
// build the version file name
|
2015-06-17 16:51:16 +02:00
|
|
|
version_file_name = build_version_file_name(column_name);
|
|
|
|
if (version_file_name == NULL)
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
// Get the file descriptor associated to the database directory
|
|
|
|
directory_file_descriptor = dirfd(dms->directory);
|
|
|
|
if (directory_file_descriptor < 0)
|
|
|
|
{
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
//open the version file
|
2015-06-17 16:51:16 +02:00
|
|
|
version_file_descriptor = private_openat(directory_file_descriptor, version_file_name, O_RDWR);
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (version_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
|
|
|
|
if (errno == ENOENT)
|
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
return create_version_file(dms, column_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test if the version file size is ok
|
2015-06-17 16:51:16 +02:00
|
|
|
if (lseek(version_file_descriptor, 0, SEEK_END) < loc_size)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare the file for locking
|
2015-06-17 16:51:16 +02:00
|
|
|
if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock the file
|
2015-06-17 16:51:16 +02:00
|
|
|
if (lockf(version_file_descriptor, lock_mode, loc_size) < -1)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the endianess of the file
|
2015-06-17 16:51:16 +02:00
|
|
|
if (read(version_file_descriptor, &little_endian, sizeof(bool)) < sizeof(bool))
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if endianess is correct
|
2015-06-17 16:51:16 +02:00
|
|
|
if (little_endian != obi_is_little_endian())
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read the current version number
|
2015-06-17 16:51:16 +02:00
|
|
|
if (read(version_file_descriptor, &new_version_number, sizeof(obiversion_t)) < sizeof(obiversion_t))
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
new_version_number++;
|
2015-05-26 10:38:56 +02:00
|
|
|
|
|
|
|
// write the new version number
|
2015-06-17 16:51:16 +02:00
|
|
|
if (lseek(version_file_descriptor, sizeof(bool), SEEK_SET) != sizeof(bool))
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (write(version_file_descriptor, &new_version_number, sizeof(obiversion_t)) < sizeof(obiversion_t))
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare for the unlocking
|
2015-06-17 16:51:16 +02:00
|
|
|
if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlock the file
|
2015-06-17 16:51:16 +02:00
|
|
|
if (lockf(version_file_descriptor, F_ULOCK, loc_size) < -1)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
free(version_file_name);
|
|
|
|
return new_version_number;
|
2015-05-26 10:38:56 +02:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
static int create_version_file(OBIDMS_p dms, char *column_name)
|
|
|
|
{
|
|
|
|
off_t loc_size;
|
|
|
|
obiversion_t version_number;
|
|
|
|
char* version_file_name;
|
|
|
|
int directory_file_descriptor;
|
|
|
|
int version_file_descriptor;
|
|
|
|
bool little_endian;
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
loc_size = sizeof(bool) + sizeof(obiversion_t);
|
|
|
|
version_number = 0;
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
version_file_name = build_version_file_name(column_name);
|
|
|
|
|
|
|
|
if (version_file_name == NULL)
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
directory_file_descriptor = dirfd(dms->directory);
|
|
|
|
if (directory_file_descriptor < 0)
|
|
|
|
{
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
version_file_descriptor = private_openat(directory_file_descriptor,
|
|
|
|
version_file_name,
|
|
|
|
O_RDWR | O_CREAT);
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (version_file_descriptor < 0)
|
|
|
|
{
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock the file
|
2015-06-17 16:51:16 +02:00
|
|
|
if (lockf(version_file_descriptor, F_LOCK, loc_size) < -1)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (ftruncate(version_file_descriptor, loc_size) < 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
little_endian = obi_is_little_endian();
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (write(version_file_descriptor, &little_endian, sizeof(bool)) < sizeof(bool))
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (write(version_file_descriptor, &version_number, sizeof(obiversion_t)) < sizeof(obiversion_t))
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepare for the unlocking
|
2015-06-17 16:51:16 +02:00
|
|
|
if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// unlock the file
|
2015-06-17 16:51:16 +02:00
|
|
|
if (lockf(version_file_descriptor, F_ULOCK, loc_size) < -1)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
free(version_file_name);
|
|
|
|
return version_number;
|
2015-05-26 10:38:56 +02:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
/**********************************************************************
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
2015-06-17 16:51:16 +02:00
|
|
|
* 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
|
2015-05-26 10:38:56 +02:00
|
|
|
*
|
2015-06-17 16:51:16 +02:00
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
obiversion_t obi_get_latest_version_number(OBIDMS_p dms, char *column_name)
|
|
|
|
{
|
|
|
|
off_t loc_size;
|
|
|
|
obiversion_t latest_version_number;
|
|
|
|
char * version_file_name;
|
|
|
|
int directory_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);
|
|
|
|
if (version_file_name==NULL)
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
directory_file_descriptor = dirfd(dms->directory);
|
|
|
|
if (directory_file_descriptor < 0) {
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
version_file_descriptor = private_openat(directory_file_descriptor, version_file_name, O_RDONLY);
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (version_file_descriptor < 0) {
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (lseek(version_file_descriptor, 0, SEEK_END) < loc_size)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (lseek(version_file_descriptor, 0, SEEK_SET) != 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (read(version_file_descriptor, &little_endian, sizeof(bool)) < sizeof(bool))
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (little_endian != obi_is_little_endian())
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (read(version_file_descriptor, &latest_version_number, sizeof(obiversion_t)) < sizeof(obiversion_t))
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
free(version_file_name);
|
|
|
|
return latest_version_number;
|
2015-05-26 10:38:56 +02:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-05-26 10:38:56 +02:00
|
|
|
size_t obi_get_platform_header_size()
|
|
|
|
{
|
|
|
|
return getpagesize() * 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
OBIDMS_column_p obi_create_column(OBIDMS_p dms,
|
2015-06-17 16:51:16 +02:00
|
|
|
char *column_name,
|
2015-05-26 10:38:56 +02:00
|
|
|
OBIType_t type,
|
2015-06-17 16:51:16 +02:00
|
|
|
size_t nb_elements)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
OBIDMS_column_p new_column;
|
|
|
|
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;
|
|
|
|
size_t header_size;
|
|
|
|
size_t data_size;
|
|
|
|
|
|
|
|
new_column = NULL;
|
|
|
|
|
|
|
|
directory_file_descriptor = dirfd(dms->directory);
|
|
|
|
if (directory_file_descriptor < 0) {
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
header_size = obi_get_platform_header_size();
|
|
|
|
data_size = obi_array_sizeof(type, nb_elements);
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
file_size = header_size + data_size;
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
version_number = obi_get_new_version_number(dms, column_name, true);
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (version_number < 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
column_file_name = build_column_file_name(column_name, version_number);
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
column_file_descriptor = private_openat(directory_file_descriptor,
|
|
|
|
column_file_name,
|
|
|
|
O_RDWR | O_CREAT);
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (ftruncate(column_file_descriptor, file_size) < 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(column_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(column_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
new_column = (OBIDMS_column_p) malloc(sizeof(OBIDMS_column_t));
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (new_column)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(column_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(column_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
new_column->dms = dms;
|
|
|
|
new_column->header = mmap(NULL,
|
|
|
|
header_size,
|
2015-05-26 10:38:56 +02:00
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED,
|
2015-06-17 16:51:16 +02:00
|
|
|
column_file_descriptor,
|
2015-05-26 10:38:56 +02:00
|
|
|
0
|
|
|
|
);
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (new_column->header == MAP_FAILED)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
close(column_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(column_file_name);
|
|
|
|
free(new_column);
|
2015-05-26 10:38:56 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
new_column->data = mmap(NULL,
|
|
|
|
data_size,
|
2015-05-26 10:38:56 +02:00
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED,
|
2015-06-17 16:51:16 +02:00
|
|
|
column_file_descriptor,
|
|
|
|
header_size
|
2015-05-26 10:38:56 +02:00
|
|
|
);
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (new_column->data == MAP_FAILED)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
munmap(new_column->header, header_size);
|
|
|
|
close(column_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(column_file_name);
|
|
|
|
free(new_column);
|
2015-05-26 10:38:56 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
new_column->writable= true;
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
header = new_column->header;
|
2015-06-10 15:19:02 +02:00
|
|
|
header->little_endian = obi_is_little_endian();
|
2015-06-17 16:51:16 +02:00
|
|
|
header->header_size = header_size;
|
|
|
|
header->line_count = nb_elements;
|
|
|
|
header->lines_used = 0;
|
2015-05-26 10:38:56 +02:00
|
|
|
header->data_type = type;
|
|
|
|
header->creation_date = time(NULL);
|
2015-06-17 16:51:16 +02:00
|
|
|
header->version = version_number;
|
2015-05-26 10:38:56 +02:00
|
|
|
header->comments[0] = 0x0;
|
2015-06-17 16:51:16 +02:00
|
|
|
strncpy(header->name, column_name, OBIDMS_MAX_COLNAME);
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
free(column_file_name);
|
|
|
|
return new_column;
|
2015-05-26 10:38:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// if( access( fname, F_OK ) != -1 ) {
|
|
|
|
// // file exists
|
|
|
|
// } else {
|
|
|
|
// // file doesn't exist
|
|
|
|
// }
|
|
|
|
|
|
|
|
|