2 Virtual File System garbage collection code
4 Copyright (C) 2003, 2004, 2005, 2007, 2011
5 The Free Software Foundation, Inc.
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/>.
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
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"
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 /* --------------------------------------------------------------------------------------------- */
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
);
86 stamp
= g_new (struct vfs_stamping
, 1);
90 gettimeofday (&(stamp
->time
), NULL
);
96 last_stamp
->next
= stamp
;
100 /* Add first element */
106 /* --------------------------------------------------------------------------------------------- */
107 /** Compare two timeval structures. Return 0 is t1 is less than t2. */
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 /* --------------------------------------------------------------------------------------------- */
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
);
133 /* --------------------------------------------------------------------------------------------- */
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
)
145 stamps
= stamp
->next
;
149 st1
->next
= stamp
->next
;
157 /* --------------------------------------------------------------------------------------------- */
160 vfs_stamp_path (const char *path
)
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.
180 vfs_stamp_create (struct vfs_class
*vclass
, vfsid id
)
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"))
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 */
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
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
)))
238 (*stamp
->v
->free
) (stamp
->id
);
239 vfs_rmstamp (stamp
->v
, stamp
->id
);
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.
259 return stamps
? 10 : 0;
262 /* --------------------------------------------------------------------------------------------- */
265 vfs_timeout_handler (void)
270 /* --------------------------------------------------------------------------------------------- */
273 vfs_release_path (const char *dir
)
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 /* --------------------------------------------------------------------------------------------- */
291 struct vfs_stamping
*stamp
, *st
;
293 for (stamp
= stamps
, stamps
= 0; stamp
!= NULL
;)
296 (*stamp
->v
->free
) (stamp
->id
);
303 vfs_rmstamp (stamps
->v
, stamps
->id
);
306 /* --------------------------------------------------------------------------------------------- */