Commit b04b4b59 by Celine Mercier

made POSIX compliant

parent 383e738a
......@@ -354,7 +354,8 @@ static char* build_avl_file_name(const char* avl_name)
char* file_name;
// Build the file name
if (asprintf(&file_name,"%s.oda", avl_name) < 0)
file_name = (char*) malloc((strlen(avl_name) + 5)*sizeof(char));
if (sprintf(file_name,"%s.oda", avl_name) < 0)
{
obi_set_errno(OBI_AVL_ERROR);
obidebug(1, "\nError building an AVL tree file name");
......@@ -379,7 +380,8 @@ static char* build_avl_data_file_name(const char* avl_name)
char* file_name;
// Build the file name
if (asprintf(&file_name,"%s.odd", avl_name) < 0)
file_name = (char*) malloc((strlen(avl_name) + 5)*sizeof(char));
if (sprintf(file_name,"%s.odd", avl_name) < 0)
{
obi_set_errno(OBI_AVL_ERROR);
obidebug(1, "\nError building an AVL tree data file name");
......@@ -942,15 +944,20 @@ int obi_avl_exists(OBIDMS_p dms, const char* avl_name)
struct stat buffer;
char* avl_file_path;
char* avl_file_name;
char* avl_file_relative_path;
int relative_path_size;
int check_dir;
// Build file name
// Build the AVL tree file path
avl_file_name = build_avl_file_name(avl_name);
if (avl_file_name == NULL)
return -1;
// Build the AVL tree file path
avl_file_path = get_full_path(dms->avl_dir_fd, avl_file_name);
relative_path_size = strlen(avl_file_name) + strlen(AVL_TREES_DIR_NAME) + 2;
avl_file_relative_path = (char*) malloc(relative_path_size*sizeof(char));
strcpy(avl_file_relative_path, AVL_TREES_DIR_NAME);
strcat(avl_file_relative_path, "/");
strcat(avl_file_relative_path, avl_file_name);
avl_file_path = get_full_path(dms, avl_file_relative_path);
if (avl_file_path == NULL)
{
obidebug(1, "\nError getting the file path for an AVL tree file");
......@@ -961,6 +968,7 @@ int obi_avl_exists(OBIDMS_p dms, const char* avl_name)
free(avl_file_path);
free(avl_file_name);
free(avl_file_relative_path);
if (check_dir == 0)
return 1;
......
......@@ -105,7 +105,8 @@ static char* build_directory_name(const char* dms_name)
char* directory_name;
// Build the database directory name
if (asprintf(&directory_name, "%s.obidms", dms_name) < 0)
directory_name = (char*) malloc((strlen(dms_name) + 8)*sizeof(char));
if (sprintf(directory_name, "%s.obidms", dms_name) < 0)
{
obi_set_errno(OBIDMS_MEMORY_ERROR);
obidebug(1, "\nProblem building an OBIDMS directory name");
......@@ -130,7 +131,8 @@ static char* build_infos_file_name(const char* dms_name)
char* file_name;
// Build file name
if (asprintf(&file_name, "%s_infos", dms_name) < 0)
file_name = (char*) malloc((strlen(dms_name) + 7)*sizeof(char));
if (sprintf(file_name, "%s_infos", dms_name) < 0)
{
obi_set_errno(OBIDMS_MEMORY_ERROR);
obidebug(1, "\nProblem building an informations file name");
......@@ -391,7 +393,7 @@ OBIDMS_p obi_open_dms(const char* dms_name)
dms->little_endian = little_endian_dms;
// Open the AVL trees directory
dms->avl_directory = private_opendirat(dms->dir_fd, AVL_TREES_DIR_NAME);
dms->avl_directory = opendir_in_dms(dms, AVL_TREES_DIR_NAME);
if (dms->avl_directory == NULL)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
......
......@@ -379,8 +379,9 @@ OBIDMS_taxonomy_p obi_read_taxonomy(OBIDMS_p dms, const char* taxonomy_name, boo
buffer_size = 2048; // TODO
main_taxonomy_dir_path = get_full_path(dms->dir_fd, TAXONOMY_DIR_NAME);
if (asprintf(&taxonomy_path, "%s/%s/%s", main_taxonomy_dir_path, taxonomy_name, taxonomy_name) < 0)
main_taxonomy_dir_path = get_full_path(dms, TAXONOMY_DIR_NAME);
taxonomy_path = (char*) malloc((strlen(main_taxonomy_dir_path) + strlen(taxonomy_name) + strlen(taxonomy_name) + 3)*sizeof(char));
if (sprintf(taxonomy_path, "%s/%s/%s", main_taxonomy_dir_path, taxonomy_name, taxonomy_name) < 0)
{
free(main_taxonomy_dir_path);
obi_close_taxonomy(tax);
......
......@@ -156,9 +156,12 @@ static index_t get_line_count_per_page(OBIType_t data_type, index_t nb_elements_
static char* build_column_file_name(const char* column_name, obiversion_t version_number)
{
char* file_name;
int version_number_length;
// Build the file name
if (asprintf(&file_name,"%s@%d.odc", column_name, version_number) < 0)
version_number_length = (version_number == 0 ? 1 : (int)(log10(version_number)+1));
file_name = (char*) malloc((strlen(column_name) + version_number_length + 6)*sizeof(char)); // TODO check the mallocs...
if (sprintf(file_name,"%s@%d.odc", column_name, version_number) < 0)
{
obi_set_errno(OBICOL_MEMORY_ERROR);
obidebug(1, "\nError building a column file name");
......@@ -174,7 +177,8 @@ static char* build_version_file_name(const char* column_name)
char* file_name;
// Build the file name
if (asprintf(&file_name,"%s.odv", column_name) < 0)
file_name = (char*) malloc((strlen(column_name) + 5)*sizeof(char));
if (sprintf(file_name,"%s.odv", column_name) < 0)
{
obi_set_errno(OBICOL_MEMORY_ERROR);
obidebug(1, "\nError building a version file name");
......
......@@ -65,7 +65,8 @@ static char* build_column_directory_name(const char* column_name)
char* column_directory_name;
// Build the database directory name
if (asprintf(&column_directory_name, "%s.obicol", column_name) < 0)
column_directory_name = (char*) malloc((strlen(column_name) + 8)*sizeof(char));
if (sprintf(column_directory_name, "%s.obicol", column_name) < 0)
{
obi_set_errno(OBICOLDIR_MEMORY_ERROR);
obidebug(1, "\nError building a column directory name");
......@@ -104,7 +105,7 @@ int obi_column_directory_exists(OBIDMS_p dms, const char* column_name)
return -1;
// Get the full path for the column directory
full_path = get_full_path(dms->dir_fd, column_directory_name);
full_path = get_full_path(dms, column_directory_name);
if (full_path == NULL)
{
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
......@@ -169,7 +170,7 @@ OBIDMS_column_directory_p obi_open_column_directory(OBIDMS_p dms, const char* co
return NULL;
// Try to open the column directory
directory = private_opendirat(dms->dir_fd, column_directory_name);
directory = opendir_in_dms(dms, column_directory_name);
if (directory == NULL) {
switch (errno)
{
......
......@@ -84,7 +84,8 @@ static char* build_obiview_file_name()
char* file_name;
// Build file name
if (asprintf(&file_name, OBIVIEW_FILE_NAME) < 0)
file_name = (char*) malloc((strlen(OBIVIEW_FILE_NAME) + 1)*sizeof(char));
if (sprintf(file_name, OBIVIEW_FILE_NAME) < 0)
{
obi_set_errno(OBIVIEW_ERROR);
obidebug(1, "\nProblem building an obiview file name");
......@@ -984,7 +985,7 @@ int obi_save_view(Obiview_p view)
return -1;
// Get the full path for the column directory
full_path = get_full_path((view->dms)->dir_fd, view_file_name);
full_path = get_full_path(view->dms, view_file_name);
if (full_path == NULL)
{
obi_set_errno(OBIVIEW_ERROR);
......
......@@ -17,16 +17,18 @@
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include "private_at_functions.h"
#include "obidebug.h"
#include "obierrno.h"
#include "obidms.h"
#define DEBUG_LEVEL 0 // TODO has to be defined somewhere else (cython compil flag?)
char* get_full_path(int directory_file_descriptor, const char* path_name)
char* get_full_path(OBIDMS_p dms, const char* path_name)
{
char* full_path;
......@@ -37,26 +39,28 @@ char* get_full_path(int directory_file_descriptor, const char* path_name)
return NULL;
}
if (fcntl(directory_file_descriptor, F_GETPATH, full_path) < 0)
if (getcwd(full_path, MAX_PATH_LEN) == NULL)
{
obidebug(1, "\nError getting the path to a file or directory");
return NULL;
}
// TODO check errors?
strlcat(full_path, "/", MAX_PATH_LEN);
strlcat(full_path, path_name, MAX_PATH_LEN);
strcat(full_path, "/");
strcat(full_path, dms->directory_name);
strcat(full_path, "/");
strcat(full_path, path_name);
return full_path;
}
DIR* private_opendirat(int directory_file_descriptor, const char* path_name)
DIR* opendir_in_dms(OBIDMS_p dms, const char* path_name)
{
char* full_path;
DIR* directory;
full_path = get_full_path(directory_file_descriptor, path_name);
full_path = get_full_path(dms, path_name);
if (full_path == NULL)
return NULL;
......
......@@ -16,6 +16,7 @@
#include <sys/stat.h>
#include "obidms.h"
#define MAX_PATH_LEN 4096 /**< Maximum length for the character string defining a
file or directory path */
......@@ -37,7 +38,7 @@
* @since June 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
char* get_full_path(int directory_file_descriptor, const char* path_name);
char* get_full_path(OBIDMS_p dms, const char* path_name);
/**
......@@ -52,7 +53,7 @@ char* get_full_path(int directory_file_descriptor, const char* path_name);
* @since June 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
DIR* private_opendirat(int directory_file_descriptor, const char* path_name);
DIR* opendir_in_dms(OBIDMS_p dms, const char* path_name);
#endif /* PRIVATEOPENAT_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment