First bunch of mhl_mem_free removal patches
[midnight-commander.git] / vfs / gc.c
blob961d7f8b4dc93c8567e9d31470519db276b4a647
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 #include <config.h>
25 #include <stdio.h>
26 #include <stdlib.h> /* For atol() */
27 #include <stdarg.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <sys/types.h>
31 #include <signal.h>
32 #include <ctype.h> /* is_digit() */
34 #include <mhl/memory.h>
35 #include <mhl/string.h>
37 #include "../src/global.h"
38 #include "../src/tty.h" /* enable/disable interrupt key */
39 #include "../src/wtools.h" /* message() */
40 #include "../src/main.h" /* print_vfs_message */
41 #include "utilvfs.h"
42 #include "vfs-impl.h"
43 #include "gc.h"
44 #include "vfs.h"
46 #include "../src/panel.h" /* get_current_panel() */
47 #include "../src/layout.h" /* get_current_type() */
50 int vfs_timeout = 60; /* VFS timeout in seconds */
52 static struct vfs_stamping *stamps;
55 static void
56 vfs_addstamp (struct vfs_class *v, vfsid id)
58 if (!(v->flags & VFSF_LOCAL) && id != NULL) {
59 struct vfs_stamping *stamp;
60 struct vfs_stamping *last_stamp = NULL;
62 for (stamp = stamps; stamp != NULL; stamp = stamp->next) {
63 if (stamp->v == v && stamp->id == id) {
64 gettimeofday (&(stamp->time), NULL);
65 return;
67 last_stamp = stamp;
69 stamp = g_new (struct vfs_stamping, 1);
70 stamp->v = v;
71 stamp->id = id;
73 gettimeofday (&(stamp->time), NULL);
74 stamp->next = 0;
76 if (stamps) {
77 /* Add to the end */
78 last_stamp->next = stamp;
79 } else {
80 /* Add first element */
81 stamps = stamp;
87 void
88 vfs_stamp (struct vfs_class *v, vfsid id)
90 struct vfs_stamping *stamp;
92 for (stamp = stamps; stamp != NULL; stamp = stamp->next)
93 if (stamp->v == v && stamp->id == id) {
94 gettimeofday (&(stamp->time), NULL);
95 return;
100 void
101 vfs_rmstamp (struct vfs_class *v, vfsid id)
103 struct vfs_stamping *stamp, *st1;
105 for (stamp = stamps, st1 = NULL; stamp != NULL;
106 st1 = stamp, stamp = stamp->next)
107 if (stamp->v == v && stamp->id == id) {
108 if (st1 == NULL) {
109 stamps = stamp->next;
110 } else {
111 st1->next = stamp->next;
113 g_free (stamp);
115 return;
120 /* Find VFS id for given directory name */
121 vfsid
122 vfs_getid (struct vfs_class *vclass, const char *dir)
124 char *dir1;
125 vfsid id = NULL;
127 /* append slash if needed */
128 dir1 = mhl_str_dir_plus_file (dir, "");
129 if (vclass->getid)
130 id = (*vclass->getid) (vclass, dir1);
132 g_free (dir1);
133 return id;
137 static void
138 vfs_stamp_path (char *path)
140 struct vfs_class *vfs;
141 vfsid id;
143 vfs = vfs_get_class (path);
144 id = vfs_getid (vfs, path);
145 vfs_addstamp (vfs, id);
150 * Create a new timestamp item by VFS class and VFS id.
152 void
153 vfs_stamp_create (struct vfs_class *oldvfs, vfsid oldvfsid)
155 struct vfs_class *nvfs, *n2vfs, *n3vfs;
156 vfsid nvfsid, n2vfsid, n3vfsid;
158 /* There are three directories we have to take care of: current_dir,
159 current_panel->cwd and other_panel->cwd. Athough most of the time either
160 current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
161 same, it's possible that all three are different -- Norbert */
163 if (!current_panel)
164 return;
166 nvfs = vfs_get_class (vfs_get_current_dir ());
167 nvfsid = vfs_getid (nvfs, vfs_get_current_dir ());
168 vfs_rmstamp (nvfs, nvfsid);
170 if ((nvfs == oldvfs && nvfsid == oldvfsid) || oldvfsid == NULL) {
171 return;
174 if (get_current_type () == view_listing) {
175 n2vfs = vfs_get_class (current_panel->cwd);
176 n2vfsid = vfs_getid (n2vfs, current_panel->cwd);
177 if (n2vfs == oldvfs && n2vfsid == oldvfsid)
178 return;
179 } else {
180 n2vfs = NULL;
181 n2vfsid = NULL;
184 if (get_other_type () == view_listing) {
185 n3vfs = vfs_get_class (other_panel->cwd);
186 n3vfsid = vfs_getid (n3vfs, other_panel->cwd);
187 if (n3vfs == oldvfs && n3vfsid == oldvfsid)
188 return;
189 } else {
190 n3vfs = NULL;
191 n3vfsid = NULL;
194 if (!oldvfs->nothingisopen || !(*oldvfs->nothingisopen) (oldvfsid))
195 return;
197 vfs_addstamp (oldvfs, oldvfsid);
201 void
202 vfs_add_current_stamps (void)
204 vfs_stamp_path (vfs_get_current_dir ());
206 if (current_panel) {
207 if (get_current_type () == view_listing)
208 vfs_stamp_path (current_panel->cwd);
211 if (other_panel) {
212 if (get_other_type () == view_listing)
213 vfs_stamp_path (other_panel->cwd);
218 /* Compare two timeval structures. Return 0 is t1 is less than t2. */
219 static inline int
220 timeoutcmp (struct timeval *t1, struct timeval *t2)
222 return ((t1->tv_sec < t2->tv_sec)
223 || ((t1->tv_sec == t2->tv_sec)
224 && (t1->tv_usec <= t2->tv_usec)));
228 /* This is called from timeout handler with now = 0, or can be called
229 with now = 1 to force freeing all filesystems that are not in use */
230 void
231 vfs_expire (int now)
233 static int locked = 0;
234 struct timeval time;
235 struct vfs_stamping *stamp, *st;
237 /* Avoid recursive invocation, e.g. when one of the free functions
238 calls message */
239 if (locked)
240 return;
241 locked = 1;
243 gettimeofday (&time, NULL);
244 time.tv_sec -= vfs_timeout;
246 for (stamp = stamps; stamp != NULL;) {
247 if (now || (timeoutcmp (&stamp->time, &time))) {
248 st = stamp->next;
249 if (stamp->v->free)
250 (*stamp->v->free) (stamp->id);
251 vfs_rmstamp (stamp->v, stamp->id);
252 stamp = st;
253 } else
254 stamp = stamp->next;
256 locked = 0;
261 * Return the number of seconds remaining to the vfs timeout.
262 * FIXME: The code should be improved to actually return the number of
263 * seconds until the next item times out.
266 vfs_timeouts ()
268 return stamps ? 10 : 0;
272 void
273 vfs_timeout_handler (void)
275 vfs_expire (0);
279 void
280 vfs_release_path (const char *dir)
282 struct vfs_class *oldvfs;
283 vfsid oldvfsid;
285 oldvfs = vfs_get_class (dir);
286 oldvfsid = vfs_getid (oldvfs, dir);
287 vfs_stamp_create (oldvfs, oldvfsid);
291 /* Free all data */
292 void
293 vfs_gc_done (void)
295 struct vfs_stamping *stamp, *st;
297 for (stamp = stamps, stamps = 0; stamp != NULL;) {
298 if (stamp->v->free)
299 (*stamp->v->free) (stamp->id);
300 st = stamp->next;
301 g_free (stamp);
302 stamp = st;
305 if (stamps)
306 vfs_rmstamp (stamps->v, stamps->id);