2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Virtual File System garbage collection code
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2019-01-01 08:48:53 +03:00
|
|
|
Copyright (C) 2003-2019
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
Written by:
|
|
|
|
Miguel de Icaza, 1995
|
|
|
|
Jakub Jelinek, 1995
|
|
|
|
Pavel Machek, 1998
|
|
|
|
Pavel Roskin, 2003
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
|
|
|
The Midnight Commander is free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
|
|
|
|
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
2003-11-08 02:43:55 +03:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2011-10-15 14:56:47 +04:00
|
|
|
GNU General Public License for more details.
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2009-02-07 15:54:58 +03:00
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief Source: Virtual File System: garbage collection code
|
|
|
|
* \author Miguel de Icaza
|
|
|
|
* \author Jakub Jelinek
|
|
|
|
* \author Pavel Machek
|
|
|
|
* \author Pavel Roskin
|
|
|
|
* \date 1995, 1998, 2003
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-11-08 02:43:55 +03:00
|
|
|
#include <config.h>
|
|
|
|
|
2019-03-23 16:50:19 +03:00
|
|
|
#include <stdlib.h> /* For atol() */
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h> /* gettimeofday() */
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2010-01-20 18:11:52 +03:00
|
|
|
#include "lib/global.h"
|
2011-02-15 17:41:22 +03:00
|
|
|
#include "lib/event.h"
|
2009-05-08 14:01:05 +04:00
|
|
|
|
2011-02-15 16:44:17 +03:00
|
|
|
#include "vfs.h"
|
2010-07-08 11:32:37 +04:00
|
|
|
#include "utilvfs.h"
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2010-07-22 12:07:07 +04:00
|
|
|
#include "gc.h"
|
|
|
|
|
2015-05-25 20:20:40 +03:00
|
|
|
/*
|
|
|
|
* The garbage collection mechanism is based on "stamps".
|
|
|
|
*
|
|
|
|
* A stamp is a record that says "I'm a filesystem which is no longer in
|
|
|
|
* use. Free me when you get a chance."
|
|
|
|
*
|
|
|
|
* This file contains a set of functions used for managing this stamp. You
|
|
|
|
* should use them when you write your own filesystem. Here are some rules
|
|
|
|
* of thumb:
|
|
|
|
*
|
|
|
|
* (1) When the last open file in your filesystem gets closed, conditionally
|
|
|
|
* create a stamp. You do this with vfs_stamp_create(). (The meaning
|
|
|
|
* of "conditionaly" is explained below.)
|
|
|
|
*
|
|
|
|
* (2) When a file in your filesystem is opened, delete the stamp. You do
|
|
|
|
* this with vfs_rmstamp().
|
|
|
|
*
|
|
|
|
* (3) When a path inside your filesystem is invoked, call vfs_stamp() to
|
|
|
|
* postpone the free'ing of your filesystem a bit. (This simply updates
|
|
|
|
* a timestamp variable inside the stamp.)
|
|
|
|
*
|
|
|
|
* Additionally, when a user navigates to a new directory in a panel (or a
|
|
|
|
* programmer uses mc_chdir()), a stamp is conditionally created for the
|
|
|
|
* previous directory's filesystem. This ensures that that filesystem is
|
|
|
|
* free'ed. (see: _do_panel_cd() -> vfs_release_path(); mc_chdir()).
|
|
|
|
*
|
|
|
|
* We've spoken here of "conditionally creating" a stamp. What we mean is
|
|
|
|
* that vfs_stamp_create() is to be used: this function creates a stamp
|
|
|
|
* only if no directories are open (aka "active") in your filesystem. (If
|
|
|
|
* there _are_ directories open, it means that the filesystem is in use, in
|
|
|
|
* which case we don't want to free it.)
|
|
|
|
*/
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
int vfs_timeout = 60; /* VFS timeout in seconds */
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
2019-01-26 16:26:02 +03:00
|
|
|
#define VFS_STAMPING(a) ((struct vfs_stamping *)(a))
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
2019-01-26 12:56:07 +03:00
|
|
|
struct vfs_stamping
|
|
|
|
{
|
|
|
|
struct vfs_class *v;
|
|
|
|
vfsid id;
|
2019-03-23 16:50:19 +03:00
|
|
|
struct timeval time;
|
2019-01-26 12:56:07 +03:00
|
|
|
};
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2019-01-26 16:26:02 +03:00
|
|
|
static GSList *stamps = NULL;
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2019-01-26 16:26:02 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2019-03-23 16:50:29 +03:00
|
|
|
/** Compare two timeval structures. Return TRUE is t1 is less than t2. */
|
|
|
|
static gboolean
|
2019-03-23 16:50:19 +03:00
|
|
|
timeoutcmp (const struct timeval *t1, const struct timeval *t2)
|
|
|
|
{
|
|
|
|
return ((t1->tv_sec < t2->tv_sec)
|
|
|
|
|| ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec <= t2->tv_usec)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2019-01-26 16:26:02 +03:00
|
|
|
static gint
|
|
|
|
vfs_stamp_compare (gconstpointer a, gconstpointer b)
|
|
|
|
{
|
|
|
|
const struct vfs_stamping *vsa = (const struct vfs_stamping *) a;
|
|
|
|
const struct vfs_stamping *vsb = (const struct vfs_stamping *) b;
|
|
|
|
|
|
|
|
return (vsa->v == vsb->v && vsa->id == vsb->id) ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2003-11-08 02:43:55 +03:00
|
|
|
static void
|
2003-11-27 00:10:42 +03:00
|
|
|
vfs_addstamp (struct vfs_class *v, vfsid id)
|
2003-11-08 02:43:55 +03:00
|
|
|
{
|
2019-01-26 16:26:02 +03:00
|
|
|
if ((v->flags & VFSF_LOCAL) == 0 && id != NULL && !vfs_stamp (v, id))
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
|
|
|
struct vfs_stamping *stamp;
|
2019-01-26 16:26:02 +03:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
stamp = g_new (struct vfs_stamping, 1);
|
|
|
|
stamp->v = v;
|
|
|
|
stamp->id = id;
|
2019-03-23 16:50:19 +03:00
|
|
|
gettimeofday (&(stamp->time), NULL);
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2019-01-26 16:26:02 +03:00
|
|
|
stamps = g_slist_append (stamps, stamp);
|
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2019-01-26 16:26:02 +03:00
|
|
|
gboolean
|
2003-11-08 02:43:55 +03:00
|
|
|
vfs_stamp (struct vfs_class *v, vfsid id)
|
|
|
|
{
|
2019-01-26 16:26:02 +03:00
|
|
|
struct vfs_stamping what = {
|
|
|
|
.v = v,
|
|
|
|
.id = id
|
|
|
|
};
|
|
|
|
GSList *stamp;
|
|
|
|
gboolean ret = FALSE;
|
|
|
|
|
|
|
|
stamp = g_slist_find_custom (stamps, &what, vfs_stamp_compare);
|
|
|
|
if (stamp != NULL)
|
|
|
|
{
|
2019-03-23 16:50:19 +03:00
|
|
|
gettimeofday (&(VFS_STAMPING (stamp->data)->time), NULL);
|
2019-01-26 16:26:02 +03:00
|
|
|
ret = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2003-11-08 02:43:55 +03:00
|
|
|
|
|
|
|
void
|
2003-11-27 00:10:42 +03:00
|
|
|
vfs_rmstamp (struct vfs_class *v, vfsid id)
|
2003-11-08 02:43:55 +03:00
|
|
|
{
|
2019-01-26 16:26:02 +03:00
|
|
|
struct vfs_stamping what = {
|
|
|
|
.v = v,
|
|
|
|
.id = id
|
|
|
|
};
|
|
|
|
GSList *stamp;
|
|
|
|
|
|
|
|
stamp = g_slist_find_custom (stamps, &what, vfs_stamp_compare);
|
|
|
|
if (stamp != NULL)
|
|
|
|
{
|
|
|
|
g_free (stamp->data);
|
|
|
|
stamps = g_slist_delete_link (stamps, stamp);
|
|
|
|
}
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2010-07-08 11:32:37 +04:00
|
|
|
void
|
2018-12-16 10:44:17 +03:00
|
|
|
vfs_stamp_path (const vfs_path_t * vpath)
|
2003-11-08 02:43:55 +03:00
|
|
|
{
|
|
|
|
vfsid id;
|
2012-01-03 13:35:57 +04:00
|
|
|
const vfs_path_element_t *path_element;
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1);
|
2011-04-21 14:27:56 +04:00
|
|
|
|
|
|
|
id = vfs_getid (vpath);
|
|
|
|
vfs_addstamp (path_element->class, id);
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
2003-11-27 00:10:42 +03:00
|
|
|
* Create a new timestamp item by VFS class and VFS id.
|
|
|
|
*/
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2003-11-27 00:10:42 +03:00
|
|
|
void
|
2011-02-15 17:41:22 +03:00
|
|
|
vfs_stamp_create (struct vfs_class *vclass, vfsid id)
|
2003-11-08 02:43:55 +03:00
|
|
|
{
|
2011-02-15 17:41:22 +03:00
|
|
|
vfsid nvfsid;
|
|
|
|
|
|
|
|
ev_vfs_stamp_create_t event_data = { vclass, id, FALSE };
|
2012-03-23 15:38:36 +04:00
|
|
|
const vfs_path_t *vpath;
|
2012-01-03 13:35:57 +04:00
|
|
|
const vfs_path_element_t *path_element;
|
2003-11-08 02:43:55 +03:00
|
|
|
|
|
|
|
/* There are three directories we have to take care of: current_dir,
|
|
|
|
current_panel->cwd and other_panel->cwd. Athough most of the time either
|
|
|
|
current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
|
|
|
|
same, it's possible that all three are different -- Norbert */
|
|
|
|
|
2011-02-15 17:41:22 +03:00
|
|
|
if (!mc_event_present (MCEVENT_GROUP_CORE, "vfs_timestamp"))
|
2010-11-08 13:21:45 +03:00
|
|
|
return;
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2011-04-29 15:13:29 +04:00
|
|
|
vpath = vfs_get_raw_current_dir ();
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1);
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2011-04-21 14:27:56 +04:00
|
|
|
nvfsid = vfs_getid (vpath);
|
|
|
|
vfs_rmstamp (path_element->class, nvfsid);
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2011-04-21 14:27:56 +04:00
|
|
|
if (!(id == NULL || (path_element->class == vclass && nvfsid == id)))
|
|
|
|
{
|
|
|
|
mc_event_raise (MCEVENT_GROUP_CORE, "vfs_timestamp", (gpointer) & event_data);
|
2003-11-09 02:53:00 +03:00
|
|
|
|
2011-04-21 14:27:56 +04:00
|
|
|
if (!event_data.ret && vclass != NULL && vclass->nothingisopen != NULL
|
|
|
|
&& vclass->nothingisopen (id) != 0)
|
|
|
|
vfs_addstamp (vclass, id);
|
|
|
|
}
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2019-03-23 16:50:29 +03:00
|
|
|
/** This is called from timeout handler with now == FALSE,
|
|
|
|
or can be called with now == TRUE to force freeing all filesystems */
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2003-11-08 02:43:55 +03:00
|
|
|
void
|
2012-04-07 17:07:58 +04:00
|
|
|
vfs_expire (gboolean now)
|
2003-11-08 02:43:55 +03:00
|
|
|
{
|
2012-04-07 17:07:58 +04:00
|
|
|
static gboolean locked = FALSE;
|
2003-11-08 02:43:55 +03:00
|
|
|
|
|
|
|
/* Avoid recursive invocation, e.g. when one of the free functions
|
|
|
|
calls message */
|
|
|
|
if (locked)
|
2010-11-08 13:21:45 +03:00
|
|
|
return;
|
2019-01-26 16:26:02 +03:00
|
|
|
locked = TRUE;
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2019-01-26 16:26:02 +03:00
|
|
|
if (now)
|
2019-03-23 16:50:29 +03:00
|
|
|
{
|
|
|
|
/* reverse list to free nested VFSes at first */
|
|
|
|
stamps = g_slist_reverse (stamps);
|
|
|
|
|
|
|
|
while (stamps != NULL)
|
|
|
|
{
|
|
|
|
struct vfs_stamping *stamping = VFS_STAMPING (stamps->data);
|
|
|
|
|
|
|
|
if (stamping->v->free != NULL)
|
|
|
|
stamping->v->free (stamping->id);
|
|
|
|
g_free (stamping);
|
|
|
|
stamps = g_slist_delete_link (stamps, stamps);
|
|
|
|
}
|
|
|
|
}
|
2019-01-26 16:26:02 +03:00
|
|
|
else
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2019-03-23 16:50:19 +03:00
|
|
|
struct timeval lc_time;
|
2019-03-23 16:50:29 +03:00
|
|
|
GSList *stamp;
|
2019-03-23 16:50:19 +03:00
|
|
|
|
|
|
|
gettimeofday (&lc_time, NULL);
|
|
|
|
lc_time.tv_sec -= vfs_timeout;
|
2019-01-26 16:26:02 +03:00
|
|
|
|
2019-03-23 16:50:29 +03:00
|
|
|
for (stamp = stamps; stamp != NULL;)
|
|
|
|
{
|
|
|
|
struct vfs_stamping *stamping = VFS_STAMPING (stamp->data);
|
|
|
|
|
|
|
|
if (!timeoutcmp (&stamping->time, &lc_time))
|
|
|
|
stamp = g_slist_next (stamp);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GSList *st;
|
|
|
|
|
|
|
|
st = g_slist_next (stamp);
|
|
|
|
if (stamping->v->free != NULL)
|
|
|
|
stamping->v->free (stamping->id);
|
|
|
|
vfs_rmstamp (stamping->v, stamping->id);
|
|
|
|
stamp = st;
|
|
|
|
}
|
|
|
|
}
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
2012-04-07 17:07:58 +04:00
|
|
|
|
|
|
|
locked = FALSE;
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2003-11-08 02:43:55 +03:00
|
|
|
/*
|
|
|
|
* Return the number of seconds remaining to the vfs timeout.
|
|
|
|
* FIXME: The code should be improved to actually return the number of
|
|
|
|
* seconds until the next item times out.
|
|
|
|
*/
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2003-11-08 02:43:55 +03:00
|
|
|
int
|
2012-04-07 17:07:58 +04:00
|
|
|
vfs_timeouts (void)
|
2003-11-08 02:43:55 +03:00
|
|
|
{
|
2019-01-26 16:26:02 +03:00
|
|
|
return stamps != NULL ? 10 : 0;
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2003-11-08 02:43:55 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
vfs_timeout_handler (void)
|
|
|
|
{
|
2012-04-07 17:07:58 +04:00
|
|
|
vfs_expire (FALSE);
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2003-11-08 02:43:55 +03:00
|
|
|
|
|
|
|
void
|
2013-06-20 15:24:34 +04:00
|
|
|
vfs_release_path (const vfs_path_t * vpath)
|
2003-11-08 02:43:55 +03:00
|
|
|
{
|
2012-01-03 13:35:57 +04:00
|
|
|
const vfs_path_element_t *path_element;
|
2011-04-21 14:27:56 +04:00
|
|
|
|
2011-04-28 15:39:59 +04:00
|
|
|
path_element = vfs_path_get_by_index (vpath, -1);
|
2011-04-21 14:27:56 +04:00
|
|
|
vfs_stamp_create (path_element->class, vfs_getid (vpath));
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2003-11-08 02:43:55 +03:00
|
|
|
/* Free all data */
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2003-11-08 02:43:55 +03:00
|
|
|
void
|
|
|
|
vfs_gc_done (void)
|
|
|
|
{
|
2019-03-23 16:50:29 +03:00
|
|
|
vfs_expire (TRUE);
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|