mirror of
https://github.com/octoleo/lsyncd.git
synced 2024-09-27 14:39:01 +00:00
structering the core some more
This commit is contained in:
parent
b3212c0786
commit
fc8c02a749
100
core/util.c
Normal file
100
core/util.c
Normal 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
35
core/util.h
Normal 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
|
Loading…
Reference in New Issue
Block a user