gc.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /* Virtual File System garbage collection code
  2. Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
  3. Written by: 1995 Miguel de Icaza
  4. 1995 Jakub Jelinek
  5. 1998 Pavel Machek
  6. 2003 Pavel Roskin
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public License
  9. as published by the Free Software Foundation; either version 2 of
  10. the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  18. /**
  19. * \file
  20. * \brief Source: Virtual File System: garbage collection code
  21. * \author Miguel de Icaza
  22. * \author Jakub Jelinek
  23. * \author Pavel Machek
  24. * \author Pavel Roskin
  25. * \date 1995, 1998, 2003
  26. */
  27. #include <config.h>
  28. #include <stdlib.h> /* For atol() */
  29. #include <sys/types.h>
  30. #include <sys/time.h> /* gettimeofday() */
  31. #include "lib/global.h"
  32. #include "lib/event.h"
  33. #include "vfs.h"
  34. #include "utilvfs.h"
  35. #include "gc.h"
  36. /*** global variables ****************************************************************************/
  37. int vfs_timeout = 60; /* VFS timeout in seconds */
  38. /*** file scope macro definitions ****************************************************************/
  39. /*** file scope type declarations ****************************************************************/
  40. /*** file scope variables ************************************************************************/
  41. static struct vfs_stamping *stamps;
  42. /*** file scope functions ************************************************************************/
  43. /* --------------------------------------------------------------------------------------------- */
  44. static void
  45. vfs_addstamp (struct vfs_class *v, vfsid id)
  46. {
  47. if (!(v->flags & VFSF_LOCAL) && id != NULL)
  48. {
  49. struct vfs_stamping *stamp;
  50. struct vfs_stamping *last_stamp = NULL;
  51. for (stamp = stamps; stamp != NULL; stamp = stamp->next)
  52. {
  53. if (stamp->v == v && stamp->id == id)
  54. {
  55. gettimeofday (&(stamp->time), NULL);
  56. return;
  57. }
  58. last_stamp = stamp;
  59. }
  60. stamp = g_new (struct vfs_stamping, 1);
  61. stamp->v = v;
  62. stamp->id = id;
  63. gettimeofday (&(stamp->time), NULL);
  64. stamp->next = 0;
  65. if (stamps)
  66. {
  67. /* Add to the end */
  68. last_stamp->next = stamp;
  69. }
  70. else
  71. {
  72. /* Add first element */
  73. stamps = stamp;
  74. }
  75. }
  76. }
  77. /* --------------------------------------------------------------------------------------------- */
  78. /** Compare two timeval structures. Return 0 is t1 is less than t2. */
  79. static inline int
  80. timeoutcmp (struct timeval *t1, struct timeval *t2)
  81. {
  82. return ((t1->tv_sec < t2->tv_sec)
  83. || ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec <= t2->tv_usec)));
  84. }
  85. /* --------------------------------------------------------------------------------------------- */
  86. /*** public functions ****************************************************************************/
  87. /* --------------------------------------------------------------------------------------------- */
  88. void
  89. vfs_stamp (struct vfs_class *v, vfsid id)
  90. {
  91. struct vfs_stamping *stamp;
  92. for (stamp = stamps; stamp != NULL; stamp = stamp->next)
  93. if (stamp->v == v && stamp->id == id)
  94. {
  95. gettimeofday (&(stamp->time), NULL);
  96. return;
  97. }
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. void
  101. vfs_rmstamp (struct vfs_class *v, vfsid id)
  102. {
  103. struct vfs_stamping *stamp, *st1;
  104. for (stamp = stamps, st1 = NULL; stamp != NULL; st1 = stamp, stamp = stamp->next)
  105. if (stamp->v == v && stamp->id == id)
  106. {
  107. if (st1 == NULL)
  108. {
  109. stamps = stamp->next;
  110. }
  111. else
  112. {
  113. st1->next = stamp->next;
  114. }
  115. g_free (stamp);
  116. return;
  117. }
  118. }
  119. /* --------------------------------------------------------------------------------------------- */
  120. void
  121. vfs_stamp_path (const char *path)
  122. {
  123. vfsid id;
  124. vfs_path_t *vpath;
  125. vfs_path_element_t *path_element;
  126. vpath = vfs_path_from_str (path);
  127. path_element = vfs_path_get_by_index (vpath, -1);
  128. id = vfs_getid (vpath);
  129. vfs_addstamp (path_element->class, id);
  130. vfs_path_free (vpath);
  131. }
  132. /* --------------------------------------------------------------------------------------------- */
  133. /**
  134. * Create a new timestamp item by VFS class and VFS id.
  135. */
  136. void
  137. vfs_stamp_create (struct vfs_class *vclass, vfsid id)
  138. {
  139. vfsid nvfsid;
  140. ev_vfs_stamp_create_t event_data = { vclass, id, FALSE };
  141. vfs_path_t *vpath;
  142. vfs_path_element_t *path_element;
  143. /* There are three directories we have to take care of: current_dir,
  144. current_panel->cwd and other_panel->cwd. Athough most of the time either
  145. current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
  146. same, it's possible that all three are different -- Norbert */
  147. if (!mc_event_present (MCEVENT_GROUP_CORE, "vfs_timestamp"))
  148. return;
  149. vpath = vfs_get_raw_current_dir ();
  150. path_element = vfs_path_get_by_index (vpath, -1);
  151. nvfsid = vfs_getid (vpath);
  152. vfs_rmstamp (path_element->class, nvfsid);
  153. if (!(id == NULL || (path_element->class == vclass && nvfsid == id)))
  154. {
  155. mc_event_raise (MCEVENT_GROUP_CORE, "vfs_timestamp", (gpointer) & event_data);
  156. if (!event_data.ret && vclass != NULL && vclass->nothingisopen != NULL
  157. && vclass->nothingisopen (id) != 0)
  158. vfs_addstamp (vclass, id);
  159. }
  160. }
  161. /* --------------------------------------------------------------------------------------------- */
  162. /** This is called from timeout handler with now = 0, or can be called
  163. with now = 1 to force freeing all filesystems that are not in use */
  164. void
  165. vfs_expire (int now)
  166. {
  167. static int locked = 0;
  168. struct timeval lc_time;
  169. struct vfs_stamping *stamp, *st;
  170. /* Avoid recursive invocation, e.g. when one of the free functions
  171. calls message */
  172. if (locked)
  173. return;
  174. locked = 1;
  175. gettimeofday (&lc_time, NULL);
  176. lc_time.tv_sec -= vfs_timeout;
  177. for (stamp = stamps; stamp != NULL;)
  178. {
  179. if (now || (timeoutcmp (&stamp->time, &lc_time)))
  180. {
  181. st = stamp->next;
  182. if (stamp->v->free)
  183. (*stamp->v->free) (stamp->id);
  184. vfs_rmstamp (stamp->v, stamp->id);
  185. stamp = st;
  186. }
  187. else
  188. stamp = stamp->next;
  189. }
  190. locked = 0;
  191. }
  192. /* --------------------------------------------------------------------------------------------- */
  193. /*
  194. * Return the number of seconds remaining to the vfs timeout.
  195. * FIXME: The code should be improved to actually return the number of
  196. * seconds until the next item times out.
  197. */
  198. int
  199. vfs_timeouts ()
  200. {
  201. return stamps ? 10 : 0;
  202. }
  203. /* --------------------------------------------------------------------------------------------- */
  204. void
  205. vfs_timeout_handler (void)
  206. {
  207. vfs_expire (0);
  208. }
  209. /* --------------------------------------------------------------------------------------------- */
  210. void
  211. vfs_release_path (const char *dir)
  212. {
  213. vfs_path_t *vpath;
  214. vfs_path_element_t *path_element;
  215. vpath = vfs_path_from_str (dir);
  216. path_element = vfs_path_get_by_index (vpath, -1);
  217. vfs_stamp_create (path_element->class, vfs_getid (vpath));
  218. vfs_path_free (vpath);
  219. }
  220. /* --------------------------------------------------------------------------------------------- */
  221. /* Free all data */
  222. void
  223. vfs_gc_done (void)
  224. {
  225. struct vfs_stamping *stamp, *st;
  226. for (stamp = stamps, stamps = 0; stamp != NULL;)
  227. {
  228. if (stamp->v->free)
  229. (*stamp->v->free) (stamp->id);
  230. st = stamp->next;
  231. g_free (stamp);
  232. stamp = st;
  233. }
  234. if (stamps)
  235. vfs_rmstamp (stamps->v, stamps->id);
  236. }
  237. /* --------------------------------------------------------------------------------------------- */