From 72ca728d67d7c6bcdce94070409d500502b27e68 Mon Sep 17 00:00:00 2001 From: YuQing <384681@qq.com> Date: Tue, 15 Oct 2019 09:49:44 +0800 Subject: [PATCH] test programs use func my_daemon_init --- make.sh | 2 +- test/common_func.c | 67 ++++++++++++++------------------------------ test/common_func.h | 2 +- test/test_delete.c | 7 +++-- test/test_download.c | 7 +++-- test/test_upload.c | 8 +++--- 6 files changed, 35 insertions(+), 58 deletions(-) diff --git a/make.sh b/make.sh index 86f9318..da084ce 100755 --- a/make.sh +++ b/make.sh @@ -1,6 +1,6 @@ ENABLE_STATIC_LIB=0 ENABLE_SHARED_LIB=1 -DESTDIR=/usr/local +DESTDIR=/usr TARGET_PREFIX=$DESTDIR TARGET_CONF_PATH=$DESTDIR/etc/fdfs TARGET_INIT_PATH=$DESTDIR/etc/init.d diff --git a/test/common_func.c b/test/common_func.c index 6899a73..9faec20 100644 --- a/test/common_func.c +++ b/test/common_func.c @@ -6,56 +6,31 @@ #include #include #include +#include "fastcommon/shared_func.h" +#include "fastcommon/logger.h" #include "common_func.h" -int getFileContent(const char *filename, char **buff, int64_t *file_size) +int my_daemon_init() { - int fd; - - fd = open(filename, O_RDONLY); - if (fd < 0) + char cwd[256]; + if (getcwd(cwd, sizeof(cwd)) == NULL) + { + logError("file: "__FILE__", line: %d, " + "getcwd fail, errno: %d, error info: %s", + __LINE__, errno, STRERROR(errno)); + return errno != 0 ? errno : EPERM; + } +#ifndef WIN32 + daemon_init(false); +#endif + + if (chdir(cwd) != 0) { - *buff = NULL; - *file_size = 0; - return errno != 0 ? errno : ENOENT; + logError("file: "__FILE__", line: %d, " + "chdir to %s fail, errno: %d, error info: %s", + __LINE__, cwd, errno, STRERROR(errno)); + return errno != 0 ? errno : EPERM; } - if ((*file_size=lseek(fd, 0, SEEK_END)) < 0) - { - *buff = NULL; - *file_size = 0; - close(fd); - return errno != 0 ? errno : EIO; - } - - *buff = (char *)malloc(*file_size + 1); - if (*buff == NULL) - { - *file_size = 0; - close(fd); - - return errno != 0 ? errno : ENOMEM; - } - - if (lseek(fd, 0, SEEK_SET) < 0) - { - *buff = NULL; - *file_size = 0; - close(fd); - return errno != 0 ? errno : EIO; - } - if (read(fd, *buff, *file_size) != *file_size) - { - free(*buff); - *buff = NULL; - *file_size = 0; - close(fd); - return errno != 0 ? errno : EIO; - } - - (*buff)[*file_size] = '\0'; - close(fd); - - return 0; + return 0; } - diff --git a/test/common_func.h b/test/common_func.h index bf6386a..8b90adf 100644 --- a/test/common_func.h +++ b/test/common_func.h @@ -7,7 +7,7 @@ extern "C" { #endif -int getFileContent(const char *filename, char **buff, int64_t *file_size); +int my_daemon_init(); #ifdef __cplusplus } diff --git a/test/test_delete.c b/test/test_delete.c index f08d120..d34b1e1 100644 --- a/test/test_delete.c +++ b/test/test_delete.c @@ -119,9 +119,10 @@ int main(int argc, char **argv) return result; } -#ifndef WIN32 - daemon_init(false); -#endif + if ((result=my_daemon_init()) != 0) + { + return result; + } /* printf("file_count = %d\n", file_count); diff --git a/test/test_download.c b/test/test_download.c index da3a042..63365aa 100644 --- a/test/test_download.c +++ b/test/test_download.c @@ -127,9 +127,10 @@ int main(int argc, char **argv) return result; } -#ifndef WIN32 - daemon_init(false); -#endif + if ((result=my_daemon_init()) != 0) + { + return result; + } /* printf("file_count = %d\n", file_count); diff --git a/test/test_upload.c b/test/test_upload.c index a54f6c3..c73a4f4 100644 --- a/test/test_upload.c +++ b/test/test_upload.c @@ -9,7 +9,6 @@ #include #include #include "fastcommon/common_define.h" -#include "fastcommon/shared_func.h" #include "fastcommon/logger.h" #include "test_types.h" #include "common_func.h" @@ -121,9 +120,10 @@ int main(int argc, char **argv) return result; } -#ifndef WIN32 - daemon_init(false); -#endif + if ((result=my_daemon_init()) != 0) + { + return result; + } memset(&storages, 0, sizeof(storages)); upload_count = 0;