structering the core some more

This commit is contained in:
Axel Kittenberger 2018-03-27 09:09:12 +02:00
parent b3212c0786
commit fc8c02a749
2 changed files with 135 additions and 0 deletions

100
core/util.c Normal file
View File

@ -0,0 +1,100 @@
/*
| util.c from Lsyncd - Live (Mirror) Syncing Demon
|
|
| Small in Lsyncd commonly used utils.
|
|
| License: GPLv2 (see COPYING) or any later version
| Authors: Axel Kittenberger <axkibe@gmail.com>
*/
#include "lsyncd.h"
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "log.h"
/*
| Returns the absolute path of a path.
|
| This is a wrapper to various C-Library differences.
*/
char *
get_realpath( char const * rpath )
{
#ifdef __GLIBC__
// in case of GLIBC the task is easy.
return realpath( rpath, NULL );
#else
#warning having to use old style realpath()
// otherwise less so and requires PATH_MAX limit
char buf[ PATH_MAX] ;
char *asw = realpath( rpath, buf );
if( !asw ) return NULL;
return s_strdup( asw );
#endif
}
/*
| Sets the non-blocking flag of a file descriptor.
*/
void
non_block_fd( int fd )
{
int flags;
flags = fcntl( fd, F_GETFL );
if( flags == -1 )
{
logstring( "Error", "cannot get status flags!" );
exit( -1 );
}
flags |= O_NONBLOCK;
if( fcntl( fd, F_SETFL, flags ) == -1 )
{
logstring( "Error", "cannot set status flags!" );
exit( -1 );
}
}
/*
| Sets the close-on-exit flag of a file descriptor.
*/
void
close_exec_fd( int fd )
{
int flags;
flags = fcntl( fd, F_GETFD );
if( flags == -1 )
{
logstring( "Error", "cannot get descriptor flags!" );
exit( -1 );
}
flags |= FD_CLOEXEC;
if( fcntl( fd, F_SETFD, flags ) == -1 )
{
logstring( "Error", "cannot set descripptor flags!" );
exit( -1 );
}
}

35
core/util.h Normal file
View File

@ -0,0 +1,35 @@
/*
| util.h from Lsyncd - Live (Mirror) Syncing Demon
|
|
| Small in Lsyncd commonly used utils.
|
|
| License: GPLv2 (see COPYING) or any later version
| Authors: Axel Kittenberger <axkibe@gmail.com>
*/
#ifndef LSYNCD_UTIL_H
#define LSYNCD_UTIL_H
/*
| Returns the absolute path of a path.
|
| This is a wrapper to various C-Library differences.
*/
extern char * get_realpath( char const * rpath );
/*
| Sets the non-blocking flag on a file descriptor.
*/
extern void non_block_fd( int fd );
/*
| Sets the close-on-exit flag on a file descriptor.
*/
extern void close_exec_fd( int fd );
#endif