(vfs_stamp_path): get rid of path conversion.
[midnight-commander.git] / lib / vfs / gc.c
blob737ee7d05692b285ba44e57812875e0849b63ff6
1 /*
2 Virtual File System garbage collection code
4 Copyright (C) 2003-2018
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 vfs_path_t * vpath)
195 vfsid id;
196 const vfs_path_element_t *path_element;
198 path_element = vfs_path_get_by_index (vpath, -1);
200 id = vfs_getid (vpath);
201 vfs_addstamp (path_element->class, id);
204 /* --------------------------------------------------------------------------------------------- */
206 * Create a new timestamp item by VFS class and VFS id.
209 void
210 vfs_stamp_create (struct vfs_class *vclass, vfsid id)
212 vfsid nvfsid;
214 ev_vfs_stamp_create_t event_data = { vclass, id, FALSE };
215 const vfs_path_t *vpath;
216 const vfs_path_element_t *path_element;
218 /* There are three directories we have to take care of: current_dir,
219 current_panel->cwd and other_panel->cwd. Athough most of the time either
220 current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
221 same, it's possible that all three are different -- Norbert */
223 if (!mc_event_present (MCEVENT_GROUP_CORE, "vfs_timestamp"))
224 return;
226 vpath = vfs_get_raw_current_dir ();
227 path_element = vfs_path_get_by_index (vpath, -1);
229 nvfsid = vfs_getid (vpath);
230 vfs_rmstamp (path_element->class, nvfsid);
232 if (!(id == NULL || (path_element->class == vclass && nvfsid == id)))
234 mc_event_raise (MCEVENT_GROUP_CORE, "vfs_timestamp", (gpointer) & event_data);
236 if (!event_data.ret && vclass != NULL && vclass->nothingisopen != NULL
237 && vclass->nothingisopen (id) != 0)
238 vfs_addstamp (vclass, id);
242 /* --------------------------------------------------------------------------------------------- */
243 /** This is called from timeout handler with now = 0, or can be called
244 with now = 1 to force freeing all filesystems that are not in use */
246 void
247 vfs_expire (gboolean now)
249 static gboolean locked = FALSE;
250 struct timeval lc_time;
251 struct vfs_stamping *stamp, *st;
253 /* Avoid recursive invocation, e.g. when one of the free functions
254 calls message */
255 if (locked)
256 return;
257 locked = TRUE;
259 gettimeofday (&lc_time, NULL);
260 lc_time.tv_sec -= vfs_timeout;
262 for (stamp = stamps; stamp != NULL;)
264 if (now || (timeoutcmp (&stamp->time, &lc_time)))
266 st = stamp->next;
267 if (stamp->v->free)
268 (*stamp->v->free) (stamp->id);
269 vfs_rmstamp (stamp->v, stamp->id);
270 stamp = st;
272 else
273 stamp = stamp->next;
276 locked = FALSE;
279 /* --------------------------------------------------------------------------------------------- */
281 * Return the number of seconds remaining to the vfs timeout.
282 * FIXME: The code should be improved to actually return the number of
283 * seconds until the next item times out.
287 vfs_timeouts (void)
289 return stamps ? 10 : 0;
292 /* --------------------------------------------------------------------------------------------- */
294 void
295 vfs_timeout_handler (void)
297 vfs_expire (FALSE);
300 /* --------------------------------------------------------------------------------------------- */
302 void
303 vfs_release_path (const vfs_path_t * vpath)
305 const vfs_path_element_t *path_element;
307 path_element = vfs_path_get_by_index (vpath, -1);
308 vfs_stamp_create (path_element->class, vfs_getid (vpath));
311 /* --------------------------------------------------------------------------------------------- */
312 /* Free all data */
314 void
315 vfs_gc_done (void)
317 struct vfs_stamping *stamp, *st;
319 for (stamp = stamps, stamps = 0; stamp != NULL;)
321 if (stamp->v->free)
322 (*stamp->v->free) (stamp->id);
323 st = stamp->next;
324 g_free (stamp);
325 stamp = st;
328 if (stamps)
329 vfs_rmstamp (stamps->v, stamps->id);
332 /* --------------------------------------------------------------------------------------------- */