gc.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 = {
  116. .v = v,
  117. .id = id
  118. };
  119. GSList *stamp;
  120. gboolean ret = FALSE;
  121. stamp = g_slist_find_custom (stamps, &what, vfs_stamp_compare);
  122. if (stamp != NULL && stamp->data != NULL)
  123. {
  124. VFS_STAMPING (stamp->data)->time = g_get_monotonic_time ();
  125. ret = TRUE;
  126. }
  127. return ret;
  128. }
  129. /* --------------------------------------------------------------------------------------------- */
  130. void
  131. vfs_rmstamp (struct vfs_class *v, vfsid id)
  132. {
  133. struct vfs_stamping what = {
  134. .v = v,
  135. .id = id
  136. };
  137. GSList *stamp;
  138. stamp = g_slist_find_custom (stamps, &what, vfs_stamp_compare);
  139. if (stamp != NULL)
  140. {
  141. g_free (stamp->data);
  142. stamps = g_slist_delete_link (stamps, stamp);
  143. }
  144. }
  145. /* --------------------------------------------------------------------------------------------- */
  146. void
  147. vfs_stamp_path (const vfs_path_t *vpath)
  148. {
  149. vfsid id;
  150. struct vfs_class *me;
  151. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
  152. id = vfs_getid (vpath);
  153. vfs_addstamp (me, id);
  154. }
  155. /* --------------------------------------------------------------------------------------------- */
  156. /**
  157. * Create a new timestamp item by VFS class and VFS id.
  158. */
  159. void
  160. vfs_stamp_create (struct vfs_class *vclass, vfsid id)
  161. {
  162. vfsid nvfsid;
  163. ev_vfs_stamp_create_t event_data = { vclass, id, FALSE };
  164. const vfs_path_t *vpath;
  165. struct vfs_class *me;
  166. /* There are three directories we have to take care of: current_dir,
  167. current_panel->cwd and other_panel->cwd. Although most of the time either
  168. current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
  169. same, it's possible that all three are different -- Norbert */
  170. if (!mc_event_present (MCEVENT_GROUP_CORE, "vfs_timestamp"))
  171. return;
  172. vpath = vfs_get_raw_current_dir ();
  173. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
  174. nvfsid = vfs_getid (vpath);
  175. vfs_rmstamp (me, nvfsid);
  176. if (!(id == NULL || (me == vclass && nvfsid == id)))
  177. {
  178. mc_event_raise (MCEVENT_GROUP_CORE, "vfs_timestamp", (gpointer) & event_data);
  179. if (!event_data.ret && vclass != NULL && vclass->nothingisopen != NULL
  180. && vclass->nothingisopen (id))
  181. vfs_addstamp (vclass, id);
  182. }
  183. }
  184. /* --------------------------------------------------------------------------------------------- */
  185. /** This is called from timeout handler with now = FALSE,
  186. or can be called with now = TRUE to force freeing all filesystems */
  187. void
  188. vfs_expire (gboolean now)
  189. {
  190. static gboolean locked = FALSE;
  191. gint64 curr_time, exp_time;
  192. GSList *stamp;
  193. /* Avoid recursive invocation, e.g. when one of the free functions
  194. calls message */
  195. if (locked)
  196. return;
  197. locked = TRUE;
  198. curr_time = g_get_monotonic_time ();
  199. exp_time = curr_time - vfs_timeout * G_USEC_PER_SEC;
  200. if (now)
  201. {
  202. /* reverse list to free nested VFSes at first */
  203. stamps = g_slist_reverse (stamps);
  204. }
  205. /* NULLize stamps that point to expired VFS */
  206. for (stamp = stamps; stamp != NULL; stamp = g_slist_next (stamp))
  207. {
  208. struct vfs_stamping *stamping = VFS_STAMPING (stamp->data);
  209. if (now)
  210. {
  211. /* free VFS forced */
  212. if (stamping->v->free != NULL)
  213. stamping->v->free (stamping->id);
  214. MC_PTR_FREE (stamp->data);
  215. }
  216. else if (stamping->time <= exp_time)
  217. {
  218. /* update timestamp of VFS that is in use, or free unused VFS */
  219. if (stamping->v->nothingisopen != NULL && !stamping->v->nothingisopen (stamping->id))
  220. stamping->time = curr_time;
  221. else
  222. {
  223. if (stamping->v->free != NULL)
  224. stamping->v->free (stamping->id);
  225. MC_PTR_FREE (stamp->data);
  226. }
  227. }
  228. }
  229. /* then remove NULLized stamps */
  230. stamps = g_slist_remove_all (stamps, NULL);
  231. locked = FALSE;
  232. }
  233. /* --------------------------------------------------------------------------------------------- */
  234. /*
  235. * Return the number of seconds remaining to the vfs timeout.
  236. * FIXME: The code should be improved to actually return the number of
  237. * seconds until the next item times out.
  238. */
  239. int
  240. vfs_timeouts (void)
  241. {
  242. return stamps != NULL ? 10 : 0;
  243. }
  244. /* --------------------------------------------------------------------------------------------- */
  245. void
  246. vfs_timeout_handler (void)
  247. {
  248. vfs_expire (FALSE);
  249. }
  250. /* --------------------------------------------------------------------------------------------- */
  251. void
  252. vfs_release_path (const vfs_path_t *vpath)
  253. {
  254. vfsid id;
  255. struct vfs_class *me;
  256. me = VFS_CLASS (vfs_path_get_last_path_vfs (vpath));
  257. id = vfs_getid (vpath);
  258. vfs_stamp_create (me, id);
  259. }
  260. /* --------------------------------------------------------------------------------------------- */
  261. /* Free all data */
  262. void
  263. vfs_gc_done (void)
  264. {
  265. vfs_expire (TRUE);
  266. }
  267. /* --------------------------------------------------------------------------------------------- */