fixed replacement *at functions and made get_full_path() function public

This commit is contained in:
Celine Mercier
2015-06-23 17:55:00 +02:00
parent e6e68786f7
commit 0a78a786c1
2 changed files with 79 additions and 97 deletions

View File

@ -1,69 +1,26 @@
/****************************************************************************
* Private *at functions *
****************************************************************************/
/**
* @file private_at_functions.h
* @author Celine Mercier
* @date 15 June 2015
* @brief Private replacement functions for *at functions.
*/
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <dirent.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
@ -71,10 +28,29 @@ static char* get_full_path(int directory_file_descriptor, const char* path_name)
**********************************************************************/
char* get_full_path(int directory_file_descriptor, const char* path_name)
{
char* full_path;
full_path = (char*) malloc((MAX_PATH_LEN)*sizeof(char));
if (full_path == NULL)
return NULL;
if (fcntl(directory_file_descriptor, F_GETPATH, full_path) < 0)
return NULL;
// check errors TODO
strlcat(full_path, "/", MAX_PATH_LEN);
strlcat(full_path, path_name, MAX_PATH_LEN);
return full_path;
}
int private_openat(int directory_file_descriptor, const char* path_name, int flags)
{
char* full_path;
int file_descriptor;
char full_path[MAX_PATH_LEN];
full_path = get_full_path(directory_file_descriptor, path_name);
if (full_path == NULL)
@ -82,50 +58,45 @@ int private_openat(int directory_file_descriptor, const char* path_name, int fla
file_descriptor = open(full_path, flags);
free(full_path);
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];
char* full_path;
full_path = get_full_path(directory_file_descriptor, path_name);
if (full_path == NULL)
return -1;
file_descriptor = mkdir(full_path, mode);
if (mkdir(full_path, mode) < 0)
{
free(full_path);
return -1;
}
return file_descriptor;
free(full_path);
return 0;
}
int private_opendirat(int directory_file_descriptor, const char* path_name)
DIR* private_opendirat(int directory_file_descriptor, const char* path_name)
{
int file_descriptor;
char full_path[MAX_PATH_LEN];
char* full_path;
DIR* directory;
full_path = get_full_path(directory_file_descriptor, path_name);
if (full_path == NULL)
return -1;
return NULL;
file_descriptor = opendir(full_path);
directory = opendir(full_path);
return file_descriptor;
free(full_path);
return directory;
}
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;
}

View File

@ -2,7 +2,7 @@
* @file private_openat.h
* @author Celine Mercier
* @date 15 June 2015
* @brief Header file for a replacement function for openat.
* @brief Header file for the replacement *at function.
*/
#ifndef PRIVATE_OPENAT_H_
@ -15,6 +15,31 @@
#define MAX_PATH_LEN 4096
/**
* 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)
*/
char* get_full_path(int directory_file_descriptor, const char* path_name);
/**
* @brief Replacement function for openat() : open a file relative to a directory file descriptor.
*
@ -56,21 +81,7 @@ int private_mkdirat(int directory_file_descriptor, const char* path_name, mode_t
* @since June 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
int private_opendirat(int directory_file_descriptor, const char* path_name);
/**
* @brief Replacement function for closedirat() : close a directory relative to a directory file descriptor.
*
* @param directory_file_descriptor the file descriptor for the directory in which the directory should be closed
* @param path_name the path name for the directory to be closed, relative to directory_file_descriptor
*
* @return 0 on success and -1 on error
*
* @since June 2015
* @author Celine Mercier (celine.mercier@metabarcoding.org)
*/
int private_closedirat(int directory_file_descriptor, const char* path_name);
DIR* private_opendirat(int directory_file_descriptor, const char* path_name);
#endif /* PRIVATEOPENAT_H_ */