diff --git a/HISTORY b/HISTORY index 3636ba5..30c2ea7 100644 --- a/HISTORY +++ b/HISTORY @@ -1,5 +1,5 @@ -Version 1.40 2018-10-26 +Version 1.40 2018-10-29 * add function conn_pool_parse_server_info and conn_pool_load_server_info * support directive: #@add_annotation, for example: #@add_annotation CONFIG_GET /usr/lib/libshmcache.so /etc/libshmcache.conf @@ -9,6 +9,7 @@ Version 1.40 2018-10-26 * add function fc_memmem * add function format_http_date * add function hash_find1 and hash_find2 + * add function resolve_path and fast_buffer_append_file Version 1.39 2018-07-31 * add #@function REPLACE_VARS diff --git a/src/fast_buffer.c b/src/fast_buffer.c index 24955a1..3b5ce22 100644 --- a/src/fast_buffer.c +++ b/src/fast_buffer.c @@ -5,7 +5,9 @@ #include #include #include +#include #include "logger.h" +#include "shared_func.h" #include "fast_buffer.h" int fast_buffer_init_ex(FastBuffer *buffer, const int init_capacity) @@ -155,3 +157,46 @@ int fast_buffer_append_int64(FastBuffer *buffer, const int64_t n) return 0; } +int fast_buffer_append_file(FastBuffer *buffer, const char *filename) +{ + struct stat st; + int result; + int64_t file_size; + + if (stat(filename, &st) != 0) { + result = errno != 0 ? errno : ENOENT; + if (result == ENOENT) { + logError("file: "__FILE__", line: %d, " + "file %s not exist!", __LINE__, + filename); + } else { + logError("file: "__FILE__", line: %d, " + "stat file %s fail, " + "result: %d, error info: %s", __LINE__, + filename, result, strerror(result)); + } + + return result; + } + + if (!S_ISREG(st.st_mode)) { + logError("file: "__FILE__", line: %d, " + "file %s is NOT a regular file!", + __LINE__, filename); + return EINVAL; + } + + file_size = st.st_size + 1; + if ((result=fast_buffer_check(buffer, file_size)) != 0) { + return result; + } + + if ((result=getFileContentEx(filename, buffer->data + buffer->length, + 0, &file_size)) != 0) + { + return result; + } + + buffer->length += file_size; + return 0; +} diff --git a/src/fast_buffer.h b/src/fast_buffer.h index 38f462a..da873f7 100644 --- a/src/fast_buffer.h +++ b/src/fast_buffer.h @@ -51,6 +51,8 @@ int fast_buffer_append_int(FastBuffer *buffer, const int n); int fast_buffer_append_int64(FastBuffer *buffer, const int64_t n); +int fast_buffer_append_file(FastBuffer *buffer, const char *filename); + static inline int fast_buffer_append_string(FastBuffer *buffer, const char *str) { return fast_buffer_append_buff(buffer, str, strlen(str)); diff --git a/src/logger.h b/src/logger.h index 2ed41db..9f091b9 100644 --- a/src/logger.h +++ b/src/logger.h @@ -119,7 +119,7 @@ int log_init(); * do nothing when already inited * return: 0 for success, != 0 fail */ -static int log_try_init() +static inline int log_try_init() { if (g_log_context.log_buff != NULL) { diff --git a/src/shared_func.c b/src/shared_func.c index 009ad79..43517c7 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -2821,3 +2821,27 @@ char *format_http_date(time_t t, BufferInfo *buffer) "%a, %d %b %Y %H:%M:%S GMT", &tm_info); return buffer->buff; } + +char *resolve_path(const char *from, const char *filename, + char *full_filename, const int size) +{ + const char *last; + int len; + + if (*filename == '/') { + snprintf(full_filename, size, "%s", filename); + return full_filename; + } + + last = strrchr(from, '/'); + if (last != NULL) { + len = last - from; + snprintf(full_filename, size, "%.*s/%s", len, from, filename); + } else { + logWarning("file: "__FILE__", line: %d, " + "no \"/\" in the from filename: %s", + __LINE__, from); + snprintf(full_filename, size, "%s", filename); + } + return full_filename; +} diff --git a/src/shared_func.h b/src/shared_func.h index f8029a4..8a00bb1 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -840,6 +840,17 @@ const char *fc_memmem(const string_t *str, const string_t *needle); */ char *format_http_date(time_t t, BufferInfo *buffer); +/** return full path for the filename (the second parameter) + * parameters: + * from: the input full path filename to get base path + * filename: the filename to resolve path + * full_filename: store the resolved full path filename + * size: the max size of full_filename + * return: the resolved full path filename +*/ +char *resolve_path(const char *from, const char *filename, + char *full_filename, const int size); + #ifdef __cplusplus } #endif