From 0bf8b7903f9e22c0e42f77b92b503f7b1ca19d64 Mon Sep 17 00:00:00 2001 From: hxy7yx <1595670487@qq.com> Date: Tue, 15 Aug 2023 14:28:42 +0800 Subject: [PATCH] (file):add error code for reading empty files --- include/neuron/errcodes.h | 1 + plugins/file/file_req.c | 14 ++++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/include/neuron/errcodes.h b/include/neuron/errcodes.h index e80068bb..9bc72d49 100644 --- a/include/neuron/errcodes.h +++ b/include/neuron/errcodes.h @@ -137,6 +137,7 @@ typedef enum { NEU_ERR_FILE_OPEN_FAILURE = 4101, NEU_ERR_FILE_READ_FAILURE = 4102, NEU_ERR_FILE_WRITE_FAILURE = 4103, + NEU_ERR_FILE_READ_NULL = 4104, NEU_ERR_PLUGIN_ERROR_START = 10000, NEU_ERR_PLUGIN_ERROR_END = 20000, diff --git a/plugins/file/file_req.c b/plugins/file/file_req.c index 925d95b5..257a08ef 100644 --- a/plugins/file/file_req.c +++ b/plugins/file/file_req.c @@ -84,10 +84,16 @@ int file_group_timer(neu_plugin_t *plugin, neu_plugin_group_t *group) fseek(f, 0, SEEK_SET); int ret = fread(buf, sizeof(char), length, f); if (ret == 0) { - nlog_warn("read file failed:%s", tag->address); - dvalue.type = NEU_TYPE_ERROR; - dvalue.value.i32 = NEU_ERR_FILE_READ_FAILURE; - + if (feof(f)) { + nlog_warn("file is empty:%s", tag->address); + dvalue.type = NEU_TYPE_ERROR; + dvalue.value.i32 = NEU_ERR_FILE_READ_NULL; + } else if (ferror(f)) { + nlog_warn("read file failed: %s", tag->address); + clearerr(f); + dvalue.type = NEU_TYPE_ERROR; + dvalue.value.i32 = NEU_ERR_FILE_READ_FAILURE; + } fclose(f); goto dvalue_result; } else if (ret >= NEU_VALUE_SIZE) {