/[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.2 by sysadm, Sat May 17 05:54:42 2025 UTC Revision 1.14 by sysadm, Wed Nov 19 03:12:58 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>
 #include <errno.h>  
20  #include <sys/shm.h>  #include <sys/shm.h>
21    
22  const char *files[] = {  const char *files[] = {
# Line 33  int files_cnt = 6; Line 31  int files_cnt = 6;
31    
32  int main(int argc, char *argv[])  int main(int argc, char *argv[])
33  {  {
34          int ret;          char file_path_temp[FILE_PATH_LEN];
35          int i;          int i;
36          const void *p_file_shm;          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 (log_begin("../log/bbsd.log", "../log/error.log") < 0)          if (chdir(dirname(file_path_temp)) < 0)
47          {          {
48                  printf("Open log error\n");                  fprintf(stderr, "chdir(%s) error: %d\n", dirname(file_path_temp), errno);
49                  return -1;                  return -1;
50          }          }
51    
52          log_std_redirect(STDOUT_FILENO);          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
         log_err_redirect(STDERR_FILENO);  
   
         ret = file_loader_init();  
         if (ret < 0)  
53          {          {
54                  printf("file_loader_init() error (%d)\n", ret);                  printf("Open log error\n");
55                  return ret;                  return -1;
56          }          }
57    
58          ret = file_loader_init();          log_common_redir(STDOUT_FILENO);
59          if (ret == 0)          log_error_redir(STDERR_FILENO);
         {  
                 printf("Rerun file_loader_init() error\n");  
         }  
60    
61          printf("Testing #1\n");          printf("Testing #1\n");
62    
63          for (i = 0; i < files_cnt; i++)          for (i = 0; i < files_cnt; i++)
64          {          {
65                  if (load_file_shm(files[i]) < 0)                  if (load_file(files[i]) < 0)
66                  {                  {
67                          printf("Load file %s error\n", files[i]);                          printf("Load file %s error\n", files[i]);
68                  }                  }
# Line 71  int main(int argc, char *argv[]) Line 70  int main(int argc, char *argv[])
70    
71          for (i = 0; i < files_cnt; i++)          for (i = 0; i < files_cnt; i++)
72          {          {
73                  if ((p_file_shm = get_file_shm(files[i])) == NULL)                  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]);                          printf("Get file shm %s error\n", files[i]);
76                  }                  }
77                  else                  else
78                  {                  {
79                          printf("File: %s size: %ld lines: %ld\n", files[i], *((size_t *)p_file_shm), *((size_t *)(p_file_shm + sizeof(size_t))));                          printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total);
80    
81                          if (shmdt(p_file_shm) == -1)                          for (int j = 0; j < line_total; j++)
82                          {                          {
83                                  log_error("shmdt() error (%d)\n", errno);                                  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          }          }
# Line 90  int main(int argc, char *argv[]) Line 94  int main(int argc, char *argv[])
94    
95          for (i = 0; i < files_cnt; i++)          for (i = 0; i < files_cnt; i++)
96          {          {
97                  if (unload_file_shm(files[i]) < 0)                  if (unload_file(files[i]) < 0)
98                  {                  {
99                          printf("Unload file %s error\n", files[i]);                          printf("Unload file %s error\n", files[i]);
100                  }                  }
# Line 98  int main(int argc, char *argv[]) Line 102  int main(int argc, char *argv[])
102    
103          for (i = 0; i < files_cnt; i++)          for (i = 0; i < files_cnt; i++)
104          {          {
105                  if (load_file_shm(files[i]) < 0)                  if (load_file(files[i]) < 0)
106                  {                  {
107                          printf("Load file %s error\n", files[i]);                          printf("Load file %s error\n", files[i]);
108                  }                  }
# Line 110  int main(int argc, char *argv[]) Line 114  int main(int argc, char *argv[])
114          {          {
115                  if (i % 2 == 0)                  if (i % 2 == 0)
116                  {                  {
117                          if (unload_file_shm(files[i]) < 0)                          if (unload_file(files[i]) < 0)
118                          {                          {
119                                  printf("Unload file %s error\n", files[i]);                                  printf("Unload file %s error\n", files[i]);
120                          }                          }
# Line 121  int main(int argc, char *argv[]) Line 125  int main(int argc, char *argv[])
125          {          {
126                  if (i % 2 != 0)                  if (i % 2 != 0)
127                  {                  {
128                          if ((p_file_shm = get_file_shm(files[i])) == NULL)                          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]);                                  printf("Get file shm %s error\n", files[i]);
131                          }                          }
132                          else                          else
133                          {                          {
134                                  printf("File: %s size: %ld lines: %ld\n", files[i], *((size_t *)p_file_shm), *((size_t *)(p_file_shm + sizeof(size_t))));                                  printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total);
135    
136                                  if (shmdt(p_file_shm) == -1)                                  if (detach_file_shm(p_shm) < 0)
137                                  {                                  {
138                                          log_error("shmdt() error (%d)\n", errno);                                          log_error("detach_file_shm(%s) error\n", files[i]);
139                                  }                                  }
140                          }                          }
141                  }                  }
142          }          }
143    
         file_loader_cleanup();  
         file_loader_cleanup();  
   
144          log_end();          log_end();
145    
146          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