File descriptors for dms, column and array directories are now stored in

structures.
This commit is contained in:
Celine Mercier
2015-11-09 15:06:02 +01:00
parent 05e3956a0c
commit b37bd8f21c
7 changed files with 134 additions and 307 deletions

View File

@ -282,38 +282,24 @@ int close_array_data(OBIDMS_array_data_p array_data)
int grow_array(OBIDMS_array_p array) // TODO Lock when needed int grow_array(OBIDMS_array_p array) // TODO Lock when needed
{ {
size_t file_size; size_t file_size;
size_t old_data_size; size_t old_data_size;
size_t new_data_size; size_t new_data_size;
size_t header_size; size_t header_size;
int array_dir_file_descriptor; int array_file_descriptor;
int array_file_descriptor; char* array_file_name;
char* array_file_name;
// Get the file descriptor associated to the array directory
array_dir_file_descriptor = dirfd(array->directory);
if (array_dir_file_descriptor < 0)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError getting the file descriptor for an array directory");
return -1;
}
// Get the array file name // Get the array file name
array_file_name = build_array_file_name((array->header)->array_name); array_file_name = build_array_file_name((array->header)->array_name);
if (array_file_name == NULL) if (array_file_name == NULL)
{
close(array_dir_file_descriptor);
return -1; return -1;
}
// Open the array file // Open the array file
array_file_descriptor = openat(array_dir_file_descriptor, array_file_name, O_RDWR); array_file_descriptor = openat(array->dir_fd, array_file_name, O_RDWR);
if (array_file_descriptor < 0) if (array_file_descriptor < 0)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError opening an array file"); obidebug(1, "\nError opening an array file");
close(array_dir_file_descriptor);
free(array_file_name); free(array_file_name);
return -1; return -1;
} }
@ -330,7 +316,6 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError enlarging an array file"); obidebug(1, "\nError enlarging an array file");
close(array_dir_file_descriptor);
close(array_file_descriptor); close(array_file_descriptor);
return -1; return -1;
} }
@ -341,7 +326,6 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError munmapping the array of an array file before enlarging"); obidebug(1, "\nError munmapping the array of an array file before enlarging");
close(array_dir_file_descriptor);
close(array_file_descriptor); close(array_file_descriptor);
return -1; return -1;
} }
@ -358,7 +342,6 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError re-mmapping the array of an array file after enlarging the file"); obidebug(1, "\nError re-mmapping the array of an array file after enlarging the file");
close(array_dir_file_descriptor);
close(array_file_descriptor); close(array_file_descriptor);
return -1; return -1;
} }
@ -380,38 +363,24 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed
int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed
{ {
size_t file_size; size_t file_size;
index_t old_data_size; index_t old_data_size;
index_t new_data_size; index_t new_data_size;
size_t header_size; size_t header_size;
int array_dir_file_descriptor; int array_data_file_descriptor;
int array_data_file_descriptor; char* array_data_file_name;
char* array_data_file_name;
// Get the file descriptor associated to the array directory
array_dir_file_descriptor = dirfd(array->directory);
if (array_dir_file_descriptor < 0)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError getting the file descriptor for an array directory");
return -1;
}
// Get the array data file name // Get the array data file name
array_data_file_name = build_array_data_file_name((array->header)->array_name); array_data_file_name = build_array_data_file_name((array->header)->array_name);
if (array_data_file_name == NULL) if (array_data_file_name == NULL)
{
close(array_dir_file_descriptor);
return -1; return -1;
}
// Open the array data file // Open the array data file
array_data_file_descriptor = openat(array_dir_file_descriptor, array_data_file_name, O_RDWR); array_data_file_descriptor = openat(array->dir_fd, array_data_file_name, O_RDWR);
if (array_data_file_descriptor < 0) if (array_data_file_descriptor < 0)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError opening an array data file"); obidebug(1, "\nError opening an array data file");
close(array_dir_file_descriptor);
free(array_data_file_name); free(array_data_file_name);
return -1; return -1;
} }
@ -428,7 +397,6 @@ int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError enlarging an array data file"); obidebug(1, "\nError enlarging an array data file");
close(array_dir_file_descriptor);
close(array_data_file_descriptor); close(array_data_file_descriptor);
return -1; return -1;
} }
@ -439,7 +407,6 @@ int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError munmapping the data of an array data file before enlarging"); obidebug(1, "\nError munmapping the data of an array data file before enlarging");
close(array_dir_file_descriptor);
close(array_data_file_descriptor); close(array_data_file_descriptor);
return -1; return -1;
} }
@ -456,7 +423,6 @@ int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError re-mmapping the data of an array data file after enlarging the file"); obidebug(1, "\nError re-mmapping the data of an array data file after enlarging the file");
close(array_dir_file_descriptor);
close(array_data_file_descriptor); close(array_data_file_descriptor);
return -1; return -1;
} }
@ -475,7 +441,7 @@ int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed
int array_compare(byte_t* value_1, byte_t* value_2) int array_compare(byte_t* value_1, byte_t* value_2)
{ {
int comp; int comp;
uint8_t size_1; uint8_t size_1;
uint8_t size_2; uint8_t size_2;
int32_t len_1; int32_t len_1;
@ -521,31 +487,18 @@ size_t array_sizeof(byte_t* value)
int obi_array_exists(OBIDMS_p dms, const char* array_name) int obi_array_exists(OBIDMS_p dms, const char* array_name)
{ {
struct stat buffer; struct stat buffer;
char* array_file_path; char* array_file_path;
char* array_file_name; char* array_file_name;
int check_dir; int check_dir;
int array_dir_file_descriptor;
// Get the file descriptor of the array directory
array_dir_file_descriptor = dirfd(dms->array_directory);
if (array_dir_file_descriptor < 0)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError getting the file descriptor for an array directory");
return -1;
}
// Build file name // Build file name
array_file_name = build_array_file_name(array_name); array_file_name = build_array_file_name(array_name);
if (array_file_name == NULL) if (array_file_name == NULL)
{
close(array_dir_file_descriptor);
return -1; return -1;
}
// Build the array file path // Build the array file path
array_file_path = get_full_path(array_dir_file_descriptor, array_file_name); array_file_path = get_full_path(dms->array_dir_fd, array_file_name);
if (array_file_path == NULL) if (array_file_path == NULL)
{ {
obidebug(1, "\nError getting the file path for an array file"); obidebug(1, "\nError getting the file path for an array file");
@ -585,8 +538,8 @@ OBIDMS_array_p obi_array(OBIDMS_p dms, const char* array_name)
OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name) OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
{ {
char* array_file_name; char* array_file_name;
char* array_data_file_name; char* array_data_file_name;
size_t header_size; size_t header_size;
size_t data_size; size_t data_size;
size_t file_size; size_t file_size;
@ -598,22 +551,13 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
// Create the data file // Create the data file
// Get the file descriptor of the array directory
array_dir_file_descriptor = dirfd(dms->array_directory);
if (array_dir_file_descriptor < 0)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError getting the file descriptor for an array directory");
return NULL;
}
// Build file name // Build file name
array_data_file_name = build_array_data_file_name(array_name); array_data_file_name = build_array_data_file_name(array_name);
if (array_data_file_name == NULL) if (array_data_file_name == NULL)
{
close(array_dir_file_descriptor);
return NULL; return NULL;
}
// Get the file descriptor of the array directory
array_dir_file_descriptor = dms->array_dir_fd;
// Create file // Create file
array_data_file_descriptor = openat(array_dir_file_descriptor, array_data_file_name, O_RDWR | O_CREAT | O_EXCL, 0777); array_data_file_descriptor = openat(array_dir_file_descriptor, array_data_file_name, O_RDWR | O_CREAT | O_EXCL, 0777);
@ -621,7 +565,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError creating an array data file"); obidebug(1, "\nError creating an array data file");
close(array_dir_file_descriptor);
free(array_data_file_name); free(array_data_file_name);
return NULL; return NULL;
} }
@ -637,7 +580,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError truncating an array data file to the right size"); obidebug(1, "\nError truncating an array data file to the right size");
close(array_dir_file_descriptor);
close(array_data_file_descriptor); close(array_data_file_descriptor);
return NULL; return NULL;
} }
@ -648,7 +590,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError allocating the memory for the array data structure"); obidebug(1, "\nError allocating the memory for the array data structure");
close(array_dir_file_descriptor);
close(array_data_file_descriptor); close(array_data_file_descriptor);
return NULL; return NULL;
} }
@ -665,7 +606,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError mmapping the header of an array data file"); obidebug(1, "\nError mmapping the header of an array data file");
close(array_dir_file_descriptor);
close(array_data_file_descriptor); close(array_data_file_descriptor);
free(array_data); free(array_data);
return NULL; return NULL;
@ -683,7 +623,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError mmapping the data of an array data file"); obidebug(1, "\nError mmapping the data of an array data file");
munmap(array_data->header, header_size); munmap(array_data->header, header_size);
close(array_dir_file_descriptor);
close(array_data_file_descriptor); close(array_data_file_descriptor);
free(array_data); free(array_data);
return NULL; return NULL;
@ -708,7 +647,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
array_file_name = build_array_file_name(array_name); array_file_name = build_array_file_name(array_name);
if (array_file_name == NULL) if (array_file_name == NULL)
{ {
close(array_dir_file_descriptor);
close_array_data(array_data); close_array_data(array_data);
return NULL; return NULL;
} }
@ -724,7 +662,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError creating an array file"); obidebug(1, "\nError creating an array file");
close(array_dir_file_descriptor);
close_array_data(array_data); close_array_data(array_data);
free(array_file_name); free(array_file_name);
return NULL; return NULL;
@ -736,7 +673,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError truncating an array file to the right size"); obidebug(1, "\nError truncating an array file to the right size");
close(array_dir_file_descriptor);
close_array_data(array_data); close_array_data(array_data);
close(array_file_descriptor); close(array_file_descriptor);
return NULL; return NULL;
@ -748,7 +684,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError allocating the memory for the array structure"); obidebug(1, "\nError allocating the memory for the array structure");
close(array_dir_file_descriptor);
close_array_data(array_data); close_array_data(array_data);
close(array_file_descriptor); close(array_file_descriptor);
return NULL; return NULL;
@ -766,7 +701,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError mmapping the header of an array file"); obidebug(1, "\nError mmapping the header of an array file");
close(array_dir_file_descriptor);
close_array_data(array_data); close_array_data(array_data);
close(array_file_descriptor); close(array_file_descriptor);
free(array); free(array);
@ -784,7 +718,6 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError mmapping the data of an array file"); obidebug(1, "\nError mmapping the data of an array file");
close(array_dir_file_descriptor);
close_array_data(array_data); close_array_data(array_data);
munmap(array->header, header_size); munmap(array->header, header_size);
close(array_file_descriptor); close(array_file_descriptor);
@ -794,6 +727,7 @@ OBIDMS_array_p obi_create_array(OBIDMS_p dms, const char* array_name)
array->data = array_data; array->data = array_data;
array->directory = dms->array_directory; array->directory = dms->array_directory;
array->dir_fd = array_dir_file_descriptor;
(array->header)->header_size = header_size; (array->header)->header_size = header_size;
(array->header)->array_size = data_size; (array->header)->array_size = data_size;
@ -825,21 +759,12 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
// Open the data file // Open the data file
// Get the file descriptor of the array directory // Get the file descriptor of the array directory
array_dir_file_descriptor = dirfd(dms->array_directory); array_dir_file_descriptor = dms->array_dir_fd;
if (array_dir_file_descriptor < 0)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError getting the file descriptor for an array directory");
return NULL;
}
// Build file name // Build file name
array_data_file_name = build_array_data_file_name(array_name); array_data_file_name = build_array_data_file_name(array_name);
if (array_data_file_name == NULL) if (array_data_file_name == NULL)
{
close(array_dir_file_descriptor);
return NULL; return NULL;
}
// Open file // Open file
array_data_file_descriptor = openat(array_dir_file_descriptor, array_data_file_name, O_RDWR, 0777); array_data_file_descriptor = openat(array_dir_file_descriptor, array_data_file_name, O_RDWR, 0777);
@ -847,7 +772,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError opening an array data file"); obidebug(1, "\nError opening an array data file");
close(array_dir_file_descriptor);
free(array_data_file_name); free(array_data_file_name);
return NULL; return NULL;
} }
@ -859,7 +783,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError allocating the memory for the array data structure"); obidebug(1, "\nError allocating the memory for the array data structure");
close(array_dir_file_descriptor);
close(array_data_file_descriptor); close(array_data_file_descriptor);
return NULL; return NULL;
} }
@ -877,7 +800,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError mmapping the header of an array data file"); obidebug(1, "\nError mmapping the header of an array data file");
close(array_dir_file_descriptor);
close(array_data_file_descriptor); close(array_data_file_descriptor);
free(array_data); free(array_data);
return NULL; return NULL;
@ -894,7 +816,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError mmapping the data of an array data file"); obidebug(1, "\nError mmapping the data of an array data file");
close(array_dir_file_descriptor);
munmap(array_data->header, header_size); munmap(array_data->header, header_size);
close(array_data_file_descriptor); close(array_data_file_descriptor);
free(array_data); free(array_data);
@ -910,7 +831,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
array_file_name = build_array_file_name(array_name); array_file_name = build_array_file_name(array_name);
if (array_file_name == NULL) if (array_file_name == NULL)
{ {
close(array_dir_file_descriptor);
close_array_data(array_data); close_array_data(array_data);
return NULL; return NULL;
} }
@ -921,7 +841,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError opening an array file"); obidebug(1, "\nError opening an array file");
close(array_dir_file_descriptor);
close_array_data(array_data); close_array_data(array_data);
free(array_file_name); free(array_file_name);
return NULL; return NULL;
@ -934,7 +853,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError allocating the memory for the array structure"); obidebug(1, "\nError allocating the memory for the array structure");
close(array_dir_file_descriptor);
close_array_data(array_data); close_array_data(array_data);
close(array_file_descriptor); close(array_file_descriptor);
return NULL; return NULL;
@ -953,7 +871,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError mmapping the header of an array file"); obidebug(1, "\nError mmapping the header of an array file");
close(array_dir_file_descriptor);
close_array_data(array_data); close_array_data(array_data);
close(array_file_descriptor); close(array_file_descriptor);
free(array); free(array);
@ -971,7 +888,6 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
{ {
obi_set_errno(OBI_ARRAY_ERROR); obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError mmapping the data of an array file"); obidebug(1, "\nError mmapping the data of an array file");
close(array_dir_file_descriptor);
close_array_data(array_data); close_array_data(array_data);
munmap(array->header, header_size); munmap(array->header, header_size);
close(array_file_descriptor); close(array_file_descriptor);
@ -981,6 +897,7 @@ OBIDMS_array_p obi_open_array(OBIDMS_p dms, const char* array_name)
array->data = array_data; array->data = array_data;
array->directory = dms->array_directory; array->directory = dms->array_directory;
array->dir_fd = array_dir_file_descriptor;
close(array_file_descriptor); close(array_file_descriptor);
@ -1090,15 +1007,15 @@ byte_t* obi_array_get(OBIDMS_array_p array, index_t idx)
index_t obi_array_search(OBIDMS_array_p array, byte_t* value) index_t obi_array_search(OBIDMS_array_p array, byte_t* value)
{ {
index_t idx_min; index_t idx_min;
index_t idx_max; index_t idx_max;
index_t idx_mid; index_t idx_mid;
index_t idx; index_t idx;
index_t* a; index_t* a;
byte_t* data; byte_t* data;
byte_t* to_compare; byte_t* to_compare;
index_t nb_items; index_t nb_items;
int comp; int comp;
nb_items = (array->header)->nb_items; nb_items = (array->header)->nb_items;
data = (array->data)->data; data = (array->data)->data;

View File

@ -101,6 +101,9 @@ typedef struct OBIDMS_array {
DIR* directory; /**< A directory entry usable to DIR* directory; /**< A directory entry usable to
* refer and scan the array directory. * refer and scan the array directory.
*/ */
int dir_fd; /**< The file descriptor of the directory entry
* usable to refer and scan the array directory.
*/
} OBIDMS_array_t, *OBIDMS_array_p; } OBIDMS_array_t, *OBIDMS_array_p;

View File

@ -164,7 +164,6 @@ OBIDMS_p obi_create_dms(const char* dms_name)
return NULL; return NULL;
} }
return obi_open_dms(dms_name); return obi_open_dms(dms_name);
} }
@ -173,19 +172,32 @@ OBIDMS_p obi_open_dms(const char* dms_name)
{ {
OBIDMS_p dms; OBIDMS_p dms;
char* directory_name; char* directory_name;
DIR* directory;
int dms_file_descriptor;
dms = NULL; dms = NULL;
// 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;
}
// Build and check the directory name // Build and check the directory name
directory_name = build_directory_name(dms_name); directory_name = build_directory_name(dms_name);
if (directory_name == NULL) if (directory_name == NULL)
{
free(dms);
return NULL; return NULL;
}
strncpy(dms->directory_name, directory_name, OBIDMS_MAX_NAME);
free(directory_name);
// Try to open the directory // Try to open the directory
directory = opendir(directory_name); dms->directory = opendir(dms->directory_name);
if (directory == NULL) if (dms->directory == NULL)
{ {
switch (errno) switch (errno)
{ {
@ -205,45 +217,43 @@ OBIDMS_p obi_open_dms(const char* dms_name)
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
} }
obidebug(1, "\nCan't open OBIDMS directory"); obidebug(1, "\nCan't open OBIDMS directory");
free(directory_name); free(dms);
return NULL; return NULL;
} }
// 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");
free(directory_name);
return NULL;
}
// Initialize the data structure
strcpy(dms->directory_name, directory_name);
dms->directory = directory;
// Get file descriptor of DMS directory to open the arrays directory // Get file descriptor of DMS directory to open the arrays directory
dms_file_descriptor = dirfd(directory); dms->dir_fd = dirfd(dms->directory);
if (dms_file_descriptor < 0) if (dms->dir_fd < 0)
{ {
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obidebug(1, "\nError getting the file descriptor for a newly created OBIDMS directory"); obidebug(1, "\nError getting the file descriptor for a newly created OBIDMS directory");
free(directory_name); closedir(dms->directory);
free(dms);
return NULL; return NULL;
} }
// Open the arrays directory // Open the arrays directory
dms->array_directory = private_opendirat(dms_file_descriptor, ARRAY_DIR_NAME); dms->array_directory = private_opendirat(dms->dir_fd, ARRAY_DIR_NAME);
if (dms->array_directory == NULL) if (dms->array_directory == NULL)
{ {
obi_set_errno(OBIDMS_UNKNOWN_ERROR); obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obidebug(1, "\nError opening the arrays directory"); obidebug(1, "\nError opening the arrays directory");
free(directory_name); closedir(dms->directory);
free(dms);
return NULL; return NULL;
} }
free(directory_name); // Store the array directory's file descriptor
dms->array_dir_fd = dirfd(dms->array_directory);
if (dms->array_dir_fd < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obidebug(1, "\nError opening the arrays directory");
closedir(dms->directory);
closedir(dms->array_directory);
free(dms);
return NULL;
}
return dms; return dms;
} }

