add function fc_check_rename_ex

recovery_and_balance
YuQing 2022-03-09 08:21:07 +08:00
parent 6836337d0a
commit 08f74db732
4 changed files with 63 additions and 7 deletions

View File

@ -1,11 +1,12 @@
Version 1.56 2022-02-25 Version 1.56 2022-03-09
* add function fc_gettid * add function fc_gettid
* function normalize_path: NULL from parameter for getcwd * function normalize_path: NULL from parameter for getcwd
* sockopt.[hc] support tcpwritev and tcpreadv * sockopt.[hc] support tcpwritev and tcpreadv
* add function fc_iov_get_bytes * add function fc_iov_get_bytes
* rename hash_xxx to fc_hash_xxx * rename hash_xxx to fc_hash_xxx
* rename trim to fc_trim * rename trim to fc_trim
* add function fc_check_rename_ex
Version 1.55 2022-01-12 Version 1.55 2022-01-12
* fastcommon php extension adapt to php 8 * fastcommon php extension adapt to php 8

View File

@ -203,12 +203,12 @@ done
if [ -n "$pthread_path" ]; then if [ -n "$pthread_path" ]; then
LIBS="$LIBS -lpthread" 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 if [ -n "$line" ]; then
CFLAGS="$CFLAGS -DWITH_PTHREAD_RWLOCKATTR_SETKIND_NP=1" CFLAGS="$CFLAGS -DWITH_PTHREAD_RWLOCKATTR_SETKIND_NP=1"
fi fi
elif [ -f /usr/lib/libc_r.so ]; then 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 if [ -n "$line" ]; then
LIBS="$LIBS -lc_r" LIBS="$LIBS -lc_r"
fi fi

View File

@ -1369,10 +1369,10 @@ int safeWriteToFile(const char *filename, const char *buff, \
if (rename(tmpFilename, filename) != 0) if (rename(tmpFilename, filename) != 0)
{ {
result = errno != 0 ? errno : EIO; result = errno != 0 ? errno : EIO;
logError("file: "__FILE__", line: %d, " \ logError("file: "__FILE__", line: %d, "
"rename file \"%s\" to \"%s\" fail, " \ "rename file \"%s\" to \"%s\" fail, "
"errno: %d, error info: %s", \ "errno: %d, error info: %s",
__LINE__, tmpFilename, filename, \ __LINE__, tmpFilename, filename,
result, STRERROR(result)); result, STRERROR(result));
return result; return result;
} }
@ -3446,6 +3446,52 @@ int fc_mkdirs_ex(const char *path, const mode_t mode, int *create_count)
return 0; 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, int fc_get_first_line(const char *filename, char *buff,
const int buff_size, string_t *line) const int buff_size, string_t *line)
{ {

View File

@ -1089,6 +1089,15 @@ static inline int fc_mkdirs(const char *path, const mode_t mode)
return fc_mkdirs_ex(path, mode, &create_count); 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, int fc_get_first_line(const char *filename, char *buff,
const int buff_size, string_t *line); const int buff_size, string_t *line);