/[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.15 - (show annotations)
Thu Nov 20 01:09:27 2025 UTC (3 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.14: +0 -11 lines
Content type: text/x-csrc
Update

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 int i;
35 void *p_shm;
36 size_t data_len;
37 long line_total;
38 const void *p_data;
39 const long *p_line_offsets;
40
41 if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
42 {
43 printf("Open log error\n");
44 return -1;
45 }
46
47 log_common_redir(STDOUT_FILENO);
48 log_error_redir(STDERR_FILENO);
49
50 printf("Testing #1\n");
51
52 for (i = 0; i < files_cnt; i++)
53 {
54 if (load_file(files[i]) < 0)
55 {
56 printf("Load file %s error\n", files[i]);
57 }
58 }
59
60 for (i = 0; i < files_cnt; i++)
61 {
62 if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
63 {
64 printf("Get file shm %s error\n", files[i]);
65 }
66 else
67 {
68 printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total);
69
70 for (int j = 0; j < line_total; j++)
71 {
72 printf("Line %d: %ld ~ %ld\n", j, p_line_offsets[j], p_line_offsets[j + 1]);
73 }
74
75 if (detach_file_shm(p_shm) < 0)
76 {
77 log_error("detach_file_shm(%s) error\n", files[i]);
78 }
79 }
80 }
81
82 printf("Testing #2\n");
83
84 for (i = 0; i < files_cnt; i++)
85 {
86 if (unload_file(files[i]) < 0)
87 {
88 printf("Unload file %s error\n", files[i]);
89 }
90 }
91
92 for (i = 0; i < files_cnt; i++)
93 {
94 if (load_file(files[i]) < 0)
95 {
96 printf("Load file %s error\n", files[i]);
97 }
98 }
99
100 printf("Testing #3\n");
101
102 for (i = 0; i < files_cnt; i++)
103 {
104 if (i % 2 == 0)
105 {
106 if (unload_file(files[i]) < 0)
107 {
108 printf("Unload file %s error\n", files[i]);
109 }
110 }
111 }
112
113 for (i = 0; i < files_cnt; i++)
114 {
115 if (i % 2 != 0)
116 {
117 if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
118 {
119 printf("Get file shm %s error\n", files[i]);
120 }
121 else
122 {
123 printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total);
124
125 if (detach_file_shm(p_shm) < 0)
126 {
127 log_error("detach_file_shm(%s) error\n", files[i]);
128 }
129 }
130 }
131 }
132
133 log_end();
134
135 return 0;
136 }

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