diff --git a/core/util.c b/core/util.c new file mode 100644 index 0000000..cb287b5 --- /dev/null +++ b/core/util.c @@ -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 +*/ + +#include "lsyncd.h" + +#include +#include +#include +#include + +#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 ); + } +} + + diff --git a/core/util.h b/core/util.h new file mode 100644 index 0000000..5335973 --- /dev/null +++ b/core/util.h @@ -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 +*/ +#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