Updated doc/NEWS file
[midnight-commander.git] / lib / vfs / gc.c
blobc38080aecd4e3c2b1c06573a44db3fff5b7e5119
1 /*
2 Virtual File System garbage collection code
4 Copyright (C) 2003, 2004, 2005, 2007, 2011
5 The 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"
54 /*** global variables ****************************************************************************/
56 int vfs_timeout = 60; /* VFS timeout in seconds */
58 /*** file scope macro definitions ****************************************************************/
60 /*** file scope type declarations ****************************************************************/
62 /*** file scope variables ************************************************************************/
64 static struct vfs_stamping *stamps;
66 /*** file scope functions ************************************************************************/
67 /* --------------------------------------------------------------------------------------------- */
69 static void
70 vfs_addstamp (struct vfs_class *v, vfsid id)
72 if (!(v->flags & VFSF_LOCAL) && id != NULL)
74 struct vfs_stamping *stamp;
75 struct vfs_stamping *last_stamp = NULL;
77 for (stamp = stamps; stamp != NULL; stamp = stamp->next)
79 if (stamp->v == v && stamp->id == id)
81 gettimeofday (&(stamp->time), NULL);
82 return;
84 last_stamp = stamp;
86 stamp = g_new (struct vfs_stamping, 1);
87 stamp->v = v;
88 stamp->id = id;
90 gettimeofday (&(stamp->time), NULL);
91 stamp->next = 0;
93 if (stamps)
95 /* Add to the end */
96 last_stamp->next = stamp;
98 else
100 /* Add first element */
101 stamps = stamp;
106 /* --------------------------------------------------------------------------------------------- */
107 /** Compare two timeval structures. Return 0 is t1 is less than t2. */
109 static inline int
110 timeoutcmp (struct timeval *t1, struct timeval *t2)
112 return ((t1->tv_sec < t2->tv_sec)
113 || ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec <= t2->tv_usec)));
116 /* --------------------------------------------------------------------------------------------- */
117 /*** public functions ****************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
120 void
121 vfs_stamp (struct vfs_class *v, vfsid id)
123 struct vfs_stamping *stamp;
125 for (stamp = stamps; stamp != NULL; stamp = stamp->next)
126 if (stamp->v == v && stamp->id == id)
128 gettimeofday (&(stamp->time), NULL);
129 return;
133 /* --------------------------------------------------------------------------------------------- */
135 void
136 vfs_rmstamp (struct vfs_class *v, vfsid id)
138 struct vfs_stamping *stamp, *st1;
140 for (stamp = stamps, st1 = NULL; stamp != NULL; st1 = stamp, stamp = stamp->next)
141 if (stamp->v == v && stamp->id == id)
143 if (st1 == NULL)
145 stamps = stamp->next;
147 else
149 st1->next = stamp->next;
151 g_free (stamp);
153 return;
157 /* --------------------------------------------------------------------------------------------- */
159 void
160 vfs_stamp_path (const char *path)
162 vfsid id;
163 vfs_path_t *vpath;
164 const vfs_path_element_t *path_element;
166 vpath = vfs_path_from_str (path);
167 path_element = vfs_path_get_by_index (vpath, -1);
169 id = vfs_getid (vpath);
170 vfs_addstamp (path_element->class, id);
171 vfs_path_free (vpath);
174 /* --------------------------------------------------------------------------------------------- */
176 * Create a new timestamp item by VFS class and VFS id.
179 void
180 vfs_stamp_create (struct vfs_class *vclass, vfsid id)
182 vfsid nvfsid;
184 ev_vfs_stamp_create_t event_data = { vclass, id, FALSE };
185 const vfs_path_t *vpath;
186 const vfs_path_element_t *path_element;
188 /* There are three directories we have to take care of: current_dir,
189 current_panel->cwd and other_panel->cwd. Athough most of the time either
190 current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
191 same, it's possible that all three are different -- Norbert */
193 if (!mc_event_present (MCEVENT_GROUP_CORE, "vfs_timestamp"))
194 return;
196 vpath = vfs_get_raw_current_dir ();
197 path_element = vfs_path_get_by_index (vpath, -1);
199 nvfsid = vfs_getid (vpath);
200 vfs_rmstamp (path_element->class, nvfsid);
202 if (!(id == NULL || (path_element->class == vclass && nvfsid == id)))
204 mc_event_raise (MCEVENT_GROUP_CORE, "vfs_timestamp", (gpointer) & event_data);
206 if (!event_data.ret && vclass != NULL && vclass->nothingisopen != NULL
207 && vclass->nothingisopen (id) != 0)
208 vfs_addstamp (vclass, id);
212 /* --------------------------------------------------------------------------------------------- */
213 /** This is called from timeout handler with now = 0, or can be called
214 with now = 1 to force freeing all filesystems that are not in use */
216 void
217 vfs_expire (gboolean now)
219 static gboolean locked = FALSE;
220 struct timeval lc_time;
221 struct vfs_stamping *stamp, *st;
223 /* Avoid recursive invocation, e.g. when one of the free functions
224 calls message */
225 if (locked)
226 return;
227 locked = TRUE;
229 gettimeofday (&lc_time, NULL);
230 lc_time.tv_sec -= vfs_timeout;
232 for (stamp = stamps; stamp != NULL;)
234 if (now || (timeoutcmp (&stamp->time, &lc_time)))
236 st = stamp->next;
237 if (stamp->v->free)
238 (*stamp->v->free) (stamp->id);
239 vfs_rmstamp (stamp->v, stamp->id);
240 stamp = st;
242 else
243 stamp = stamp->next;
246 locked = FALSE;
249 /* --------------------------------------------------------------------------------------------- */
251 * Return the number of seconds remaining to the vfs timeout.
252 * FIXME: The code should be improved to actually return the number of
253 * seconds until the next item times out.
257 vfs_timeouts (void)
259 return stamps ? 10 : 0;
262 /* --------------------------------------------------------------------------------------------- */
264 void
265 vfs_timeout_handler (void)
267 vfs_expire (FALSE);
270 /* --------------------------------------------------------------------------------------------- */
272 void
273 vfs_release_path (const char *dir)
275 vfs_path_t *vpath;
276 const vfs_path_element_t *path_element;
278 vpath = vfs_path_from_str (dir);
279 path_element = vfs_path_get_by_index (vpath, -1);
281 vfs_stamp_create (path_element->class, vfs_getid (vpath));
282 vfs_path_free (vpath);
285 /* --------------------------------------------------------------------------------------------- */
286 /* Free all data */
288 void
289 vfs_gc_done (void)
291 struct vfs_stamping *stamp, *st;
293 for (stamp = stamps, stamps = 0; stamp != NULL;)
295 if (stamp->v->free)
296 (*stamp->v->free) (stamp->id);
297 st = stamp->next;
298 g_free (stamp);
299 stamp = st;
302 if (stamps)
303 vfs_rmstamp (stamps->v, stamps->id);
306 /* --------------------------------------------------------------------------------------------- */