View File

@ -43,9 +43,15 @@ typedef struct OBIDMS {
DIR* directory; /**< A directory entry usable to DIR* directory; /**< A directory entry usable to
* refer and scan the database directory. * refer and scan the database directory.
*/ */
int dir_fd; /**< The file descriptor of the directory entry
* usable to refer and scan the database directory.
*/
DIR* array_directory; /**< A directory entry usable to DIR* array_directory; /**< A directory entry usable to
* refer and scan the array directory. * refer and scan the array directory.
*/ */
int array_dir_fd; /**< The file descriptor of the directory entry
* usable to refer and scan the array directory.
*/
} OBIDMS_t, *OBIDMS_p; } OBIDMS_t, *OBIDMS_p;

View File

@ -190,7 +190,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_
off_t loc_size; off_t loc_size;
obiversion_t new_version_number; obiversion_t new_version_number;
char* version_file_name; char* version_file_name;
int column_dir_file_descriptor;
int version_file_descriptor; int version_file_descriptor;
bool little_endian; bool little_endian;
int lock_mode; int lock_mode;
@ -209,18 +208,8 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_
if (version_file_name == NULL) if (version_file_name == NULL)
return -1; 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(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError getting the file descriptor for a column file directory");
free(version_file_name);
return -1;
}
// Open the version file // Open the version file
version_file_descriptor = openat(column_dir_file_descriptor, version_file_name, O_RDWR); version_file_descriptor = openat(column_directory->dir_fd, version_file_name, O_RDWR);
if (version_file_descriptor < 0) if (version_file_descriptor < 0)
{ {
if (errno == ENOENT) if (errno == ENOENT)
@ -338,7 +327,6 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo
off_t loc_size; off_t loc_size;
obiversion_t version_number; obiversion_t version_number;
char* version_file_name; char* version_file_name;
int column_dir_file_descriptor;
int version_file_descriptor; int version_file_descriptor;
bool little_endian; bool little_endian;
@ -349,18 +337,8 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo
if (version_file_name == NULL) if (version_file_name == NULL)
return -1; 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(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError getting the file descriptor for a column directory");
free(version_file_name);
return -1;
}
// Get the file descriptor associated to the version file // Get the file descriptor associated to the version file
version_file_descriptor = openat(column_dir_file_descriptor, version_file_name, O_RDWR | O_CREAT, 0777); version_file_descriptor = openat(column_directory->dir_fd, version_file_name, O_RDWR | O_CREAT, 0777);
if (version_file_descriptor < 0) if (version_file_descriptor < 0)
{ {
obi_set_errno(OBICOL_UNKNOWN_ERROR); obi_set_errno(OBICOL_UNKNOWN_ERROR);
@ -465,7 +443,6 @@ obiversion_t obi_get_latest_version_number(OBIDMS_column_directory_p column_dire
off_t loc_size; off_t loc_size;
obiversion_t latest_version_number; obiversion_t latest_version_number;
char * version_file_name; char * version_file_name;
int column_dir_file_descriptor;
int version_file_descriptor; int version_file_descriptor;
bool little_endian; bool little_endian;
@ -476,18 +453,8 @@ obiversion_t obi_get_latest_version_number(OBIDMS_column_directory_p column_dire
if (version_file_name==NULL) if (version_file_name==NULL)
return -1; 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(OBICOL_UNKNOWN_ERROR);
obidebug(1, "\nError getting the file descriptor for a column directory");
free(version_file_name);
return -1;
}
// Get the file descriptor associated to the version file // Get the file descriptor associated to the version file
version_file_descriptor = openat(column_dir_file_descriptor, version_file_name, O_RDONLY); version_file_descriptor = openat(column_directory->dir_fd, version_file_name, O_RDONLY);
if (version_file_descriptor < 0) if (version_file_descriptor < 0)
{ {
obi_set_errno(OBICOL_UNKNOWN_ERROR); obi_set_errno(OBICOL_UNKNOWN_ERROR);
@ -596,7 +563,6 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
obiversion_t version_number; obiversion_t version_number;
char* column_file_name; char* column_file_name;
int column_file_descriptor; int column_file_descriptor;
int column_dir_file_descriptor;
size_t header_size; size_t header_size;
size_t data_size; size_t data_size;
index_t minimum_line_count; index_t minimum_line_count;
@ -674,15 +640,6 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
return NULL; 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);
obidebug(1, "\nError getting the file descriptor for a column directory");
return NULL;
}
// Calculate the size needed // Calculate the size needed
header_size = obi_get_platform_header_size(); header_size = obi_get_platform_header_size();
data_size = obi_array_sizeof(data_type, nb_lines, nb_elements_per_line); data_size = obi_array_sizeof(data_type, nb_lines, nb_elements_per_line);
@ -703,7 +660,7 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
} }
// Open the column file // Open the column file
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR | O_CREAT, 0777); column_file_descriptor = openat(column_directory->dir_fd, column_file_name, O_RDWR | O_CREAT, 0777);
if (column_file_descriptor < 0) if (column_file_descriptor < 0)
{ {
obi_set_errno(OBICOL_UNKNOWN_ERROR); obi_set_errno(OBICOL_UNKNOWN_ERROR);
@ -823,7 +780,6 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms,
OBIDMS_array_p array; OBIDMS_array_p array;
char* column_file_name; char* column_file_name;
int column_file_descriptor; int column_file_descriptor;
int column_dir_file_descriptor;
size_t header_size; size_t header_size;
size_t data_size; size_t data_size;
@ -837,15 +793,6 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms,
return NULL; 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);
obidebug(1, "\nError getting the file descriptor for a column directory");
return NULL;
}
// Calculate the header size // Calculate the header size
header_size = obi_get_platform_header_size(); header_size = obi_get_platform_header_size();
@ -868,7 +815,7 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms,
} }
// Open the column file, ALWAYS READ-ONLY // Open the column file, ALWAYS READ-ONLY
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDONLY); column_file_descriptor = openat(column_directory->dir_fd, column_file_name, O_RDONLY);
if (column_file_descriptor < 0) if (column_file_descriptor < 0)
{ {
obi_set_errno(OBICOL_UNKNOWN_ERROR); obi_set_errno(OBICOL_UNKNOWN_ERROR);
@ -909,8 +856,6 @@ OBIDMS_column_p obi_open_column(OBIDMS_p dms,
return NULL; return NULL;
} }
// TODO Check endianness?
// Compute data size from the informations in the header // Compute data size from the informations in the header
data_size = obi_array_sizeof((column->header)->data_type, (column->header)->line_count, (column->header)->nb_elements_per_line); data_size = obi_array_sizeof((column->header)->data_type, (column->header)->line_count, (column->header)->nb_elements_per_line);
@ -1045,7 +990,7 @@ int obi_close_column(OBIDMS_column_p column)
return -1; return -1;
} }
obi_close_column_directory(column->column_directory); obi_close_column_directory(column->column_directory); // TODO or not
free(column); free(column);
@ -1055,13 +1000,12 @@ int obi_close_column(OBIDMS_column_p column)
int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it necessary to unmap/remap? int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it necessary to unmap/remap?
{ {
size_t file_size; size_t file_size;
size_t data_size; size_t data_size;
index_t new_line_count; index_t new_line_count;
double multiple; double multiple;
int column_dir_file_descriptor; int column_file_descriptor;
int column_file_descriptor; char* column_file_name;
char* column_file_name;
// Compute the new line count = the number of lines used rounded to the nearest greater multiple of page size greater than 0 // Compute the new line count = the number of lines used rounded to the nearest greater multiple of page size greater than 0
multiple = ceil((double) (ONE_IF_ZERO((column->header)->lines_used) * (column->header)->nb_elements_per_line * obi_sizeof((column->header)->data_type)) / (double) getpagesize()); multiple = ceil((double) (ONE_IF_ZERO((column->header)->lines_used) * (column->header)->nb_elements_per_line * obi_sizeof((column->header)->data_type)) / (double) getpagesize());
@ -1071,15 +1015,6 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece
if ((column->header)->line_count == new_line_count) if ((column->header)->line_count == new_line_count)
return 0; return 0;
// 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 // Get the column file name
column_file_name = build_column_file_name((column->header)->name, (column->header)->version); column_file_name = build_column_file_name((column->header)->name, (column->header)->version);
if (column_file_name == NULL) if (column_file_name == NULL)
@ -1088,7 +1023,7 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece
} }
// Open the column file // Open the column file
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR); column_file_descriptor = openat((column->column_directory)->dir_fd, column_file_name, O_RDWR);
if (column_file_descriptor < 0) if (column_file_descriptor < 0)
{ {
obi_set_errno(OBICOL_UNKNOWN_ERROR); obi_set_errno(OBICOL_UNKNOWN_ERROR);
@ -1121,13 +1056,13 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece
} }
// Remap the data // Remap the data
column->data = mmap(NULL, column->data = mmap(NULL,
data_size, data_size,
PROT_READ | PROT_WRITE, PROT_READ | PROT_WRITE,
MAP_SHARED, MAP_SHARED,
column_file_descriptor, column_file_descriptor,
(column->header)->header_size (column->header)->header_size
); );
if (column->data == MAP_FAILED) if (column->data == MAP_FAILED)
{ {
@ -1148,25 +1083,15 @@ int obi_truncate_column_to_lines_used(OBIDMS_column_p column) // TODO is it nece
int obi_enlarge_column(OBIDMS_column_p column) int obi_enlarge_column(OBIDMS_column_p column)
{ {
size_t file_size; size_t file_size;
size_t old_data_size; size_t old_data_size;
size_t new_data_size; size_t new_data_size;
size_t header_size; size_t header_size;
index_t old_line_count; index_t old_line_count;
index_t new_line_count; index_t new_line_count;
int column_dir_file_descriptor; int column_file_descriptor;
int column_file_descriptor; char* column_file_name;
char* column_file_name; void* new_data;
void* new_data;
// 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 // Get the column file name
column_file_name = build_column_file_name((column->header)->name, (column->header)->version); column_file_name = build_column_file_name((column->header)->name, (column->header)->version);
@ -1176,7 +1101,7 @@ int obi_enlarge_column(OBIDMS_column_p column)
} }
// Open the column file // Open the column file
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDWR); column_file_descriptor = openat((column->column_directory)->dir_fd, column_file_name, O_RDWR);
if (column_file_descriptor < 0) if (column_file_descriptor < 0)
{ {
obi_set_errno(OBICOL_UNKNOWN_ERROR); obi_set_errno(OBICOL_UNKNOWN_ERROR);
@ -1336,7 +1261,6 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char*
OBIDMS_column_directory_p column_directory; OBIDMS_column_directory_p column_directory;
char* column_file_name; char* column_file_name;
int column_file_descriptor; int column_file_descriptor;
int column_dir_file_descriptor;
size_t header_size; size_t header_size;
obiversion_t version_number; obiversion_t version_number;
@ -1348,16 +1272,6 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char*
return NULL; 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);
obidebug(1, "\nError getting the file descriptor of a column directory");
obi_close_column_directory(column_directory);
return NULL;
}
// Calculate the header size // Calculate the header size
header_size = obi_get_platform_header_size(); // TODO read in header itself header_size = obi_get_platform_header_size(); // TODO read in header itself
@ -1377,7 +1291,7 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char*
} }
// Open the column file (READ-ONLY) // Open the column file (READ-ONLY)
column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDONLY); column_file_descriptor = openat(column_directory->dir_fd, column_file_name, O_RDONLY);
if (column_file_descriptor < 0) if (column_file_descriptor < 0)
{ {
obidebug(1, "\nError opening a column file"); obidebug(1, "\nError opening a column file");
@ -1405,8 +1319,6 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char*
return NULL; return NULL;
} }
// TODO Check endianness?
close(column_file_descriptor); close(column_file_descriptor);
return header; return header;
@ -1428,8 +1340,8 @@ int obi_unmap_header(OBIDMS_column_header_p header)
// TODO to be rewritten in an optimized and safe way // TODO to be rewritten in an optimized and safe way
index_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char* element_name) index_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char* element_name)
{ {
char* elements_names; char* elements_names;
char* name; char* name;
index_t element_index; index_t element_index;
elements_names = strdup((column->header)->elements_names); elements_names = strdup((column->header)->elements_names);
@ -1468,8 +1380,8 @@ index_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const cha
char* obi_column_format_date(time_t date) char* obi_column_format_date(time_t date)
{ {
char* formatted_time; char* formatted_time;
struct tm* tmp; struct tm* tmp;
formatted_time = (char*) malloc(FORMATTED_TIME_LENGTH*sizeof(char)); formatted_time = (char*) malloc(FORMATTED_TIME_LENGTH*sizeof(char));

View File

@ -97,25 +97,14 @@ int obi_column_directory_exists(OBIDMS_p dms, const char* column_name)
char* column_directory_name; char* column_directory_name;
char* full_path; char* full_path;
int check_dir; int check_dir;
int dms_file_descriptor;
// Build and check the directory name // Build and check the directory name
column_directory_name = build_column_directory_name(column_name); column_directory_name = build_column_directory_name(column_name);
if (column_directory_name == NULL) if (column_directory_name == NULL)
return -1; return -1;
// Get the file descriptor for the dms
dms_file_descriptor = dirfd(dms->directory);
if (dms_file_descriptor < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obidebug(1, "\nError getting a file descriptor for an OBIDMS directory");
free(column_directory_name);
return -1;
}
// Get the full path for the column directory // Get the full path for the column directory
full_path = get_full_path(dms_file_descriptor, column_directory_name); full_path = get_full_path(dms->dir_fd, column_directory_name);
if (full_path == NULL) if (full_path == NULL)
{ {
obi_set_errno(OBICOLDIR_UNKNOWN_ERROR); obi_set_errno(OBICOLDIR_UNKNOWN_ERROR);
@ -139,7 +128,6 @@ int obi_column_directory_exists(OBIDMS_p dms, const char* column_name)
OBIDMS_column_directory_p obi_create_column_directory(OBIDMS_p dms, const char* column_name) OBIDMS_column_directory_p obi_create_column_directory(OBIDMS_p dms, const char* column_name)
{ {
char* column_directory_name; char* column_directory_name;
int dms_file_descriptor;
// Build and check the directory name // Build and check the directory name
column_directory_name = build_column_directory_name(column_name); column_directory_name = build_column_directory_name(column_name);
@ -149,18 +137,8 @@ OBIDMS_column_directory_p obi_create_column_directory(OBIDMS_p dms, const char*
return NULL; return NULL;
} }
// Get the file descriptor for the dms
dms_file_descriptor = dirfd(dms->directory);
if (dms_file_descriptor < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obidebug(1, "\nError getting a file descriptor for an OBIDMS directory");
free(column_directory_name);
return NULL;
}
// Try to create the directory // Try to create the directory
if (mkdirat(dms_file_descriptor, column_directory_name, 00777) < 0) if (mkdirat(dms->dir_fd, column_directory_name, 00777) < 0)
{ {
if (errno == EEXIST) if (errno == EEXIST)
obi_set_errno(OBICOLDIR_EXIST_ERROR); obi_set_errno(OBICOLDIR_EXIST_ERROR);
@ -182,7 +160,6 @@ OBIDMS_column_directory_p obi_open_column_directory(OBIDMS_p dms, const char* co
OBIDMS_column_directory_p column_directory; OBIDMS_column_directory_p column_directory;
char* column_directory_name; char* column_directory_name;
DIR* directory; DIR* directory;
int dms_file_descriptor;
column_directory = NULL; column_directory = NULL;
@ -191,18 +168,8 @@ OBIDMS_column_directory_p obi_open_column_directory(OBIDMS_p dms, const char* co
if (column_directory_name == NULL) if (column_directory_name == NULL)
return NULL; return NULL;
// Get the file descriptor for the dms
dms_file_descriptor = dirfd(dms->directory);
if (dms_file_descriptor < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obidebug(1, "\nError getting a file descriptor for an OBIDMS directory");
free(column_directory_name);
return NULL;
}
// Try to open the column directory // Try to open the column directory
directory = private_opendirat(dms_file_descriptor, column_directory_name); directory = private_opendirat(dms->dir_fd, column_directory_name);
if (directory == NULL) { if (directory == NULL) {
switch (errno) switch (errno)
{ {
@ -241,6 +208,15 @@ OBIDMS_column_directory_p obi_open_column_directory(OBIDMS_p dms, const char* co
strcpy(column_directory->column_name, column_name); strcpy(column_directory->column_name, column_name);
column_directory->directory = directory; column_directory->directory = directory;
column_directory->dir_fd = dirfd(directory);
if (column_directory->dir_fd < 0)
{
obi_set_errno(OBICOLDIR_MEMORY_ERROR);
obidebug(1, "\nError allocating the memory for an OBIDMS column directory structure");
free(column_directory_name);
free(column_directory);
}
free(column_directory_name); free(column_directory_name);
return column_directory; return column_directory;

View File

@ -33,16 +33,19 @@
*/ */
typedef struct OBIDMS_column_directory { typedef struct OBIDMS_column_directory {
OBIDMS_p dms; /**< A pointer to a DMS instance. OBIDMS_p dms; /**< A pointer to a DMS instance.
*/ */
char column_name[OBIDMS_COLUMN_MAX_NAME+1]; /**< The name of the column char column_name[OBIDMS_COLUMN_MAX_NAME+1]; /**< The name of the column
* contained in the directory. * contained in the directory.
*/ */
char directory_name[OBIDMS_COLUMN_MAX_NAME+1]; /**< The name of the directory char directory_name[OBIDMS_COLUMN_MAX_NAME+1]; /**< The name of the directory
* containing the column. * containing the column.
*/ */
DIR* directory; /**< A directory entry usable to DIR* directory; /**< A directory entry usable to
* refer and scan the database directory. * refer and scan the column directory.
*/ */
int dir_fd; /**< The file descriptor of the directory entry
* usable to refer and scan the column directory.
*/
} OBIDMS_column_directory_t, *OBIDMS_column_directory_p; } OBIDMS_column_directory_t, *OBIDMS_column_directory_p;