2015-05-22 17:54:34 +02:00
|
|
|
/****************************************************************************
|
2015-06-23 18:35:34 +02:00
|
|
|
* OBIDMS_column functions *
|
2015-05-22 17:54:34 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2015-07-31 18:03:48 +02:00
|
|
|
* @file obidmscolumn.c
|
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>
|
2015-07-31 18:03:48 +02:00
|
|
|
#include <string.h>
|
2015-06-17 16:51:16 +02:00
|
|
|
#include <sys/types.h>
|
2015-06-23 18:35:34 +02:00
|
|
|
#include <dirent.h>
|
2015-06-17 16:51:16 +02:00
|
|
|
#include <unistd.h>
|
2015-05-26 10:38:56 +02:00
|
|
|
#include <fcntl.h>
|
2015-06-23 18:35:34 +02:00
|
|
|
#include <stdbool.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"
|
2015-06-26 17:53:03 +02:00
|
|
|
#include "obidmscolumndir.h"
|
2015-06-17 16:51:16 +02:00
|
|
|
#include "obidms.h"
|
|
|
|
#include "obitypes.h"
|
|
|
|
#include "obierrno.h"
|
2015-06-26 17:53:03 +02:00
|
|
|
#include "obidebug.h"
|
2015-06-17 16:51:16 +02:00
|
|
|
#include "obilittlebigman.h"
|
2015-07-20 11:38:43 +02:00
|
|
|
|
2015-07-31 18:03:48 +02:00
|
|
|
|
|
|
|
#define DEBUG_LEVEL 0
|
|
|
|
|
2015-05-22 17:54:34 +02:00
|
|
|
|
2015-06-23 18:35:34 +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-23 18:35:34 +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-23 18:35:34 +02:00
|
|
|
* - OBIDMS_MEMORY_ERROR : something wrong occurred 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-26 17:53:03 +02:00
|
|
|
static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_directory, 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-26 17:53:03 +02:00
|
|
|
static int create_version_file(OBIDMS_column_directory_p column_directory);
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-07-31 18:03:48 +02:00
|
|
|
/**
|
|
|
|
* @brief Internal function setting the elements names of the lines of a
|
|
|
|
* column in the header of the OBIDMS column structure.
|
|
|
|
*
|
|
|
|
* @param column a pointer as returned by obi_create_column()
|
|
|
|
* @param elements_names the names of the elements with ';' as separator
|
|
|
|
*
|
|
|
|
* @return 0 if the operation was successfully completed
|
|
|
|
* @retvalue -1 if an error occurred
|
|
|
|
*
|
|
|
|
* @since July 2015
|
|
|
|
* @author Celine Mercier (celine.mercier@metabarcoding.org)
|
|
|
|
*/
|
|
|
|
int obi_column_set_elements_names(OBIDMS_column_p column, const char* elements_names);
|
|
|
|
|
|
|
|
|
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-26 17:53:03 +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-07-20 11:12:48 +02:00
|
|
|
if (asprintf(&filename,"%s@%d.odc", column_name, version_number) < 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_MEMORY_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError building a column file name");
|
2015-05-26 10:38:56 +02:00
|
|
|
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
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_MEMORY_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError building a version file name");
|
2015-05-26 10:38:56 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_directory, bool block)
|
2015-06-17 16:51:16 +02:00
|
|
|
{
|
|
|
|
off_t loc_size;
|
|
|
|
obiversion_t new_version_number;
|
|
|
|
char* version_file_name;
|
2015-06-23 18:35:34 +02:00
|
|
|
int column_dir_file_descriptor;
|
2015-06-17 16:51:16 +02:00
|
|
|
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
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Build the version file name
|
2015-06-26 17:53:03 +02:00
|
|
|
version_file_name = build_version_file_name(column_directory->column_name);
|
2015-06-17 16:51:16 +02:00
|
|
|
if (version_file_name == NULL)
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
// Get the file descriptor associated to the column directory
|
|
|
|
column_dir_file_descriptor = dirfd(column_directory->directory);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (column_dir_file_descriptor < 0)
|
2015-06-17 16:51:16 +02:00
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError getting the file descriptor for a column file directory");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Open the version file
|
2015-07-20 11:38:43 +02:00
|
|
|
version_file_descriptor = openat(column_dir_file_descriptor, version_file_name, O_RDWR);
|
2015-06-17 16:51:16 +02:00
|
|
|
if (version_file_descriptor < 0)
|
|
|
|
{
|
2015-05-26 10:38:56 +02:00
|
|
|
if (errno == ENOENT)
|
2015-06-26 17:53:03 +02:00
|
|
|
return create_version_file(column_directory);
|
2015-05-26 10:38:56 +02:00
|
|
|
else
|
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError opening a version file");
|
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError testing if a version file size is ok");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// 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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError preparing a version file for locking");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock the file
|
2015-06-23 18:35:34 +02:00
|
|
|
if (lockf(version_file_descriptor, lock_mode, loc_size) < 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError locking a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Read the endianness 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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError reading the endianness of a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Check if endianness 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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_BAD_ENDIAN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nEndianness of a version file is incorrect");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// 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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError reading a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
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
|
|
|
|
2015-06-23 18:35:34 +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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError writing a new version number in a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError writing a new version number in a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Prepare for 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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError preparing the unlocking of a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Unlock the file
|
|
|
|
if (lockf(version_file_descriptor, F_ULOCK, loc_size) < 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError unlocking a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(version_file_descriptor);
|
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-24 13:53:12 +02:00
|
|
|
close(version_file_descriptor);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
return new_version_number;
|
2015-05-26 10:38:56 +02:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
static int create_version_file(OBIDMS_column_directory_p column_directory)
|
2015-06-17 16:51:16 +02:00
|
|
|
{
|
|
|
|
off_t loc_size;
|
|
|
|
obiversion_t version_number;
|
|
|
|
char* version_file_name;
|
2015-06-23 18:35:34 +02:00
|
|
|
int column_dir_file_descriptor;
|
2015-06-17 16:51:16 +02:00
|
|
|
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-26 17:53:03 +02:00
|
|
|
version_file_name = build_version_file_name(column_directory->column_name);
|
2015-06-17 16:51:16 +02:00
|
|
|
if (version_file_name == NULL)
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
// Get the file descriptor associated to the column directory
|
|
|
|
column_dir_file_descriptor = dirfd(column_directory->directory);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (column_dir_file_descriptor < 0)
|
2015-06-17 16:51:16 +02:00
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError getting the file descriptor for a column directory");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Get the file descriptor associated to the version file
|
2015-07-20 11:38:43 +02:00
|
|
|
version_file_descriptor = openat(column_dir_file_descriptor, version_file_name, O_RDWR | O_CREAT);
|
2015-06-17 16:51:16 +02:00
|
|
|
if (version_file_descriptor < 0)
|
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError opening a version file");
|
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-23 18:35:34 +02:00
|
|
|
if (lockf(version_file_descriptor, F_LOCK, loc_size) < 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError locking a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Truncate the version file to the right size
|
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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError truncating a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Position offset to 0 to prepare for writing
|
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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError changing offset of a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
close(version_file_descriptor);
|
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-23 18:35:34 +02:00
|
|
|
// Write endianness
|
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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError writing endianness of a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Write version number
|
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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError writing version number in a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Prepare for 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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError preparing a version file for unlocking");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Unlock the file
|
|
|
|
if (lockf(version_file_descriptor, F_ULOCK, loc_size) < 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError unlocking a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-24 13:53:12 +02:00
|
|
|
close(version_file_descriptor);
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
return version_number;
|
2015-05-26 10:38:56 +02:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-07-31 18:03:48 +02:00
|
|
|
int obi_column_set_elements_names(OBIDMS_column_p column, const char* elements_names)
|
|
|
|
{
|
|
|
|
strcpy((column->header)->elements_names, elements_names);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
**********************************************************************/
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
obiversion_t obi_get_latest_version_number(OBIDMS_column_directory_p column_directory)
|
2015-06-17 16:51:16 +02:00
|
|
|
{
|
|
|
|
off_t loc_size;
|
|
|
|
obiversion_t latest_version_number;
|
|
|
|
char * version_file_name;
|
2015-06-23 18:35:34 +02:00
|
|
|
int column_dir_file_descriptor;
|
2015-06-17 16:51:16 +02:00
|
|
|
int version_file_descriptor;
|
|
|
|
bool little_endian;
|
|
|
|
|
|
|
|
loc_size = sizeof(bool) + sizeof(obiversion_t);
|
|
|
|
latest_version_number = 0;
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
version_file_name = build_version_file_name(column_directory->column_name);
|
2015-06-17 16:51:16 +02:00
|
|
|
if (version_file_name==NULL)
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
// Get the file descriptor associated to the column directory
|
|
|
|
column_dir_file_descriptor = dirfd(column_directory->directory);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (column_dir_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError getting the file descriptor for a column directory");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Get the file descriptor associated to the version file
|
2015-07-20 11:38:43 +02:00
|
|
|
version_file_descriptor = openat(column_dir_file_descriptor, version_file_name, O_RDONLY);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (version_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError opening a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Check that 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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError testing if a version file size is ok");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Set the offset to 0 in the version file
|
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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError setting the offset of a version file to 0");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Read the endianness
|
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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError reading the endianness of a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Verify the endianness
|
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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_BAD_ENDIAN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nEndianness of a version file is incorrect");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Read the latest version number
|
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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError reading the latest version number in a version file");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
close(version_file_descriptor);
|
2015-05-26 10:38:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
free(version_file_name);
|
2015-06-24 13:53:12 +02:00
|
|
|
close(version_file_descriptor);
|
2015-06-23 18:35:34 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
return latest_version_number;
|
2015-05-26 10:38:56 +02:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-07-31 18:03:48 +02:00
|
|
|
obiversion_t obi_column_get_latest_version_from_name(OBIDMS_p dms, const char* column_name)
|
|
|
|
{
|
|
|
|
OBIDMS_column_directory_p column_directory;
|
|
|
|
obiversion_t latest_version;
|
|
|
|
|
|
|
|
// Get the column directory structure associated to the column
|
|
|
|
column_directory = obi_open_column_directory(dms, column_name);
|
|
|
|
if (column_directory == NULL)
|
|
|
|
{
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nProblem opening a column directory structure");
|
2015-07-31 18:03:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the latest version number
|
|
|
|
latest_version = obi_get_latest_version_number(column_directory);
|
|
|
|
if (latest_version < 0)
|
|
|
|
{
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nProblem getting the latest version number in a column directory");
|
2015-07-31 18:03:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return latest_version;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-23 18:35:34 +02:00
|
|
|
const char *column_name,
|
|
|
|
OBIType_t type,
|
2015-08-26 10:52:19 +02:00
|
|
|
size_t nb_lines,
|
2015-07-20 16:08:50 +02:00
|
|
|
size_t nb_elements_per_line,
|
|
|
|
const char* elements_names)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
OBIDMS_column_p new_column;
|
2015-06-26 17:53:03 +02:00
|
|
|
OBIDMS_column_directory_p column_directory;
|
2015-06-17 16:51:16 +02:00
|
|
|
OBIDMS_column_header_p header;
|
|
|
|
size_t file_size;
|
|
|
|
obiversion_t version_number;
|
|
|
|
char* column_file_name;
|
|
|
|
int column_file_descriptor;
|
2015-06-23 18:35:34 +02:00
|
|
|
int column_dir_file_descriptor;
|
2015-06-17 16:51:16 +02:00
|
|
|
size_t header_size;
|
|
|
|
size_t data_size;
|
|
|
|
|
|
|
|
new_column = NULL;
|
|
|
|
|
2015-08-03 15:10:39 +02:00
|
|
|
// TODO check that informations are not NULL/invalid?
|
2015-07-31 18:03:48 +02:00
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
// Get the column directory structure associated to the column
|
|
|
|
column_directory = obi_column_directory(dms, column_name);
|
|
|
|
if (column_directory == NULL)
|
2015-08-03 15:10:39 +02:00
|
|
|
{
|
|
|
|
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError opening a column directory structure");
|
2015-06-23 18:35:34 +02:00
|
|
|
return NULL;
|
2015-08-03 15:10:39 +02:00
|
|
|
}
|
2015-06-23 18:35:34 +02:00
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
// Get the file descriptor associated to the column directory
|
|
|
|
column_dir_file_descriptor = dirfd(column_directory->directory);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (column_dir_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError getting the file descriptor for a column directory");
|
2015-05-26 10:38:56 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Calculate the size needed
|
2015-06-17 16:51:16 +02:00
|
|
|
header_size = obi_get_platform_header_size();
|
2015-08-26 10:52:19 +02:00
|
|
|
data_size = obi_array_sizeof(type, nb_lines, nb_elements_per_line);
|
2015-06-17 16:51:16 +02:00
|
|
|
file_size = header_size + data_size;
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Get the latest version number
|
2015-06-26 17:53:03 +02:00
|
|
|
version_number = obi_get_new_version_number(column_directory, true);
|
2015-06-17 16:51:16 +02:00
|
|
|
if (version_number < 0)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Get the column file name
|
2015-06-17 16:51:16 +02:00
|
|
|
column_file_name = build_column_file_name(column_name, version_number);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (column_file_name == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Open the column file
|
2015-07-20 11:38:43 +02:00
|
|
|
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR | O_CREAT);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (column_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
free(column_file_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Truncate the column file to the right size
|
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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError truncating a column file to the right size");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(column_file_descriptor);
|
|
|
|
free(column_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Allocate the memory for the column structure
|
2015-06-17 16:51:16 +02:00
|
|
|
new_column = (OBIDMS_column_p) malloc(sizeof(OBIDMS_column_t));
|
2015-06-23 18:35:34 +02:00
|
|
|
if (new_column == NULL)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError allocating the memory for the column structure");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(column_file_descriptor);
|
|
|
|
free(column_file_name);
|
2015-05-26 10:38:56 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Fill the column structure
|
|
|
|
new_column->dms = dms;
|
2015-07-20 16:08:50 +02:00
|
|
|
new_column->column_directory = column_directory;
|
2015-06-23 18:35:34 +02:00
|
|
|
new_column->header = mmap(NULL,
|
|
|
|
header_size,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED,
|
|
|
|
column_file_descriptor,
|
|
|
|
0
|
|
|
|
);
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
if (new_column->header == MAP_FAILED)
|
2015-05-26 10:38:56 +02:00
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError mmapping the header of a column");
|
2015-06-17 16:51:16 +02:00
|
|
|
close(column_file_descriptor);
|
|
|
|
free(column_file_name);
|
|
|
|
free(new_column);
|
2015-05-26 10:38:56 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
new_column->data = mmap(NULL,
|
|
|
|
data_size,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED,
|
|
|
|
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-23 18:35:34 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError mmapping the data of a column");
|
|
|
|
munmap(new_column->header, header_size);
|
2015-06-17 16:51:16 +02:00
|
|
|
close(column_file_descriptor);
|
|
|
|
free(column_file_name);
|
|
|
|
free(new_column);
|
2015-05-26 10:38:56 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
new_column->writable = true;
|
2015-05-26 10:38:56 +02:00
|
|
|
|
2015-07-20 16:08:50 +02:00
|
|
|
header = new_column->header;
|
|
|
|
header->little_endian = obi_is_little_endian();
|
|
|
|
header->header_size = header_size;
|
2015-08-26 10:52:19 +02:00
|
|
|
header->line_count = nb_lines;
|
2015-07-20 16:08:50 +02:00
|
|
|
header->lines_used = 0;
|
|
|
|
header->nb_elements_per_line = nb_elements_per_line;
|
|
|
|
header->data_type = type;
|
|
|
|
header->creation_date = time(NULL);
|
|
|
|
header->version = version_number;
|
2015-08-26 10:29:07 +02:00
|
|
|
header->cloned_from = -1;
|
2015-07-20 16:08:50 +02:00
|
|
|
header->comments[0] = 0x0;
|
|
|
|
|
2015-07-31 18:03:48 +02:00
|
|
|
obi_column_set_elements_names(new_column, elements_names);
|
2015-07-20 16:08:50 +02:00
|
|
|
|
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-08-26 12:00:38 +02:00
|
|
|
// Fill the data with NA values
|
2015-09-01 17:38:08 +02:00
|
|
|
obi_ini_to_NA_values (new_column, 0, nb_lines);
|
2015-08-26 12:00:38 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
free(column_file_name);
|
2015-06-24 13:53:12 +02:00
|
|
|
close(column_file_descriptor);
|
2015-06-23 18:35:34 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
return new_column;
|
2015-05-26 10:38:56 +02:00
|
|
|
}
|
2015-07-31 18:03:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
OBIDMS_column_p obi_open_column(OBIDMS_p dms, const char* column_name, obiversion_t version_number)
|
|
|
|
{
|
|
|
|
OBIDMS_column_p column;
|
|
|
|
OBIDMS_column_directory_p column_directory;
|
|
|
|
char* column_file_name;
|
|
|
|
int column_file_descriptor;
|
|
|
|
int column_dir_file_descriptor;
|
|
|
|
size_t header_size;
|
|
|
|
size_t data_size;
|
|
|
|
|
|
|
|
column = NULL;
|
|
|
|
|
|
|
|
// Get the column directory structure associated to the column
|
|
|
|
column_directory = obi_open_column_directory(dms, column_name);
|
|
|
|
if (column_directory == NULL)
|
|
|
|
{
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError opening a column directory structure");
|
2015-07-31 18:03:48 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the file descriptor associated to the column directory
|
|
|
|
column_dir_file_descriptor = dirfd(column_directory->directory);
|
|
|
|
if (column_dir_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError getting the file descriptor for a column directory");
|
2015-07-31 18:03:48 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the header size
|
|
|
|
header_size = obi_get_platform_header_size();
|
|
|
|
|
|
|
|
// Get the latest version number if it has the value -1 (not given by user)
|
|
|
|
if (version_number == -1)
|
|
|
|
{
|
|
|
|
version_number = obi_get_latest_version_number(column_directory);
|
|
|
|
if (version_number < 0)
|
|
|
|
{
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError getting the latest version number in a column directory");
|
2015-07-31 18:03:48 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the column file name
|
|
|
|
column_file_name = build_column_file_name(column_name, version_number);
|
|
|
|
if (column_file_name == NULL)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the column file, ALWAYS READ-ONLY
|
|
|
|
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDONLY);
|
|
|
|
if (column_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError opening column file");
|
2015-07-31 18:03:48 +02:00
|
|
|
free(column_file_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the memory for the column structure
|
|
|
|
column = (OBIDMS_column_p) malloc(sizeof(OBIDMS_column_t));
|
|
|
|
if (column == NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError allocating the memory for a column structure");
|
2015-07-31 18:03:48 +02:00
|
|
|
close(column_file_descriptor);
|
|
|
|
free(column_file_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill the column structure
|
|
|
|
column->dms = dms;
|
|
|
|
column->column_directory = column_directory;
|
|
|
|
column->header = mmap(NULL,
|
|
|
|
header_size,
|
|
|
|
PROT_READ,
|
2015-08-26 10:29:07 +02:00
|
|
|
MAP_PRIVATE,
|
2015-07-31 18:03:48 +02:00
|
|
|
column_file_descriptor,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
if (column->header == MAP_FAILED)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError mmapping the header of a column");
|
2015-07-31 18:03:48 +02:00
|
|
|
close(column_file_descriptor);
|
|
|
|
free(column_file_name);
|
|
|
|
free(column);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check endianness?
|
|
|
|
|
|
|
|
// Compute data size from the informations in the header
|
2015-09-02 13:06:21 +02:00
|
|
|
data_size = ((column->header)->line_count) * sizeof((column->header)->data_type);
|
2015-07-31 18:03:48 +02:00
|
|
|
|
|
|
|
column->data = mmap(NULL,
|
|
|
|
data_size,
|
|
|
|
PROT_READ,
|
2015-08-26 10:29:07 +02:00
|
|
|
MAP_PRIVATE,
|
2015-07-31 18:03:48 +02:00
|
|
|
column_file_descriptor,
|
|
|
|
header_size
|
|
|
|
);
|
|
|
|
|
|
|
|
if (column->data == MAP_FAILED)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError mmapping the data of a column");
|
|
|
|
munmap(column->header, header_size);
|
2015-07-31 18:03:48 +02:00
|
|
|
close(column_file_descriptor);
|
|
|
|
free(column_file_name);
|
|
|
|
free(column);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
column->writable = false;
|
|
|
|
|
|
|
|
free(column_file_name);
|
|
|
|
close(column_file_descriptor);
|
|
|
|
|
|
|
|
return column;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-26 10:29:07 +02:00
|
|
|
OBIDMS_column_p obi_clone_column(OBIDMS_p dms, const char* column_name, obiversion_t version_number, bool clone_data)
|
|
|
|
{
|
|
|
|
OBIDMS_column_p column_to_clone;
|
|
|
|
OBIDMS_column_p new_column;
|
|
|
|
size_t nb_lines;
|
|
|
|
size_t nb_elements_per_line;
|
|
|
|
OBIType_t data_type;
|
|
|
|
|
|
|
|
column_to_clone = obi_open_column(dms, column_name, version_number);
|
|
|
|
if (column_to_clone == NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError opening the column to clone");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (clone_data)
|
|
|
|
nb_lines = (column_to_clone->header)->line_count;
|
|
|
|
else
|
|
|
|
nb_lines = INITIAL_LINE_COUNT;
|
|
|
|
|
|
|
|
nb_elements_per_line = (column_to_clone->header)->nb_elements_per_line;
|
|
|
|
data_type = (column_to_clone->header)->data_type;
|
|
|
|
|
|
|
|
new_column = obi_create_column(dms,
|
|
|
|
column_name,
|
|
|
|
data_type,
|
|
|
|
nb_lines,
|
|
|
|
nb_elements_per_line,
|
|
|
|
(column_to_clone->header)->elements_names);
|
|
|
|
|
|
|
|
if (new_column == NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError creating the new column when cloning a column");
|
2015-09-02 13:06:21 +02:00
|
|
|
// The new file is deleted
|
|
|
|
const char* column_file_name = build_column_file_name(column_name, version_number);
|
|
|
|
if (remove(column_file_name) < 0)
|
|
|
|
obidebug(1, "\nError deleting a bad cloned file");
|
2015-08-26 10:29:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
(new_column->header)->cloned_from = version_number;
|
|
|
|
// TODO copy header->comments?
|
|
|
|
|
|
|
|
if (clone_data)
|
2015-08-26 10:38:07 +02:00
|
|
|
{
|
2015-08-26 10:29:07 +02:00
|
|
|
memcpy(new_column->data, column_to_clone->data, nb_lines*nb_elements_per_line*sizeof(data_type));
|
2015-08-26 10:38:07 +02:00
|
|
|
(new_column->header)->lines_used = nb_lines;
|
|
|
|
}
|
2015-08-26 10:29:07 +02:00
|
|
|
|
2015-09-02 10:36:00 +02:00
|
|
|
// close column_to_clone
|
|
|
|
if (obi_close_column(column_to_clone) < 0)
|
|
|
|
{
|
|
|
|
obidebug(1, "\nError closing a column that has been cloned");
|
|
|
|
// TODO return NULL or not?
|
|
|
|
}
|
2015-08-26 17:01:54 +02:00
|
|
|
|
2015-08-26 10:29:07 +02:00
|
|
|
return new_column;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-31 18:03:48 +02:00
|
|
|
int obi_close_column(OBIDMS_column_p column)
|
|
|
|
{
|
2015-08-26 17:01:54 +02:00
|
|
|
size_t data_size;
|
|
|
|
|
|
|
|
// Munmap data
|
|
|
|
data_size = (column->header)->line_count * (column->header)->nb_elements_per_line * sizeof((column->header)->data_type);
|
|
|
|
if (munmap(column->data, data_size) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError munmapping column data");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Munmap header
|
|
|
|
if (munmap(column->header, (column->header)->header_size) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError munmapping a column header");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-07-31 18:03:48 +02:00
|
|
|
free(column);
|
2015-08-26 17:01:54 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int obi_truncate_column_to_lines_used(OBIDMS_column_p column)
|
|
|
|
{
|
|
|
|
size_t file_size;
|
|
|
|
size_t data_size;
|
|
|
|
int column_dir_file_descriptor;
|
|
|
|
int column_file_descriptor;
|
|
|
|
char* column_file_name;
|
|
|
|
|
|
|
|
// Get the file descriptor associated to the column directory
|
|
|
|
column_dir_file_descriptor = dirfd((column->column_directory)->directory);
|
|
|
|
if (column_dir_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError getting the file descriptor for a column directory");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the column file name
|
|
|
|
column_file_name = build_column_file_name((column->header)->name, (column->header)->version);
|
|
|
|
if (column_file_name == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the column file
|
2015-09-01 17:38:08 +02:00
|
|
|
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR);
|
2015-08-26 17:01:54 +02:00
|
|
|
if (column_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-26 17:05:37 +02:00
|
|
|
obidebug(1, "\nError getting the file descriptor of a column file");
|
2015-08-26 17:01:54 +02:00
|
|
|
free(column_file_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmap the data before truncating the file
|
|
|
|
data_size = (column->header)->line_count * (column->header)->nb_elements_per_line * sizeof((column->header)->data_type);
|
|
|
|
if (munmap(column->data, data_size) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError munmapping the data of a column before truncating");
|
|
|
|
free(column_file_name);
|
|
|
|
close(column_file_descriptor);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Truncate the column file at the number of lines used
|
|
|
|
data_size = (column->header)->lines_used * (column->header)->nb_elements_per_line * sizeof((column->header)->data_type);
|
|
|
|
file_size = (column->header)->header_size + data_size;
|
|
|
|
if (ftruncate(column_file_descriptor, file_size) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError truncating a column file at the number of lines used");
|
|
|
|
free(column_file_name);
|
|
|
|
close(column_file_descriptor);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remap the data
|
|
|
|
column->data = mmap(NULL,
|
|
|
|
data_size,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED,
|
|
|
|
column_file_descriptor,
|
|
|
|
(column->header)->header_size
|
|
|
|
);
|
|
|
|
|
|
|
|
if (column->data == MAP_FAILED)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError re-mmapping the data of a column after truncating");
|
|
|
|
free(column_file_name);
|
|
|
|
close(column_file_descriptor);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set line_count to lines_used
|
|
|
|
(column->header)->line_count = (column->header)->lines_used;
|
|
|
|
|
|
|
|
free(column_file_name);
|
|
|
|
close(column_file_descriptor);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-01 17:38:08 +02:00
|
|
|
int obi_enlarge_column(OBIDMS_column_p column)
|
|
|
|
{
|
|
|
|
size_t file_size;
|
|
|
|
size_t old_data_size;
|
|
|
|
size_t new_data_size;
|
|
|
|
size_t header_size;
|
|
|
|
size_t old_line_count;
|
|
|
|
size_t new_line_count;
|
|
|
|
int column_dir_file_descriptor;
|
|
|
|
int column_file_descriptor;
|
|
|
|
char* column_file_name;
|
|
|
|
|
|
|
|
// Get the file descriptor associated to the column directory
|
|
|
|
column_dir_file_descriptor = dirfd((column->column_directory)->directory);
|
|
|
|
if (column_dir_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError getting the file descriptor for a column directory");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the column file name
|
|
|
|
column_file_name = build_column_file_name((column->header)->name, (column->header)->version);
|
|
|
|
if (column_file_name == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the column file
|
|
|
|
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR | O_CREAT);
|
|
|
|
if (column_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError getting the file descriptor of a column file");
|
|
|
|
free(column_file_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the new file size
|
|
|
|
old_line_count = (column->header)->line_count;
|
|
|
|
new_line_count = old_line_count * GROWTH_FACTOR;
|
|
|
|
if (new_line_count > MAXIMUM_LINE_COUNT)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError enlarging a column file: new line count greater than the maximum allowed");
|
|
|
|
free(column_file_name);
|
|
|
|
close(column_file_descriptor);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
old_data_size = (column->header)->line_count * (column->header)->nb_elements_per_line * sizeof((column->header)->data_type);
|
|
|
|
new_data_size = old_data_size * GROWTH_FACTOR;
|
|
|
|
header_size = (column->header)->header_size;
|
|
|
|
file_size = header_size + new_data_size;
|
|
|
|
|
|
|
|
// Unmap the data
|
|
|
|
if (munmap(column->data, old_data_size) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError munmapping the data of a column before enlarging");
|
|
|
|
free(column_file_name);
|
|
|
|
close(column_file_descriptor);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmap the header
|
|
|
|
if (munmap(column->header, header_size) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError munmapping the header of a column before enlarging");
|
|
|
|
free(column_file_name);
|
|
|
|
close(column_file_descriptor);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enlarge the file
|
|
|
|
if (ftruncate(column_file_descriptor, file_size) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError enlarging a column file");
|
|
|
|
free(column_file_name);
|
|
|
|
close(column_file_descriptor);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remap the header (TODO not sure if necessary??)
|
|
|
|
column->header = mmap(NULL,
|
|
|
|
header_size,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED,
|
|
|
|
column_file_descriptor,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
if (column->header == MAP_FAILED)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError mmapping the header of a column after enlarging file");
|
|
|
|
close(column_file_descriptor);
|
|
|
|
free(column_file_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remap the data
|
|
|
|
column->data = mmap(NULL,
|
|
|
|
new_data_size,
|
|
|
|
PROT_READ | PROT_WRITE,
|
|
|
|
MAP_SHARED,
|
|
|
|
column_file_descriptor,
|
|
|
|
(column->header)->header_size
|
|
|
|
);
|
|
|
|
|
|
|
|
if (column->data == MAP_FAILED)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError re-mmapping the data of a column after enlarging the file");
|
|
|
|
free(column_file_name);
|
|
|
|
close(column_file_descriptor);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set new line count
|
|
|
|
(column->header)->line_count = new_line_count;
|
|
|
|
|
|
|
|
// Initialize new data lines to NA
|
|
|
|
obi_ini_to_NA_values(column, old_line_count, new_line_count - old_line_count);
|
|
|
|
|
|
|
|
free(column_file_name);
|
|
|
|
close(column_file_descriptor);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-26 17:01:54 +02:00
|
|
|
int obi_truncate_and_close_column(OBIDMS_column_p column)
|
|
|
|
{
|
|
|
|
if (obi_truncate_column_to_lines_used(column) < 0)
|
|
|
|
return -1;
|
|
|
|
if (obi_close_column(column) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2015-07-31 18:03:48 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-01 17:38:08 +02:00
|
|
|
void obi_ini_to_NA_values(OBIDMS_column_p column, size_t start, size_t nb_lines)
|
|
|
|
{
|
|
|
|
size_t i, end, nb_elements;
|
|
|
|
|
|
|
|
nb_elements = nb_lines*((column->header)->nb_elements_per_line);
|
|
|
|
end = start + nb_elements;
|
|
|
|
|
|
|
|
switch ((column->header)->data_type) {
|
|
|
|
case OBI_VOID: // TODO;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OBI_INT: for (i=start;i<end;i++)
|
|
|
|
{
|
|
|
|
*(((obiint_t*) (column->data)) + i) = OBIInt_NA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OBI_FLOAT: for (i=start;i<end;i++)
|
|
|
|
{
|
|
|
|
*(((obifloat_t*) (column->data)) + i) = OBIFloat_NA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OBI_BOOL: for (i=start;i<end;i++)
|
|
|
|
{
|
|
|
|
*(((obibool_t*) (column->data)) + i) = OBIBool_NA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OBI_CHAR: for (i=start;i<end;i++)
|
|
|
|
{
|
|
|
|
*(((obichar_t*) (column->data)) + i) = OBIChar_NA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case OBI_IDX: for (i=start;i<end;i++)
|
|
|
|
{
|
|
|
|
*(((obiidx_t*) (column->data)) + i) = OBIIdx_NA;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-31 18:03:48 +02:00
|
|
|
void obi_column_make_unwritable(OBIDMS_column_p column)
|
|
|
|
{
|
|
|
|
column->writable = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t obi_column_get_line_count(OBIDMS_column_p column)
|
|
|
|
{
|
|
|
|
return (column->header)->line_count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-26 17:25:15 +02:00
|
|
|
size_t obi_column_get_nb_lines_used(OBIDMS_column_p column)
|
|
|
|
{
|
|
|
|
return (column->header)->lines_used;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-07-31 18:03:48 +02:00
|
|
|
OBIType_t obi_column_get_data_type(OBIDMS_column_p column)
|
|
|
|
{
|
|
|
|
return (column->header)->data_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OBIType_t obi_column_get_data_type_from_name(OBIDMS_p dms, const char* column_name)
|
|
|
|
{
|
|
|
|
OBIDMS_column_header_p header;
|
|
|
|
OBIDMS_column_directory_p column_directory;
|
|
|
|
char* column_file_name;
|
|
|
|
int column_file_descriptor;
|
|
|
|
int column_dir_file_descriptor;
|
|
|
|
size_t header_size;
|
|
|
|
OBIType_t data_type;
|
|
|
|
obiversion_t version_number;
|
|
|
|
|
|
|
|
// Get the column directory structure associated to the column
|
|
|
|
column_directory = obi_open_column_directory(dms, column_name);
|
|
|
|
if (column_directory == NULL)
|
|
|
|
{
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError opening a column directory structure");
|
2015-07-31 18:03:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the file descriptor associated to the column directory
|
|
|
|
column_dir_file_descriptor = dirfd(column_directory->directory);
|
|
|
|
if (column_dir_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError getting the file descriptor of a column directory");
|
2015-07-31 18:03:48 +02:00
|
|
|
obi_close_column_directory(column_directory);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate the header size
|
|
|
|
header_size = obi_get_platform_header_size();
|
|
|
|
|
|
|
|
// Get the latest version number
|
|
|
|
version_number = obi_get_latest_version_number(column_directory);
|
|
|
|
if (version_number < 0)
|
|
|
|
{
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError getting the latest version number in a column directory");
|
2015-07-31 18:03:48 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the column file name
|
|
|
|
column_file_name = build_column_file_name(column_name, version_number);
|
|
|
|
if (column_file_name == NULL)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the column file (READ-ONLY)
|
|
|
|
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDONLY);
|
|
|
|
if (column_file_descriptor < 0)
|
|
|
|
{
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError opening a column file");
|
2015-07-31 18:03:48 +02:00
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
|
|
|
free(column_file_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill the header structure
|
|
|
|
header = mmap(NULL,
|
|
|
|
header_size,
|
|
|
|
PROT_READ,
|
|
|
|
MAP_SHARED,
|
|
|
|
column_file_descriptor,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
if (header == MAP_FAILED)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOL_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError mmapping the header of a column");
|
2015-07-31 18:03:48 +02:00
|
|
|
close(column_file_descriptor);
|
|
|
|
free(column_file_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check endianness?
|
|
|
|
|
|
|
|
data_type = header->data_type;
|
|
|
|
|
|
|
|
free(column_file_name);
|
|
|
|
close(column_file_descriptor);
|
|
|
|
munmap(header, header_size);
|
|
|
|
return data_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* obi_column_get_elements_names(OBIDMS_column_p column)
|
|
|
|
{
|
|
|
|
return (column->header)->elements_names;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// to be rewritten in an optimized and safe way
|
|
|
|
size_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char* element_name)
|
|
|
|
{
|
|
|
|
char* elements_names;
|
|
|
|
char* name;
|
|
|
|
size_t element_index;
|
|
|
|
|
|
|
|
elements_names = strdup((column->header)->elements_names);
|
|
|
|
if (elements_names == NULL)
|
|
|
|
{
|
|
|
|
obidebug(1, "\nError strdup-ing the elements names");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
element_index = 0;
|
|
|
|
|
|
|
|
name = strtok (elements_names, ";"); // not thread safe, see strtok_r maybe
|
|
|
|
if (strcmp(element_name, name) == 0)
|
|
|
|
{
|
|
|
|
free(elements_names);
|
|
|
|
return element_index;
|
|
|
|
}
|
|
|
|
element_index++;
|
|
|
|
|
|
|
|
while (name != NULL)
|
|
|
|
{
|
|
|
|
name = strtok (NULL, ";"); // not thread safe, see strtok_r maybe
|
|
|
|
if (strcmp(element_name, name) == 0)
|
|
|
|
{
|
|
|
|
free(elements_names);
|
|
|
|
return element_index;
|
|
|
|
}
|
|
|
|
element_index++;
|
|
|
|
}
|
|
|
|
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nCan't find an element name");
|
2015-07-31 18:03:48 +02:00
|
|
|
free(elements_names);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
size_t obi_column_get_nb_elements_per_line(OBIDMS_column_p column)
|
|
|
|
{
|
|
|
|
return (column->header)->nb_elements_per_line;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|