Following prototypes of functions was changed in VFS-module API:
[midnight-commander.git] / lib / vfs / gc.c
blob09f8fe8bfc37d7a4edb1229bec70939732a89212
1 /* Virtual File System garbage collection code
2 Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
4 Written by: 1995 Miguel de Icaza
5 1995 Jakub Jelinek
6 1998 Pavel Machek
7 2003 Pavel Roskin
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public License
11 as published by the Free Software Foundation; either version 2 of
12 the License, or (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /**
24 * \file
25 * \brief Source: Virtual File System: garbage collection code
26 * \author Miguel de Icaza
27 * \author Jakub Jelinek
28 * \author Pavel Machek
29 * \author Pavel Roskin
30 * \date 1995, 1998, 2003
34 #include <config.h>
36 #include <stdlib.h> /* For atol() */
37 #include <sys/types.h>
38 #include <sys/time.h> /* gettimeofday() */
40 #include "lib/global.h"
41 #include "lib/event.h"
43 #include "vfs.h"
44 #include "utilvfs.h"
46 #include "gc.h"
48 /*** global variables ****************************************************************************/
50 int vfs_timeout = 60; /* VFS timeout in seconds */
52 /*** file scope macro definitions ****************************************************************/
54 /*** file scope type declarations ****************************************************************/
56 /*** file scope variables ************************************************************************/
58 static struct vfs_stamping *stamps;
60 /*** file scope functions ************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
63 static void
64 vfs_addstamp (struct vfs_class *v, vfsid id)
66 if (!(v->flags & VFSF_LOCAL) && id != NULL)
68 struct vfs_stamping *stamp;
69 struct vfs_stamping *last_stamp = NULL;
71 for (stamp = stamps; stamp != NULL; stamp = stamp->next)
73 if (stamp->v == v && stamp->id == id)
75 gettimeofday (&(stamp->time), NULL);
76 return;
78 last_stamp = stamp;
80 stamp = g_new (struct vfs_stamping, 1);
81 stamp->v = v;
82 stamp->id = id;
84 gettimeofday (&(stamp->time), NULL);
85 stamp->next = 0;
87 if (stamps)
89 /* Add to the end */
90 last_stamp->next = stamp;
92 else
94 /* Add first element */
95 stamps = stamp;
100 /* --------------------------------------------------------------------------------------------- */
101 /** Compare two timeval structures. Return 0 is t1 is less than t2. */
103 static inline int
104 timeoutcmp (struct timeval *t1, struct timeval *t2)
106 return ((t1->tv_sec < t2->tv_sec)
107 || ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec <= t2->tv_usec)));
110 /* --------------------------------------------------------------------------------------------- */
111 /*** public functions ****************************************************************************/
112 /* --------------------------------------------------------------------------------------------- */
114 void
115 vfs_stamp (struct vfs_class *v, vfsid id)
117 struct vfs_stamping *stamp;
119 for (stamp = stamps; stamp != NULL; stamp = stamp->next)
120 if (stamp->v == v && stamp->id == id)
122 gettimeofday (&(stamp->time), NULL);
123 return;
127 /* --------------------------------------------------------------------------------------------- */
129 void
130 vfs_rmstamp (struct vfs_class *v, vfsid id)
132 struct vfs_stamping *stamp, *st1;
134 for (stamp = stamps, st1 = NULL; stamp != NULL; st1 = stamp, stamp = stamp->next)
135 if (stamp->v == v && stamp->id == id)
137 if (st1 == NULL)
139 stamps = stamp->next;
141 else
143 st1->next = stamp->next;
145 g_free (stamp);
147 return;
151 /* --------------------------------------------------------------------------------------------- */
153 void
154 vfs_stamp_path (const char *path)
156 vfsid id;
157 vfs_path_t *vpath;
158 vfs_path_element_t *path_element;
160 vpath = vfs_path_from_str (path);
161 path_element = vfs_path_get_by_index (vpath, vfs_path_length (vpath) - 1);
163 id = vfs_getid (vpath);
164 vfs_addstamp (path_element->class, id);
165 vfs_path_free (vpath);
168 /* --------------------------------------------------------------------------------------------- */
170 * Create a new timestamp item by VFS class and VFS id.
173 void
174 vfs_stamp_create (struct vfs_class *vclass, vfsid id)
176 vfsid nvfsid;
178 ev_vfs_stamp_create_t event_data = { vclass, id, FALSE };
179 vfs_path_t *vpath;
180 vfs_path_element_t *path_element;
182 /* There are three directories we have to take care of: current_dir,
183 current_panel->cwd and other_panel->cwd. Athough most of the time either
184 current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
185 same, it's possible that all three are different -- Norbert */
187 if (!mc_event_present (MCEVENT_GROUP_CORE, "vfs_timestamp"))
188 return;
190 vpath = vfs_path_from_str (vfs_get_current_dir ());
191 path_element = vfs_path_get_by_index (vpath, vfs_path_length (vpath) - 1);
193 nvfsid = vfs_getid (vpath);
194 vfs_rmstamp (path_element->class, nvfsid);
196 if (!(id == NULL || (path_element->class == vclass && nvfsid == id)))
198 mc_event_raise (MCEVENT_GROUP_CORE, "vfs_timestamp", (gpointer) & event_data);
200 if (!event_data.ret && vclass != NULL && vclass->nothingisopen != NULL
201 && vclass->nothingisopen (id) != 0)
202 vfs_addstamp (vclass, id);
204 vfs_path_free (vpath);
207 /* --------------------------------------------------------------------------------------------- */
208 /** This is called from timeout handler with now = 0, or can be called
209 with now = 1 to force freeing all filesystems that are not in use */
211 void
212 vfs_expire (int now)
214 static int locked = 0;
215 struct timeval lc_time;
216 struct vfs_stamping *stamp, *st;
218 /* Avoid recursive invocation, e.g. when one of the free functions
219 calls message */
220 if (locked)
221 return;
222 locked = 1;
224 gettimeofday (&lc_time, NULL);
225 lc_time.tv_sec -= vfs_timeout;
227 for (stamp = stamps; stamp != NULL;)
229 if (now || (timeoutcmp (&stamp->time, &lc_time)))
231 st = stamp->next;
232 if (stamp->v->free)
233 (*stamp->v->free) (stamp->id);
234 vfs_rmstamp (stamp->v, stamp->id);
235 stamp = st;
237 else
238 stamp = stamp->next;
240 locked = 0;
243 /* --------------------------------------------------------------------------------------------- */
245 * Return the number of seconds remaining to the vfs timeout.
246 * FIXME: The code should be improved to actually return the number of
247 * seconds until the next item times out.
251 vfs_timeouts ()
253 return stamps ? 10 : 0;
256 /* --------------------------------------------------------------------------------------------- */
258 void
259 vfs_timeout_handler (void)
261 vfs_expire (0);
264 /* --------------------------------------------------------------------------------------------- */
266 void
267 vfs_release_path (const char *dir)
269 vfs_path_t *vpath;
270 vfs_path_element_t *path_element;
272 vpath = vfs_path_from_str (dir);
273 path_element = vfs_path_get_by_index (vpath, vfs_path_length (vpath) - 1);
275 vfs_stamp_create (path_element->class, vfs_getid (vpath));
278 /* --------------------------------------------------------------------------------------------- */
279 /* Free all data */
281 void
282 vfs_gc_done (void)
284 struct vfs_stamping *stamp, *st;
286 for (stamp = stamps, stamps = 0; stamp != NULL;)
288 if (stamp->v->free)
289 (*stamp->v->free) (stamp->id);
290 st = stamp->next;
291 g_free (stamp);
292 stamp = st;
295 if (stamps)
296 vfs_rmstamp (stamps->v, stamps->id);
299 /* --------------------------------------------------------------------------------------------- */