Very old versions for history.
[opsoft_archive.git] / silentbob / silent_bob-1.2 / src / plugins / plugin_cache.cpp
blobc762c6d4adcf59f91c0ea365c6f8e95088153d41
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, fsize (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_info ()
78 printf ("System cache.\n");
79 printf ("Version: 1.0\n");
80 printf ("Load files to system cache.\n");
81 printf ("options: [--cache-load | --cache-free]\n");
84 DArray * plugin_init (struct env_t *env)
86 DArray * Ret;
87 struct mod_feature * pm;
89 ENV = env;
90 Ret = new DArray (1);
91 pm = CNEW (mod_feature, 1);
92 memset (pm, 0, sizeof (mod_feature));
93 pm->mod.Type = TYPE_FEATURE;
94 pm->mod.Version = strdup ("1.0");
95 pm->mod.Name = strdup ("Cache");
96 pm->opt2 = cache_opt;
97 pm->mod.info = cache_info;
99 Ret->add (LPCHAR (pm));
100 return Ret;