2015-09-30 12:03:46 +02:00
|
|
|
/********************************************************************
|
|
|
|
* OBIDMS functions *
|
|
|
|
********************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file obidms.c
|
|
|
|
* @author Eric Coissac (eric.coissac@metabarcoding.org)
|
|
|
|
* @date 23 May 2015
|
|
|
|
* @brief OBIDMS functions.
|
2015-05-23 16:29:06 +03:00
|
|
|
*/
|
|
|
|
|
2015-09-30 12:03:46 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2015-06-23 18:35:34 +02:00
|
|
|
#include <string.h>
|
2015-11-18 15:35:09 +01:00
|
|
|
#include <stdbool.h>
|
2015-06-23 18:35:34 +02:00
|
|
|
#include <sys/stat.h>
|
2015-11-03 14:22:00 +01:00
|
|
|
#include <fcntl.h>
|
2015-06-23 18:35:34 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
2016-04-29 17:46:36 +02:00
|
|
|
#include <unistd.h>
|
2015-06-17 16:51:16 +02:00
|
|
|
|
|
|
|
#include "obidms.h"
|
2015-06-23 18:35:34 +02:00
|
|
|
#include "obierrno.h"
|
2015-07-31 18:03:48 +02:00
|
|
|
#include "obidebug.h"
|
|
|
|
#include "obidmscolumn.h"
|
2016-04-15 10:49:12 +02:00
|
|
|
#include "obiblob_indexer.h"
|
2016-04-08 15:38:57 +02:00
|
|
|
#include "utils.h"
|
2015-11-16 14:37:51 +01:00
|
|
|
#include "obilittlebigman.h"
|
2015-07-31 18:03:48 +02:00
|
|
|
|
2015-08-03 15:10:39 +02:00
|
|
|
|
2015-09-30 12:03:46 +02:00
|
|
|
#define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?)
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-05-26 21:36:55 +02:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
/**************************************************************************
|
2015-05-26 21:36:55 +02:00
|
|
|
*
|
2015-06-23 18:35:34 +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 21:36:55 +02:00
|
|
|
*
|
2015-06-23 18:35:34 +02:00
|
|
|
**************************************************************************/
|
2015-05-26 21:36:55 +02:00
|
|
|
|
|
|
|
|
2015-05-23 16:29:06 +03:00
|
|
|
/**
|
2015-09-30 12:03:46 +02:00
|
|
|
* Internal function building the OBIDMS directory name from an OBIDMS name.
|
2015-05-23 16:29:06 +03:00
|
|
|
*
|
2015-06-10 15:19:02 +02:00
|
|
|
* The function builds the directory name corresponding to an OBIDMS.
|
2015-09-30 12:03:46 +02:00
|
|
|
* It also checks that the name is not too long.
|
2015-05-23 16:29:06 +03:00
|
|
|
*
|
|
|
|
* @warning The returned pointer has to be freed by the caller.
|
|
|
|
*
|
2015-09-30 12:03:46 +02:00
|
|
|
* @param dms_name The name of the OBIDMS.
|
2015-05-23 16:29:06 +03:00
|
|
|
*
|
2015-09-30 12:03:46 +02:00
|
|
|
* @returns A pointer to the directory name.
|
|
|
|
* @retval NULL if an error occurred.
|
2015-05-23 16:29:06 +03:00
|
|
|
*
|
|
|
|
* @since May 2015
|
|
|
|
* @author Eric Coissac (eric.coissac@metabarcoding.org)
|
|
|
|
*/
|
2015-09-30 12:03:46 +02:00
|
|
|
static char* build_directory_name(const char* dms_name);
|
2015-06-17 16:51:16 +02:00
|
|
|
|
|
|
|
|
2015-11-16 14:37:51 +01:00
|
|
|
/**
|
|
|
|
* Internal function building the informations file name from an OBIDMS name.
|
|
|
|
*
|
|
|
|
* The function builds the file name for the informations file of an OBIDMS.
|
|
|
|
*
|
|
|
|
* @warning The returned pointer has to be freed by the caller.
|
|
|
|
*
|
|
|
|
* @param dms_name The name of the OBIDMS.
|
|
|
|
*
|
|
|
|
* @returns A pointer to the file name.
|
|
|
|
* @retval NULL if an error occurred.
|
|
|
|
*
|
|
|
|
* @since November 2015
|
|
|
|
* @author Celine Mercier (celine.mercier@metabarcoding.org)
|
|
|
|
*/
|
|
|
|
static char* build_infos_file_name(const char* dms_name);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal function creating the file containing basic informations on the OBIDMS.
|
|
|
|
*
|
|
|
|
* This file contains:
|
|
|
|
* - The endianness of the platform
|
|
|
|
*
|
|
|
|
* @warning The returned pointer has to be freed by the caller.
|
|
|
|
*
|
|
|
|
* @param dms_file_descriptor The file descriptor for the OBIDMS directory.
|
|
|
|
* @param dms_name The name of the OBIDMS.
|
|
|
|
*
|
|
|
|
* @retval 0 if the operation was successfully completed.
|
|
|
|
* @retval -1 if an error occurred.
|
|
|
|
*
|
|
|
|
* @since November 2015
|
|
|
|
* @author Celine Mercier (celine.mercier@metabarcoding.org)
|
|
|
|
*/
|
|
|
|
int create_dms_infos_file(int dms_file_descriptor, const char* dms_name);
|
|
|
|
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
/************************************************************************
|
2015-06-17 16:51:16 +02:00
|
|
|
*
|
2015-06-23 18:35:34 +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-06-17 16:51:16 +02:00
|
|
|
*
|
2015-06-23 18:35:34 +02:00
|
|
|
************************************************************************/
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-09-30 12:03:46 +02:00
|
|
|
static char* build_directory_name(const char* dms_name)
|
2015-06-17 16:51:16 +02:00
|
|
|
{
|
2015-09-30 12:03:46 +02:00
|
|
|
char* directory_name;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
|
|
|
// Build the database directory name
|
2016-03-21 11:33:06 +01:00
|
|
|
directory_name = (char*) malloc((strlen(dms_name) + 8)*sizeof(char));
|
|
|
|
if (sprintf(directory_name, "%s.obidms", dms_name) < 0)
|
2015-05-23 16:29:06 +03:00
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nProblem building an OBIDMS directory name");
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test if the database name is not too long
|
2015-06-17 16:51:16 +02:00
|
|
|
if (strlen(directory_name) >= OBIDMS_MAX_NAME)
|
2015-05-23 16:29:06 +03:00
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_LONG_NAME_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nProblem building an OBIDMS directory name");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(directory_name);
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
return directory_name;
|
2015-05-23 16:29:06 +03:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-11-16 14:37:51 +01:00
|
|
|
static char* build_infos_file_name(const char* dms_name)
|
|
|
|
{
|
|
|
|
char* file_name;
|
|
|
|
|
|
|
|
// Build file name
|
2016-03-21 11:33:06 +01:00
|
|
|
file_name = (char*) malloc((strlen(dms_name) + 7)*sizeof(char));
|
|
|
|
if (sprintf(file_name, "%s_infos", dms_name) < 0)
|
2015-11-16 14:37:51 +01:00
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
|
|
|
obidebug(1, "\nProblem building an informations file name");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return file_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int create_dms_infos_file(int dms_file_descriptor, const char* dms_name)
|
|
|
|
{
|
|
|
|
char* file_name;
|
|
|
|
int infos_file_descriptor;
|
|
|
|
off_t file_size;
|
|
|
|
bool little_endian;
|
|
|
|
|
|
|
|
file_size = sizeof(bool);
|
|
|
|
|
|
|
|
// Create file name
|
|
|
|
file_name = build_infos_file_name(dms_name);
|
|
|
|
if (file_name == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Create file
|
|
|
|
infos_file_descriptor = openat(dms_file_descriptor, file_name, O_RDWR | O_CREAT | O_EXCL, 0777);
|
|
|
|
if (infos_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError creating an informations file");
|
|
|
|
free(file_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(file_name);
|
|
|
|
|
|
|
|
// Truncate the infos file to the right size
|
|
|
|
if (ftruncate(infos_file_descriptor, file_size) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError truncating an informations file");
|
|
|
|
close(infos_file_descriptor);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write endianness
|
|
|
|
little_endian = obi_is_little_endian();
|
|
|
|
if (write(infos_file_descriptor, &little_endian, sizeof(bool)) < ((ssize_t) sizeof(bool)))
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError writing the endianness in an informations file");
|
|
|
|
close(infos_file_descriptor);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close file
|
|
|
|
close(infos_file_descriptor);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
/**********************************************************************
|
2015-05-26 21:36:55 +02:00
|
|
|
*
|
2015-06-23 18:35:34 +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 21:36:55 +02:00
|
|
|
*
|
2015-06-23 18:35:34 +02:00
|
|
|
**********************************************************************/
|
2015-05-26 21:36:55 +02:00
|
|
|
|
2016-04-29 17:46:36 +02:00
|
|
|
int obi_dms_exists(const char* dms_path)
|
2015-06-17 16:51:16 +02:00
|
|
|
{
|
2015-05-26 21:36:55 +02:00
|
|
|
struct stat buffer;
|
2016-02-18 10:38:51 +01:00
|
|
|
char* directory_name;
|
|
|
|
int check_dir;
|
2015-05-26 21:36:55 +02:00
|
|
|
|
|
|
|
// Build and check the directory name
|
2016-04-29 17:46:36 +02:00
|
|
|
directory_name = build_directory_name(dms_path);
|
2015-06-17 16:51:16 +02:00
|
|
|
if (directory_name == NULL)
|
2015-05-26 21:36:55 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
check_dir = stat(directory_name, &buffer);
|
2015-05-26 21:36:55 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
free(directory_name);
|
2015-05-26 21:36:55 +02:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
if (check_dir == 0)
|
2015-05-26 21:36:55 +02:00
|
|
|
return 1;
|
2015-06-23 18:35:34 +02:00
|
|
|
else
|
|
|
|
return 0;
|
2015-05-26 21:36:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-29 17:46:36 +02:00
|
|
|
OBIDMS_p obi_create_dms(const char* dms_path)
|
2015-06-17 16:51:16 +02:00
|
|
|
{
|
2016-04-29 17:46:36 +02:00
|
|
|
char* directory_name;
|
|
|
|
DIR* dms_dir;
|
|
|
|
int dms_file_descriptor;
|
|
|
|
size_t i, j;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
|
|
|
// Build and check the directory name
|
2016-04-29 17:46:36 +02:00
|
|
|
directory_name = build_directory_name(dms_path);
|
2015-06-17 16:51:16 +02:00
|
|
|
if (directory_name == NULL)
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Try to create the directory
|
2015-09-21 15:48:02 +02:00
|
|
|
if (mkdir(directory_name, 00777) < 0)
|
2015-05-23 16:29:06 +03:00
|
|
|
{
|
2015-06-17 16:51:16 +02:00
|
|
|
if (errno == EEXIST)
|
2015-11-09 15:22:01 +01:00
|
|
|
{
|
2015-05-23 16:29:06 +03:00
|
|
|
obi_set_errno(OBIDMS_EXIST_ERROR);
|
2015-11-09 15:22:01 +01:00
|
|
|
obidebug(1, "\nAn OBIDMS directory with the same name already exists in this directory.");
|
|
|
|
}
|
2015-05-23 16:29:06 +03:00
|
|
|
else
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nProblem creating an OBIDMS directory");
|
2015-06-17 16:51:16 +02:00
|
|
|
free(directory_name);
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-30 11:41:30 +02:00
|
|
|
// Get file descriptor of DMS directory to create other directories
|
2015-11-03 14:22:00 +01:00
|
|
|
dms_dir = opendir(directory_name);
|
|
|
|
if (dms_dir == NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nProblem opening a newly created OBIDMS directory");
|
|
|
|
free(directory_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-11-09 11:22:51 +01:00
|
|
|
|
|
|
|
free(directory_name);
|
|
|
|
|
2015-11-03 14:22:00 +01:00
|
|
|
dms_file_descriptor = dirfd(dms_dir);
|
|
|
|
if (dms_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nProblem getting the file descriptor of a newly created OBIDMS directory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-04-12 14:53:33 +02:00
|
|
|
// Create the indexer directory
|
|
|
|
if (mkdirat(dms_file_descriptor, INDEXER_DIR_NAME, 00777) < 0)
|
2015-11-03 14:22:00 +01:00
|
|
|
{
|
2016-04-12 14:53:33 +02:00
|
|
|
obi_set_errno(OBI_INDEXER_ERROR);
|
|
|
|
obidebug(1, "\nProblem creating an indexer directory");
|
2015-11-03 14:22:00 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-06-30 11:41:30 +02:00
|
|
|
// Create the view directory
|
|
|
|
if (mkdirat(dms_file_descriptor, VIEW_DIR_NAME, 00777) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIVIEW_ERROR);
|
|
|
|
obidebug(1, "\nProblem creating a view directory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-04-29 17:46:36 +02:00
|
|
|
// Isolate the dms name
|
|
|
|
j = 0;
|
|
|
|
for (i=0; i<strlen(dms_path); i++)
|
|
|
|
{
|
|
|
|
if (dms_path[i] == '/')
|
|
|
|
j = i+1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2015-11-16 14:37:51 +01:00
|
|
|
// Create the informations file
|
2016-04-29 17:46:36 +02:00
|
|
|
if (create_dms_infos_file(dms_file_descriptor, dms_path+j) < 0)
|
2015-11-16 14:37:51 +01:00
|
|
|
return NULL;
|
|
|
|
|
2016-04-29 17:46:36 +02:00
|
|
|
return obi_open_dms(dms_path);
|
2015-05-23 16:29:06 +03:00
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2016-04-29 17:46:36 +02:00
|
|
|
OBIDMS_p obi_open_dms(const char* dms_path)
|
2015-06-17 16:51:16 +02:00
|
|
|
{
|
|
|
|
OBIDMS_p dms;
|
2016-08-30 11:09:45 +02:00
|
|
|
char* complete_dms_path;
|
2015-11-16 14:37:51 +01:00
|
|
|
char* infos_file_name;
|
|
|
|
int infos_file_descriptor;
|
|
|
|
bool little_endian_dms;
|
|
|
|
bool little_endian_platform;
|
2016-08-30 11:09:45 +02:00
|
|
|
size_t i, j;
|
2015-06-17 16:51:16 +02:00
|
|
|
|
|
|
|
dms = NULL;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-11-09 15:06:02 +01:00
|
|
|
// Allocate the data structure
|
|
|
|
dms = (OBIDMS_p) malloc(sizeof(OBIDMS_t));
|
|
|
|
if (dms == NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
|
|
|
obidebug(1, "\nError allocating the memory for the OBIDMS structure");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-08-29 17:30:31 +02:00
|
|
|
// Isolate and store the dms name
|
|
|
|
j = 0;
|
|
|
|
for (i=0; i<strlen(dms_path); i++)
|
|
|
|
{
|
|
|
|
if (dms_path[i] == '/')
|
|
|
|
j = i+1;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
strcpy(dms->dms_name, dms_path+j);
|
|
|
|
|
2016-08-30 11:09:45 +02:00
|
|
|
// Build and check the directory name including the relative path
|
|
|
|
complete_dms_path = build_directory_name(dms_path);
|
|
|
|
if (complete_dms_path == NULL)
|
2015-11-09 15:06:02 +01:00
|
|
|
{
|
|
|
|
free(dms);
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
2015-11-09 15:06:02 +01:00
|
|
|
}
|
|
|
|
|
2016-08-30 11:09:45 +02:00
|
|
|
// Get and store the absolute path to the DMS directory
|
|
|
|
if (realpath(complete_dms_path, dms->directory_path) == NULL)
|
2016-04-29 17:46:36 +02:00
|
|
|
{
|
2016-08-30 11:09:45 +02:00
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError getting the absolute path to the DMS directory");
|
|
|
|
free(complete_dms_path);
|
|
|
|
return NULL;
|
2016-04-29 17:46:36 +02:00
|
|
|
}
|
2016-08-29 17:30:31 +02:00
|
|
|
|
2016-08-30 11:09:45 +02:00
|
|
|
free(complete_dms_path);
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
// Try to open the directory
|
2016-04-29 17:46:36 +02:00
|
|
|
dms->directory = opendir(dms->directory_path);
|
2015-11-09 15:06:02 +01:00
|
|
|
if (dms->directory == NULL)
|
2015-06-23 18:35:34 +02:00
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case ENOENT:
|
|
|
|
obi_set_errno(OBIDMS_NOT_EXIST_ERROR);
|
|
|
|
break;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
case EACCES:
|
|
|
|
obi_set_errno(OBIDMS_ACCESS_ERROR);
|
|
|
|
break;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
case ENOMEM:
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
|
|
|
break;
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
default:
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2015-05-23 16:29:06 +03:00
|
|
|
}
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nCan't open OBIDMS directory");
|
2015-11-09 15:06:02 +01:00
|
|
|
free(dms);
|
2015-05-23 16:29:06 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-16 14:37:51 +01:00
|
|
|
// Get and store file descriptor of DMS directory to open the informations file
|
2015-11-09 15:06:02 +01:00
|
|
|
dms->dir_fd = dirfd(dms->directory);
|
|
|
|
if (dms->dir_fd < 0)
|
2015-11-03 14:22:00 +01:00
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError getting the file descriptor for a newly created OBIDMS directory");
|
2015-11-09 15:06:02 +01:00
|
|
|
closedir(dms->directory);
|
|
|
|
free(dms);
|
2015-11-03 14:22:00 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-16 14:37:51 +01:00
|
|
|
// Open informations file to check endianness
|
2016-04-29 17:46:36 +02:00
|
|
|
infos_file_name = build_infos_file_name(dms->dms_name);
|
2015-11-16 14:37:51 +01:00
|
|
|
infos_file_descriptor = openat(dms->dir_fd, infos_file_name, O_RDONLY, 0777);
|
|
|
|
if (infos_file_descriptor < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError opening an informations file");
|
|
|
|
closedir(dms->directory);
|
|
|
|
free(dms);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(infos_file_name);
|
|
|
|
|
|
|
|
// Check endianness of the platform and DMS
|
|
|
|
little_endian_platform = obi_is_little_endian();
|
|
|
|
if (read(infos_file_descriptor, &little_endian_dms, sizeof(bool)) < ((ssize_t) sizeof(bool)))
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError reading the endianness in an informations file");
|
|
|
|
close(infos_file_descriptor);
|
|
|
|
closedir(dms->directory);
|
|
|
|
free(dms);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (little_endian_platform != little_endian_dms)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_BAD_ENDIAN_ERROR);
|
|
|
|
obidebug(1, "\nError: The DMS and the platform have different endianness");
|
|
|
|
close(infos_file_descriptor);
|
|
|
|
closedir(dms->directory);
|
|
|
|
free(dms);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
close(infos_file_descriptor);
|
|
|
|
|
2015-11-18 15:35:09 +01:00
|
|
|
dms->little_endian = little_endian_dms;
|
|
|
|
|
2016-04-12 14:53:33 +02:00
|
|
|
// Open the indexer directory
|
|
|
|
dms->indexer_directory = opendir_in_dms(dms, INDEXER_DIR_NAME);
|
|
|
|
if (dms->indexer_directory == NULL)
|
2015-11-03 14:22:00 +01:00
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2016-04-12 14:53:33 +02:00
|
|
|
obidebug(1, "\nError opening the indexer directory");
|
2015-11-09 15:06:02 +01:00
|
|
|
closedir(dms->directory);
|
|
|
|
free(dms);
|
2015-11-03 14:22:00 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-04-12 14:53:33 +02:00
|
|
|
// Store the indexer directory's file descriptor
|
|
|
|
dms->indexer_dir_fd = dirfd(dms->indexer_directory);
|
|
|
|
if (dms->indexer_dir_fd < 0)
|
2015-11-09 15:06:02 +01:00
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
2016-04-12 14:53:33 +02:00
|
|
|
obidebug(1, "\nError getting the file descriptor of the indexer directory");
|
|
|
|
closedir(dms->indexer_directory);
|
2015-11-16 14:37:51 +01:00
|
|
|
closedir(dms->directory);
|
2015-11-09 15:06:02 +01:00
|
|
|
free(dms);
|
|
|
|
return NULL;
|
|
|
|
}
|
2015-06-23 18:35:34 +02:00
|
|
|
|
2016-06-30 11:41:30 +02:00
|
|
|
// Open the view directory
|
|
|
|
dms->view_directory = opendir_in_dms(dms, VIEW_DIR_NAME);
|
|
|
|
if (dms->view_directory == NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError opening the view directory");
|
|
|
|
closedir(dms->indexer_directory);
|
|
|
|
closedir(dms->directory);
|
|
|
|
free(dms);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the view directory's file descriptor
|
|
|
|
dms->view_dir_fd = dirfd(dms->view_directory);
|
|
|
|
if (dms->view_dir_fd < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
|
|
|
|
obidebug(1, "\nError getting the file descriptor of the view directory");
|
|
|
|
closedir(dms->view_directory);
|
|
|
|
closedir(dms->directory);
|
|
|
|
free(dms);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-12-02 17:32:07 +01:00
|
|
|
// Initialize the list of opened columns
|
|
|
|
dms->opened_columns = (Opened_columns_list_p) malloc(sizeof(Opened_columns_list_t));
|
|
|
|
(dms->opened_columns)->nb_opened_columns = 0;
|
|
|
|
|
2016-04-22 11:28:09 +02:00
|
|
|
// Initialize the list of opened indexers
|
2016-04-12 14:53:33 +02:00
|
|
|
dms->opened_indexers = (Opened_indexers_list_p) malloc(sizeof(Opened_indexers_list_t));
|
|
|
|
(dms->opened_indexers)->nb_opened_indexers = 0;
|
2015-12-02 17:32:07 +01:00
|
|
|
|
2015-05-23 16:29:06 +03:00
|
|
|
return dms;
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:02 +02:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
OBIDMS_p obi_dms(const char* dms_name)
|
|
|
|
{
|
2015-06-23 18:35:34 +02:00
|
|
|
int exists;
|
2015-05-26 21:36:55 +02:00
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
exists = obi_dms_exists(dms_name);
|
|
|
|
|
|
|
|
switch (exists)
|
|
|
|
{
|
2015-05-26 21:36:55 +02:00
|
|
|
case 0:
|
2015-06-17 16:51:16 +02:00
|
|
|
return obi_create_dms(dms_name);
|
2015-05-26 21:36:55 +02:00
|
|
|
case 1:
|
2015-06-17 16:51:16 +02:00
|
|
|
return obi_open_dms(dms_name);
|
2015-05-26 21:36:55 +02:00
|
|
|
};
|
|
|
|
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError checking if an OBIDMS directory exists");
|
2015-05-26 21:36:55 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-23 16:29:06 +03:00
|
|
|
|
2015-06-17 16:51:16 +02:00
|
|
|
int obi_close_dms(OBIDMS_p dms)
|
|
|
|
{
|
2015-05-23 16:29:06 +03:00
|
|
|
if (dms != NULL)
|
|
|
|
{
|
2015-12-02 17:32:07 +01:00
|
|
|
// Close all columns
|
|
|
|
while ((dms->opened_columns)->nb_opened_columns > 0)
|
|
|
|
obi_close_column(*((dms->opened_columns)->columns));
|
|
|
|
|
2016-06-30 11:41:30 +02:00
|
|
|
// Close dms, and view and indexer directories
|
2015-05-23 16:29:06 +03:00
|
|
|
if (closedir(dms->directory) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIDMS_MEMORY_ERROR);
|
2015-11-03 14:22:00 +01:00
|
|
|
obidebug(1, "\nError closing an OBIDMS directory");
|
|
|
|
free(dms);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-04-22 11:28:09 +02:00
|
|
|
if (closedir(dms->indexer_directory) < 0)
|
2015-11-03 14:22:00 +01:00
|
|
|
{
|
2016-04-12 14:53:33 +02:00
|
|
|
obi_set_errno(OBI_INDEXER_ERROR);
|
|
|
|
obidebug(1, "\nError closing an indexer directory");
|
2015-05-23 16:29:06 +03:00
|
|
|
free(dms);
|
|
|
|
return -1;
|
|
|
|
}
|
2016-06-30 11:41:30 +02:00
|
|
|
if (closedir(dms->view_directory) < 0)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBIVIEW_ERROR);
|
|
|
|
obidebug(1, "\nError closing a view directory");
|
|
|
|
free(dms);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-23 16:29:06 +03:00
|
|
|
free(dms);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-15 10:49:12 +02:00
|
|
|
|
|
|
|
int obi_dms_is_column_name_in_list(OBIDMS_p dms, const char* column_name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Opened_columns_list_p columns_list;
|
|
|
|
|
|
|
|
columns_list = dms->opened_columns;
|
|
|
|
|
|
|
|
for (i=0; i < (columns_list->nb_opened_columns); i++)
|
|
|
|
{
|
|
|
|
if (!strcmp(((*((columns_list->columns)+i))->header)->name, column_name))
|
|
|
|
{ // Found it
|
2016-04-22 11:28:09 +02:00
|
|
|
return 1;
|
2016-04-15 10:49:12 +02:00
|
|
|
}
|
|
|
|
}
|
2016-04-22 11:28:09 +02:00
|
|
|
return 0;
|
2016-04-15 10:49:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OBIDMS_column_p obi_dms_get_column_from_list(OBIDMS_p dms, const char* column_name, obiversion_t version)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=0; i < ((dms->opened_columns)->nb_opened_columns); i++)
|
|
|
|
{
|
|
|
|
if (!strcmp(((*(((dms->opened_columns)->columns)+i))->header)->name, column_name)
|
|
|
|
&& (((*(((dms->opened_columns)->columns)+i))->header)->version == version))
|
|
|
|
{ // Found the column already opened, return it
|
|
|
|
return *(((dms->opened_columns)->columns)+i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Didn't find the column
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-22 11:28:09 +02:00
|
|
|
void obi_dms_list_column(OBIDMS_p dms, OBIDMS_column_p column) // TODO add check if column already in list?
|
2016-04-15 10:49:12 +02:00
|
|
|
{
|
|
|
|
*(((dms->opened_columns)->columns)+((dms->opened_columns)->nb_opened_columns)) = column;
|
|
|
|
((dms->opened_columns)->nb_opened_columns)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int obi_dms_unlist_column(OBIDMS_p dms, OBIDMS_column_p column)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Opened_columns_list_p columns_list;
|
|
|
|
|
|
|
|
columns_list = dms->opened_columns;
|
|
|
|
|
|
|
|
for (i=0; i < columns_list->nb_opened_columns; i++)
|
|
|
|
{
|
|
|
|
if (!strcmp(((*((columns_list->columns)+i))->header)->name, (column->header)->name)
|
|
|
|
&& (((*((columns_list->columns)+i))->header)->version == (column->header)->version))
|
|
|
|
{ // Found the column. Rearrange list
|
|
|
|
(columns_list->nb_opened_columns)--;
|
|
|
|
(columns_list->columns)[i] = (columns_list->columns)[columns_list->nb_opened_columns];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obidebug(1, "\nCould not find the column to delete from list of open columns");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Obi_indexer_p obi_dms_get_indexer_from_list(OBIDMS_p dms, const char* indexer_name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Opened_indexers_list_p indexers_list;
|
|
|
|
|
|
|
|
indexers_list = dms->opened_indexers;
|
|
|
|
|
|
|
|
for (i=0; i < (indexers_list->nb_opened_indexers); i++)
|
|
|
|
{
|
2016-04-29 16:18:56 +02:00
|
|
|
if (!strcmp(obi_indexer_get_name((indexers_list->indexers)[i]), indexer_name))
|
2016-04-15 10:49:12 +02:00
|
|
|
{ // Found the indexer already opened, return it
|
|
|
|
return (indexers_list->indexers)[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Didn't find the indexer
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-29 16:06:01 +02:00
|
|
|
void obi_dms_list_indexer(OBIDMS_p dms, Obi_indexer_p indexer) // TODO add check if indexer already in list?
|
2016-04-15 10:49:12 +02:00
|
|
|
{
|
|
|
|
*(((dms->opened_indexers)->indexers)+((dms->opened_indexers)->nb_opened_indexers)) = indexer;
|
|
|
|
((dms->opened_indexers)->nb_opened_indexers)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int obi_dms_unlist_indexer(OBIDMS_p dms, Obi_indexer_p indexer)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
Opened_indexers_list_p indexers_list;
|
|
|
|
|
|
|
|
indexers_list = dms->opened_indexers;
|
|
|
|
|
|
|
|
for (i=0; i < indexers_list->nb_opened_indexers; i++)
|
|
|
|
{
|
2016-04-29 16:18:56 +02:00
|
|
|
if (!strcmp(obi_indexer_get_name((indexers_list->indexers)[i]), indexer->name))
|
2016-04-15 10:49:12 +02:00
|
|
|
{ // Found the indexer. Rearrange list
|
|
|
|
(indexers_list->nb_opened_indexers)--;
|
|
|
|
(indexers_list->indexers)[i] = (indexers_list->indexers)[indexers_list->nb_opened_indexers];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obidebug(1, "\nCould not find the indexer to delete from list of open indexers");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-15 11:11:13 +02:00
|
|
|
char* obi_dms_get_dms_path(OBIDMS_p dms)
|
2016-04-15 10:49:12 +02:00
|
|
|
{
|
|
|
|
char* full_path;
|
|
|
|
|
|
|
|
full_path = (char*) malloc((MAX_PATH_LEN)*sizeof(char));
|
|
|
|
if (full_path == NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBI_MALLOC_ERROR);
|
|
|
|
obidebug(1, "\nError allocating memory for the char* path to a file or directory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-04-29 17:46:36 +02:00
|
|
|
strcpy(full_path, dms->directory_path);
|
2016-04-15 11:11:13 +02:00
|
|
|
|
|
|
|
return full_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char* obi_dms_get_full_path(OBIDMS_p dms, const char* path_name)
|
|
|
|
{
|
|
|
|
char* full_path;
|
|
|
|
|
|
|
|
full_path = obi_dms_get_dms_path(dms);
|
2016-04-15 10:49:12 +02:00
|
|
|
strcat(full_path, "/");
|
|
|
|
strcat(full_path, path_name);
|
|
|
|
|
|
|
|
return full_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DIR* opendir_in_dms(OBIDMS_p dms, const char* path_name)
|
|
|
|
{
|
|
|
|
char* full_path;
|
|
|
|
DIR* directory;
|
|
|
|
|
2016-04-15 11:11:13 +02:00
|
|
|
full_path = obi_dms_get_full_path(dms, path_name);
|
2016-04-15 10:49:12 +02:00
|
|
|
if (full_path == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
directory = opendir(full_path);
|
|
|
|
if (directory == NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBI_UTILS_ERROR);
|
|
|
|
obidebug(1, "\nError opening a directory");
|
|
|
|
}
|
|
|
|
|
|
|
|
free(full_path);
|
|
|
|
|
|
|
|
return directory;
|
|
|
|
}
|