/[LeafOK_CVS]/lbbs/src/test_file_loader.c
ViewVC logotype

Contents of /lbbs/src/test_file_loader.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.14 - (show annotations)
Wed Nov 19 03:12:58 2025 UTC (3 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.13: +13 -42 lines
Content type: text/x-csrc
Use POSIX shared object instead of SysV shared segment in file_loader

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * test_file_loader
4 * - tester for shared memory based file loader
5 *
6 * 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"
14 #include "log.h"
15 #include <errno.h>
16 #include <libgen.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include <sys/shm.h>
21
22 const char *files[] = {
23 "../data/welcome.txt",
24 "../data/copyright.txt",
25 "../data/register.txt",
26 "../data/license.txt",
27 "../data/login_error.txt",
28 "../data/read_help.txt"};
29
30 int files_cnt = 6;
31
32 int main(int argc, char *argv[])
33 {
34 char file_path_temp[FILE_PATH_LEN];
35 int i;
36 void *p_shm;
37 size_t data_len;
38 long line_total;
39 const void *p_data;
40 const long *p_line_offsets;
41
42 // Change current dir
43 strncpy(file_path_temp, argv[0], sizeof(file_path_temp) - 1);
44 file_path_temp[sizeof(file_path_temp) - 1] = '\0';
45
46 if (chdir(dirname(file_path_temp)) < 0)
47 {
48 fprintf(stderr, "chdir(%s) error: %d\n", dirname(file_path_temp), errno);
49 return -1;
50 }
51
52 if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
53 {
54 printf("Open log error\n");
55 return -1;
56 }
57
58 log_common_redir(STDOUT_FILENO);
59 log_error_redir(STDERR_FILENO);
60
61 printf("Testing #1\n");
62
63 for (i = 0; i < files_cnt; i++)
64 {
65 if (load_file(files[i]) < 0)
66 {
67 printf("Load file %s error\n", files[i]);
68 }
69 }
70
71 for (i = 0; i < files_cnt; i++)
72 {
73 if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
74 {
75 printf("Get file shm %s error\n", files[i]);
76 }
77 else
78 {
79 printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total);
80
81 for (int j = 0; j < line_total; j++)
82 {
83 printf("Line %d: %ld ~ %ld\n", j, p_line_offsets[j], p_line_offsets[j + 1]);
84 }
85
86 if (detach_file_shm(p_shm) < 0)
87 {
88 log_error("detach_file_shm(%s) error\n", files[i]);
89 }
90 }
91 }
92
93 printf("Testing #2\n");
94
95 for (i = 0; i < files_cnt; i++)
96 {
97 if (unload_file(files[i]) < 0)
98 {
99 printf("Unload file %s error\n", files[i]);
100 }
101 }
102
103 for (i = 0; i < files_cnt; i++)
104 {
105 if (load_file(files[i]) < 0)
106 {
107 printf("Load file %s error\n", files[i]);
108 }
109 }
110
111 printf("Testing #3\n");
112
113 for (i = 0; i < files_cnt; i++)
114 {
115 if (i % 2 == 0)
116 {
117 if (unload_file(files[i]) < 0)
118 {
119 printf("Unload file %s error\n", files[i]);
120 }
121 }
122 }
123
124 for (i = 0; i < files_cnt; i++)
125 {
126 if (i % 2 != 0)
127 {
128 if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
129 {
130 printf("Get file shm %s error\n", files[i]);
131 }
132 else
133 {
134 printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total);
135
136 if (detach_file_shm(p_shm) < 0)
137 {
138 log_error("detach_file_shm(%s) error\n", files[i]);
139 }
140 }
141 }
142 }
143
144 log_end();
145
146 return 0;
147 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1