2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Virtual File System garbage collection code
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2015-01-07 10:06:55 +03:00
|
|
|
Copyright (C) 2003-2015
|
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>
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
#include <stdlib.h> /* For atol() */
|
2003-11-08 02:43:55 +03:00
|
|
|
#include <sys/types.h>
|
2010-11-08 13:21:45 +03:00
|
|
|
#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"
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
int vfs_timeout = 60; /* VFS timeout in seconds */
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
2003-11-08 02:43:55 +03:00
|
|
|
|
|
|
|
static struct vfs_stamping *stamps;
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
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
|
|
|
{
|
2010-11-08 13:21:45 +03:00
|
|
|
if (!(v->flags & VFSF_LOCAL) && id != NULL)
|
|
|
|
{
|
|
|
|
struct vfs_stamping *stamp;
|
|
|
|
struct vfs_stamping *last_stamp = NULL;
|
|
|
|
|
|
|
|
for (stamp = stamps; stamp != NULL; stamp = stamp->next)
|
|
|
|
{
|
|
|
|
if (stamp->v == v && stamp->id == id)
|
|
|
|
{
|
|
|
|
gettimeofday (&(stamp->time), NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
last_stamp = stamp;
|
|
|
|
}
|
|
|
|
stamp = g_new (struct vfs_stamping, 1);
|
|
|
|
stamp->v = v;
|
|
|
|
stamp->id = id;
|
|
|
|
|
|
|
|
gettimeofday (&(stamp->time), NULL);
|
|
|
|
stamp->next = 0;
|
|
|
|
|
|
|
|
if (stamps)
|
|
|
|
{
|
|
|
|
/* Add to the end */
|
|
|
|
last_stamp->next = stamp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Add first element */
|
|
|
|
stamps = stamp;
|
|
|
|
}
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/** Compare two timeval structures. Return 0 is t1 is less than t2. */
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
timeoutcmp (struct timeval *t1, struct timeval *t2)
|
|
|
|
{
|
|
|
|
return ((t1->tv_sec < t2->tv_sec)
|
|
|
|
|| ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec <= t2->tv_usec)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2003-11-08 02:43:55 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
vfs_stamp (struct vfs_class *v, vfsid id)
|
|
|
|
{
|
|
|
|
struct vfs_stamping *stamp;
|
|
|
|
|
|
|
|
for (stamp = stamps; stamp != NULL; stamp = stamp->next)
|
2010-11-08 13:21:45 +03:00
|
|
|
if (stamp->v == v && stamp->id == id)
|
|
|
|
{
|
|
|
|
gettimeofday (&(stamp->time), NULL);
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
struct vfs_stamping *stamp, *st1;
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
for (stamp = stamps, st1 = NULL; stamp != NULL; st1 = stamp, stamp = stamp->next)
|
|
|
|
if (stamp->v == v && stamp->id == id)
|
|
|
|
{
|
|
|
|
if (st1 == NULL)
|
|
|
|
{
|
|
|
|
stamps = stamp->next;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
st1->next = stamp->next;
|
|
|
|
}
|
|
|
|
g_free (stamp);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
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
|
|
|
|
vfs_stamp_path (const char *path)
|
2003-11-08 02:43:55 +03:00
|
|
|
{
|
|
|
|
vfsid id;
|
2011-04-21 14:27:56 +04:00
|
|
|
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
|
|
|
|
2011-04-21 14:27:56 +04:00
|
|
|
vpath = vfs_path_from_str (path);
|
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);
|
|
|
|
vfs_path_free (vpath);
|
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;
|
|
|
|
|
2014-04-22 14:04:33 +04:00
|
|
|
ev_vfs_stamp_create_t event_data = { vclass, id };
|
|
|
|
event_return_t ret;
|
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)))
|
|
|
|
{
|
2015-04-06 17:08:37 +03:00
|
|
|
mc_event_dispatch (MCEVENT_GROUP_CORE, "vfs_timestamp", (gpointer) & event_data, &ret,
|
|
|
|
NULL);
|
2003-11-09 02:53:00 +03:00
|
|
|
|
2014-04-22 14:04:33 +04:00
|
|
|
if (!ret.b && vclass != NULL && vclass->nothingisopen != NULL
|
2011-04-21 14:27:56 +04:00
|
|
|
&& vclass->nothingisopen (id) != 0)
|
|
|
|
vfs_addstamp (vclass, id);
|
|
|
|
}
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/** This is called from timeout handler with now = 0, or can be called
|
2003-11-08 02:43:55 +03:00
|
|
|
with now = 1 to force freeing all filesystems that are not in use */
|
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;
|
2009-10-30 04:12:04 +03:00
|
|
|
struct timeval lc_time;
|
2003-11-08 02:43:55 +03:00
|
|
|
struct vfs_stamping *stamp, *st;
|
|
|
|
|
|
|
|
/* Avoid recursive invocation, e.g. when one of the free functions
|
|
|
|
calls message */
|
|
|
|
if (locked)
|
2010-11-08 13:21:45 +03:00
|
|
|
return;
|
2012-04-07 17:07:58 +04:00
|
|
|
locked = TRUE;
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2009-10-30 04:12:04 +03:00
|
|
|
gettimeofday (&lc_time, NULL);
|
|
|
|
lc_time.tv_sec -= vfs_timeout;
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
for (stamp = stamps; stamp != NULL;)
|
|
|
|
{
|
|
|
|
if (now || (timeoutcmp (&stamp->time, &lc_time)))
|
|
|
|
{
|
|
|
|
st = stamp->next;
|
|
|
|
if (stamp->v->free)
|
|
|
|
(*stamp->v->free) (stamp->id);
|
|
|
|
vfs_rmstamp (stamp->v, stamp->id);
|
|
|
|
stamp = st;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
stamp = stamp->next;
|
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
|
|
|
{
|
|
|
|
return stamps ? 10 : 0;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct vfs_stamping *stamp, *st;
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
for (stamp = stamps, stamps = 0; stamp != NULL;)
|
|
|
|
{
|
|
|
|
if (stamp->v->free)
|
|
|
|
(*stamp->v->free) (stamp->id);
|
|
|
|
st = stamp->next;
|
|
|
|
g_free (stamp);
|
|
|
|
stamp = st;
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stamps)
|
2010-11-08 13:21:45 +03:00
|
|
|
vfs_rmstamp (stamps->v, stamps->id);
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|