panel.h: remove unused variable declaration.
[midnight-commander.git] / lib / vfs / gc.c
blob0a5d4b68a5544c55635dd95e12d677b9eb225b02
1 /*
2 Virtual File System garbage collection code
4 Copyright (C) 2003-2015
5 Free Software Foundation, Inc.
7 Written by:
8 Miguel de Icaza, 1995
9 Jakub Jelinek, 1995
10 Pavel Machek, 1998
11 Pavel Roskin, 2003
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /**
30 * \file
31 * \brief Source: Virtual File System: garbage collection code
32 * \author Miguel de Icaza
33 * \author Jakub Jelinek
34 * \author Pavel Machek
35 * \author Pavel Roskin
36 * \date 1995, 1998, 2003
40 #include <config.h>
42 #include <stdlib.h> /* For atol() */
43 #include <sys/types.h>
44 #include <sys/time.h> /* gettimeofday() */
46 #include "lib/global.h"
47 #include "lib/event.h"
49 #include "vfs.h"
50 #include "utilvfs.h"
52 #include "gc.h"
55 * The garbage collection mechanism is based on "stamps".
57 * A stamp is a record that says "I'm a filesystem which is no longer in
58 * use. Free me when you get a chance."
60 * This file contains a set of functions used for managing this stamp. You
61 * should use them when you write your own filesystem. Here are some rules
62 * of thumb:
64 * (1) When the last open file in your filesystem gets closed, conditionally
65 * create a stamp. You do this with vfs_stamp_create(). (The meaning
66 * of "conditionaly" is explained below.)
68 * (2) When a file in your filesystem is opened, delete the stamp. You do
69 * this with vfs_rmstamp().
71 * (3) When a path inside your filesystem is invoked, call vfs_stamp() to
72 * postpone the free'ing of your filesystem a bit. (This simply updates
73 * a timestamp variable inside the stamp.)
75 * Additionally, when a user navigates to a new directory in a panel (or a
76 * programmer uses mc_chdir()), a stamp is conditionally created for the
77 * previous directory's filesystem. This ensures that that filesystem is
78 * free'ed. (see: _do_panel_cd() -> vfs_release_path(); mc_chdir()).
80 * We've spoken here of "conditionally creating" a stamp. What we mean is
81 * that vfs_stamp_create() is to be used: this function creates a stamp
82 * only if no directories are open (aka "active") in your filesystem. (If
83 * there _are_ directories open, it means that the filesystem is in use, in
84 * which case we don't want to free it.)
87 /*** global variables ****************************************************************************/
89 int vfs_timeout = 60; /* VFS timeout in seconds */
91 /*** file scope macro definitions ****************************************************************/
93 /*** file scope type declarations ****************************************************************/
95 /*** file scope variables ************************************************************************/
97 static struct vfs_stamping *stamps;
99 /*** file scope functions ************************************************************************/
100 /* --------------------------------------------------------------------------------------------- */
102 static void
103 vfs_addstamp (struct vfs_class *v, vfsid id)
105 if (!(v->flags & VFSF_LOCAL) && id != NULL)
107 struct vfs_stamping *stamp;
108 struct vfs_stamping *last_stamp = NULL;
110 for (stamp = stamps; stamp != NULL; stamp = stamp->next)
112 if (stamp->v == v && stamp->id == id)
114 gettimeofday (&(stamp->time), NULL);
115 return;
117 last_stamp = stamp;
119 stamp = g_new (struct vfs_stamping, 1);
120 stamp->v = v;
121 stamp->id = id;
123 gettimeofday (&(stamp->time), NULL);
124 stamp->next = 0;
126 if (stamps)
128 /* Add to the end */
129 last_stamp->next = stamp;
131 else
133 /* Add first element */
134 stamps = stamp;
139 /* --------------------------------------------------------------------------------------------- */
140 /** Compare two timeval structures. Return 0 is t1 is less than t2. */
142 static inline int
143 timeoutcmp (struct timeval *t1, struct timeval *t2)
145 return ((t1->tv_sec < t2->tv_sec)
146 || ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec <= t2->tv_usec)));
149 /* --------------------------------------------------------------------------------------------- */
150 /*** public functions ****************************************************************************/
151 /* --------------------------------------------------------------------------------------------- */
153 void
154 vfs_stamp (struct vfs_class *v, vfsid id)
156 struct vfs_stamping *stamp;
158 for (stamp = stamps; stamp != NULL; stamp = stamp->next)
159 if (stamp->v == v && stamp->id == id)
161 gettimeofday (&(stamp->time), NULL);
162 return;
166 /* --------------------------------------------------------------------------------------------- */
168 void
169 vfs_rmstamp (struct vfs_class *v, vfsid id)
171 struct vfs_stamping *stamp, *st1;
173 for (stamp = stamps, st1 = NULL; stamp != NULL; st1 = stamp, stamp = stamp->next)
174 if (stamp->v == v && stamp->id == id)
176 if (st1 == NULL)
178 stamps = stamp->next;
180 else
182 st1->next = stamp->next;
184 g_free (stamp);
186 return;
190 /* --------------------------------------------------------------------------------------------- */
192 void
193 vfs_stamp_path (const char *path)
195 vfsid id;
196 vfs_path_t *vpath;
197 const vfs_path_element_t *path_element;
199 vpath = vfs_path_from_str (path);
200 path_element = vfs_path_get_by_index (vpath, -1);
202 id = vfs_getid (vpath);
203 vfs_addstamp (path_element->class, id);
204 vfs_path_free (vpath);
207 /* --------------------------------------------------------------------------------------------- */
209 * Create a new timestamp item by VFS class and VFS id.
212 void
213 vfs_stamp_create (struct vfs_class *vclass, vfsid id)
215 vfsid nvfsid;
217 ev_vfs_stamp_create_t event_data = { vclass, id, FALSE };
218 const vfs_path_t *vpath;
219 const vfs_path_element_t *path_element;
221 /* There are three directories we have to take care of: current_dir,
222 current_panel->cwd and other_panel->cwd. Athough most of the time either
223 current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
224 same, it's possible that all three are different -- Norbert */
226 if (!mc_event_present (MCEVENT_GROUP_CORE, "vfs_timestamp"))
227 return;
229 vpath = vfs_get_raw_current_dir ();
230 path_element = vfs_path_get_by_index (vpath, -1);
232 nvfsid = vfs_getid (vpath);
233 vfs_rmstamp (path_element->class, nvfsid);
235 if (!(id == NULL || (path_element->class == vclass && nvfsid == id)))
237 mc_event_raise (MCEVENT_GROUP_CORE, "vfs_timestamp", (gpointer) & event_data);
239 if (!event_data.ret && vclass != NULL && vclass->nothingisopen != NULL
240 && vclass->nothingisopen (id) != 0)
241 vfs_addstamp (vclass, id);
245 /* --------------------------------------------------------------------------------------------- */
246 /** This is called from timeout handler with now = 0, or can be called
247 with now = 1 to force freeing all filesystems that are not in use */
249 void
250 vfs_expire (gboolean now)
252 static gboolean locked = FALSE;
253 struct timeval lc_time;
254 struct vfs_stamping *stamp, *st;
256 /* Avoid recursive invocation, e.g. when one of the free functions
257 calls message */
258 if (locked)
259 return;
260 locked = TRUE;
262 gettimeofday (&lc_time, NULL);
263 lc_time.tv_sec -= vfs_timeout;
265 for (stamp = stamps; stamp != NULL;)
267 if (now || (timeoutcmp (&stamp->time, &lc_time)))
269 st = stamp->next;
270 if (stamp->v->free)
271 (*stamp->v->free) (stamp->id);
272 vfs_rmstamp (stamp->v, stamp->id);
273 stamp = st;
275 else
276 stamp = stamp->next;
279 locked = FALSE;
282 /* --------------------------------------------------------------------------------------------- */
284 * Return the number of seconds remaining to the vfs timeout.
285 * FIXME: The code should be improved to actually return the number of
286 * seconds until the next item times out.
290 vfs_timeouts (void)
292 return stamps ? 10 : 0;
295 /* --------------------------------------------------------------------------------------------- */
297 void
298 vfs_timeout_handler (void)
300 vfs_expire (FALSE);
303 /* --------------------------------------------------------------------------------------------- */
305 void
306 vfs_release_path (const vfs_path_t * vpath)
308 const vfs_path_element_t *path_element;
310 path_element = vfs_path_get_by_index (vpath, -1);
311 vfs_stamp_create (path_element->class, vfs_getid (vpath));
314 /* --------------------------------------------------------------------------------------------- */
315 /* Free all data */
317 void
318 vfs_gc_done (void)
320 struct vfs_stamping *stamp, *st;
322 for (stamp = stamps, stamps = 0; stamp != NULL;)
324 if (stamp->v->free)
325 (*stamp->v->free) (stamp->id);
326 st = stamp->next;
327 g_free (stamp);
328 stamp = st;
331 if (stamps)
332 vfs_rmstamp (stamps->v, stamps->id);
335 /* --------------------------------------------------------------------------------------------- */