Commit b37bd8f2 authored by Celine Mercier's avatar Celine Mercier

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

structures.
parent 05e3956a
......@@ -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
{
size_t file_size;
size_t old_data_size;
size_t new_data_size;
size_t header_size;
int array_dir_file_descriptor;
int array_file_descriptor;
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;
}
size_t file_size;
size_t old_data_size;
size_t new_data_size;
size_t header_size;
int array_file_descriptor;
char* array_file_name;
// Get the array file name
array_file_name = build_array_file_name((array->header)->array_name);
if (array_file_name == NULL)
{
close(array_dir_file_descriptor);
return -1;
}
// 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)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError opening an array file");
close(array_dir_file_descriptor);
free(array_file_name);
return -1;
}
......@@ -330,7 +316,6 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError enlarging an array file");
close(array_dir_file_descriptor);
close(array_file_descriptor);
return -1;
}
......@@ -341,7 +326,6 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError munmapping the array of an array file before enlarging");
close(array_dir_file_descriptor);
close(array_file_descriptor);
return -1;
}
......@@ -358,7 +342,6 @@ int grow_array(OBIDMS_array_p array) // TODO Lock when needed
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError re-mmapping the array of an array file after enlarging the file");
close(array_dir_file_descriptor);
close(array_file_descriptor);
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
{
size_t file_size;
size_t file_size;
index_t old_data_size;
index_t new_data_size;
size_t header_size;
int array_dir_file_descriptor;
int array_data_file_descriptor;
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;
}
size_t header_size;
int array_data_file_descriptor;
char* array_data_file_name;
// Get the array data file name
array_data_file_name = build_array_data_file_name((array->header)->array_name);
if (array_data_file_name == NULL)
{
close(array_dir_file_descriptor);
return -1;
}
// 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)
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError opening an array data file");
close(array_dir_file_descriptor);
free(array_data_file_name);
return -1;
}
......@@ -428,7 +397,6 @@ int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError enlarging an array data file");
close(array_dir_file_descriptor);
close(array_data_file_descriptor);
return -1;
}
......@@ -439,7 +407,6 @@ int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed
{
obi_set_errno(OBI_ARRAY_ERROR);
obidebug(1, "\nError munmapping the data of an array data file before enlarging");
close(array_dir_file_descriptor);
close(array_data_file_descriptor);
return -1;
}
......@@ -456,7 +423,6 @@ int grow_array_data(OBIDMS_array_p array) // TODO Lock when needed
{
obi_set_errno(OBI_ARRAY_ERROR);
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);
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 comp;
int comp;
uint8_t size_1;
uint8_t size_2;
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)
{
struct stat buffer;
char* array_file_path;
char* array_file_name;
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;
}
struct stat buffer;
char* array_file_path;
char* array_file_name;
int check_dir;
// Build file name
array_file_name = build_array_file_name(array_name);
if (array_file_name == NULL)
{
close(array_dir_file_descriptor);
return -1;
}
// 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)
{
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)
{
char* array_file_name;
char* array_data_file_name;
char* array_file_name;
char* array_data_file_name;
size_t header_size;
size_t data_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
// 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
array_data_file_name = build_array_data_file_name(array_name);
if (array_data_file_name == NULL)
{
close(array_dir_file_descriptor);
return NULL;
}
// Get the file descriptor of the array directory
array_dir_file_descriptor = dms->array_dir_fd;
// Create file
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);
obidebug(1, "\nError creating an array data file");
close(array_dir_file_descriptor);
free(array_data_file_name);
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);
obidebug(1, "\nError truncating an array data file to the right size");
close(array_dir_file_descriptor);
close(array_data_file_descriptor);
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);
obidebug(1, "\nError allocating the memory for the array data structure");
close(array_dir_file_descriptor);
close(array_data_file_descriptor);
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);
obidebug(1, "\nError mmapping the header of an array data file");
close(array_dir_file_descriptor);
close(array_data_file_descriptor);
free(array_data);
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);
obidebug(1, "\nError mmapping the data of an array data file");
munmap(array_data->header, header_size);
close(array_dir_file_descriptor);
close(array_data_file_descriptor);
free(array_data);
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);
if (array_file_name == NULL)
{
close(array_dir_file_descriptor);
close_array_data(array_data);
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);
obidebug(1, "\nError creating an array file");
close(array_dir_file_descriptor);
close_array_data(array_data);
free(array_file_name);
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);
obidebug(1, "\nError truncating an array file to the right size");
close(array_dir_file_descriptor);
close_array_data(array_data);
close(array_file_descriptor);
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);
obidebug(1, "\nError allocating the memory for the array structure");
close(array_dir_file_descriptor);
close_array_data(array_data);
close(array_file_descriptor);
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);
obidebug(1, "\nError mmapping the header of an array file");
close(array_dir_file_descriptor);
close_array_data(array_data);
close(array_file_descriptor);
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);
obidebug(1, "\nError mmapping the data of an array file");
close(array_dir_file_descriptor);
close_array_data(array_data);
munmap(array->header, header_size);
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->directory = dms->array_directory;
array->dir_fd = array_dir_file_descriptor;
(array->header)->header_size = header_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
// 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;
}
array_dir_file_descriptor = dms->array_dir_fd;
// Build file name
array_data_file_name = build_array_data_file_name(array_name);
if (array_data_file_name == NULL)
{
close(array_dir_file_descriptor);
return NULL;
}
// Open file
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);
obidebug(1, "\nError opening an array data file");
close(array_dir_file_descriptor);
free(array_data_file_name);
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);
obidebug(1, "\nError allocating the memory for the array data structure");
close(array_dir_file_descriptor);
close(array_data_file_descriptor);
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);
obidebug(1, "\nError mmapping the header of an array data file");
close(array_dir_file_descriptor);
close(array_data_file_descriptor);
free(array_data);
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);
obidebug(1, "\nError mmapping the data of an array data file");
close(array_dir_file_descriptor);
munmap(array_data->header, header_size);
close(array_data_file_descriptor);
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);
if (array_file_name == NULL)
{
close(array_dir_file_descriptor);
close_array_data(array_data);
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);
obidebug(1, "\nError opening an array file");
close(array_dir_file_descriptor);
close_array_data(array_data);
free(array_file_name);
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);
obidebug(1, "\nError allocating the memory for the array structure");
close(array_dir_file_descriptor);
close_array_data(array_data);
close(array_file_descriptor);
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);
obidebug(1, "\nError mmapping the header of an array file");
close(array_dir_file_descriptor);
close_array_data(array_data);
close(array_file_descriptor);
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);
obidebug(1, "\nError mmapping the data of an array file");
close(array_dir_file_descriptor);
close_array_data(array_data);
munmap(array->header, header_size);
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->directory = dms->array_directory;
array->dir_fd = array_dir_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 idx_min;
index_t idx_max;
index_t idx_mid;
index_t idx;
index_t* a;
byte_t* data;
byte_t* to_compare;
index_t nb_items;
int comp;
index_t idx_min;
index_t idx_max;
index_t idx_mid;
index_t idx;
index_t* a;
byte_t* data;
byte_t* to_compare;
index_t nb_items;
int comp;
nb_items = (array->header)->nb_items;
data = (array->data)->data;
......
......@@ -101,6 +101,9 @@ typedef struct OBIDMS_array {
DIR* directory; /**< A directory entry usable to
* 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;
......
......@@ -164,7 +164,6 @@ OBIDMS_p obi_create_dms(const char* dms_name)
return NULL;
}
return obi_open_dms(dms_name);
}
......@@ -173,19 +172,32 @@ OBIDMS_p obi_open_dms(const char* dms_name)
{
OBIDMS_p dms;
char* directory_name;
DIR* directory;
int dms_file_descriptor;
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
directory_name = build_directory_name(dms_name);
if (directory_name == NULL)
{
free(dms);
return NULL;
}
strncpy(dms->directory_name, directory_name, OBIDMS_MAX_NAME);
free(directory_name);
// Try to open the directory
directory = opendir(directory_name);
if (directory == NULL)
dms->directory = opendir(dms->directory_name);
if (dms->directory == NULL)
{
switch (errno)
{
......@@ -205,45 +217,43 @@ OBIDMS_p obi_open_dms(const char* dms_name)
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
}
obidebug(1, "\nCan't open OBIDMS directory");
free(directory_name);
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);
free(dms);
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
dms_file_descriptor = dirfd(directory);
if (dms_file_descriptor < 0)
dms->dir_fd = dirfd(dms->directory);
if (dms->dir_fd < 0)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obidebug(1, "\nError getting the file descriptor for a newly created OBIDMS directory");
free(directory_name);
closedir(dms->directory);
free(dms);
return NULL;
}
// 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)
{
obi_set_errno(OBIDMS_UNKNOWN_ERROR);
obidebug(1, "\nError opening the arrays directory");
free(directory_name);
closedir(dms->directory);
free(dms);
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;
}
......
......@@ -43,9 +43,15 @@ typedef struct OBIDMS {
DIR* directory; /**< A directory entry usable to
* 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
* 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;
......
......@@ -190,7 +190,6 @@ static obiversion_t obi_get_new_version_number(OBIDMS_column_directory_p column_
off_t loc_size;
obiversion_t new_version_number;
char* version_file_name;
int column_dir_file_descriptor;
int version_file_descriptor;
bool little_endian;
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)
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
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 (errno == ENOENT)
......@@ -338,7 +327,6 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo
off_t loc_size;
obiversion_t version_number;
char* version_file_name;
int column_dir_file_descriptor;
int version_file_descriptor;
bool little_endian;
......@@ -349,18 +337,8 @@ static obiversion_t create_version_file(OBIDMS_column_directory_p column_directo
if (version_file_name == NULL)
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
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)
{
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;
obiversion_t latest_version_number;
char * version_file_name;
int column_dir_file_descriptor;
int version_file_descriptor;
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)
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
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)
{
obi_set_errno(OBICOL_UNKNOWN_ERROR);
......@@ -596,7 +563,6 @@ OBIDMS_column_p obi_create_column(OBIDMS_p dms,
obiversion_t version_number;