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