From 44dcf4f821d1839c9f0e33a468f3c0f057d266ef Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Sun, 8 Aug 2021 15:11:29 +0800 Subject: [PATCH] add functions getFileContentEx1 and getFileContent1 --- HISTORY | 3 +- src/shared_func.c | 173 ++++++++++++++++++++++++---------------------- src/shared_func.h | 28 +++++++- 3 files changed, 118 insertions(+), 86 deletions(-) diff --git a/HISTORY b/HISTORY index dc0b7e8..5b6eefe 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,7 @@ -Version 1.54 2021-08-06 +Version 1.54 2021-08-08 * fast_allocator.[hc]: correct reclaim_interval logic + * shared_func.[hc]: add functions getFileContentEx1 and getFileContent1 Version 1.53 2021-06-30 * process_action support action status diff --git a/src/shared_func.c b/src/shared_func.c index 7b266eb..39a063c 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -1143,6 +1143,58 @@ void chopPath(char *filePath) } } +int getFileContent1(int fd, const char *filename, + char **buff, int64_t *file_size) +{ + int result; + + if ((*file_size=lseek(fd, 0, SEEK_END)) < 0) { + *buff = NULL; + *file_size = 0; + result = errno != 0 ? errno : EIO; + logError("file: "__FILE__", line: %d, " + "lseek file %s fail, " + "errno: %d, error info: %s", __LINE__, + filename, result, STRERROR(result)); + return result; + } + + *buff = (char *)fc_malloc(*file_size + 1); + if (*buff == NULL) { + *file_size = 0; + return ENOMEM; + } + + result = 0; + do { + if (lseek(fd, 0, SEEK_SET) < 0) { + result = errno != 0 ? errno : EIO; + logError("file: "__FILE__", line: %d, " + "lseek file %s fail, " + "errno: %d, error info: %s", __LINE__, + filename, result, STRERROR(result)); + break; + } + if (read(fd, *buff, *file_size) != *file_size) { + result = errno != 0 ? errno : EIO; + logError("file: "__FILE__", line: %d, " + "read from file %s fail, size: %"PRId64", " + "errno: %d, error info: %s", __LINE__, + filename, *file_size, result, STRERROR(result)); + break; + } + } while (0); + + if (result == 0) { + (*buff)[*file_size] = '\0'; + } else { + free(*buff); + *buff = NULL; + *file_size = 0; + } + return result; +} + int getFileContent(const char *filename, char **buff, int64_t *file_size) { int fd; @@ -1190,65 +1242,49 @@ int getFileContent(const char *filename, char **buff, int64_t *file_size) return result; } - if ((*file_size=lseek(fd, 0, SEEK_END)) < 0) - { - *buff = NULL; - *file_size = 0; - result = errno != 0 ? errno : EIO; - close(fd); - logError("file: "__FILE__", line: %d, " - "lseek file %s fail, " - "errno: %d, error info: %s", __LINE__, - filename, result, STRERROR(result)); - return result; - } - - *buff = (char *)fc_malloc(*file_size + 1); - if (*buff == NULL) - { - *file_size = 0; - close(fd); - return ENOMEM; - } - - if (lseek(fd, 0, SEEK_SET) < 0) - { - *buff = NULL; - *file_size = 0; - result = errno != 0 ? errno : EIO; - close(fd); - logError("file: "__FILE__", line: %d, " - "lseek file %s fail, " - "errno: %d, error info: %s", __LINE__, - filename, result, STRERROR(result)); - return result; - } - if (read(fd, *buff, *file_size) != *file_size) - { - free(*buff); - *buff = NULL; - *file_size = 0; - result = errno != 0 ? errno : EIO; - close(fd); - logError("file: "__FILE__", line: %d, " - "read from file %s fail, " - "errno: %d, error info: %s", __LINE__, - filename, result, STRERROR(result)); - return result; - } - - (*buff)[*file_size] = '\0'; + result = getFileContent1(fd, filename, buff, file_size); close(fd); - return 0; + return result; } -int getFileContentEx(const char *filename, char *buff, \ +int getFileContentEx1(int fd, const char *filename, char *buff, + int64_t offset, int64_t *size) +{ + int result; + int read_bytes; + + if (lseek(fd, offset, SEEK_SET) < 0) { + result = errno != 0 ? errno : EIO; + logError("file: "__FILE__", line: %d, " + "lseek file %s fail, offset: %"PRId64", " + "errno: %d, error info: %s", __LINE__, + filename, offset, result, STRERROR(result)); + *size = 0; + return result; + } + + if ((read_bytes=read(fd, buff, *size - 1)) < 0) { + result = errno != 0 ? errno : EIO; + logError("file: "__FILE__", line: %d, " + "read from file %s fail, offset: %"PRId64", " + "length: %"PRId64", errno: %d, error info: %s", + __LINE__, filename, offset, *size - 1, + result, STRERROR(result)); + *size = 0; + return result; + } + + *size = read_bytes; + *(buff + (*size)) = '\0'; + return 0; +} + +int getFileContentEx(const char *filename, char *buff, int64_t offset, int64_t *size) { int fd; int result; - int read_bytes; if (*size <= 0) { @@ -1269,35 +1305,9 @@ int getFileContentEx(const char *filename, char *buff, \ return result; } - if (offset > 0 && lseek(fd, offset, SEEK_SET) < 0) - { - *size = 0; - result = errno != 0 ? errno : EIO; - close(fd); - logError("file: "__FILE__", line: %d, " - "lseek file %s fail, " - "errno: %d, error info: %s", __LINE__, - filename, result, STRERROR(result)); - return result; - } - - if ((read_bytes=read(fd, buff, *size - 1)) < 0) - { - *size = 0; - result = errno != 0 ? errno : EIO; - close(fd); - logError("file: "__FILE__", line: %d, " - "read from file %s fail, " - "errno: %d, error info: %s", __LINE__, - filename, result, STRERROR(result)); - return result; - } - - *size = read_bytes; - *(buff + (*size)) = '\0'; + result = getFileContentEx1(fd, filename, buff, offset, size); close(fd); - - return 0; + return result; } int getFileSize(const char *filename, int64_t *file_size) @@ -2618,11 +2628,6 @@ int64_t get_current_time_us() return ((int64_t)tv.tv_sec * 1000 * 1000 + (int64_t)tv.tv_usec); } -inline bool is_power2(const int64_t n) -{ - return ((n != 0) && !(n & (n - 1))); -} - static inline int do_lock_file(int fd, int cmd, int type) { struct flock lock; diff --git a/src/shared_func.h b/src/shared_func.h index 99d05b3..d2061c0 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -592,6 +592,17 @@ int get_time_item_from_str(const char *pValue, const char *item_name, */ void chopPath(char *filePath); +/** get file content by fd + * parameters: + * fd: the file descriptor + * filename: the filename + * buff: return the buff, must be freed + * file_size: store the file size + * return: error no , 0 success, != 0 fail +*/ +int getFileContent1(int fd, const char *filename, + char **buff, int64_t *file_size); + /** get file content * parameters: * filename: the filename @@ -601,6 +612,18 @@ void chopPath(char *filePath); */ int getFileContent(const char *filename, char **buff, int64_t *file_size); +/** get file content + * parameters: + * fd: the file descriptor + * filename: the filename + * buff: the buff to store file content + * offset: the start offset + * size: specify the size to fetch and return the fetched size + * return: error no , 0 success, != 0 fail +*/ +int getFileContentEx1(int fd, const char *filename, char *buff, + int64_t offset, int64_t *size); + /** get file content * parameters: * filename: the filename @@ -767,7 +790,10 @@ int64_t get_current_time_us(); * n: the number to test * return: true for power 2, otherwise false */ -bool is_power2(const int64_t n); +static inline bool is_power2(const int64_t n) +{ + return ((n != 0) && (n & (n - 1)) == 0); +} /** set file read lock * parameters: