Change indentation to follow the updated standard.
Alot of mixed indentation. use 4 chars wide soft tabs.
This commit is contained in:
parent
9cc5420447
commit
f46ae1970b
51 changed files with 634 additions and 639 deletions
|
|
@ -96,4 +96,3 @@ Things to keep in mind when modify or write code
|
||||||
------------------------------------------------
|
------------------------------------------------
|
||||||
|
|
||||||
include and change the comment found in TEMPLATE file, at the top of the .c/.h file
|
include and change the comment found in TEMPLATE file, at the top of the .c/.h file
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,4 +48,3 @@ database (String);;
|
||||||
Database to use
|
Database to use
|
||||||
table (String);;
|
table (String);;
|
||||||
Tablename to use
|
Tablename to use
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -52,36 +52,36 @@ static int load_config(const char *file) {
|
||||||
when in main loop is by signal */
|
when in main loop is by signal */
|
||||||
static void clean_exit(int excode) {
|
static void clean_exit(int excode) {
|
||||||
|
|
||||||
time_t t = time(NULL);
|
time_t t = time(NULL);
|
||||||
|
|
||||||
notify_exit();
|
notify_exit();
|
||||||
|
|
||||||
database_close();
|
database_close();
|
||||||
|
|
||||||
iniparser_freedict(config);
|
iniparser_freedict(config);
|
||||||
|
|
||||||
printf("\nprocess exit at: %s", ctime(&t));
|
printf("\nprocess exit at: %s", ctime(&t));
|
||||||
exit(excode);
|
exit(excode);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Signal handler */
|
/* Signal handler */
|
||||||
static void sighandl(int sig) {
|
static void sighandl(int sig) {
|
||||||
|
|
||||||
switch(sig) {
|
switch(sig) {
|
||||||
/* normal exit signals */
|
/* normal exit signals */
|
||||||
case SIGTERM :
|
case SIGTERM :
|
||||||
case SIGKILL :
|
case SIGKILL :
|
||||||
case SIGINT :
|
case SIGINT :
|
||||||
clean_exit(EXIT_SUCCESS);
|
clean_exit(EXIT_SUCCESS);
|
||||||
/* segmentation violation, let user now */
|
/* segmentation violation, let user now */
|
||||||
case SIGSEGV :
|
case SIGSEGV :
|
||||||
fprintf(stderr, "SEGFAULT: o no he didn't\n");
|
fprintf(stderr, "SEGFAULT: o no he didn't\n");
|
||||||
clean_exit(EXIT_FAILURE);
|
clean_exit(EXIT_FAILURE);
|
||||||
case SIGUSR1 :
|
case SIGUSR1 :
|
||||||
case SIGUSR2 :
|
case SIGUSR2 :
|
||||||
printf("notify stat:\n");
|
printf("notify stat:\n");
|
||||||
notify_stat();
|
notify_stat();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
96
src/event.c
96
src/event.c
|
|
@ -14,17 +14,17 @@
|
||||||
#include "xalloc.h"
|
#include "xalloc.h"
|
||||||
|
|
||||||
#define dealloc_data(ev) \
|
#define dealloc_data(ev) \
|
||||||
if (ev->path) \
|
if (ev->path) \
|
||||||
xfree(ev->path); \
|
xfree(ev->path); \
|
||||||
if (ev->filename) \
|
if (ev->filename) \
|
||||||
xfree(ev->filename)
|
xfree(ev->filename)
|
||||||
|
|
||||||
static void init_event(notify_event* ev) {
|
static void init_event(notify_event* ev) {
|
||||||
|
|
||||||
ev->filename = NULL;
|
ev->filename = NULL;
|
||||||
ev->path = NULL;
|
ev->path = NULL;
|
||||||
ev->type = NOTIFY_UNKNOWN;
|
ev->type = NOTIFY_UNKNOWN;
|
||||||
ev->dir = 0;
|
ev->dir = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -32,11 +32,11 @@ static void init_event(notify_event* ev) {
|
||||||
*/
|
*/
|
||||||
notify_event* notify_event_new() {
|
notify_event* notify_event_new() {
|
||||||
|
|
||||||
notify_event *ev = xmalloc(sizeof(notify_event));
|
notify_event *ev = xmalloc(sizeof(notify_event));
|
||||||
|
|
||||||
init_event(ev);
|
init_event(ev);
|
||||||
|
|
||||||
return ev;
|
return ev;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -44,11 +44,11 @@ notify_event* notify_event_new() {
|
||||||
*/
|
*/
|
||||||
void notify_event_del(notify_event *event) {
|
void notify_event_del(notify_event *event) {
|
||||||
|
|
||||||
if (event == NULL)
|
if (event == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
dealloc_data(event);
|
dealloc_data(event);
|
||||||
xfree(event);
|
xfree(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -56,11 +56,11 @@ void notify_event_del(notify_event *event) {
|
||||||
*/
|
*/
|
||||||
void notify_event_clear(notify_event *event) {
|
void notify_event_clear(notify_event *event) {
|
||||||
|
|
||||||
if (event == NULL)
|
if (event == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
dealloc_data(event);
|
dealloc_data(event);
|
||||||
init_event(event);
|
init_event(event);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -68,12 +68,12 @@ void notify_event_clear(notify_event *event) {
|
||||||
*/
|
*/
|
||||||
void notify_event_set_path(notify_event *event, const char *path) {
|
void notify_event_set_path(notify_event *event, const char *path) {
|
||||||
|
|
||||||
if (event == NULL || path == NULL)
|
if (event == NULL || path == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
event->path = xrealloc(event->path, strlen(path)+1);
|
event->path = xrealloc(event->path, strlen(path)+1);
|
||||||
|
|
||||||
memcpy(event->path, path, strlen(path)+1);
|
memcpy(event->path, path, strlen(path)+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -81,29 +81,29 @@ void notify_event_set_path(notify_event *event, const char *path) {
|
||||||
*/
|
*/
|
||||||
void notify_event_set_filename(notify_event *event, const char *filename) {
|
void notify_event_set_filename(notify_event *event, const char *filename) {
|
||||||
|
|
||||||
if (event == NULL || filename == NULL)
|
if (event == NULL || filename == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
event->filename = xrealloc(event->filename, strlen(filename)+1);
|
event->filename = xrealloc(event->filename, strlen(filename)+1);
|
||||||
|
|
||||||
memcpy(event->filename, filename, strlen(filename)+1);
|
memcpy(event->filename, filename, strlen(filename)+1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set directory */
|
/* set directory */
|
||||||
void notify_event_set_dir(notify_event *event, uint8_t dir) {
|
void notify_event_set_dir(notify_event *event, uint8_t dir) {
|
||||||
|
|
||||||
if (event == NULL)
|
if (event == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
event->dir = dir;
|
event->dir = dir;
|
||||||
}
|
}
|
||||||
|
|
||||||
void notify_event_set_type(notify_event *event, uint8_t type) {
|
void notify_event_set_type(notify_event *event, uint8_t type) {
|
||||||
|
|
||||||
if (event == NULL)
|
if (event == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
event->type = type;
|
event->type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char* notify_event_typetostr(notify_event *event) {
|
const char* notify_event_typetostr(notify_event *event) {
|
||||||
|
|
@ -111,19 +111,19 @@ const char* notify_event_typetostr(notify_event *event) {
|
||||||
if (!event)
|
if (!event)
|
||||||
return "(null)";
|
return "(null)";
|
||||||
|
|
||||||
switch(event->type) {
|
switch(event->type) {
|
||||||
case NOTIFY_CREATE :
|
case NOTIFY_CREATE :
|
||||||
return "CREATE";
|
return "CREATE";
|
||||||
case NOTIFY_DELETE :
|
case NOTIFY_DELETE :
|
||||||
return "DELETE";
|
return "DELETE";
|
||||||
break;
|
break;
|
||||||
case NOTIFY_MOVE_FROM :
|
case NOTIFY_MOVE_FROM :
|
||||||
return "MOVE_FROM";
|
return "MOVE_FROM";
|
||||||
break;
|
break;
|
||||||
case NOTIFY_MOVE_TO :
|
case NOTIFY_MOVE_TO :
|
||||||
return "MOVE_TO";
|
return "MOVE_TO";
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
return "UNKNOWN";
|
return "UNKNOWN";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
18
src/event.h
18
src/event.h
|
|
@ -15,17 +15,17 @@
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
/* event types */
|
/* event types */
|
||||||
#define NOTIFY_UNKNOWN (1 << 0)
|
#define NOTIFY_UNKNOWN (1 << 0)
|
||||||
#define NOTIFY_CREATE (1 << 1)
|
#define NOTIFY_CREATE (1 << 1)
|
||||||
#define NOTIFY_DELETE (1 << 2)
|
#define NOTIFY_DELETE (1 << 2)
|
||||||
#define NOTIFY_MOVE_FROM (1 << 3)
|
#define NOTIFY_MOVE_FROM (1 << 3)
|
||||||
#define NOTIFY_MOVE_TO (1 << 4)
|
#define NOTIFY_MOVE_TO (1 << 4)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint8_t type; /* type of event */
|
uint8_t type; /* type of event */
|
||||||
uint8_t dir; /* non zero if event is triggered on a directory */
|
uint8_t dir; /* non zero if event is triggered on a directory */
|
||||||
char *path; /* path of the triggered event */
|
char *path; /* path of the triggered event */
|
||||||
char *filename; /* the filename event was triggered on */
|
char *filename; /* the filename event was triggered on */
|
||||||
} notify_event;
|
} notify_event;
|
||||||
|
|
||||||
notify_event* notify_event_new();
|
notify_event* notify_event_new();
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@
|
||||||
(x)->fts_info == FTS_SLNONE)
|
(x)->fts_info == FTS_SLNONE)
|
||||||
|
|
||||||
struct __fscrawl {
|
struct __fscrawl {
|
||||||
FTS *fts;
|
FTS *fts;
|
||||||
fs_entry ent;
|
fs_entry ent;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -60,12 +60,12 @@ static fs_entry* ftsentcpy(fs_entry *dest, FTSENT *src) {
|
||||||
|
|
||||||
fscrawl_t fsc_open(const char *path) {
|
fscrawl_t fsc_open(const char *path) {
|
||||||
|
|
||||||
struct __fscrawl *f;
|
struct __fscrawl *f;
|
||||||
char *npath;
|
char *npath;
|
||||||
|
|
||||||
f = malloc(sizeof(struct __fscrawl));
|
f = malloc(sizeof(struct __fscrawl));
|
||||||
if (!f)
|
if (!f)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
npath = path_normalize(path, NULL, 1);
|
npath = path_normalize(path, NULL, 1);
|
||||||
if (!npath)
|
if (!npath)
|
||||||
|
|
@ -82,7 +82,7 @@ fscrawl_t fsc_open(const char *path) {
|
||||||
f->ent.base = NULL;
|
f->ent.base = NULL;
|
||||||
f->ent.name = NULL;
|
f->ent.name = NULL;
|
||||||
f->ent.dir = 0;
|
f->ent.dir = 0;
|
||||||
return f;
|
return f;
|
||||||
free:
|
free:
|
||||||
if (f)
|
if (f)
|
||||||
free(f);
|
free(f);
|
||||||
|
|
@ -102,22 +102,22 @@ void fsc_close(fscrawl_t f) {
|
||||||
|
|
||||||
fs_entry* fsc_cpy(fs_entry *ent) {
|
fs_entry* fsc_cpy(fs_entry *ent) {
|
||||||
|
|
||||||
void *ptr = malloc(sizeof(fs_entry));
|
void *ptr = malloc(sizeof(fs_entry));
|
||||||
|
|
||||||
if (ptr == NULL)
|
if (ptr == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
memcpy(ptr, ent, sizeof(fs_entry));
|
memcpy(ptr, ent, sizeof(fs_entry));
|
||||||
|
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
fs_entry* fsc_read(fscrawl_t f) {
|
fs_entry* fsc_read(fscrawl_t f) {
|
||||||
|
|
||||||
FTSENT *ent;
|
FTSENT *ent;
|
||||||
|
|
||||||
if (!f)
|
if (!f)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
ent = fts_read(f->fts);
|
ent = fts_read(f->fts);
|
||||||
|
|
|
||||||
|
|
@ -85,11 +85,11 @@ int inotify_backend_watch(const char *path) {
|
||||||
|
|
||||||
int wd = inotify_add_watch(fd, path, WATCH_MASK);
|
int wd = inotify_add_watch(fd, path, WATCH_MASK);
|
||||||
|
|
||||||
if (wd < 0) {
|
if (wd < 0) {
|
||||||
if (errno != EACCES && errno != ENOTDIR)
|
if (errno != EACCES && errno != ENOTDIR)
|
||||||
logerrno(LOG_CRIT, "inotify_watch", errno);
|
logerrno(LOG_CRIT, "inotify_watch", errno);
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
return wd;
|
return wd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,41 +16,41 @@
|
||||||
|
|
||||||
/* Structure describing an inotify event. */
|
/* Structure describing an inotify event. */
|
||||||
struct inotify_event {
|
struct inotify_event {
|
||||||
int wd; /* Watch descriptor. */
|
int wd; /* Watch descriptor. */
|
||||||
uint32_t mask; /* Watch mask. */
|
uint32_t mask; /* Watch mask. */
|
||||||
uint32_t cookie; /* Cookie to synchronize two events. */
|
uint32_t cookie; /* Cookie to synchronize two events. */
|
||||||
uint32_t len; /* Length (including NULs) of name. */
|
uint32_t len; /* Length (including NULs) of name. */
|
||||||
char name __flexarr; /* Name. */
|
char name __flexarr; /* Name. */
|
||||||
};
|
};
|
||||||
|
|
||||||
#define IN_EVENT_SIZE (sizeof(struct inotify_event) + 0x40)
|
#define IN_EVENT_SIZE (sizeof(struct inotify_event) + 0x40)
|
||||||
|
|
||||||
/* Supported events suitable for MASK parameter of INOTIFY_ADD_WATCH. */
|
/* Supported events suitable for MASK parameter of INOTIFY_ADD_WATCH. */
|
||||||
#define IN_ACCESS 0x00000001 /* File was accessed. */
|
#define IN_ACCESS 0x00000001 /* File was accessed. */
|
||||||
#define IN_MODIFY 0x00000002 /* File was modified. */
|
#define IN_MODIFY 0x00000002 /* File was modified. */
|
||||||
#define IN_ATTRIB 0x00000004 /* Metadata changed. */
|
#define IN_ATTRIB 0x00000004 /* Metadata changed. */
|
||||||
#define IN_CLOSE_WRITE 0x00000008 /* Writtable file was closed. */
|
#define IN_CLOSE_WRITE 0x00000008 /* Writtable file was closed. */
|
||||||
#define IN_CLOSE_NOWRITE 0x00000010 /* Unwrittable file closed. */
|
#define IN_CLOSE_NOWRITE 0x00000010 /* Unwrittable file closed. */
|
||||||
#define IN_CLOSE (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) /* Close. */
|
#define IN_CLOSE (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) /* Close. */
|
||||||
#define IN_OPEN 0x00000020 /* File was opened. */
|
#define IN_OPEN 0x00000020 /* File was opened. */
|
||||||
#define IN_MOVED_FROM 0x00000040 /* File was moved from X. */
|
#define IN_MOVED_FROM 0x00000040 /* File was moved from X. */
|
||||||
#define IN_MOVED_TO 0x00000080 /* File was moved to Y. */
|
#define IN_MOVED_TO 0x00000080 /* File was moved to Y. */
|
||||||
#define IN_MOVE (IN_MOVED_FROM | IN_MOVED_TO) /* Moves. */
|
#define IN_MOVE (IN_MOVED_FROM | IN_MOVED_TO) /* Moves. */
|
||||||
#define IN_CREATE 0x00000100 /* Subfile was created. */
|
#define IN_CREATE 0x00000100 /* Subfile was created. */
|
||||||
#define IN_DELETE 0x00000200 /* Subfile was deleted. */
|
#define IN_DELETE 0x00000200 /* Subfile was deleted. */
|
||||||
#define IN_DELETE_SELF 0x00000400 /* Self was deleted. */
|
#define IN_DELETE_SELF 0x00000400 /* Self was deleted. */
|
||||||
#define IN_MOVE_SELF 0x00000800 /* Self was moved. */
|
#define IN_MOVE_SELF 0x00000800 /* Self was moved. */
|
||||||
|
|
||||||
/* Events sent by the kernel. */
|
/* Events sent by the kernel. */
|
||||||
#define IN_UNMOUNT 0x00002000 /* Backing fs was unmounted. */
|
#define IN_UNMOUNT 0x00002000 /* Backing fs was unmounted. */
|
||||||
#define IN_Q_OVERFLOW 0x00004000 /* Event queued overflowed. */
|
#define IN_Q_OVERFLOW 0x00004000 /* Event queued overflowed. */
|
||||||
#define IN_IGNORED 0x00008000 /* File was ignored. */
|
#define IN_IGNORED 0x00008000 /* File was ignored. */
|
||||||
|
|
||||||
/* Helper events. */
|
/* Helper events. */
|
||||||
#define IN_CLOSE (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) /* Close. */
|
#define IN_CLOSE (IN_CLOSE_WRITE | IN_CLOSE_NOWRITE) /* Close. */
|
||||||
#define IN_MOVE (IN_MOVED_FROM | IN_MOVED_TO) /* Moves. */
|
#define IN_MOVE (IN_MOVED_FROM | IN_MOVED_TO) /* Moves. */
|
||||||
|
|
||||||
#define IN_ISDIR 0x40000000 /* Event occurred against dir. */
|
#define IN_ISDIR 0x40000000 /* Event occurred against dir. */
|
||||||
|
|
||||||
int inotify_backend_init(void);
|
int inotify_backend_init(void);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ static int unmap_wd(struct watch *watch) {
|
||||||
int index;
|
int index;
|
||||||
struct list s = { (void**)&watch, 1 }, *list;
|
struct list s = { (void**)&watch, 1 }, *list;
|
||||||
|
|
||||||
list = rbtree_search(&tree_wd_paths, &s);
|
list = rbtree_search(&tree_wd_paths, &s);
|
||||||
if (list) {
|
if (list) {
|
||||||
|
|
||||||
index = list_indexof(list, watch);
|
index = list_indexof(list, watch);
|
||||||
|
|
|
||||||
|
|
@ -6,13 +6,13 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#if defined(__i386__)
|
#if defined(__i386__)
|
||||||
# define __NR_inotify_init 291
|
# define __NR_inotify_init 291
|
||||||
# define __NR_inotify_add_watch 292
|
# define __NR_inotify_add_watch 292
|
||||||
# define __NR_inotify_rm_watch 293
|
# define __NR_inotify_rm_watch 293
|
||||||
#elif defined(__x86_64__)
|
#elif defined(__x86_64__)
|
||||||
# define __NR_inotify_init 253
|
# define __NR_inotify_init 253
|
||||||
# define __NR_inotify_add_watch 254
|
# define __NR_inotify_add_watch 254
|
||||||
# define __NR_inotify_rm_watch 255
|
# define __NR_inotify_rm_watch 255
|
||||||
#elif defined(__alpha__)
|
#elif defined(__alpha__)
|
||||||
# define __NR_inotify_init 444
|
# define __NR_inotify_init 444
|
||||||
# define __NR_inotify_add_watch 445
|
# define __NR_inotify_add_watch 445
|
||||||
|
|
@ -26,23 +26,23 @@
|
||||||
# define __NR_inotify_add_watch 152
|
# define __NR_inotify_add_watch 152
|
||||||
# define __NR_inotify_rm_watch 156
|
# define __NR_inotify_rm_watch 156
|
||||||
#elif defined (__ia64__)
|
#elif defined (__ia64__)
|
||||||
# define __NR_inotify_init 1277
|
# define __NR_inotify_init 1277
|
||||||
# define __NR_inotify_add_watch 1278
|
# define __NR_inotify_add_watch 1278
|
||||||
# define __NR_inotify_rm_watch 1279
|
# define __NR_inotify_rm_watch 1279
|
||||||
#elif defined (__s390__) || defined (__s390x__)
|
#elif defined (__s390__) || defined (__s390x__)
|
||||||
# define __NR_inotify_init 284
|
# define __NR_inotify_init 284
|
||||||
# define __NR_inotify_add_watch 285
|
# define __NR_inotify_add_watch 285
|
||||||
# define __NR_inotify_rm_watch 286
|
# define __NR_inotify_rm_watch 286
|
||||||
#elif defined (__arm__)
|
#elif defined (__arm__)
|
||||||
# define __NR_inotify_init 316
|
# define __NR_inotify_init 316
|
||||||
# define __NR_inotify_add_watch 317
|
# define __NR_inotify_add_watch 317
|
||||||
# define __NR_inotify_rm_watch 318
|
# define __NR_inotify_rm_watch 318
|
||||||
#elif defined (__SH4__)
|
#elif defined (__SH4__)
|
||||||
# define __NR_inotify_init 290
|
# define __NR_inotify_init 290
|
||||||
# define __NR_inotify_add_watch 291
|
# define __NR_inotify_add_watch 291
|
||||||
# define __NR_inotify_rm_watch 292
|
# define __NR_inotify_rm_watch 292
|
||||||
#elif defined (__SH5__)
|
#elif defined (__SH5__)
|
||||||
# define __NR_inotify_init 318
|
# define __NR_inotify_init 318
|
||||||
# define __NR_inotify_add_watch 319
|
# define __NR_inotify_add_watch 319
|
||||||
# define __NR_inotify_rm_watch 320
|
# define __NR_inotify_rm_watch 320
|
||||||
#else
|
#else
|
||||||
|
|
@ -56,12 +56,12 @@ static inline int inotify_init(void) {
|
||||||
|
|
||||||
static inline int inotify_add_watch(int fd, const char *name, __u32 mask) {
|
static inline int inotify_add_watch(int fd, const char *name, __u32 mask) {
|
||||||
|
|
||||||
return syscall(__NR_inotify_add_watch, fd, name, mask);
|
return syscall(__NR_inotify_add_watch, fd, name, mask);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int inotify_rm_watch(int fd, __u32 wd) {
|
static inline int inotify_rm_watch(int fd, __u32 wd) {
|
||||||
|
|
||||||
return syscall(__NR_inotify_rm_watch, fd, wd);
|
return syscall(__NR_inotify_rm_watch, fd, wd);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* _LINUX_INOTIFY_SYSCALLS_H */
|
#endif /* _LINUX_INOTIFY_SYSCALLS_H */
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ void inotify_exit(void) {
|
||||||
notify_event *e;
|
notify_event *e;
|
||||||
while((e = queue_dequeue(event_queue)))
|
while((e = queue_dequeue(event_queue)))
|
||||||
notify_event_del(e);
|
notify_event_del(e);
|
||||||
queue_destroy(event_queue);
|
queue_destroy(event_queue);
|
||||||
event_queue = NULL;
|
event_queue = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -69,13 +69,13 @@ static void proc_event(struct inotify_event *iev) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* lookup watch descriptors */
|
/* lookup watch descriptors */
|
||||||
watch_list = inotify_map_get_path(iev->wd);
|
watch_list = inotify_map_get_path(iev->wd);
|
||||||
|
|
||||||
if (!watch_list) {
|
if (!watch_list) {
|
||||||
logmsg(LOG_WARN, "-- IGNORING EVENT -- invalid watchdescriptor %i", iev->wd);
|
logmsg(LOG_WARN, "-- IGNORING EVENT -- invalid watchdescriptor %i", iev->wd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(i=0; i < watch_list->nr; i++) {
|
for(i=0; i < watch_list->nr; i++) {
|
||||||
|
|
||||||
|
|
|
||||||
50
src/list.c
50
src/list.c
|
|
@ -24,10 +24,10 @@ static void resize(struct list *l) {
|
||||||
|
|
||||||
struct list* list_create(void) {
|
struct list* list_create(void) {
|
||||||
|
|
||||||
struct list *list = xmalloc(sizeof(struct list));
|
struct list *list = xmalloc(sizeof(struct list));
|
||||||
list->items = NULL;
|
list->items = NULL;
|
||||||
list->nr = 0;
|
list->nr = 0;
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct list* list_copy(struct list *list) {
|
struct list* list_copy(struct list *list) {
|
||||||
|
|
@ -69,8 +69,8 @@ void list_clear_fn(struct list *list, clear_fn_t *fn) {
|
||||||
|
|
||||||
int list_insert(struct list *list, const void *item) {
|
int list_insert(struct list *list, const void *item) {
|
||||||
|
|
||||||
if (!list)
|
if (!list)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
list->items = xrealloc(list->items, sizeof(list->items) * (++list->nr));
|
list->items = xrealloc(list->items, sizeof(list->items) * (++list->nr));
|
||||||
list->items[list->nr - 1] = (void *) item;
|
list->items[list->nr - 1] = (void *) item;
|
||||||
|
|
@ -80,7 +80,7 @@ int list_insert(struct list *list, const void *item) {
|
||||||
|
|
||||||
void* list_remove(struct list *list, unsigned index) {
|
void* list_remove(struct list *list, unsigned index) {
|
||||||
|
|
||||||
void *item = NULL;
|
void *item = NULL;
|
||||||
|
|
||||||
if (list && index < list->nr) {
|
if (list && index < list->nr) {
|
||||||
if (index < --list->nr) {
|
if (index < --list->nr) {
|
||||||
|
|
@ -105,29 +105,29 @@ void* list_reduce(struct list *list) {
|
||||||
|
|
||||||
int list_indexof(struct list *list, const void *item) {
|
int list_indexof(struct list *list, const void *item) {
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for(i=0; i < list->nr; i++) {
|
for(i=0; i < list->nr; i++) {
|
||||||
|
|
||||||
if (list->items[i] == item)
|
if (list->items[i] == item)
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* list_lookup(struct list *list, const void *item, cmp_fn_t *fn) {
|
void* list_lookup(struct list *list, const void *item, cmp_fn_t *fn) {
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (fn) {
|
if (fn) {
|
||||||
for(i=0; i < list->nr; i++) {
|
for(i=0; i < list->nr; i++) {
|
||||||
if (fn(list->items[i], item) == 0)
|
if (fn(list->items[i], item) == 0)
|
||||||
return list->items[i];
|
return list->items[i];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
i = list_indexof(list, item);
|
i = list_indexof(list, item);
|
||||||
if (i >= 0)
|
if (i >= 0)
|
||||||
return list->items[i];
|
return list->items[i];
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ unsigned logstrtolvl(const char *str) {
|
||||||
|
|
||||||
void logmsg(unsigned level, const char *fmt, ...) {
|
void logmsg(unsigned level, const char *fmt, ...) {
|
||||||
|
|
||||||
va_list vl;
|
va_list vl;
|
||||||
|
|
||||||
if (!validmask(level, 1))
|
if (!validmask(level, 1))
|
||||||
die("log: invalid level: %x\n", level);
|
die("log: invalid level: %x\n", level);
|
||||||
|
|
@ -155,9 +155,9 @@ void logmsg(unsigned level, const char *fmt, ...) {
|
||||||
|
|
||||||
void logerrno(unsigned level, const char *prefix, int err) {
|
void logerrno(unsigned level, const char *prefix, int err) {
|
||||||
|
|
||||||
char *str = strerror(err);
|
char *str = strerror(err);
|
||||||
|
|
||||||
if (str && level & mask) {
|
if (str && level & mask) {
|
||||||
|
|
||||||
if (!validmask(level, 1))
|
if (!validmask(level, 1))
|
||||||
die("logerrno: invalid level: %x\n", level);
|
die("logerrno: invalid level: %x\n", level);
|
||||||
|
|
@ -169,5 +169,5 @@ void logerrno(unsigned level, const char *prefix, int err) {
|
||||||
fputs(str, logfd);
|
fputs(str, logfd);
|
||||||
fputc('\n', logfd);
|
fputc('\n', logfd);
|
||||||
fflush(logfd);
|
fflush(logfd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -93,13 +93,13 @@ static int rmwatch(const char *path, const char *name) {
|
||||||
|
|
||||||
int notify_init() {
|
int notify_init() {
|
||||||
|
|
||||||
if (!init) {
|
if (!init) {
|
||||||
if (inotify_init() < 0)
|
if (inotify_init() < 0)
|
||||||
return -1;
|
return -1;
|
||||||
init_ev_q = queue_init();
|
init_ev_q = queue_init();
|
||||||
init = 1;
|
init = 1;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void notify_exit() {
|
void notify_exit() {
|
||||||
|
|
@ -108,7 +108,7 @@ void notify_exit() {
|
||||||
notify_event *e;
|
notify_event *e;
|
||||||
while((e = queue_dequeue(init_ev_q)))
|
while((e = queue_dequeue(init_ev_q)))
|
||||||
notify_event_del(e);
|
notify_event_del(e);
|
||||||
queue_destroy(init_ev_q);
|
queue_destroy(init_ev_q);
|
||||||
init_ev_q = NULL;
|
init_ev_q = NULL;
|
||||||
|
|
||||||
inotify_exit();
|
inotify_exit();
|
||||||
|
|
@ -129,9 +129,9 @@ int notify_add_watch(const char *path) {
|
||||||
|
|
||||||
int notify_rm_watch(const char *path) {
|
int notify_rm_watch(const char *path) {
|
||||||
|
|
||||||
if (!init)
|
if (!init)
|
||||||
die("inotify is not instantiated.");
|
die("inotify is not instantiated.");
|
||||||
return rmwatch(path, NULL);
|
return rmwatch(path, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
notify_event* notify_read() {
|
notify_event* notify_read() {
|
||||||
|
|
|
||||||
14
src/path.c
14
src/path.c
|
|
@ -25,16 +25,16 @@ static char path_null = '\0';
|
||||||
|
|
||||||
static inline int has_delim(const char *str) {
|
static inline int has_delim(const char *str) {
|
||||||
|
|
||||||
if (str == NULL)
|
if (str == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return strchr(str, '/') != NULL;
|
return strchr(str, '/') != NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_abspath(const char *path) {
|
int is_abspath(const char *path) {
|
||||||
|
|
||||||
if (path == NULL || *path != '/')
|
if (path == NULL || *path != '/')
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
for(; *path; path++) {
|
for(; *path; path++) {
|
||||||
|
|
||||||
|
|
@ -47,7 +47,7 @@ int is_abspath(const char *path) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_file(const char *path) {
|
int is_file(const char *path) {
|
||||||
|
|
@ -147,7 +147,7 @@ char* path_normalize(const char *base, const char *name, unsigned dir) {
|
||||||
strbuf_t sb = STRBUF_INIT;
|
strbuf_t sb = STRBUF_INIT;
|
||||||
|
|
||||||
if (base == NULL || has_delim(name))
|
if (base == NULL || has_delim(name))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (*base == '~') {
|
if (*base == '~') {
|
||||||
base++;
|
base++;
|
||||||
|
|
|
||||||
19
src/queue.c
19
src/queue.c
|
|
@ -19,7 +19,7 @@
|
||||||
/* linked list node holding a chunk of queue elements */
|
/* linked list node holding a chunk of queue elements */
|
||||||
struct node {
|
struct node {
|
||||||
void *block[BLOCK_SIZE];
|
void *block[BLOCK_SIZE];
|
||||||
struct node *next;
|
struct node *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ref {
|
struct ref {
|
||||||
|
|
@ -28,22 +28,22 @@ struct ref {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct __queue {
|
struct __queue {
|
||||||
struct ref tail;
|
struct ref tail;
|
||||||
struct ref head;
|
struct ref head;
|
||||||
};
|
};
|
||||||
|
|
||||||
static void alloc_node(struct ref *head) {
|
static void alloc_node(struct ref *head) {
|
||||||
|
|
||||||
struct node *n = xmalloc(sizeof(struct node));
|
struct node *n = xmalloc(sizeof(struct node));
|
||||||
|
|
||||||
n->next = NULL;
|
n->next = NULL;
|
||||||
head->n = head->n->next = n;
|
head->n = head->n->next = n;
|
||||||
head->i = 0;
|
head->i = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void dealloc_node(struct ref *tail) {
|
static void dealloc_node(struct ref *tail) {
|
||||||
|
|
||||||
struct node *next = tail->n->next;
|
struct node *next = tail->n->next;
|
||||||
|
|
||||||
if (next) {
|
if (next) {
|
||||||
xfree(tail->n);
|
xfree(tail->n);
|
||||||
|
|
@ -81,10 +81,10 @@ void queue_enqueue(queue_t q, void *obj) {
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (q->head.i >= BLOCK_SIZE) {
|
if (q->head.i >= BLOCK_SIZE) {
|
||||||
alloc_node(&q->head);
|
alloc_node(&q->head);
|
||||||
}
|
}
|
||||||
|
|
||||||
q->head.n->block[q->head.i++] = obj;
|
q->head.n->block[q->head.i++] = obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
void* queue_dequeue(queue_t q) {
|
void* queue_dequeue(queue_t q) {
|
||||||
|
|
@ -129,4 +129,3 @@ size_t queue_num_items(queue_t q) {
|
||||||
len += BLOCK_SIZE;
|
len += BLOCK_SIZE;
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
264
src/rbtree.c
264
src/rbtree.c
|
|
@ -24,8 +24,8 @@
|
||||||
/* node definition */
|
/* node definition */
|
||||||
typedef struct _rbn {
|
typedef struct _rbn {
|
||||||
const void *key;
|
const void *key;
|
||||||
struct _rbn *child[2];
|
struct _rbn *child[2];
|
||||||
unsigned char color;
|
unsigned char color;
|
||||||
} rbnode;
|
} rbnode;
|
||||||
|
|
||||||
#define is_red(n) ((n) != NULL && (n)->color == RB_RED)
|
#define is_red(n) ((n) != NULL && (n)->color == RB_RED)
|
||||||
|
|
@ -33,14 +33,14 @@ typedef struct _rbn {
|
||||||
|
|
||||||
static rbnode* node_alloc(const void *key) {
|
static rbnode* node_alloc(const void *key) {
|
||||||
|
|
||||||
rbnode *n = xmalloc(sizeof(rbnode));
|
rbnode *n = xmalloc(sizeof(rbnode));
|
||||||
|
|
||||||
n->key = key;
|
n->key = key;
|
||||||
n->color = RB_RED;
|
n->color = RB_RED;
|
||||||
n->child[0] = NULL;
|
n->child[0] = NULL;
|
||||||
n->child[1] = NULL;
|
n->child[1] = NULL;
|
||||||
|
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -48,16 +48,16 @@ static rbnode* node_alloc(const void *key) {
|
||||||
*/
|
*/
|
||||||
static void node_dealloc(rbnode *n, void (*dealloc)(void *)) {
|
static void node_dealloc(rbnode *n, void (*dealloc)(void *)) {
|
||||||
|
|
||||||
if (!n)
|
if (!n)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (dealloc)
|
if (dealloc)
|
||||||
dealloc((void *)n->key);
|
dealloc((void *)n->key);
|
||||||
|
|
||||||
node_dealloc(n->child[0], dealloc);
|
node_dealloc(n->child[0], dealloc);
|
||||||
node_dealloc(n->child[1], dealloc);
|
node_dealloc(n->child[1], dealloc);
|
||||||
|
|
||||||
free(n);
|
free(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __DEBUG__
|
#ifdef __DEBUG__
|
||||||
|
|
@ -65,43 +65,43 @@ static void node_dealloc(rbnode *n, void (*dealloc)(void *)) {
|
||||||
|
|
||||||
static int rb_assert_r(rbnode *node, int (*cmp)(const void *, const void *)) {
|
static int rb_assert_r(rbnode *node, int (*cmp)(const void *, const void *)) {
|
||||||
|
|
||||||
int rh, lh;
|
int rh, lh;
|
||||||
rbnode *ln, *rn;
|
rbnode *ln, *rn;
|
||||||
|
|
||||||
if (node == NULL)
|
if (node == NULL)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
ln = node->child[0];
|
ln = node->child[0];
|
||||||
rn = node->child[1];
|
rn = node->child[1];
|
||||||
|
|
||||||
if (is_red(node)) {
|
if (is_red(node)) {
|
||||||
/* double red violation */
|
/* double red violation */
|
||||||
if (is_red(ln) || is_red(rn)) {
|
if (is_red(ln) || is_red(rn)) {
|
||||||
rb_err("Double red violation");
|
rb_err("Double red violation");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
lh = rb_assert_r(ln, cmp);
|
lh = rb_assert_r(ln, cmp);
|
||||||
rh = rb_assert_r(rn, cmp);
|
rh = rb_assert_r(rn, cmp);
|
||||||
|
|
||||||
if ( (ln && cmp(ln->key, node->key) >= 0) &&
|
if ( (ln && cmp(ln->key, node->key) >= 0) &&
|
||||||
(rn && cmp(rn->key, node->key) <= 0) ) {
|
(rn && cmp(rn->key, node->key) <= 0) ) {
|
||||||
rb_err("Binary tree violation");
|
rb_err("Binary tree violation");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rh != 0 && lh != 0) {
|
if (rh != 0 && lh != 0) {
|
||||||
|
|
||||||
if (rh != lh) {
|
if (rh != lh) {
|
||||||
rb_err("Black height violation");
|
rb_err("Black height violation");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return is_red(node) ? lh : lh+1;
|
return is_red(node) ? lh : lh+1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
#undef rb_err
|
#undef rb_err
|
||||||
|
|
||||||
|
|
@ -119,35 +119,35 @@ static void rb_assert(rbtree *tree) {
|
||||||
*/
|
*/
|
||||||
static void rbwalk(rbnode *n, void (*action)(const void *)) {
|
static void rbwalk(rbnode *n, void (*action)(const void *)) {
|
||||||
|
|
||||||
if (!n)
|
if (!n)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
rbwalk(n->child[0], action);
|
rbwalk(n->child[0], action);
|
||||||
action(n->key);
|
action(n->key);
|
||||||
rbwalk(n->child[1], action);
|
rbwalk(n->child[1], action);
|
||||||
}
|
}
|
||||||
|
|
||||||
static rbnode* rotate_single(rbnode *root, int dir) {
|
static rbnode* rotate_single(rbnode *root, int dir) {
|
||||||
|
|
||||||
rbnode *save = root->child[!dir];
|
rbnode *save = root->child[!dir];
|
||||||
|
|
||||||
root->child[!dir] = save->child[dir];
|
root->child[!dir] = save->child[dir];
|
||||||
save->child[dir] = root;
|
save->child[dir] = root;
|
||||||
|
|
||||||
root->color = RB_RED;
|
root->color = RB_RED;
|
||||||
save->color = RB_BLACK;
|
save->color = RB_BLACK;
|
||||||
|
|
||||||
return save;
|
return save;
|
||||||
}
|
}
|
||||||
|
|
||||||
static rbnode* rotate_double(rbnode *root, int dir) {
|
static rbnode* rotate_double(rbnode *root, int dir) {
|
||||||
root->child[!dir] = rotate_single(root->child[!dir], !dir);
|
root->child[!dir] = rotate_single(root->child[!dir], !dir);
|
||||||
return rotate_single(root, dir);
|
return rotate_single(root, dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
int rbtree_is_empty(rbtree *tree) {
|
int rbtree_is_empty(rbtree *tree) {
|
||||||
|
|
||||||
return tree == NULL || tree->root == NULL;
|
return tree == NULL || tree->root == NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -155,30 +155,30 @@ int rbtree_is_empty(rbtree *tree) {
|
||||||
*/
|
*/
|
||||||
void* rbtree_search(rbtree *tree, const void *key) {
|
void* rbtree_search(rbtree *tree, const void *key) {
|
||||||
|
|
||||||
rbnode *n;
|
rbnode *n;
|
||||||
|
|
||||||
if (!tree || !tree->cmp_fn)
|
if (!tree || !tree->cmp_fn)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
n = tree->root;
|
n = tree->root;
|
||||||
|
|
||||||
while(n) {
|
while(n) {
|
||||||
int cmp = tree->cmp_fn(n->key, key);
|
int cmp = tree->cmp_fn(n->key, key);
|
||||||
|
|
||||||
if (cmp == 0)
|
if (cmp == 0)
|
||||||
return (void *) n->key;
|
return (void *) n->key;
|
||||||
|
|
||||||
n = n->child[cmp < 0];
|
n = n->child[cmp < 0];
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rbtree_walk(rbtree *tree, void (*action)(const void *)) {
|
void rbtree_walk(rbtree *tree, void (*action)(const void *)) {
|
||||||
|
|
||||||
if (!tree || !action)
|
if (!tree || !action)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
rbwalk(tree->root, action);
|
rbwalk(tree->root, action);
|
||||||
|
|
||||||
#ifdef __DEBUG__
|
#ifdef __DEBUG__
|
||||||
rb_assert(tree);
|
rb_assert(tree);
|
||||||
|
|
@ -187,11 +187,11 @@ void rbtree_walk(rbtree *tree, void (*action)(const void *)) {
|
||||||
|
|
||||||
void rbtree_free(rbtree *tree, void (*free_fn)(void *)) {
|
void rbtree_free(rbtree *tree, void (*free_fn)(void *)) {
|
||||||
|
|
||||||
if (!tree)
|
if (!tree)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
node_dealloc(tree->root, free_fn);
|
node_dealloc(tree->root, free_fn);
|
||||||
tree->root = NULL;
|
tree->root = NULL;
|
||||||
|
|
||||||
#ifdef __DEBUG__
|
#ifdef __DEBUG__
|
||||||
rb_assert(tree);
|
rb_assert(tree);
|
||||||
|
|
@ -200,70 +200,70 @@ void rbtree_free(rbtree *tree, void (*free_fn)(void *)) {
|
||||||
|
|
||||||
int rbtree_insert(rbtree *tree, const void *key) {
|
int rbtree_insert(rbtree *tree, const void *key) {
|
||||||
|
|
||||||
rbnode head = {0};
|
rbnode head = {0};
|
||||||
|
|
||||||
/* grandparent and parent */
|
/* grandparent and parent */
|
||||||
rbnode *g, *t;
|
rbnode *g, *t;
|
||||||
|
|
||||||
/* iterator and parent */
|
/* iterator and parent */
|
||||||
rbnode *p, *q;
|
rbnode *p, *q;
|
||||||
|
|
||||||
int dir = 0, last = 0;
|
int dir = 0, last = 0;
|
||||||
|
|
||||||
if (!tree || !tree->cmp_fn)
|
if (!tree || !tree->cmp_fn)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (!tree->root) {
|
if (!tree->root) {
|
||||||
tree->root = q = node_alloc(key);
|
tree->root = q = node_alloc(key);
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
t = &head;
|
t = &head;
|
||||||
g = p = NULL;
|
g = p = NULL;
|
||||||
q = t->child[1] = tree->root;
|
q = t->child[1] = tree->root;
|
||||||
|
|
||||||
/* somewhere in here, there should be dragons */
|
/* somewhere in here, there should be dragons */
|
||||||
|
|
||||||
for(;;) {
|
for(;;) {
|
||||||
int cmp;
|
int cmp;
|
||||||
|
|
||||||
if (q == NULL) {
|
if (q == NULL) {
|
||||||
p->child[dir] = q = node_alloc(key);
|
p->child[dir] = q = node_alloc(key);
|
||||||
} else if (is_red(q->child[0]) && is_red(q->child[1])) {
|
} else if (is_red(q->child[0]) && is_red(q->child[1])) {
|
||||||
/* color flip case */
|
/* color flip case */
|
||||||
q->color = RB_RED;
|
q->color = RB_RED;
|
||||||
q->child[0]->color = RB_BLACK;
|
q->child[0]->color = RB_BLACK;
|
||||||
q->child[1]->color = RB_BLACK;
|
q->child[1]->color = RB_BLACK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* fix red validation */
|
/* fix red validation */
|
||||||
if (is_red(q) && is_red(p)) {
|
if (is_red(q) && is_red(p)) {
|
||||||
int dir2 = (t->child[1] == g);
|
int dir2 = (t->child[1] == g);
|
||||||
if (q == p->child[last])
|
if (q == p->child[last])
|
||||||
t->child[dir2] = rotate_single(g, !last);
|
t->child[dir2] = rotate_single(g, !last);
|
||||||
else
|
else
|
||||||
t->child[dir2] = rotate_double(g, !last);
|
t->child[dir2] = rotate_double(g, !last);
|
||||||
}
|
}
|
||||||
|
|
||||||
cmp = tree->cmp_fn(q->key, key);
|
cmp = tree->cmp_fn(q->key, key);
|
||||||
|
|
||||||
if (cmp == 0)
|
if (cmp == 0)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
last = dir;
|
last = dir;
|
||||||
dir = cmp < 0;
|
dir = cmp < 0;
|
||||||
|
|
||||||
if (g)
|
if (g)
|
||||||
t = g;
|
t = g;
|
||||||
g = p, p = q;
|
g = p, p = q;
|
||||||
q = q->child[dir];
|
q = q->child[dir];
|
||||||
}
|
}
|
||||||
|
|
||||||
tree->root = head.child[1];
|
tree->root = head.child[1];
|
||||||
|
|
||||||
done:
|
done:
|
||||||
/* root should be black */
|
/* root should be black */
|
||||||
tree->root->color = RB_BLACK;
|
tree->root->color = RB_BLACK;
|
||||||
|
|
||||||
#ifdef __DEBUG__
|
#ifdef __DEBUG__
|
||||||
rb_assert(tree);
|
rb_assert(tree);
|
||||||
|
|
@ -274,38 +274,38 @@ done:
|
||||||
|
|
||||||
void* rbtree_delete(rbtree *tree, const void *key) {
|
void* rbtree_delete(rbtree *tree, const void *key) {
|
||||||
|
|
||||||
rbnode head = {0};
|
rbnode head = {0};
|
||||||
|
|
||||||
/* helpers*/
|
/* helpers*/
|
||||||
rbnode *q, *p, *g, *s;
|
rbnode *q, *p, *g, *s;
|
||||||
|
|
||||||
/* found item */
|
/* found item */
|
||||||
rbnode *f = NULL, *ret = NULL;
|
rbnode *f = NULL, *ret = NULL;
|
||||||
|
|
||||||
int dir = 1, dir2, last;
|
int dir = 1, dir2, last;
|
||||||
|
|
||||||
if (rbtree_is_empty(tree) || !tree->cmp_fn)
|
if (rbtree_is_empty(tree) || !tree->cmp_fn)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
q = &head;
|
q = &head;
|
||||||
g = p = NULL;
|
g = p = NULL;
|
||||||
q->child[1] = tree->root;
|
q->child[1] = tree->root;
|
||||||
|
|
||||||
/* more dragons (killed some of them though) */
|
/* more dragons (killed some of them though) */
|
||||||
|
|
||||||
while(q->child[dir]) {
|
while(q->child[dir]) {
|
||||||
int cmp;
|
int cmp;
|
||||||
|
|
||||||
g = p, p = q;
|
g = p, p = q;
|
||||||
q = q->child[dir];
|
q = q->child[dir];
|
||||||
last = dir;
|
last = dir;
|
||||||
|
|
||||||
cmp = tree->cmp_fn(q->key, key);
|
cmp = tree->cmp_fn(q->key, key);
|
||||||
dir = cmp < 0;
|
dir = cmp < 0;
|
||||||
if (cmp == 0)
|
if (cmp == 0)
|
||||||
f = q;
|
f = q;
|
||||||
|
|
||||||
if (is_red(q) || is_red(q->child[dir]))
|
if (is_red(q) || is_red(q->child[dir]))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (is_red(q->child[!dir])) {
|
if (is_red(q->child[!dir])) {
|
||||||
|
|
@ -331,25 +331,25 @@ void* rbtree_delete(rbtree *tree, const void *key) {
|
||||||
g->child[dir2]->child[0]->color = RB_BLACK;
|
g->child[dir2]->child[0]->color = RB_BLACK;
|
||||||
g->child[dir2]->child[1]->color = RB_BLACK;
|
g->child[dir2]->child[1]->color = RB_BLACK;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* remove if found */
|
/* remove if found */
|
||||||
if (f) {
|
if (f) {
|
||||||
ret = (void*)f->key;
|
ret = (void*)f->key;
|
||||||
if (f != q)
|
if (f != q)
|
||||||
f->key = q->key;
|
f->key = q->key;
|
||||||
swap(p, 1, q) = swap(q, 0, NULL);
|
swap(p, 1, q) = swap(q, 0, NULL);
|
||||||
xfree(q);
|
xfree(q);
|
||||||
}
|
}
|
||||||
|
|
||||||
tree->root = head.child[1];
|
tree->root = head.child[1];
|
||||||
if (tree->root)
|
if (tree->root)
|
||||||
tree->root->color = RB_BLACK;
|
tree->root->color = RB_BLACK;
|
||||||
|
|
||||||
#ifdef __DEBUG__
|
#ifdef __DEBUG__
|
||||||
rb_assert(tree);
|
rb_assert(tree);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
struct _rbn *root;
|
struct _rbn *root;
|
||||||
/* user defined operations */
|
/* user defined operations */
|
||||||
int (*cmp_fn)(const void *, const void *);
|
int (*cmp_fn)(const void *, const void *);
|
||||||
} rbtree;
|
} rbtree;
|
||||||
|
|
|
||||||
|
|
@ -167,5 +167,3 @@ char** str_list_export(struct str_list *list) {
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
50
src/strbuf.c
50
src/strbuf.c
|
|
@ -22,8 +22,8 @@ char strbuf_null = '\0';
|
||||||
|
|
||||||
void strbuf_init(strbuf_t *s) {
|
void strbuf_init(strbuf_t *s) {
|
||||||
|
|
||||||
s->buf = &strbuf_null;
|
s->buf = &strbuf_null;
|
||||||
s->alloc_size = s->len = 0;
|
s->alloc_size = s->len = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t strbuf_avail(strbuf_t *s) {
|
size_t strbuf_avail(strbuf_t *s) {
|
||||||
|
|
@ -34,13 +34,13 @@ size_t strbuf_avail(strbuf_t *s) {
|
||||||
void strbuf_expand(strbuf_t *s, size_t len) {
|
void strbuf_expand(strbuf_t *s, size_t len) {
|
||||||
|
|
||||||
if (s->len + len + 1 < s->alloc_size)
|
if (s->len + len + 1 < s->alloc_size)
|
||||||
return;
|
return;
|
||||||
if (!s->alloc_size)
|
if (!s->alloc_size)
|
||||||
s->buf = NULL;
|
s->buf = NULL;
|
||||||
|
|
||||||
do
|
do
|
||||||
s->alloc_size += CHNK_SIZE;
|
s->alloc_size += CHNK_SIZE;
|
||||||
while(s->len + len + 1 > s->alloc_size);
|
while(s->len + len + 1 > s->alloc_size);
|
||||||
|
|
||||||
s->buf = xrealloc(s->buf, s->alloc_size);
|
s->buf = xrealloc(s->buf, s->alloc_size);
|
||||||
}
|
}
|
||||||
|
|
@ -66,18 +66,18 @@ void strbuf_setlen(strbuf_t *s, size_t len) {
|
||||||
|
|
||||||
char* strbuf_release(strbuf_t *s) {
|
char* strbuf_release(strbuf_t *s) {
|
||||||
|
|
||||||
char *ret;
|
char *ret;
|
||||||
|
|
||||||
if (!s->alloc_size)
|
if (!s->alloc_size)
|
||||||
ret = xmallocz(1);
|
ret = xmallocz(1);
|
||||||
else if (s->len + 1 != s->alloc_size)
|
else if (s->len + 1 != s->alloc_size)
|
||||||
ret = xrealloc(s->buf, s->len + 1);
|
ret = xrealloc(s->buf, s->len + 1);
|
||||||
else
|
else
|
||||||
ret = s->buf;
|
ret = s->buf;
|
||||||
|
|
||||||
strbuf_init(s);
|
strbuf_init(s);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void strbuf_free(strbuf_t *s) {
|
void strbuf_free(strbuf_t *s) {
|
||||||
|
|
@ -131,10 +131,10 @@ void strbuf_appendf(strbuf_t *s, const char *fmt, ...) {
|
||||||
va_end(va);
|
va_end(va);
|
||||||
|
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
strbuf_expand(s, len);
|
strbuf_expand(s, len);
|
||||||
va_start(va, fmt);
|
va_start(va, fmt);
|
||||||
len = strbuf_append_va(s, fmt, va);
|
len = strbuf_append_va(s, fmt, va);
|
||||||
va_end(va);
|
va_end(va);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -186,8 +186,8 @@ void strbuf_term(strbuf_t *s, char ch) {
|
||||||
|
|
||||||
void strbuf_trim(strbuf_t *s) {
|
void strbuf_trim(strbuf_t *s) {
|
||||||
|
|
||||||
strbuf_rtrim(s);
|
strbuf_rtrim(s);
|
||||||
strbuf_ltrim(s);
|
strbuf_ltrim(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
void strbuf_rtrim(strbuf_t *s) {
|
void strbuf_rtrim(strbuf_t *s) {
|
||||||
|
|
@ -201,14 +201,14 @@ void strbuf_ltrim(strbuf_t *s) {
|
||||||
|
|
||||||
size_t i, of = 0;
|
size_t i, of = 0;
|
||||||
|
|
||||||
for(; of < s->len && isspace(s->buf[of]); of++);
|
for(; of < s->len && isspace(s->buf[of]); of++);
|
||||||
|
|
||||||
if (of < 1)
|
if (of < 1)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
s->len -= of;
|
s->len -= of;
|
||||||
for(i=0; i <= s->len; i++)
|
for(i=0; i <= s->len; i++)
|
||||||
s->buf[i] = s->buf[i + of];
|
s->buf[i] = s->buf[i + of];
|
||||||
}
|
}
|
||||||
|
|
||||||
void strbuf_rev(strbuf_t *s) {
|
void strbuf_rev(strbuf_t *s) {
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@
|
||||||
# Helper for touching a number of files in a directory.
|
# Helper for touching a number of files in a directory.
|
||||||
|
|
||||||
if [ $# -lt 1 ]; then
|
if [ $# -lt 1 ]; then
|
||||||
echo "usage: $0 <dir> [ <nfiles> ]"
|
echo "usage: $0 <dir> [ <nfiles> ]"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ ! -d $1 ]; then
|
if [ ! -d $1 ]; then
|
||||||
|
|
@ -23,5 +23,5 @@ if [ $# -gt 1 ]; then
|
||||||
fi
|
fi
|
||||||
|
|
||||||
for i in `seq 0 ${NFILES}`; do
|
for i in `seq 0 ${NFILES}`; do
|
||||||
touch "${1}/file${i}"
|
touch "${1}/file${i}"
|
||||||
done
|
done
|
||||||
|
|
|
||||||
|
|
@ -5,42 +5,42 @@
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
fscrawl_t crawl;
|
fscrawl_t crawl;
|
||||||
unsigned int c_ent = 0, verbose = 0;
|
unsigned int c_ent = 0, verbose = 0;
|
||||||
time_t t1, t2, tdiff;
|
time_t t1, t2, tdiff;
|
||||||
|
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
if (argc > 2 && (argv[2][0] == '1' && argv[2][1] == '\0'))
|
if (argc > 2 && (argv[2][0] == '1' && argv[2][1] == '\0'))
|
||||||
verbose = 1;
|
verbose = 1;
|
||||||
|
|
||||||
crawl = fsc_open(argv[1]);
|
crawl = fsc_open(argv[1]);
|
||||||
|
|
||||||
if (crawl == NULL) {
|
if (crawl == NULL) {
|
||||||
printf("Invalid path\n");
|
printf("Invalid path\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
t1 = time(NULL);
|
t1 = time(NULL);
|
||||||
for(;;) {
|
for(;;) {
|
||||||
fs_entry *ent = fsc_read(crawl);
|
fs_entry *ent = fsc_read(crawl);
|
||||||
|
|
||||||
if (!ent)
|
if (!ent)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
if (verbose)
|
if (verbose)
|
||||||
printf("%s%s%c\n", ent->base, ent->name,
|
printf("%s%s%c\n", ent->base, ent->name,
|
||||||
ent->dir ? '/' : '\0');
|
ent->dir ? '/' : '\0');
|
||||||
|
|
||||||
c_ent++;
|
c_ent++;
|
||||||
}
|
}
|
||||||
t2 = time(NULL);
|
t2 = time(NULL);
|
||||||
|
|
||||||
fsc_close(crawl);
|
fsc_close(crawl);
|
||||||
|
|
||||||
tdiff = t2 - t1;
|
tdiff = t2 - t1;
|
||||||
printf("Nodes: %u\n"
|
printf("Nodes: %u\n"
|
||||||
"Time (sec): %ld\n", c_ent, tdiff);
|
"Time (sec): %ld\n", c_ent, tdiff);
|
||||||
|
|
||||||
printf("Node/sec: ");
|
printf("Node/sec: ");
|
||||||
|
|
@ -50,5 +50,5 @@ int main(int argc, char *argv[]) {
|
||||||
puts("INF");
|
puts("INF");
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,33 +4,34 @@
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
notify_event *event;
|
notify_event *event;
|
||||||
|
|
||||||
if (argc < 2)
|
if (argc < 2)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
notify_init();
|
notify_init();
|
||||||
|
|
||||||
if (!notify_add_watch(argv[1]))
|
if (!notify_add_watch(argv[1]))
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
printf("begin watching on: %s\n", argv[1]);
|
printf("begin watching on: %s\n", argv[1]);
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|
||||||
event = notify_read();
|
event = notify_read();
|
||||||
|
|
||||||
if (event == NULL)
|
if (event == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
printf("====================\n"
|
printf("====================\n"
|
||||||
"Type: %s\n"
|
"Type: %s\n"
|
||||||
"Path: %s\n"
|
"Path: %s\n"
|
||||||
"Filename: %s\n"
|
"Filename: %s\n"
|
||||||
"Directory: %u\n"
|
"Directory: %u\n"
|
||||||
"====================\n"
|
"====================\n"
|
||||||
, notify_event_typetostr(event), event->path, event->filename, event->dir);
|
, notify_event_typetostr(event), event->path,
|
||||||
}
|
event->filename, event->dir);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -95,5 +95,5 @@ int main() {
|
||||||
if (q)
|
if (q)
|
||||||
queue_destroy(q);
|
queue_destroy(q);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -61,9 +61,9 @@ void test_release() {
|
||||||
|
|
||||||
str = strbuf_release(&b);
|
str = strbuf_release(&b);
|
||||||
|
|
||||||
printf("released |%s|\n", str);
|
printf("released |%s|\n", str);
|
||||||
|
|
||||||
free(str);
|
free(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_release_empty() {
|
void test_release_empty() {
|
||||||
|
|
@ -144,7 +144,7 @@ void test_trim() {
|
||||||
strbuf_append_repeat(&b, ' ', 4);
|
strbuf_append_repeat(&b, ' ', 4);
|
||||||
strbuf_append(&b, "abcdef", 6);
|
strbuf_append(&b, "abcdef", 6);
|
||||||
|
|
||||||
print_strbuf(&b);
|
print_strbuf(&b);
|
||||||
|
|
||||||
strbuf_append(&b, "012345678901234567890123456789", 30);
|
strbuf_append(&b, "012345678901234567890123456789", 30);
|
||||||
strbuf_append_ch(&b, 'a');
|
strbuf_append_ch(&b, 'a');
|
||||||
|
|
@ -156,9 +156,9 @@ void test_trim() {
|
||||||
|
|
||||||
print_strbuf(&b);
|
print_strbuf(&b);
|
||||||
|
|
||||||
strbuf_ltrim(&b);
|
strbuf_ltrim(&b);
|
||||||
|
|
||||||
print_strbuf(&b);
|
print_strbuf(&b);
|
||||||
|
|
||||||
strbuf_free(&b);
|
strbuf_free(&b);
|
||||||
}
|
}
|
||||||
|
|
@ -171,9 +171,9 @@ void test_rev() {
|
||||||
|
|
||||||
print_strbuf(&b);
|
print_strbuf(&b);
|
||||||
|
|
||||||
strbuf_rev(&b);
|
strbuf_rev(&b);
|
||||||
|
|
||||||
print_strbuf(&b);
|
print_strbuf(&b);
|
||||||
|
|
||||||
strbuf_free(&b);
|
strbuf_free(&b);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ void __assert_str(const char *file, int line, const char *func, const char *a, c
|
||||||
|
|
||||||
if (strcmp(a, b) != 0)
|
if (strcmp(a, b) != 0)
|
||||||
__uexit(file, line, func, "\"%s\" != \"%s\"\n", a, b);
|
__uexit(file, line, func, "\"%s\" != \"%s\"\n", a, b);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void utest_init_RNG() {
|
void utest_init_RNG() {
|
||||||
|
|
|
||||||
Reference in a new issue