diff --git a/Makefile b/Makefile index 5be6660..3a4ef86 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,7 @@ SOURCES := \ src/arch.c \ src/arch/mysql.c \ src/indexer.c \ + src/common/strbuf.c \ src/common/path.c \ src/common/rbtree.c \ src/notify/inotify.c \ diff --git a/TODO b/TODO index 0523655..2116158 100644 --- a/TODO +++ b/TODO @@ -4,7 +4,5 @@ ################## * Use queue in inotify_read(), tree and remove indexer - * path: - * normalize * ini-like config support * fix mysql module diff --git a/src/common/path.c b/src/common/path.c index 6e9516e..e1b4c01 100644 --- a/src/common/path.c +++ b/src/common/path.c @@ -15,6 +15,7 @@ #include #include "debug.h" +#include "strbuf.h" #include "path.h" /* @@ -117,39 +118,26 @@ size_t pathlen(const char *path) { return size; } -char* fmt_path(const char *base, const char *name, unsigned char dir) { +char* path_normalize(const char *base, const char *name, unsigned char dir) { - char *ptr, *ret; - size_t size; - - if (base == NULL || !is_abspath(base) || has_delim(name)) + strbuf_t sb = STRBUF_INIT; + + if (base == NULL || !is_abspath(base) || has_delim(name)) return NULL; - - size = pathlen(base); - - if (name != NULL) { - size += strlen(name); - if (dir) - size++; - } - if (*(base+size) != '/') - size++; + strbuf_append_str(&sb, base); - ptr = alloc_path(size); + if (sb.buf[sb.len] != '/') + strbuf_append_ch(&sb, '/'); + + if (name) { + strbuf_append_str(&sb, name); - if (ptr == NULL) - return NULL; - - ret = ptr; - - ptr = cpy_path(ptr, base); + if (dir) + strbuf_append_ch(&sb, '/'); + } - if (name != NULL) { - memcpy(ptr, name, strlen(name)); - if (dir) - *(ptr+strlen(name)) = '/'; - } - - return ret; + strbuf_squeeze(&sb, '/'); + + return strbuf_release(&sb); } diff --git a/src/common/path.h b/src/common/path.h index ee195f5..c229b2b 100644 --- a/src/common/path.h +++ b/src/common/path.h @@ -18,6 +18,6 @@ int is_abspath(const char *path); size_t pathlen(const char *path); -char* fmt_path(const char *base, const char *name, unsigned char dir); +char* path_normalize(const char *base, const char *name, unsigned char dir); #endif /* __COMMON_PATH_H */ diff --git a/src/indexer.c b/src/indexer.c index e0a5c0d..43009f4 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -87,7 +87,7 @@ static int rm() { void indexer_register(const char *base, const char *name) { - char *fullpath = fmt_path(base, name, 1); + char *fullpath = path_normalize(base, name, 1); if (!indexer_pending()) { #if __DEBUG__ diff --git a/src/notify/inotify.c b/src/notify/inotify.c index e5400dd..8fe5a62 100644 --- a/src/notify/inotify.c +++ b/src/notify/inotify.c @@ -53,7 +53,7 @@ static int addwatch(const char *path, const char *name) { char *cpath; int wd; - cpath = fmt_path(path, name, 1); + cpath = path_normalize(path, name, 1); if (cpath == NULL) return -1; diff --git a/src/notify/tree.c b/src/notify/tree.c index b35f356..62012c4 100644 --- a/src/notify/tree.c +++ b/src/notify/tree.c @@ -43,7 +43,7 @@ struct tree* tree_new(const char *path) { if (t == NULL) return NULL; - t->path = fmt_path(path, NULL, 0); + t->path = path_normalize(path, NULL, 0); if (t->path == NULL) { free(t); diff --git a/test/Makefile b/test/Makefile index 22ef9b0..5ba8c87 100644 --- a/test/Makefile +++ b/test/Makefile @@ -12,7 +12,7 @@ strbuf : $(CC) $(CFLAGS) ../src/common/strbuf.c t_strbuf.c -o test_strbuf path : - $(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/path.c t_path.c -o test_path + $(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/strbuf.c ../src/common/path.c t_path.c -o test_path rbtree : $(CC) $(DEFS) $(CFLAGS) unit.c ../src/common/rbtree.c t_rbtree.c -o test_rbtree diff --git a/test/t_path.c b/test/t_path.c index 85254d9..1d1e478 100644 --- a/test/t_path.c +++ b/test/t_path.c @@ -4,26 +4,26 @@ #include "unit.h" #include "../src/common/path.h" -void test_fmt_path() { +void test_normalize() { char *ptr; - ptr = fmt_path("usr/", "include/", 0); + ptr = path_normalize("usr/", "include/", 0); assert(ptr == NULL); - ptr = fmt_path("/usr/src/", "linux", 0); + ptr = path_normalize("/usr/src/", "linux", 0); assert_string(ptr, "/usr/src/linux"); free(ptr); - ptr = fmt_path("/segment1/segment2/", "segment3", 1); + ptr = path_normalize("/segment1///segment2//", "segment3", 1); assert_string(ptr, "/segment1/segment2/segment3/"); free(ptr); - ptr = fmt_path("/stuff/with/ahell/lot/of/slashes/at/the/", "end", 1); + ptr = path_normalize("/stuff/with/ahell/lot/of/slashes/at/the///", "end", 1); assert_string(ptr, "/stuff/with/ahell/lot/of/slashes/at/the/end/"); free(ptr); - ptr = fmt_path("/mnt/cdrom", "keff", 0); + ptr = path_normalize("/mnt/cdrom", "keff", 0); assert_string(ptr, "/mnt/cdrom/keff"); free(ptr); } @@ -98,7 +98,7 @@ void test_dirname() { int main(int argc, char *argv[]) { test_isabspath(); - test_fmt_path(); + test_normalize(); test_pathlen(); test_basename(); test_dirname();