Skip to content

Commit

Permalink
Merge pull request #31 from wulf7/integrate-dmatveev12
Browse files Browse the repository at this point in the history
minor bugfixes & compat layer rework
  • Loading branch information
dmatveev committed Jun 28, 2015
2 parents f4a092a + f277f1f commit b41b7ee
Show file tree
Hide file tree
Showing 29 changed files with 2,342 additions and 274 deletions.
44 changes: 29 additions & 15 deletions Makefile.am
Expand Up @@ -11,7 +11,6 @@ nobase_include_HEADERS = sys/inotify.h

libinotify_la_SOURCES = \
utils.c \
compat.c \
conversions.c \
dep-list.c \
inotify-watch.c \
Expand All @@ -21,12 +20,28 @@ libinotify_la_SOURCES = \
worker.c \
controller.c

libinotify_la_CFLAGS = -I. -DNDEBUG
if FREEBSD
libinotify_la_LDFLAGS = -pthread
else
libinotify_la_LDFLAGS = -lpthread
if !HAVE_PTHREAD_BARRIER
libinotify_la_SOURCES += compat/pthread_barrier.c
endif

if !HAVE_ATFUNCS
libinotify_la_SOURCES += compat/atfuncs.c
endif

if !HAVE_OPENAT
libinotify_la_SOURCES += compat/openat.c
endif

if !HAVE_FDOPENDIR
libinotify_la_SOURCES += compat/fdopendir.c
endif

if !HAVE_FSTATAT
libinotify_la_SOURCES += compat/fstatat.c
endif

libinotify_la_CFLAGS = -I. -DNDEBUG @PTHREAD_CFLAGS@
libinotify_la_LDFLAGS = @PTHREAD_LIBS@ -export-symbols libinotify.sym
endif

############################################################
Expand Down Expand Up @@ -63,20 +78,19 @@ check_libinotify_SOURCES = \
tests/bugs_test.cc \
tests/tests.cc

check_libinotify_CXXFLAGS = @PTHREAD_CFLAGS@
check_libinotify_LDFLAGS = @PTHREAD_LIBS@

if LINUX
check_libinotify_CXXFLAGS = -std=c++0x
check_libinotify_SOURCES += compat.c
check_libinotify_CXXFLAGS += -std=c++0x
endif

if BUILD_LIBRARY
check_libinotify_LDADD = libinotify.la
if !HAVE_PTHREAD_BARRIER
check_libinotify_SOURCES += compat/pthread_barrier.c
endif

if FREEBSD
check_libinotify_LDFLAGS = -pthread
else
check_libinotify_LDFLAGS = -lpthread
if BUILD_LIBRARY
check_libinotify_LDADD = libinotify.la
endif


noinst_programs = check_libinotify
52 changes: 20 additions & 32 deletions compat.h
Expand Up @@ -25,52 +25,38 @@

#include "config.h"

#ifdef BUILD_LIBRARY
#include <sys/types.h>

#ifdef HAVE_SYS_QUEUE_H
#include <sys/queue.h>
#ifdef __APPLE__
#include </System/Library/Frameworks/Kernel.framework/Versions/Current/Headers/libkern/tree.h>
#else
#include "compat/queue.h"
#endif

#ifdef HAVE_SYS_TREE_H
#include <sys/tree.h> /* RB tree macroses */
#else
#include "compat/tree.h"
#endif

#ifdef HAVE_STDINT_H
#include <stdint.h>
#else
#include <inttypes.h>
#endif

#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#endif /* BUILD_LIBRARY */
#include <pthread.h>

#ifdef BUILD_LIBRARY
#ifndef O_SYMLINK
#define O_SYMLINK O_NOFOLLOW
#endif
#ifndef O_EVTONLY
#define O_EVTONLY O_RDONLY
#endif

#ifndef DTTOIF
#define DTTOIF(dirtype) ((dirtype) << 12)
#endif

