Skip to content

Commit

Permalink
test programs use func my_daemon_init
Browse files Browse the repository at this point in the history
  • Loading branch information
happyfish100 committed Oct 15, 2019
1 parent 58be6d0 commit 72ca728
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 58 deletions.
2 changes: 1 addition & 1 deletion make.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
67 changes: 21 additions & 46 deletions test/common_func.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,31 @@
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#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;
}

2 changes: 1 addition & 1 deletion test/common_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
extern "C" {
#endif

int getFileContent(const char *filename, char **buff, int64_t *file_size);
int my_daemon_init();

#ifdef __cplusplus
}
Expand Down
7 changes: 4 additions & 3 deletions test/test_delete.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions test/test_download.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions test/test_upload.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <sys/stat.h>
#include <sys/mman.h>
#include "fastcommon/common_define.h"
#include "fastcommon/shared_func.h"
#include "fastcommon/logger.h"
#include "test_types.h"
#include "common_func.h"
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 72ca728

Please sign in to comment.