2015-06-23 18:35:34 +02:00
|
|
|
/****************************************************************************
|
2015-06-26 17:53:03 +02:00
|
|
|
* OBIDMS column directories functions *
|
2015-06-23 18:35:34 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2015-06-26 17:53:03 +02:00
|
|
|
* @file obidmscolumndir.c
|
2015-09-30 12:03:46 +02:00
|
|
|
* @author Celine Mercier (celine.mercier@metabarcoding.org)
|
2015-06-23 18:35:34 +02:00
|
|
|
* @date 18 June 2015
|
2015-06-26 17:53:03 +02:00
|
|
|
* @brief Functions for OBIDMS column directories.
|
2015-06-23 18:35:34 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/types.h>
|
2015-07-20 11:38:43 +02:00
|
|
|
#include <sys/stat.h>
|
2015-06-23 18:35:34 +02:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
#include "obidmscolumndir.h"
|
2015-06-23 18:35:34 +02:00
|
|
|
#include "obidms.h"
|
2016-04-08 15:38:57 +02:00
|
|
|
#include "utils.h"
|
2015-06-23 18:35:34 +02:00
|
|
|
#include "obierrno.h"
|
2015-08-03 15:10:39 +02:00
|
|
|
#include "obidebug.h"
|
|
|
|
|
|
|
|
|
2015-09-30 12:03:46 +02:00
|
|
|
#define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?)
|
2015-06-23 18:35:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-26 18:58:48 +02:00
|
|
|
/**********************************************************************
|
2015-06-23 18:35:34 +02:00
|
|
|
*
|
2017-10-26 18:58:48 +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-06-23 18:35:34 +02:00
|
|
|
*
|
2017-10-26 18:58:48 +02:00
|
|
|
**********************************************************************/
|
2015-06-23 18:35:34 +02:00
|
|
|
|
|
|
|
|
2017-10-26 18:58:48 +02:00
|
|
|
char* obi_build_column_directory_name(const char* column_name)
|
2015-06-23 18:35:34 +02:00
|
|
|
{
|
|
|
|
char* column_directory_name;
|
|
|
|
|
|
|
|
// Build the database directory name
|
2016-03-21 11:33:06 +01:00
|
|
|
column_directory_name = (char*) malloc((strlen(column_name) + 8)*sizeof(char));
|
|
|
|
if (sprintf(column_directory_name, "%s.obicol", column_name) < 0)
|
2015-06-23 18:35:34 +02:00
|
|
|
{
|
|
|
|
obi_set_errno(OBICOLDIR_MEMORY_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError building a column directory name");
|
2015-06-23 18:35:34 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test if the database name is not too long
|
2015-09-30 12:03:46 +02:00
|
|
|
if (strlen(column_directory_name) >= OBIDMS_COLUMN_MAX_NAME)
|
2015-06-23 18:35:34 +02:00
|
|
|
{
|
|
|
|
obi_set_errno(OBICOLDIR_LONG_NAME_ERROR);
|
2015-09-30 12:03:46 +02:00
|
|
|
obidebug(1, "\nError due to column name too long");
|
2015-06-23 18:35:34 +02:00
|
|
|
free(column_directory_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return column_directory_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
int obi_column_directory_exists(OBIDMS_p dms, const char* column_name)
|
2015-06-23 18:35:34 +02:00
|
|
|
{
|
|
|
|
struct stat buffer;
|
|
|
|
char* column_directory_name;
|
|
|
|
char* full_path;
|
|
|
|
int check_dir;
|
|
|
|
|
|
|
|
// Build and check the directory name
|
2017-10-26 18:58:48 +02:00
|
|
|
column_directory_name = obi_build_column_directory_name(column_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (column_directory_name == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Get the full path for the column directory
|
2016-04-15 11:11:13 +02:00
|
|
|
full_path = obi_dms_get_full_path(dms, column_directory_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (full_path == NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError getting path for an OBIDMS directory");
|
2015-06-23 18:35:34 +02:00
|
|
|
free(column_directory_name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
check_dir = stat(full_path, &buffer);
|
|
|
|
|
|
|
|
free(column_directory_name);
|
|
|
|
free(full_path);
|
|
|
|
|
|
|
|
if(check_dir == 0)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
OBIDMS_column_directory_p obi_create_column_directory(OBIDMS_p dms, const char* column_name)
|
2015-06-23 18:35:34 +02:00
|
|
|
{
|
|
|
|
char* column_directory_name;
|
|
|
|
|
|
|
|
// Build and check the directory name
|
2017-10-26 18:58:48 +02:00
|
|
|
column_directory_name = obi_build_column_directory_name(column_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (column_directory_name == NULL)
|
|
|
|
{
|
|
|
|
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to create the directory
|
2015-11-09 15:06:02 +01:00
|
|
|
if (mkdirat(dms->dir_fd, column_directory_name, 00777) < 0)
|
2015-06-23 18:35:34 +02:00
|
|
|
{
|
|
|
|
if (errno == EEXIST)
|
|
|
|
obi_set_errno(OBICOLDIR_EXIST_ERROR);
|
|
|
|
else
|
|
|
|
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError creating a column directory");
|
2015-06-23 18:35:34 +02:00
|
|
|
free(column_directory_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(column_directory_name);
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
return obi_open_column_directory(dms, column_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
OBIDMS_column_directory_p obi_open_column_directory(OBIDMS_p dms, const char* column_name)
|
2015-06-23 18:35:34 +02:00
|
|
|
{
|
2015-06-26 17:53:03 +02:00
|
|
|
OBIDMS_column_directory_p column_directory;
|
2015-08-03 15:10:39 +02:00
|
|
|
char* column_directory_name;
|
|
|
|
DIR* directory;
|
2015-06-23 18:35:34 +02:00
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
column_directory = NULL;
|
2015-06-23 18:35:34 +02:00
|
|
|
|
|
|
|
// Build and check the directory name
|
2017-10-26 18:58:48 +02:00
|
|
|
column_directory_name = obi_build_column_directory_name(column_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (column_directory_name == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Try to open the column directory
|
2016-03-21 11:33:06 +01:00
|
|
|
directory = opendir_in_dms(dms, column_directory_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
if (directory == NULL) {
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case ENOENT:
|
|
|
|
obi_set_errno(OBICOLDIR_NOT_EXIST_ERROR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EACCES:
|
|
|
|
obi_set_errno(OBICOLDIR_ACCESS_ERROR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENOMEM:
|
|
|
|
obi_set_errno(OBICOLDIR_MEMORY_ERROR);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
|
|
|
|
}
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError opening a column directory");
|
2015-06-23 18:35:34 +02:00
|
|
|
free(column_directory_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate the column dir structure
|
2015-06-26 17:53:03 +02:00
|
|
|
column_directory = (OBIDMS_column_directory_p) malloc(sizeof(OBIDMS_column_directory_t));
|
|
|
|
if (column_directory == NULL)
|
2015-06-23 18:35:34 +02:00
|
|
|
{
|
|
|
|
obi_set_errno(OBICOLDIR_MEMORY_ERROR);
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError allocating the memory for an OBIDMS column directory structure");
|
2015-06-23 18:35:34 +02:00
|
|
|
free(column_directory_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the data structure
|
2019-09-04 13:16:28 +02:00
|
|
|
column_directory->dms = dms;
|
2015-06-26 17:53:03 +02:00
|
|
|
strcpy(column_directory->directory_name, column_directory_name);
|
|
|
|
strcpy(column_directory->column_name, column_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
|
2019-09-04 12:55:21 +02:00
|
|
|
if (closedir(directory) < 0)
|
2015-11-09 15:06:02 +01:00
|
|
|
{
|
|
|
|
obi_set_errno(OBICOLDIR_MEMORY_ERROR);
|
2019-09-04 12:55:21 +02:00
|
|
|
obidebug(1, "\nError closing a DIR after opening a column directory");
|
2015-11-09 15:06:02 +01:00
|
|
|
free(column_directory_name);
|
2019-09-04 12:55:21 +02:00
|
|
|
return NULL;
|
2015-11-09 15:06:02 +01:00
|
|
|
}
|
|
|
|
|
2015-06-23 18:35:34 +02:00
|
|
|
free(column_directory_name);
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
return column_directory;
|
2015-06-23 18:35:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
OBIDMS_column_directory_p obi_column_directory(OBIDMS_p dms, const char* column_name)
|
2015-06-23 18:35:34 +02:00
|
|
|
{
|
|
|
|
int exists;
|
2015-06-26 17:53:03 +02:00
|
|
|
exists = obi_column_directory_exists(dms, column_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
|
|
|
|
switch (exists)
|
|
|
|
{
|
|
|
|
case 0:
|
2015-06-26 17:53:03 +02:00
|
|
|
return obi_create_column_directory(dms, column_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
case 1:
|
2015-06-26 17:53:03 +02:00
|
|
|
return obi_open_column_directory(dms, column_name);
|
2015-06-23 18:35:34 +02:00
|
|
|
};
|
|
|
|
|
2015-08-03 15:10:39 +02:00
|
|
|
obidebug(1, "\nError checking if a column directory exists");
|
2015-06-23 18:35:34 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-26 17:53:03 +02:00
|
|
|
int obi_close_column_directory(OBIDMS_column_directory_p column_directory)
|
2015-06-23 18:35:34 +02:00
|
|
|
{
|
2015-06-26 17:53:03 +02:00
|
|
|
if (column_directory != NULL)
|
|
|
|
free(column_directory);
|
2015-06-23 18:35:34 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|