Fixes
[opsoft.git] / silentbob / plugins / plugin_cache.cxx
blob0ee45ad8280ba93acb510bd94f0e65359c74ae35
1 /*
2 * (c) Oleg Puchinin 2006
3 * graycardinalster@gmail.com
5 */
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <gclib/gclib.h>
12 #include <sys/wait.h>
13 #include <mod.h>
14 #include <head.h>
15 #include <dbg.h>
17 #ifdef POSIX_FADV_WILLNEED
18 #define cache_opt __cache_opt
19 #else
20 #define POSIX_FADV_WILLNEED 0
21 #define POSIX_FADV_DONTNEED 0
22 #define cache_opt __nofadvise
23 #endif
25 extern "C" DArray * plugin_init (struct env_t *env);
27 void cache_file (char * file, int adv)
29 int fd;
31 fd = open (file, O_RDONLY);
32 if (fd < 0)
33 return;
35 Dposix_fadvise (fd, 0, fdsize (fd), adv);
36 close (fd);
37 return;
40 char __no_fadvise (DArray * d_opts, int * pos)
42 char * S = d_opts->get (*pos);
43 if (EQ (S, "--cache-load") || EQ (S, "--cache-free")) {
44 fprintf (stderr, "Sorry, your system do not support %s option.\n", S);
45 return 1;
47 return 0;
50 char __cache_opt (DArray * d_opts, int * pos)
52 DArray * files = ENV->d_files;
53 char * S;
54 int i;
56 if (! d_opts || ! pos)
57 return 0;
59 S = d_opts->get (*pos);
60 if (EQ (S, "--cache-load")) {
61 for (i = 0; i < files->get_size (); i++)
62 cache_file (files->get (i), POSIX_FADV_WILLNEED);
63 exit (0);
64 return 1;
67 if (EQ (S, "--cache-free")) {
68 for (i = 0; i < files->get_size (); i++)
69 cache_file (files->get (i), POSIX_FADV_DONTNEED);
70 exit (0);
73 return 0;
76 void cache_short_info ()
78 printf ("System cache. (linux only)");
81 void cache_long_info ()
83 printf ("System cache. (linux only)\n");
84 printf ("Version: 1.0\n");
85 printf ("Load files to system cache.\n");
86 printf ("options: [--cache-load | --cache-free]\n");
90 DArray * plugin_init (struct env_t *env)
92 DArray * Ret;
93 struct mod_t * plug;
95 ENV = env;
96 Ret = new DArray (1);
97 plug = CNEW (mod_t, 1);
98 memset (plug, 0, sizeof (mod_t));
99 plug->Version = strdup ("1.0");
100 plug->Name = strdup ("Cache");
101 plug->opt2 = cache_opt;
102 plug->short_info = cache_short_info;
103 plug->long_info = cache_long_info;
105 ENV->listOptions->add ("--cache-load");
106 ENV->listOptions->add ("--cache-free");
108 Ret->add (LPCHAR (plug));
109 return Ret;