shared_func.[hc]: add function fc_path_contains

pull/37/head
YuQing 2020-08-31 16:02:10 +08:00
parent 3ff6cd8844
commit 6eb2d1c2e7
3 changed files with 87 additions and 4 deletions

View File

@ -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,

View File

@ -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; ps<end; ps++) {
if ((ps->len == 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<end; ns++, ps++) {
if (!fc_string_equal(ns, ps)) {
return false;
}
}
return true;
}

View File

@ -977,6 +977,16 @@ int fc_get_first_line(const char *filename, char *buff,
int fc_get_last_line(const char *filename, char *buff,
const int buff_size, int64_t *file_size, string_t *line);
/** if the input path contains the needle path
* parameters:
* path: the absolute path to match
* needle: the needle path, must be absolute
* result: store the errno
* return: true for contain, otherwise false
*/
bool fc_path_contains(const string_t *path, const string_t *needle,
int *result);
#ifdef __cplusplus
}
#endif