2003-11-13 02:00:16 +03:00
|
|
|
/* Virtual File System garbage collection code
|
2007-09-25 19:33:35 +04:00
|
|
|
Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
|
2003-11-08 02:43:55 +03:00
|
|
|
|
|
|
|
Written by: 1995 Miguel de Icaza
|
|
|
|
1995 Jakub Jelinek
|
|
|
|
1998 Pavel Machek
|
|
|
|
2003 Pavel Roskin
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Library General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2 of
|
|
|
|
the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
License along with this program; if not, write to the Free Software
|
2005-05-27 07:35:10 +04:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
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>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h> /* For atol() */
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ctype.h> /* is_digit() */
|
2009-02-06 02:30:45 +03:00
|
|
|
#include <sys/time.h> /* gettimeofday() */
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2005-02-19 19:36:38 +03:00
|
|
|
#include "../src/global.h"
|
2009-05-08 14:01:05 +04:00
|
|
|
|
|
|
|
#include "../src/wtools.h" /* message() */
|
2005-02-19 19:36:38 +03:00
|
|
|
#include "../src/main.h" /* print_vfs_message */
|
2009-05-08 14:01:05 +04:00
|
|
|
#include "../src/panel.h" /* get_current_panel() */
|
2009-03-07 15:23:15 +03:00
|
|
|
#include "../src/layout.h" /* get_current_type() */
|
2009-05-08 14:01:05 +04:00
|
|
|
|
2003-11-08 02:43:55 +03:00
|
|
|
#include "utilvfs.h"
|
2004-08-17 03:18:42 +04:00
|
|
|
#include "vfs-impl.h"
|
2003-11-08 02:43:55 +03:00
|
|
|
#include "vfs.h"
|
2009-03-07 15:23:15 +03:00
|
|
|
#include "gc.h"
|
2003-11-08 02:43:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
int vfs_timeout = 60; /* VFS timeout in seconds */
|
|
|
|
|
|
|
|
static struct vfs_stamping *stamps;
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2003-11-13 02:00:16 +03:00
|
|
|
if (!(v->flags & VFSF_LOCAL) && id != NULL) {
|
2003-11-08 02:43:55 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
vfs_stamp (struct vfs_class *v, vfsid id)
|
|
|
|
{
|
|
|
|
struct vfs_stamping *stamp;
|
|
|
|
|
|
|
|
for (stamp = stamps; stamp != NULL; stamp = stamp->next)
|
|
|
|
if (stamp->v == v && stamp->id == id) {
|
|
|
|
gettimeofday (&(stamp->time), NULL);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (stamp);
|
2003-11-08 02:43:55 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-27 00:25:30 +03:00
|
|
|
/* Find VFS id for given directory name */
|
2003-11-09 02:53:00 +03:00
|
|
|
vfsid
|
2003-11-27 00:25:30 +03:00
|
|
|
vfs_getid (struct vfs_class *vclass, const char *dir)
|
2003-11-09 02:53:00 +03:00
|
|
|
{
|
2003-11-27 00:25:30 +03:00
|
|
|
char *dir1;
|
2003-11-13 02:00:16 +03:00
|
|
|
vfsid id = NULL;
|
2003-11-09 02:53:00 +03:00
|
|
|
|
2003-11-27 00:25:30 +03:00
|
|
|
/* append slash if needed */
|
2009-02-06 13:17:03 +03:00
|
|
|
dir1 = concat_dir_and_file (dir, "");
|
2003-11-09 02:53:00 +03:00
|
|
|
if (vclass->getid)
|
2003-11-27 00:25:30 +03:00
|
|
|
id = (*vclass->getid) (vclass, dir1);
|
2003-11-09 02:53:00 +03:00
|
|
|
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (dir1);
|
2003-11-27 00:25:30 +03:00
|
|
|
return id;
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
vfs_stamp_path (char *path)
|
|
|
|
{
|
|
|
|
struct vfs_class *vfs;
|
|
|
|
vfsid id;
|
|
|
|
|
|
|
|
vfs = vfs_get_class (path);
|
2003-11-27 00:25:30 +03:00
|
|
|
id = vfs_getid (vfs, path);
|
2003-11-27 00:10:42 +03:00
|
|
|
vfs_addstamp (vfs, id);
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-27 00:10:42 +03:00
|
|
|
/*
|
|
|
|
* Create a new timestamp item by VFS class and VFS id.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
vfs_stamp_create (struct vfs_class *oldvfs, vfsid oldvfsid)
|
2003-11-08 02:43:55 +03:00
|
|
|
{
|
|
|
|
struct vfs_class *nvfs, *n2vfs, *n3vfs;
|
|
|
|
vfsid nvfsid, n2vfsid, n3vfsid;
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
|
|
|
|
if (!current_panel)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nvfs = vfs_get_class (vfs_get_current_dir ());
|
2003-11-27 00:25:30 +03:00
|
|
|
nvfsid = vfs_getid (nvfs, vfs_get_current_dir ());
|
2003-11-27 00:10:42 +03:00
|
|
|
vfs_rmstamp (nvfs, nvfsid);
|
2003-11-08 02:43:55 +03:00
|
|
|
|
2003-11-27 00:10:42 +03:00
|
|
|
if ((nvfs == oldvfs && nvfsid == oldvfsid) || oldvfsid == NULL) {
|
2003-11-08 02:43:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_current_type () == view_listing) {
|
|
|
|
n2vfs = vfs_get_class (current_panel->cwd);
|
2003-11-27 00:25:30 +03:00
|
|
|
n2vfsid = vfs_getid (n2vfs, current_panel->cwd);
|
2003-11-27 00:10:42 +03:00
|
|
|
if (n2vfs == oldvfs && n2vfsid == oldvfsid)
|
2003-11-08 02:43:55 +03:00
|
|
|
return;
|
|
|
|
} else {
|
2003-11-09 02:53:00 +03:00
|
|
|
n2vfs = NULL;
|
2003-11-13 02:00:16 +03:00
|
|
|
n2vfsid = NULL;
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (get_other_type () == view_listing) {
|
|
|
|
n3vfs = vfs_get_class (other_panel->cwd);
|
2003-11-27 00:25:30 +03:00
|
|
|
n3vfsid = vfs_getid (n3vfs, other_panel->cwd);
|
2003-11-27 00:10:42 +03:00
|
|
|
if (n3vfs == oldvfs && n3vfsid == oldvfsid)
|
2003-11-08 02:43:55 +03:00
|
|
|
return;
|
|
|
|
} else {
|
2003-11-09 02:53:00 +03:00
|
|
|
n3vfs = NULL;
|
2003-11-13 02:00:16 +03:00
|
|
|
n3vfsid = NULL;
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
2003-11-09 02:53:00 +03:00
|
|
|
if (!oldvfs->nothingisopen || !(*oldvfs->nothingisopen) (oldvfsid))
|
|
|
|
return;
|
|
|
|
|
2003-11-27 00:10:42 +03:00
|
|
|
vfs_addstamp (oldvfs, oldvfsid);
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
vfs_add_current_stamps (void)
|
|
|
|
{
|
|
|
|
vfs_stamp_path (vfs_get_current_dir ());
|
|
|
|
|
|
|
|
if (current_panel) {
|
|
|
|
if (get_current_type () == view_listing)
|
|
|
|
vfs_stamp_path (current_panel->cwd);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (other_panel) {
|
|
|
|
if (get_other_type () == view_listing)
|
|
|
|
vfs_stamp_path (other_panel->cwd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* This is called from timeout handler with now = 0, or can be called
|
|
|
|
with now = 1 to force freeing all filesystems that are not in use */
|
|
|
|
void
|
|
|
|
vfs_expire (int now)
|
|
|
|
{
|
|
|
|
static int locked = 0;
|
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)
|
|
|
|
return;
|
|
|
|
locked = 1;
|
|
|
|
|
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
|
|
|
|
|
|
|
for (stamp = stamps; stamp != NULL;) {
|
2009-10-30 04:12:04 +03:00
|
|
|
if (now || (timeoutcmp (&stamp->time, &lc_time))) {
|
2003-11-08 02:43:55 +03:00
|
|
|
st = stamp->next;
|
2003-11-09 02:53:00 +03:00
|
|
|
if (stamp->v->free)
|
|
|
|
(*stamp->v->free) (stamp->id);
|
2003-11-27 00:10:42 +03:00
|
|
|
vfs_rmstamp (stamp->v, stamp->id);
|
2003-11-08 02:43:55 +03:00
|
|
|
stamp = st;
|
|
|
|
} else
|
|
|
|
stamp = stamp->next;
|
|
|
|
}
|
|
|
|
locked = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
vfs_timeouts ()
|
|
|
|
{
|
|
|
|
return stamps ? 10 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
vfs_timeout_handler (void)
|
|
|
|
{
|
|
|
|
vfs_expire (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
vfs_release_path (const char *dir)
|
|
|
|
{
|
|
|
|
struct vfs_class *oldvfs;
|
|
|
|
vfsid oldvfsid;
|
|
|
|
|
|
|
|
oldvfs = vfs_get_class (dir);
|
2003-11-27 00:25:30 +03:00
|
|
|
oldvfsid = vfs_getid (oldvfs, dir);
|
2003-11-27 00:10:42 +03:00
|
|
|
vfs_stamp_create (oldvfs, oldvfsid);
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Free all data */
|
|
|
|
void
|
|
|
|
vfs_gc_done (void)
|
|
|
|
{
|
|
|
|
struct vfs_stamping *stamp, *st;
|
|
|
|
|
|
|
|
for (stamp = stamps, stamps = 0; stamp != NULL;) {
|
2003-11-09 02:53:00 +03:00
|
|
|
if (stamp->v->free)
|
|
|
|
(*stamp->v->free) (stamp->id);
|
2003-11-08 02:43:55 +03:00
|
|
|
st = stamp->next;
|
2009-02-06 01:27:37 +03:00
|
|
|
g_free (stamp);
|
2003-11-08 02:43:55 +03:00
|
|
|
stamp = st;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stamps)
|
2003-11-27 00:10:42 +03:00
|
|
|
vfs_rmstamp (stamps->v, stamps->id);
|
2003-11-08 02:43:55 +03:00
|
|
|
}
|