private *at functions (openat, mkdirat, opendirat, closedirat)
This commit is contained in:
131
src/private_at_functions.c
Normal file
131
src/private_at_functions.c
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
76
src/private_at_functions.h
Normal file
76
src/private_at_functions.h
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
/**
|
||||||
|
* @file private_openat.h
|
||||||
|
* @author Celine Mercier
|
||||||
|
* @date 15 June 2015
|
||||||
|
* @brief Header file for a replacement function for openat.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef PRIVATE_OPENAT_H_
|
||||||
|
#define PRIVATE_OPENAT_H_
|
||||||
|
|
||||||
|
|
||||||
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define MAX_PATH_LEN 4096
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Replacement function for openat() : open a file relative to a directory file descriptor.
|
||||||
|
*
|
||||||
|
* @param directory_file_descriptor the file descriptor for the directory in which the file should be opened
|
||||||
|
* @param path_name the path name for the file, relative to directory_file_descriptor
|
||||||
|
* @param flags the access modes
|
||||||
|
*
|
||||||
|
* @return the file descriptor of the opened file
|
||||||
|
*
|
||||||
|
* @since June 2015
|
||||||
|
* @author Celine Mercier (celine.mercier@metabarcoding.org)
|
||||||
|
*/
|
||||||
|
int private_openat(int directory_file_descriptor, const char* path_name, int flags);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Replacement function for mkdirat() : create a directory relative to a directory file descriptor.
|
||||||
|
*
|
||||||
|
* @param directory_file_descriptor the file descriptor for the directory in which the directory should be created
|
||||||
|
* @param path_name the path name for the new directory, relative to directory_file_descriptor
|
||||||
|
* @param mode the access mode
|
||||||
|
*
|
||||||
|
* @return the file descriptor of the created directory
|
||||||
|
*
|
||||||
|
* @since June 2015
|
||||||
|
* @author Celine Mercier (celine.mercier@metabarcoding.org)
|
||||||
|
*/
|
||||||
|
int private_mkdirat(int directory_file_descriptor, const char* path_name, mode_t mode);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Replacement function for opendirat() : open a directory relative to a directory file descriptor.
|
||||||
|
*
|
||||||
|
* @param directory_file_descriptor the file descriptor for the directory in which the directory should be opened
|
||||||
|
* @param path_name the path name for the directory to be opened, relative to directory_file_descriptor
|
||||||
|
*
|
||||||
|
* @return the file descriptor of the opened directory
|
||||||
|
*
|
||||||
|
* @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);
|
||||||
|
|
||||||
|
|
||||||
|
#endif /* PRIVATEOPENAT_H_ */
|
Reference in New Issue
Block a user