add function resolve_path and fast_buffer_append_file

pull/37/head
YuQing 2018-10-29 16:48:08 +08:00
parent f64a00c0f2
commit 32c1445d41
6 changed files with 85 additions and 2 deletions

View File

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

View File

@ -5,7 +5,9 @@
#include <unistd.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#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;
}

View File

@ -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));

View File

@ -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)
{

View File

@ -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;
}

View File

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