From 6eb2d1c2e76e28f3f724a594d3d249ffdc4d7f61 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Mon, 31 Aug 2020 16:02:10 +0800 Subject: [PATCH] shared_func.[hc]: add function fc_path_contains --- HISTORY | 3 +- src/shared_func.c | 78 +++++++++++++++++++++++++++++++++++++++++++++-- src/shared_func.h | 10 ++++++ 3 files changed, 87 insertions(+), 4 deletions(-) diff --git a/HISTORY b/HISTORY index 5e11b5d..37ac67b 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.44 2020-08-29 +Version 1.44 2020-08-31 * add test file src/tests/test_pthread_lock.c * add uniq_skiplist.[hc] * add function split_string_ex @@ -34,6 +34,7 @@ Version 1.44 2020-08-29 * add files: fc_memory.[hc] * add files: shared_buffer.[hc] * add files: thread_pool.[hc] + * shared_func.[hc]: add function fc_path_contains Version 1.43 2019-12-25 * replace function call system to getExecResult, diff --git a/src/shared_func.c b/src/shared_func.c index ffaed57..20d21be 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -2558,9 +2558,9 @@ static inline int do_lock_file(int fd, int cmd, int type) if ((result=fcntl(fd, cmd, &lock)) != 0) { result = errno != 0 ? errno : ENOMEM; - fprintf(stderr, "call fcntl fail, " - "errno: %d, error info: %s\n", - result, STRERROR(result)); + fprintf(stderr, "file: "__FILE__", line: %d, " + "call fcntl fail, errno: %d, error info: %s\n", + __LINE__, result, STRERROR(result)); } } while (result == EINTR); @@ -3165,3 +3165,75 @@ int fc_get_last_line(const char *filename, char *buff, line->len = (buff + read_bytes) - line->str; return 0; } + +static bool path_contains_special(const string_t *pts, const int count) +{ + const string_t *ps; + const string_t *end; + + end = pts + count; + for (ps=pts; pslen == 1 && *ps->str == '.') || + fc_string_equal2(ps, "..", 2)) + { + return true; + } + } + + return false; +} + +bool fc_path_contains(const string_t *path, const string_t *needle, + int *result) +{ +#define MAX_PATH_SECTION_COUNT 128 + + string_t pts[MAX_PATH_SECTION_COUNT]; + string_t nds[MAX_PATH_SECTION_COUNT]; + string_t *ps; + string_t *ns; + string_t *end; + int pc; + int nc; + + if ((path->len == 0 || *path->str != '/') || + (needle->len == 0 || *needle->str != '/')) + { + *result = EINVAL; + return false; + } + + pc = split_string_ex(path, '/', pts, MAX_PATH_SECTION_COUNT, true); + if (pc == MAX_PATH_SECTION_COUNT) { + *result = ENAMETOOLONG; + return false; + } + if (path_contains_special(pts, pc)) { + *result = EINVAL; + return false; + } + + nc = split_string_ex(needle, '/', nds, MAX_PATH_SECTION_COUNT, true); + if (nc == MAX_PATH_SECTION_COUNT) { + *result = ENAMETOOLONG; + return false; + } + if (path_contains_special(nds, nc)) { + *result = EINVAL; + return false; + } + + *result = 0; + if (nc > pc) { + return false; + } + + end = nds + nc; + for (ns=nds, ps=pts; ns