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
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. */
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
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"
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 /* --------------------------------------------------------------------------------------------- */
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
);
80 stamp
= g_new (struct vfs_stamping
, 1);
84 gettimeofday (&(stamp
->time
), NULL
);
90 last_stamp
->next
= stamp
;
94 /* Add first element */
100 /* --------------------------------------------------------------------------------------------- */
101 /** Compare two timeval structures. Return 0 is t1 is less than t2. */
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 /* --------------------------------------------------------------------------------------------- */
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
);
127 /* --------------------------------------------------------------------------------------------- */
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
)
139 stamps
= stamp
->next
;
143 st1
->next
= stamp
->next
;
151 /* --------------------------------------------------------------------------------------------- */
154 vfs_stamp_path (const char *path
)
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.
174 vfs_stamp_create (struct vfs_class
*vclass
, vfsid id
)
178 ev_vfs_stamp_create_t event_data
= { vclass
, id
, FALSE
};
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"))
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 */
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
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
)))
233 (*stamp
->v
->free
) (stamp
->id
);
234 vfs_rmstamp (stamp
->v
, stamp
->id
);
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.
253 return stamps
? 10 : 0;
256 /* --------------------------------------------------------------------------------------------- */
259 vfs_timeout_handler (void)
264 /* --------------------------------------------------------------------------------------------- */
267 vfs_release_path (const char *dir
)
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 /* --------------------------------------------------------------------------------------------- */
284 struct vfs_stamping
*stamp
, *st
;
286 for (stamp
= stamps
, stamps
= 0; stamp
!= NULL
;)
289 (*stamp
->v
->free
) (stamp
->id
);
296 vfs_rmstamp (stamps
->v
, stamps
->id
);
299 /* --------------------------------------------------------------------------------------------- */