#ifndef SLIST_FOREACH_SAFE
#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
for ((var) = SLIST_FIRST((head)); \
(var) && ((tvar) = SLIST_NEXT((var), field), 1); \
(var) = (tvar))
#ifndef SIZE_MAX
#define SIZE_MAX SIZE_T_MAX
#endif
#ifndef SLIST_REMOVE_AFTER
#define SLIST_REMOVE_AFTER(elm, field) do { \
SLIST_NEXT(elm, field) = \
SLIST_NEXT(SLIST_NEXT(elm, field), field); \
} while (0)
#endif
#ifndef RB_FOREACH_SAFE
#define RB_FOREACH_SAFE(x, name, head, y) \
for ((x) = RB_MIN(name, head); \
((x) != NULL) && ((y) = name##_RB_NEXT(x), (x) != NULL); \
(x) = (y))
#endif
#endif /* BUILD_LIBRARY */

#ifndef HAVE_PTHREAD_BARRIER
typedef struct {
Expand All @@ -92,14 +78,17 @@ void pthread_barrier_wait (pthread_barrier_t *impl);
void pthread_barrier_destroy (pthread_barrier_t *impl);
#endif

#ifdef BUILD_LIBRARY
#ifndef AT_FDCWD
#define AT_FDCWD -100
#endif
#ifndef AT_SYMLINK_NOFOLLOW
#define AT_SYMLINK_NOFOLLOW 0x200 /* Do not follow symbolic links */
#endif

#ifndef HAVE_ATFUNCS
char *fd_getpath_cached (int fd);
char *fd_concat (int fd, const char *file);
#endif
#ifndef HAVE_OPENAT
int openat (int fd, const char *path, int flags, ...);
#endif
Expand All @@ -109,6 +98,5 @@ DIR *fdopendir (int fd);
#ifndef HAVE_FSTATAT
int fstatat (int fd, const char *path, struct stat *buf, int flag);
#endif
#endif /* BUILD_LIBRARY */

#endif /* __COMPAT_H__ */
166 changes: 10 additions & 156 deletions compat.c → compat/atfuncs.c
@@ -1,5 +1,5 @@
/*******************************************************************************
Copyright (c) 2011-2014 Dmitry Matveev <me@dmitrymatveev.co.uk>
Copyright (c) 2014 Vladimir Kondratiev <wulf@cicgroup.ru>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -29,90 +29,12 @@
#include <errno.h> /* errno */
#include <fcntl.h> /* fcntl */
#include <limits.h> /* PATH_MAX */
#include <stdarg.h> /* va_start */
#include <stdlib.h> /* malloc */
#include <string.h> /* memset */

#include "compat.h"
#include "config.h"

#ifndef HAVE_PTHREAD_BARRIER
/**
* Initialize a barrier
*
* @param[in] impl A pointer to barrier
* @param[in] attr A barrier attributes (not implemented)
* @param[in] count The number of threads to wait on the barrier
**/
void
pthread_barrier_init (pthread_barrier_t *impl,
const pthread_barrierattr_t *attr,
unsigned count)
{
assert (impl != NULL);

memset (impl, 0, sizeof (pthread_barrier_t));
impl->count = count;

pthread_mutex_init (&impl->mtx, NULL);
pthread_cond_init (&impl->cnd, NULL);
}


/**
* Wait on a barrier.
*
* If this thread is not the last expected one, it will be blocked
* until all the expected threads will check in on the barrier.
* Otherwise the barrier will be marked as passed and all blocked
* threads will be unlocked.
*
* This barrier implementation is based on:
* http://siber.cankaya.edu.tr/ozdogan/GraduateParallelComputing.old/ceng505/node94.html
*
* @param[in] impl A pointer to barrier
**/
void
pthread_barrier_wait (pthread_barrier_t *impl)
{
while (impl->entered == 0 && impl->sleeping != 0);

pthread_mutex_lock (&impl->mtx);
impl->entered++;
if (impl->entered == impl->count) {
impl->entered = 0;
pthread_cond_broadcast (&impl->cnd);
} else {
++impl->sleeping;
while (pthread_cond_wait (&impl->cnd, &impl->mtx) != 0
&& impl->entered != 0);
--impl->sleeping;
}
pthread_mutex_unlock (&impl->mtx);
}


/**
* Destroy the barrier and all associated resources.
*
* @param[in] impl A pointer to barrier
**/
void
pthread_barrier_destroy (pthread_barrier_t *impl)
{
assert (impl != NULL);

pthread_cond_destroy (&impl->cnd);
pthread_mutex_destroy (&impl->mtx);

impl->count = 0;
impl->entered = 0;
impl->sleeping = 0;
}
#endif /* HAVE_PTHREAD_BARRIER */

#ifdef BUILD_LIBRARY
#ifndef HAVE_ATFUNCS
typedef struct dirpath_t {
ino_t inode; /* inode number */
dev_t dev; /* device number */
Expand Down Expand Up @@ -276,7 +198,7 @@ dir_insert (const char* path, ino_t inode, dev_t dev)
* @param[in] fd A file descriptor of opened directory
* @return a pointer to the pathname on success, NULL otherwise
**/
static char *
char *
fd_getpath_cached (int fd)
{
assert (fd != -1);
Expand Down Expand Up @@ -363,7 +285,7 @@ path_concat (const char *dir, const char *file)
* @param[in] file File name.
* @return A concatenated path. Should be freed with free().
**/
static char*
char*
fd_concat (int fd, const char *file)
{
char *path = NULL;
Expand All @@ -373,13 +295,18 @@ fd_concat (int fd, const char *file)

if (stat (file, &st) != -1
&& S_ISDIR (st.st_mode)
&& (path = realpath (file, NULL)) != NULL) {
&& (path = malloc (PATH_MAX + 1)) != NULL
&& realpath (file, path) == path) {

pthread_mutex_lock (&dirs_mtx);
dir_insert (path, st.st_ino, st.st_dev);
pthread_mutex_unlock (&dirs_mtx);
} else {
path = strdup (file);
if (path == NULL) {
path = strdup (file);
} else {
strlcpy (path, file, PATH_MAX + 1);
}
}
} else {

Expand All @@ -391,76 +318,3 @@ fd_concat (int fd, const char *file)

return path;
}
#endif /* !HAVE_ATFUNCS */

#ifndef HAVE_OPENAT
int
openat (int fd, const char *path, int flags, ...)
{
char *fullpath;
int newfd, save_errno;
mode_t mode;
va_list ap;

if (flags & O_CREAT) {
va_start(ap, flags);
mode = va_arg(ap, int);
va_end(ap);
} else {
mode = 0;
}

fullpath = fd_concat (fd, path);
if (fullpath == NULL) {
return -1;
}

newfd = open (fullpath, flags, mode);

save_errno = errno;
free (fullpath);
errno = save_errno;

return newfd;
}
#endif /* HAVE_OPENAT */

#ifndef HAVE_FDOPENDIR
DIR *
fdopendir (int fd)
{
char *dirpath = fd_getpath_cached (fd);
if (dirpath == NULL) {
return NULL;
}

return opendir (dirpath);
}
#endif /* HAVE_FDOPENDIR */

#ifndef HAVE_FSTATAT
int
fstatat (int fd, const char *path, struct stat *buf, int flag)
{
char *fullpath;
int retval, save_errno;

fullpath = fd_concat (fd, path);
if (fullpath == NULL) {
return -1;
}

if (flag & AT_SYMLINK_NOFOLLOW) {
retval = lstat (fullpath, buf);
} else {
retval = stat (fullpath, buf);
}

save_errno = errno;
free (fullpath);
errno = save_errno;

return retval;
}
#endif /* HAVE_FSTATAT */
#endif /* BUILD_LIBRARY */

0 comments on commit b41b7ee

Please sign in to comment.