From 08f74db732a7d05291c2f0d53bca04753563dac5 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Wed, 9 Mar 2022 08:21:07 +0800 Subject: [PATCH] add function fc_check_rename_ex --- HISTORY | 3 ++- make.sh | 4 ++-- src/shared_func.c | 54 +++++++++++++++++++++++++++++++++++++++++++---- src/shared_func.h | 9 ++++++++ 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/HISTORY b/HISTORY index e04aff4..3df4a2f 100644 --- a/HISTORY +++ b/HISTORY @@ -1,11 +1,12 @@ -Version 1.56 2022-02-25 +Version 1.56 2022-03-09 * add function fc_gettid * function normalize_path: NULL from parameter for getcwd * sockopt.[hc] support tcpwritev and tcpreadv * add function fc_iov_get_bytes * rename hash_xxx to fc_hash_xxx * rename trim to fc_trim + * add function fc_check_rename_ex Version 1.55 2022-01-12 * fastcommon php extension adapt to php 8 diff --git a/make.sh b/make.sh index 940bf4c..16b9c90 100755 --- a/make.sh +++ b/make.sh @@ -203,12 +203,12 @@ done if [ -n "$pthread_path" ]; then LIBS="$LIBS -lpthread" - line=$(nm $pthread_path | fgrep pthread_rwlockattr_setkind_np | fgrep -w T) + line=$(nm $pthread_path 2>/dev/null | fgrep pthread_rwlockattr_setkind_np | fgrep -w T) if [ -n "$line" ]; then CFLAGS="$CFLAGS -DWITH_PTHREAD_RWLOCKATTR_SETKIND_NP=1" fi elif [ -f /usr/lib/libc_r.so ]; then - line=$(nm -D /usr/lib/libc_r.so | grep pthread_create | grep -w T) + line=$(nm -D /usr/lib/libc_r.so 2>/dev/null | grep pthread_create | grep -w T) if [ -n "$line" ]; then LIBS="$LIBS -lc_r" fi diff --git a/src/shared_func.c b/src/shared_func.c index 61bd9ed..087703f 100644 --- a/src/shared_func.c +++ b/src/shared_func.c @@ -1369,10 +1369,10 @@ int safeWriteToFile(const char *filename, const char *buff, \ if (rename(tmpFilename, filename) != 0) { result = errno != 0 ? errno : EIO; - logError("file: "__FILE__", line: %d, " \ - "rename file \"%s\" to \"%s\" fail, " \ - "errno: %d, error info: %s", \ - __LINE__, tmpFilename, filename, \ + logError("file: "__FILE__", line: %d, " + "rename file \"%s\" to \"%s\" fail, " + "errno: %d, error info: %s", + __LINE__, tmpFilename, filename, result, STRERROR(result)); return result; } @@ -3446,6 +3446,52 @@ int fc_mkdirs_ex(const char *path, const mode_t mode, int *create_count) return 0; } +int fc_check_rename_ex(const char *oldpath, const char *newpath, + const bool overwritten) +{ + int result; + + if (access(oldpath, F_OK) != 0) { + result = errno != 0 ? errno : EPERM; + if (result != ENOENT) { + logError("file: "__FILE__", line: %d, " + "access %s fail, errno: %d, error info: %s", + __LINE__, oldpath, result, STRERROR(result)); + return result; + } + return 0; + } + + if (!overwritten) { + if (access(newpath, F_OK) == 0) { + logError("file: "__FILE__", line: %d, " + "dest path: %s already exist", + __LINE__, newpath); + return EEXIST; + } else { + result = errno != 0 ? errno : EPERM; + if (result != ENOENT) { + logError("file: "__FILE__", line: %d, " + "access %s fail, errno: %d, error info: %s", + __LINE__, newpath, result, STRERROR(result)); + return result; + } + } + } + + if (rename(oldpath, newpath) != 0) { + result = errno != 0 ? errno : EIO; + logError("file: "__FILE__", line: %d, " + "rename file \"%s\" to \"%s\" fail, " + "errno: %d, error info: %s", + __LINE__, oldpath, newpath, + result, STRERROR(result)); + return result; + } + + return 0; +} + int fc_get_first_line(const char *filename, char *buff, const int buff_size, string_t *line) { diff --git a/src/shared_func.h b/src/shared_func.h index a920042..5945015 100644 --- a/src/shared_func.h +++ b/src/shared_func.h @@ -1089,6 +1089,15 @@ static inline int fc_mkdirs(const char *path, const mode_t mode) return fc_mkdirs_ex(path, mode, &create_count); } +int fc_check_rename_ex(const char *oldpath, const char *newpath, + const bool overwritten); + +static inline int fc_check_rename(const char *oldpath, const char *newpath) +{ + const bool overwritten = true; + return fc_check_rename_ex(oldpath, newpath, overwritten); +} + int fc_get_first_line(const char *filename, char *buff, const int buff_size, string_t *line);