gc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. Virtual File System garbage collection code
  3. Copyright (C) 2003-2024
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Miguel de Icaza, 1995
  7. Jakub Jelinek, 1995
  8. Pavel Machek, 1998
  9. Pavel Roskin, 2003
  10. This file is part of the Midnight Commander.
  11. The Midnight Commander is free software: you can redistribute it
  12. and/or modify it under the terms of the GNU General Public License as
  13. published by the Free Software Foundation, either version 3 of the License,
  14. or (at your option) any later version.
  15. The Midnight Commander is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file
  24. * \brief Source: Virtual File System: garbage collection code
  25. * \author Miguel de Icaza
  26. * \author Jakub Jelinek
  27. * \author Pavel Machek
  28. * \author Pavel Roskin
  29. * \date 1995, 1998, 2003
  30. */
  31. #include <config.h>
  32. #include <stdlib.h>
  33. #include "lib/global.h"
  34. #include "lib/event.h"
  35. #include "lib/util.h" /* MC_PTR_FREE */
  36. #include "vfs.h"
  37. #include "utilvfs.h"
  38. #include "gc.h"
  39. /*
  40. * The garbage collection mechanism is based on "stamps".
  41. *
  42. * A stamp is a record that says "I'm a filesystem which is no longer in
  43. * use. Free me when you get a chance."
  44. *
  45. * This file contains a set of functions used for managing this stamp. You
  46. * should use them when you write your own filesystem. Here are some rules
  47. * of thumb:
  48. *
  49. * (1) When the last open file in your filesystem gets closed, conditionally
  50. * create a stamp. You do this with vfs_stamp_create(). (The meaning
  51. * of "conditionally" is explained below.)
  52. *
  53. * (2) When a file in your filesystem is opened, delete the stamp. You do
  54. * this with vfs_rmstamp().
  55. *
  56. * (3) When a path inside your filesystem is invoked, call vfs_stamp() to
  57. * postpone the free'ing of your filesystem a bit. (This simply updates
  58. * a timestamp variable inside the stamp.)
  59. *
  60. * Additionally, when a user navigates to a new directory in a panel (or a
  61. * programmer uses mc_chdir()), a stamp is conditionally created for the
  62. * previous directory's filesystem. This ensures that that filesystem is
  63. * free'ed. (see: _do_panel_cd() -> vfs_release_path(); mc_chdir()).
  64. *
  65. * We've spoken here of "conditionally creating" a stamp. What we mean is
  66. * that vfs_stamp_create() is to be used: this function creates a stamp
  67. * only if no directories are open (aka "active") in your filesystem. (If
  68. * there _are_ directories open, it means that the filesystem is in use, in
  69. * which case we don't want to free it.)
  70. */
  71. /*** global variables ****************************************************************************/
  72. int vfs_timeout = 60; /* VFS timeout in seconds */
  73. /*** file scope macro definitions ****************************************************************/
  74. #define VFS_STAMPING(a) ((struct vfs_stamping *) (a))
  75. /*** file scope type declarations ****************************************************************/
  76. struct vfs_stamping
  77. {
  78. struct vfs_class *v;
  79. vfsid id;
  80. gint64 time;
  81. };
  82. /*** forward declarations (file scope functions) *************************************************/
  83. /*** file scope variables ************************************************************************/
  84. static GSList *stamps = NULL;
  85. /* --------------------------------------------------------------------------------------------- */
  86. /*** file scope functions ************************************************************************/
  87. /* --------------------------------------------------------------------------------------------- */
  88. static gint
  89. vfs_stamp_compare (gconstpointer a, gconstpointer b)
  90. {
  91. const struct vfs_stamping *vsa = (const struct vfs_stamping *) a;
  92. const struct vfs_stamping *vsb = (const struct vfs_stamping *) b;
  93. return (vsa == NULL || vsb == NULL || (vsa->v == vsb->v && vsa->id == vsb->id)) ? 0 : 1;
  94. }
  95. /* --------------------------------------------------------------------------------------------- */
  96. static void
  97. vfs_addstamp (struct vfs_class *v, vfsid id)
  98. {
  99. if ((v->flags & VFSF_LOCAL) == 0 && id != NULL && !vfs_stamp (v, id))
  100. {
  101. struct vfs_stamping *stamp;
  102. stamp = g_new (struct vfs_stamping, 1);
  103. stamp->v = v;
  104. stamp->id = id;
  105. stamp->time = g_get_monotonic_time ();
  106. stamps = g_slist_append (stamps, stamp);
  107. }
  108. }
  109. /* --------------------------------------------------------------------------------------------- */
  110. /*** public functions ****************************************************************************/
  111. /* --------------------------------------------------------------------------------------------- */
  112. gboolean
  113. vfs_stamp (struct vfs_class *v, vfsid id)
  114. {
  115. struct vfs_stamping what = { .v = v, .id = id };
  116. GSList *stamp;
  117. gboolean ret = FALSE;
  118. stamp = g_slist_find_custom (stamps, &what, vfs_stamp_compare);
  119. if (stamp != NULL && stamp->data != NULL)
  120. {
  121. VFS_STAMPING (stamp->data)->time = g_get_monotonic_time ();
  122. ret = TRUE;
  123. }
  124. return ret;
  125. }
  126. /* --------------------------------------------------------------------------------------------- */
  127. void
  128. vfs_rmstamp (struct vfs_class *v, vfsid id)
  129. {
  130. struct vfs_stamping what = { .v = v, .id = id };
  131. GSList *stamp;
  132. stamp = g_slist_find_custom (stamps, &what, vfs_stamp_compare);
  133. if (stamp != NULL)
  134. {
  135. g_free (stamp->data);
  136. stamps = g_slist_delete_link (stamps, stamp);
  137. }
  138. }
  139. /* --------------------------------------------------------------------------------------------- */
  140. void
  141. vfs_stamp_path (const vfs_path_t *vpath)
  142. {
  143. vfsid id;
  144. struct vfs_class *me;
  145. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
  146. id = vfs_getid (vpath);
  147. vfs_addstamp (me, id);
  148. }
  149. /* --------------------------------------------------------------------------------------------- */
  150. /**
  151. * Create a new timestamp item by VFS class and VFS id.
  152. */
  153. void
  154. vfs_stamp_create (struct vfs_class *vclass, vfsid id)
  155. {
  156. vfsid nvfsid;
  157. ev_vfs_stamp_create_t event_data = { vclass, id, FALSE };
  158. const vfs_path_t *vpath;
  159. struct vfs_class *me;
  160. /* There are three directories we have to take care of: current_dir,
  161. current_panel->cwd and other_panel->cwd. Although most of the time either
  162. current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
  163. same, it's possible that all three are different -- Norbert */
  164. if (!mc_event_present (MCEVENT_GROUP_CORE, "vfs_timestamp"))
  165. return;
  166. vpath = vfs_get_raw_current_dir ();
  167. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
  168. nvfsid = vfs_getid (vpath);
  169. vfs_rmstamp (me, nvfsid);
  170. if (!(id == NULL || (me == vclass && nvfsid == id)))
  171. {
  172. mc_event_raise (MCEVENT_GROUP_CORE, "vfs_timestamp", (gpointer) &event_data);
  173. if (!event_data.ret && vclass != NULL && vclass->nothingisopen != NULL
  174. && vclass->nothingisopen (id))
  175. vfs_addstamp (vclass, id);
  176. }
  177. }
  178. /* --------------------------------------------------------------------------------------------- */
  179. /** This is called from timeout handler with now = FALSE,
  180. or can be called with now = TRUE to force freeing all filesystems */
  181. void
  182. vfs_expire (gboolean now)
  183. {
  184. static gboolean locked = FALSE;
  185. gint64 curr_time, exp_time;
  186. GSList *stamp;
  187. /* Avoid recursive invocation, e.g. when one of the free functions
  188. calls message */
  189. if (locked)
  190. return;
  191. locked = TRUE;
  192. curr_time = g_get_monotonic_time ();
  193. exp_time = curr_time - vfs_timeout * G_USEC_PER_SEC;
  194. if (now)
  195. {
  196. /* reverse list to free nested VFSes at first */
  197. stamps = g_slist_reverse (stamps);
  198. }
  199. /* NULLize stamps that point to expired VFS */
  200. for (stamp = stamps; stamp != NULL; stamp = g_slist_next (stamp))
  201. {
  202. struct vfs_stamping *stamping = VFS_STAMPING (stamp->data);
  203. if (now)
  204. {
  205. /* free VFS forced */
  206. if (stamping->v->free != NULL)
  207. stamping->v->free (stamping->id);
  208. MC_PTR_FREE (stamp->data);
  209. }
  210. else if (stamping->time <= exp_time)
  211. {
  212. /* update timestamp of VFS that is in use, or free unused VFS */
  213. if (stamping->v->nothingisopen != NULL && !stamping->v->nothingisopen (stamping->id))
  214. stamping->time = curr_time;
  215. else
  216. {
  217. if (stamping->v->free != NULL)
  218. stamping->v->free (stamping->id);
  219. MC_PTR_FREE (stamp->data);
  220. }
  221. }
  222. }
  223. /* then remove NULLized stamps */
  224. stamps = g_slist_remove_all (stamps, NULL);
  225. locked = FALSE;
  226. }
  227. /* --------------------------------------------------------------------------------------------- */
  228. /*
  229. * Return the number of seconds remaining to the vfs timeout.
  230. * FIXME: The code should be improved to actually return the number of
  231. * seconds until the next item times out.
  232. */
  233. int
  234. vfs_timeouts (void)
  235. {
  236. return stamps != NULL ? 10 : 0;
  237. }
  238. /* --------------------------------------------------------------------------------------------- */
  239. void
  240. vfs_timeout_handler (void)
  241. {
  242. vfs_expire (FALSE);
  243. }
  244. /* --------------------------------------------------------------------------------------------- */
  245. void
  246. vfs_release_path (const vfs_path_t *vpath)
  247. {
  248. vfsid id;
  249. struct vfs_class *me;
  250. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
  251. id = vfs_getid (vpath);
  252. vfs_stamp_create (me, id);
  253. }
  254. /* --------------------------------------------------------------------------------------------- */
  255. /* Free all data */
  256. void
  257. vfs_gc_done (void)
  258. {
  259. vfs_expire (TRUE);
  260. }
  261. /* --------------------------------------------------------------------------------------------- */