/[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.4 by sysadm, Sun May 18 08:17:25 2025 UTC Revision 1.13 by sysadm, Tue Nov 11 00:28:05 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 "trie_dict.h"
16    #include <errno.h>
17  #include <stdio.h>  #include <stdio.h>
18  #include <unistd.h>  #include <unistd.h>
 #include <errno.h>  
19  #include <sys/shm.h>  #include <sys/shm.h>
20    
21    static const char TRIE_DICT_SHM_FILE[] = "~trie_dict_shm.dat";
22    
23  const char *files[] = {  const char *files[] = {
24          "../data/welcome.txt",          "../data/welcome.txt",
25          "../data/copyright.txt",          "../data/copyright.txt",
# Line 40  int main(int argc, char *argv[]) Line 39  int main(int argc, char *argv[])
39          long line_total;          long line_total;
40          const void *p_data;          const void *p_data;
41          const long *p_line_offsets;          const long *p_line_offsets;
42            FILE *fp;
43    
44          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)          if (log_begin("../log/bbsd.log", "../log/error.log") < 0)
45          {          {
# Line 47  int main(int argc, char *argv[]) Line 47  int main(int argc, char *argv[])
47                  return -1;                  return -1;
48          }          }
49    
50          log_std_redirect(STDOUT_FILENO);          log_common_redir(STDOUT_FILENO);
51          log_err_redirect(STDERR_FILENO);          log_error_redir(STDERR_FILENO);
52    
53            if ((fp = fopen(TRIE_DICT_SHM_FILE, "w")) == NULL)
54            {
55                    log_error("fopen(%s) error\n", TRIE_DICT_SHM_FILE);
56                    return -1;
57            }
58            fclose(fp);
59    
60            if (trie_dict_init(TRIE_DICT_SHM_FILE, TRIE_NODE_PER_POOL) < 0)
61            {
62                    printf("trie_dict_init failed\n");
63                    return -1;
64            }
65    
66          ret = file_loader_init();          ret = file_loader_init();
67          if (ret < 0)          if (ret < 0)
# Line 67  int main(int argc, char *argv[]) Line 80  int main(int argc, char *argv[])
80    
81          for (i = 0; i < files_cnt; i++)          for (i = 0; i < files_cnt; i++)
82          {          {
83                  if (load_file_shm(files[i]) < 0)                  if (load_file(files[i]) < 0)
84                  {                  {
85                          printf("Load file %s error\n", files[i]);                          printf("Load file %s error\n", files[i]);
86                  }                  }
# Line 75  int main(int argc, char *argv[]) Line 88  int main(int argc, char *argv[])
88    
89          for (i = 0; i < files_cnt; i++)          for (i = 0; i < files_cnt; i++)
90          {          {
91                  if ((p_shm = get_file_shm(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)                  if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
92                  {                  {
93                          printf("Get file shm %s error\n", files[i]);                          printf("Get file shm %s error\n", files[i]);
94                  }                  }
# Line 87  int main(int argc, char *argv[]) Line 100  int main(int argc, char *argv[])
100                          {                          {
101                                  printf("Line %d: %ld ~ %ld\n", j, p_line_offsets[j], p_line_offsets[j + 1]);                                  printf("Line %d: %ld ~ %ld\n", j, p_line_offsets[j], p_line_offsets[j + 1]);
102                          }                          }
103    
104                            if (detach_file_shm(p_shm) < 0)
105                            {
106                                    log_error("detach_file_shm(%s) error\n", files[i]);
107                            }
108                  }                  }
109          }          }
110    
# Line 94  int main(int argc, char *argv[]) Line 112  int main(int argc, char *argv[])
112    
113          for (i = 0; i < files_cnt; i++)          for (i = 0; i < files_cnt; i++)
114          {          {
115                  if (unload_file_shm(files[i]) < 0)                  if (unload_file(files[i]) < 0)
116                  {                  {
117                          printf("Unload file %s error\n", files[i]);                          printf("Unload file %s error\n", files[i]);
118                  }                  }
# Line 102  int main(int argc, char *argv[]) Line 120  int main(int argc, char *argv[])
120    
121          for (i = 0; i < files_cnt; i++)          for (i = 0; i < files_cnt; i++)
122          {          {
123                  if (load_file_shm(files[i]) < 0)                  if (load_file(files[i]) < 0)
124                  {                  {
125                          printf("Load file %s error\n", files[i]);                          printf("Load file %s error\n", files[i]);
126                  }                  }
# Line 114  int main(int argc, char *argv[]) Line 132  int main(int argc, char *argv[])
132          {          {
133                  if (i % 2 == 0)                  if (i % 2 == 0)
134                  {                  {
135                          if (unload_file_shm(files[i]) < 0)                          if (unload_file(files[i]) < 0)
136                          {                          {
137                                  printf("Unload file %s error\n", files[i]);                                  printf("Unload file %s error\n", files[i]);
138                          }                          }
# Line 125  int main(int argc, char *argv[]) Line 143  int main(int argc, char *argv[])
143          {          {
144                  if (i % 2 != 0)                  if (i % 2 != 0)
145                  {                  {
146                          if ((p_shm = get_file_shm(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)                          if ((p_shm = get_file_shm_readonly(files[i], &data_len, &line_total, &p_data, &p_line_offsets)) == NULL)
147                          {                          {
148                                  printf("Get file shm %s error\n", files[i]);                                  printf("Get file shm %s error\n", files[i]);
149                          }                          }
150                          else                          else
151                          {                          {
152                                  printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total);                                  printf("File: %s size: %ld lines: %ld\n", files[i], data_len, line_total);
153    
154                                    if (detach_file_shm(p_shm) < 0)
155                                    {
156                                            log_error("detach_file_shm(%s) error\n", files[i]);
157                                    }
158                          }                          }
159                  }                  }
160          }          }
# Line 139  int main(int argc, char *argv[]) Line 162  int main(int argc, char *argv[])
162          file_loader_cleanup();          file_loader_cleanup();
163          file_loader_cleanup();          file_loader_cleanup();
164    
165            trie_dict_cleanup();
166    
167            if (unlink(TRIE_DICT_SHM_FILE) < 0)
168            {
169                    log_error("unlink(%s) error\n", TRIE_DICT_SHM_FILE);
170                    return -1;
171            }
172    
173          log_end();          log_end();
174    
175          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