| 3 |
* test_file_loader |
* test_file_loader |
| 4 |
* - tester for shared memory based file loader |
* - tester for shared memory based file loader |
| 5 |
* |
* |
| 6 |
* Copyright (C) 2004-2025 by Leaflet <leaflet@leafok.com> |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
|
#ifdef HAVE_CONFIG_H |
| 10 |
|
#include "config.h" |
| 11 |
|
#endif |
| 12 |
|
|
| 13 |
#include "file_loader.h" |
#include "file_loader.h" |
| 14 |
#include "log.h" |
#include "log.h" |
|
#include "trie_dict.h" |
|
| 15 |
#include <errno.h> |
#include <errno.h> |
| 16 |
|
#include <libgen.h> |
| 17 |
#include <stdio.h> |
#include <stdio.h> |
| 18 |
|
#include <string.h> |
| 19 |
#include <unistd.h> |
#include <unistd.h> |
| 20 |
#include <sys/shm.h> |
#include <sys/shm.h> |
| 21 |
|
|
|
#define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat" |
|
|
|
|
| 22 |
const char *files[] = { |
const char *files[] = { |
| 23 |
"../data/welcome.txt", |
"../data/welcome.txt", |
| 24 |
"../data/copyright.txt", |
"../data/copyright.txt", |
| 31 |
|
|
| 32 |
int main(int argc, char *argv[]) |
int main(int argc, char *argv[]) |
| 33 |
{ |
{ |
| 34 |
int ret; |
char file_path_temp[FILE_PATH_LEN]; |
| 35 |
int i; |
int i; |
| 36 |
const void *p_shm; |
void *p_shm; |
| 37 |
size_t data_len; |
size_t data_len; |
| 38 |
long line_total; |
long line_total; |
| 39 |
const void *p_data; |
const void *p_data; |
| 40 |
const long *p_line_offsets; |
const long *p_line_offsets; |
|
FILE *fp; |
|
| 41 |
|
|
| 42 |
if (log_begin("../log/bbsd.log", "../log/error.log") < 0) |
// Change current dir |
| 43 |
{ |
strncpy(file_path_temp, argv[0], sizeof(file_path_temp) - 1); |
| 44 |
printf("Open log error\n"); |
file_path_temp[sizeof(file_path_temp) - 1] = '\0'; |
|
return -1; |
|
|
} |
|
| 45 |
|
|
| 46 |
log_common_redir(STDOUT_FILENO); |
if (chdir(dirname(file_path_temp)) < 0) |
|
log_error_redir(STDERR_FILENO); |
|
|
|
|
|
if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL) |
|
| 47 |
{ |
{ |
| 48 |
log_error("fopen(%s) error\n", TRIE_DICT_SHM_FILE); |
fprintf(stderr, "chdir(%s) error: %d\n", dirname(file_path_temp), errno); |
| 49 |
return -1; |
return -1; |
| 50 |
} |
} |
|
fclose(fp); |
|
| 51 |
|
|
| 52 |
if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0) |
if (log_begin("../log/bbsd.log", "../log/error.log") < 0) |
| 53 |
{ |
{ |
| 54 |
printf("trie_dict_init failed\n"); |
printf("Open log error\n"); |
| 55 |
return -1; |
return -1; |
| 56 |
} |
} |
| 57 |
|
|
| 58 |
ret = file_loader_init(); |
log_common_redir(STDOUT_FILENO); |
| 59 |
if (ret < 0) |
log_error_redir(STDERR_FILENO); |
|
{ |
|
|
printf("file_loader_init() error (%d)\n", ret); |
|
|
return ret; |
|
|
} |
|
|
|
|
|
ret = file_loader_init(); |
|
|
if (ret == 0) |
|
|
{ |
|
|
printf("Rerun file_loader_init() error\n"); |
|
|
} |
|
| 60 |
|
|
| 61 |
printf("Testing #1\n"); |
printf("Testing #1\n"); |
| 62 |
|
|
| 141 |
} |
} |
| 142 |
} |
} |
| 143 |
|
|
|
file_loader_cleanup(); |
|
|
file_loader_cleanup(); |
|
|
|
|
|
trie_dict_cleanup(); |
|
|
|
|
|
if (unlink(TRIE_DICT_SHM_FILE) < 0) |
|
|
{ |
|
|
log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE); |
|
|
return -1; |
|
|
} |
|
|
|
|
| 144 |
log_end(); |
log_end(); |
| 145 |
|
|
| 146 |
return 0; |
return 0; |