Files
obitools3/src/private_at_functions.c

132 lines
3.3 KiB
C
Raw Normal View History

#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include "private_at_functions.h"
/**************************************************************************
*
* D E C L A R A T I O N O F T H E P R I V A T E F U N C T I O N S
*
**************************************************************************/
/**
* Internal function getting the full path of a file or a directory from its
* path relative to a directory file descriptor.
*
*
* @warning The returned pointer has to be freed by the caller.
*
* @param directory_file_descriptor the file descriptor for the directory to which
* path_name is relative
* @param path_name the path name for the file or directory, relative to directory_file_descriptor
*
* @return a pointer to the full path
* @retvalue <full_path> if everything is ok
* @retvalue NULL if an error occurs
*
* ###Error values
* - OBIDMS_MEMORY_ERROR : something wrong occurred during memory allocation.
* - OBIDMS_LONG_NAME_ERROR : the database name exceeds the limit.
*
* @since June 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
static char* get_full_path(int directory_file_descriptor, const char* path_name);
/************************************************************************
*
* D E F I N I T I O N O F T H E P R I V A T E F U N C T I O N S
*
************************************************************************/
static char* get_full_path(int directory_file_descriptor, const char* path_name)
{
char full_path[MAX_PATH_LEN];
if (fcntl(directory_file_descriptor, F_GETPATH, full_path) < 0)
return NULL;
if (strlcat(full_path, "/", MAX_PATH_LEN) >= sizeof(full_path))
return NULL;
if (strlcat(full_path, path_name, MAX_PATH_LEN) >= sizeof(full_path))
return NULL;
return full_path;
}
/**********************************************************************
*
* 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
*
**********************************************************************/
int private_openat(int directory_file_descriptor, const char* path_name, int flags)
{
int file_descriptor;
char full_path[MAX_PATH_LEN];
full_path = get_full_path(directory_file_descriptor, path_name);
if (full_path == NULL)
return -1;
file_descriptor = open(full_path, flags);
return file_descriptor;
}
int private_mkdirat(int directory_file_descriptor, const char* path_name, mode_t mode)
{
int file_descriptor;
char full_path[MAX_PATH_LEN];
full_path = get_full_path(directory_file_descriptor, path_name);
if (full_path == NULL)
return -1;
file_descriptor = mkdir(full_path, mode);
return file_descriptor;
}
int private_opendirat(int directory_file_descriptor, const char* path_name)
{
int file_descriptor;
char full_path[MAX_PATH_LEN];
full_path = get_full_path(directory_file_descriptor, path_name);
if (full_path == NULL)
return -1;
file_descriptor = opendir(full_path);
return file_descriptor;
}
int private_closedirat(int directory_file_descriptor, const char* path_name)
{
int file_descriptor;
char full_path[MAX_PATH_LEN];
full_path = get_full_path(directory_file_descriptor, path_name);
if (full_path == NULL)
return -1;
file_descriptor = closedir(full_path);
return file_descriptor;
}