| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
file_loader.c - description |
test_file_loader.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
Copyright : (C) 2004-2025 by Leaflet |
| 5 |
Email : leaflet@leafok.com |
Email : leaflet@leafok.com |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
#include "file_loader.h" |
#include "file_loader.h" |
| 18 |
|
#include "trie_dict.h" |
| 19 |
#include "log.h" |
#include "log.h" |
| 20 |
#include <stdio.h> |
#include <stdio.h> |
| 21 |
#include <unistd.h> |
#include <unistd.h> |
| 22 |
#include <errno.h> |
#include <errno.h> |
| 23 |
#include <sys/shm.h> |
#include <sys/shm.h> |
| 24 |
|
|
| 25 |
|
#define TRIE_DICT_SHM_FILE "~trie_dict_shm.dat" |
| 26 |
|
|
| 27 |
const char *files[] = { |
const char *files[] = { |
| 28 |
"../data/welcome.txt", |
"../data/welcome.txt", |
| 29 |
"../data/copyright.txt", |
"../data/copyright.txt", |
| 43 |
long line_total; |
long line_total; |
| 44 |
const void *p_data; |
const void *p_data; |
| 45 |
const long *p_line_offsets; |
const long *p_line_offsets; |
| 46 |
|
FILE *fp; |
| 47 |
|
|
| 48 |
if (log_begin("../log/bbsd.log", "../log/error.log") < 0) |
if (log_begin("../log/bbsd.log", "../log/error.log") < 0) |
| 49 |
{ |
{ |
| 51 |
return -1; |
return -1; |
| 52 |
} |
} |
| 53 |
|
|
| 54 |
log_std_redirect(STDOUT_FILENO); |
log_common_redir(STDOUT_FILENO); |
| 55 |
log_err_redirect(STDERR_FILENO); |
log_error_redir(STDERR_FILENO); |
| 56 |
|
|
| 57 |
|
if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL) |
| 58 |
|
{ |
| 59 |
|
log_error("fopen(%s) error\n", TRIE_DICT_SHM_FILE); |
| 60 |
|
return -1; |
| 61 |
|
} |
| 62 |
|
fclose(fp); |
| 63 |
|
|
| 64 |
|
if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0) |
| 65 |
|
{ |
| 66 |
|
printf("trie_dict_init failed\n"); |
| 67 |
|
return -1; |
| 68 |
|
} |
| 69 |
|
|
| 70 |
ret = file_loader_init(); |
ret = file_loader_init(); |
| 71 |
if (ret < 0) |
if (ret < 0) |
| 84 |
|
|
| 85 |
for (i = 0; i < files_cnt; i++) |
for (i = 0; i < files_cnt; i++) |
| 86 |
{ |
{ |
| 87 |
if (load_file_shm(files[i]) < 0) |
if (load_file(files[i]) < 0) |
| 88 |
{ |
{ |
| 89 |
printf("Load file %s error\n", files[i]); |
printf("Load file %s error\n", files[i]); |
| 90 |
} |
} |
| 92 |
|
|
| 93 |
for (i = 0; i < files_cnt; i++) |
for (i = 0; i < files_cnt; i++) |
| 94 |
{ |
{ |
| 95 |
if ((p_shm = get_file_shm(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL) |
if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL) |
| 96 |
{ |
{ |
| 97 |
printf("Get file shm %s error\n", files[i]); |
printf("Get file shm %s error\n", files[i]); |
| 98 |
} |
} |
| 104 |
{ |
{ |
| 105 |
printf("Line %d: %ld ~ %ld\n", j, p_line_offsets[j], p_line_offsets[j + 1]); |
printf("Line %d: %ld ~ %ld\n", j, p_line_offsets[j], p_line_offsets[j + 1]); |
| 106 |
} |
} |
| 107 |
|
|
| 108 |
|
if (detach_file_shm(p_shm) < 0) |
| 109 |
|
{ |
| 110 |
|
log_error("detach_file_shm(%s) error\n", files[i]); |
| 111 |
|
} |
| 112 |
} |
} |
| 113 |
} |
} |
| 114 |
|
|
| 116 |
|
|
| 117 |
for (i = 0; i < files_cnt; i++) |
for (i = 0; i < files_cnt; i++) |
| 118 |
{ |
{ |
| 119 |
if (unload_file_shm(files[i]) < 0) |
if (unload_file(files[i]) < 0) |
| 120 |
{ |
{ |
| 121 |
printf("Unload file %s error\n", files[i]); |
printf("Unload file %s error\n", files[i]); |
| 122 |
} |
} |
| 124 |
|
|
| 125 |
for (i = 0; i < files_cnt; i++) |
for (i = 0; i < files_cnt; i++) |
| 126 |
{ |
{ |
| 127 |
if (load_file_shm(files[i]) < 0) |
if (load_file(files[i]) < 0) |
| 128 |
{ |
{ |
| 129 |
printf("Load file %s error\n", files[i]); |
printf("Load file %s error\n", files[i]); |
| 130 |
} |
} |
| 136 |
{ |
{ |
| 137 |
if (i % 2 == 0) |
if (i % 2 == 0) |
| 138 |
{ |
{ |
| 139 |
if (unload_file_shm(files[i]) < 0) |
if (unload_file(files[i]) < 0) |
| 140 |
{ |
{ |
| 141 |
printf("Unload file %s error\n", files[i]); |
printf("Unload file %s error\n", files[i]); |
| 142 |
} |
} |
| 147 |
{ |
{ |
| 148 |
if (i % 2 != 0) |
if (i % 2 != 0) |
| 149 |
{ |
{ |
| 150 |
if ((p_shm = get_file_shm(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL) |
if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL) |
| 151 |
{ |
{ |
| 152 |
printf("Get file shm %s error\n", files[i]); |
printf("Get file shm %s error\n", files[i]); |
| 153 |
} |
} |
| 154 |
else |
else |
| 155 |
{ |
{ |
| 156 |
printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total); |
printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total); |
| 157 |
|
|
| 158 |
|
if (detach_file_shm(p_shm) < 0) |
| 159 |
|
{ |
| 160 |
|
log_error("detach_file_shm(%s) error\n", files[i]); |
| 161 |
|
} |
| 162 |
} |
} |
| 163 |
} |
} |
| 164 |
} |
} |
| 166 |
file_loader_cleanup(); |
file_loader_cleanup(); |
| 167 |
file_loader_cleanup(); |
file_loader_cleanup(); |
| 168 |
|
|
| 169 |
|
trie_dict_cleanup(); |
| 170 |
|
|
| 171 |
|
if (unlink(TRIE_DICT_SHM_FILE) < 0) |
| 172 |
|
{ |
| 173 |
|
log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE); |
| 174 |
|
return -1; |
| 175 |
|
} |
| 176 |
|
|
| 177 |
log_end(); |
log_end(); |
| 178 |
|
|
| 179 |
return 0; |
return 0; |