Commit b37bd8f2 by Celine Mercier

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

structures.
parent 05e3956a
......@@ -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;
......
......@@ -97,25 +97,14 @@ int obi_column_directory_exists(OBIDMS_p dms, const char* column_name)
char* column_directory_name;
char* full_path;
int check_dir;
int dms_file_descriptor;
// Build and check the directory name
column_directory_name = build_column_directory_name(column_name);
if (column_directory_name == NULL)
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
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)
{
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)
{
char* column_directory_name;
int dms_file_descriptor;
// Build and check the directory 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;
}
// 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
if (mkdirat(dms_file_descriptor, column_directory_name, 00777) < 0)
if (mkdirat(dms->dir_fd, column_directory_name, 00777) < 0)
{
if (errno == EEXIST)
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;
char* column_directory_name;
DIR* directory;
int dms_file_descriptor;
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)
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
directory = private_opendirat(dms_file_descriptor, column_directory_name);
directory = private_opendirat(dms->dir_fd, column_directory_name);
if (directory == NULL) {
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);
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);
return column_directory;
......
......@@ -33,16 +33,19 @@
*/
typedef struct OBIDMS_column_directory {
OBIDMS_p dms; /**< A pointer to a DMS instance.
*/
*/
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
* containing the column.
*/
* containing the column.
*/
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;
......
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