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

Diff of /lbbs/src/test_file_loader.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 1.1 by sysadm, Fri May 16 12:22:35 2025 UTC Revision 1.17 by sysadm, Fri Dec 19 06:16:27 2025 UTC
# Line 1  Line 1 
1  /***************************************************************************  /* SPDX-License-Identifier: GPL-3.0-or-later */
2                                                  file_loader.c  -  description  /*
3                                                           -------------------   * test_file_loader
4          Copyright            : (C) 2004-2025 by Leaflet   *   - tester for shared memory based file loader
5          Email                : leaflet@leafok.com   *
6   ***************************************************************************/   * Copyright (C) 2004-2025  Leaflet <leaflet@leafok.com>
7     */
8  /***************************************************************************  
9   *                                                                         *  #ifdef HAVE_CONFIG_H
10   *   This program is free software; you can redistribute it and/or modify  *  #include "config.h"
11   *   it under the terms of the GNU General Public License as published by  *  #endif
  *   the Free Software Foundation; either version 3 of the License, or     *  
  *   (at your option) any later version.                                   *  
  *                                                                         *  
  ***************************************************************************/  
12    
13  #include "file_loader.h"  #include "file_loader.h"
14  #include "log.h"  #include "log.h"
15    #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    
21  const char *files[] = {  const char *files[] = {
# Line 24  const char *files[] = { Line 23  const char *files[] = {
23          "../data/copyright.txt",          "../data/copyright.txt",
24          "../data/register.txt",          "../data/register.txt",
25          "../data/license.txt",          "../data/license.txt",
26          "../data/login_error.txt"};          "../data/login_error.txt",
27            "../data/read_help.txt"};
28    
29  int files_cnt = 5;  int files_cnt = 6;
30    
31  int main(int argc, char *argv[])  int main(int argc, char *argv[])
32  {  {
         int ret;  
         int file_loader_pool_size = 2;  
33          int i;          int i;
34          const FILE_MMAP *p_file_mmap;          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)          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
41          {          {
# Line 41  int main(int argc, char *argv[]) Line 43  int main(int argc, char *argv[])
43                  return -1;                  return -1;
44          }          }
45    
46          log_std_redirect(STDOUT_FILENO);          log_common_redir(STDOUT_FILENO);
47          log_err_redirect(STDERR_FILENO);          log_error_redir(STDERR_FILENO);
   
         ret = file_loader_init(file_loader_pool_size);  
         if (ret < 0)  
         {  
                 printf("file_loader_init() error (%d)\n", ret);  
                 return ret;  
         }  
   
         ret = file_loader_init(file_loader_pool_size);  
         if (ret == 0)  
         {  
                 printf("Rerun file_loader_init() error\n");  
         }  
48    
49          printf("Testing #1\n");          printf("Testing #1\n");
50    
51          for (i = 0; i < file_loader_pool_size; i++)          for (i = 0; i < files_cnt; i++)
52          {          {
53                  if (load_file_mmap(files[i]) < 0)                  if (load_file(files[i]) < 0)
54                  {                  {
55                          printf("Load file %s error\n", files[i]);                          printf("Load file %s error\n", files[i]);
56                  }                  }
57          }          }
58    
59          for (i = 0; i < file_loader_pool_size; i++)          for (i = 0; i < files_cnt; i++)
60          {          {
61                  if ((p_file_mmap = get_file_mmap(files[i])) == NULL)                  if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
62                  {                  {
63                          printf("Get file mmap %s error\n", files[i]);                          printf("Get file shm %s error\n", files[i]);
64                  }                  }
65                  else                  else
66                  {                  {
67                          printf("File: %s size: ", files[i]);                          printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total);
                         printf("size: %ld lines: %ld\n", p_file_mmap->size, p_file_mmap->line_total);  
                 }  
         }  
68    
69          for (i = 0; i < file_loader_pool_size; i++)                          for (int j = 0; j < line_total; j++)
70          {                          {
71                  if (unload_file_mmap(files[i]) < 0)                                  printf("Line %d: %ld ~ %ld\n", j, p_line_offsets[j], p_line_offsets[j + 1]);
72                  {                          }
73                          printf("Unload file %s error\n", files[i]);  
74                            if (detach_file_shm(p_shm) < 0)
75                            {
76                                    log_error("detach_file_shm(%s) error", files[i]);
77                            }
78                  }                  }
79          }          }
80    
# Line 92  int main(int argc, char *argv[]) Line 82  int main(int argc, char *argv[])
82    
83          for (i = 0; i < files_cnt; i++)          for (i = 0; i < files_cnt; i++)
84          {          {
85                  if (i >= file_loader_pool_size)                  if (unload_file(files[i]) < 0)
86                  {                  {
87                          if (unload_file_mmap(files[i - file_loader_pool_size]) < 0)                          printf("Unload file %s error\n", files[i]);
                         {  
                                 printf("Unload file %s error\n", files[i]);  
                         }  
88                  }                  }
89            }
90    
91                  if (load_file_mmap(files[i]) < 0)          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]);                          printf("Load file %s error\n", files[i]);
96                  }                  }
# Line 110  int main(int argc, char *argv[]) Line 100  int main(int argc, char *argv[])
100    
101          for (i = 0; i < files_cnt; i++)          for (i = 0; i < files_cnt; i++)
102          {          {
103                  if ((p_file_mmap = get_file_mmap(files[i])) == NULL)                  if (i % 2 == 0)
104                  {                  {
105                          printf("Get file mmap %s error\n", files[i]);                          if (unload_file(files[i]) < 0)
106                            {
107                                    printf("Unload file %s error\n", files[i]);
108                            }
109                  }                  }
110                  else          }
111    
112            for (i = 0; i < files_cnt; i++)
113            {
114                    if (i % 2 != 0)
115                  {                  {
116                          printf("File: %s size: ", files[i]);                          if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
117                          printf("size: %ld lines: %ld\n", p_file_mmap->size, p_file_mmap->line_total);                          {
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    
         file_loader_cleanup();  
         file_loader_cleanup();  
   
132          log_end();          log_end();
133    
134          return 0;          return 0;


Legend:
Removed lines/characters  
Changed lines/characters
  Added lines/characters

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