gc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. Virtual File System garbage collection code
  3. Copyright (C) 2003-2019
  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> /* For atol() */
  33. #include <sys/types.h>
  34. #include <sys/time.h> /* gettimeofday() */
  35. #include "lib/global.h"
  36. #include "lib/event.h"
  37. #include "lib/util.h" /* MC_PTR_FREE */
  38. #include "vfs.h"
  39. #include "utilvfs.h"
  40. #include "gc.h"
  41. /*
  42. * The garbage collection mechanism is based on "stamps".
  43. *
  44. * A stamp is a record that says "I'm a filesystem which is no longer in
  45. * use. Free me when you get a chance."
  46. *
  47. * This file contains a set of functions used for managing this stamp. You
  48. * should use them when you write your own filesystem. Here are some rules
  49. * of thumb:
  50. *
  51. * (1) When the last open file in your filesystem gets closed, conditionally
  52. * create a stamp. You do this with vfs_stamp_create(). (The meaning
  53. * of "conditionaly" is explained below.)
  54. *
  55. * (2) When a file in your filesystem is opened, delete the stamp. You do
  56. * this with vfs_rmstamp().
  57. *
  58. * (3) When a path inside your filesystem is invoked, call vfs_stamp() to
  59. * postpone the free'ing of your filesystem a bit. (This simply updates
  60. * a timestamp variable inside the stamp.)
  61. *
  62. * Additionally, when a user navigates to a new directory in a panel (or a
  63. * programmer uses mc_chdir()), a stamp is conditionally created for the
  64. * previous directory's filesystem. This ensures that that filesystem is
  65. * free'ed. (see: _do_panel_cd() -> vfs_release_path(); mc_chdir()).
  66. *
  67. * We've spoken here of "conditionally creating" a stamp. What we mean is
  68. * that vfs_stamp_create() is to be used: this function creates a stamp
  69. * only if no directories are open (aka "active") in your filesystem. (If
  70. * there _are_ directories open, it means that the filesystem is in use, in
  71. * which case we don't want to free it.)
  72. */
  73. /*** global variables ****************************************************************************/
  74. int vfs_timeout = 60; /* VFS timeout in seconds */
  75. /*** file scope macro definitions ****************************************************************/
  76. #define VFS_STAMPING(a) ((struct vfs_stamping *)(a))
  77. /*** file scope type declarations ****************************************************************/
  78. struct vfs_stamping
  79. {
  80. struct vfs_class *v;
  81. vfsid id;
  82. struct timeval time;
  83. };
  84. /*** file scope variables ************************************************************************/
  85. static GSList *stamps = NULL;
  86. /* --------------------------------------------------------------------------------------------- */
  87. /*** file scope functions ************************************************************************/
  88. /* --------------------------------------------------------------------------------------------- */
  89. /** Compare two timeval structures. Return TRUE if t1 is less than t2. */
  90. static gboolean
  91. timeoutcmp (const struct timeval *t1, const struct timeval *t2)
  92. {
  93. return ((t1->tv_sec < t2->tv_sec)
  94. || ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec <= t2->tv_usec)));
  95. }
  96. /* --------------------------------------------------------------------------------------------- */
  97. static gint
  98. vfs_stamp_compare (gconstpointer a, gconstpointer b)
  99. {
  100. const struct vfs_stamping *vsa = (const struct vfs_stamping *) a;
  101. const struct vfs_stamping *vsb = (const struct vfs_stamping *) b;
  102. return (vsa == NULL || vsb == NULL || (vsa->v == vsb->v && vsa->id == vsb->id)) ? 0 : 1;
  103. }
  104. /* --------------------------------------------------------------------------------------------- */
  105. static void
  106. vfs_addstamp (struct vfs_class *v, vfsid id)
  107. {
  108. if ((v->flags & VFSF_LOCAL) == 0 && id != NULL && !vfs_stamp (v, id))
  109. {
  110. struct vfs_stamping *stamp;
  111. stamp = g_new (struct vfs_stamping, 1);
  112. stamp->v = v;
  113. stamp->id = id;
  114. gettimeofday (&(stamp->time), NULL);
  115. stamps = g_slist_append (stamps, stamp);
  116. }
  117. }
  118. /* --------------------------------------------------------------------------------------------- */
  119. /*** public functions ****************************************************************************/
  120. /* --------------------------------------------------------------------------------------------- */
  121. gboolean
  122. vfs_stamp (struct vfs_class *v, vfsid id)
  123. {
  124. struct vfs_stamping what = {
  125. .v = v,
  126. .id = id
  127. };
  128. GSList *stamp;
  129. gboolean ret = FALSE;
  130. stamp = g_slist_find_custom (stamps, &what, vfs_stamp_compare);
  131. if (stamp != NULL && stamp->data != NULL)
  132. {
  133. gettimeofday (&(VFS_STAMPING (stamp->data)->time), NULL);
  134. ret = TRUE;
  135. }
  136. return ret;
  137. }
  138. /* --------------------------------------------------------------------------------------------- */
  139. void
  140. vfs_rmstamp (struct vfs_class *v, vfsid id)
  141. {
  142. struct vfs_stamping what = {
  143. .v = v,
  144. .id = id
  145. };
  146. GSList *stamp;
  147. stamp = g_slist_find_custom (stamps, &what, vfs_stamp_compare);
  148. if (stamp != NULL)
  149. {
  150. g_free (stamp->data);
  151. stamps = g_slist_delete_link (stamps, stamp);
  152. }
  153. }
  154. /* --------------------------------------------------------------------------------------------- */
  155. void
  156. vfs_stamp_path (const vfs_path_t * vpath)
  157. {
  158. vfsid id;
  159. const vfs_path_element_t *path_element;
  160. path_element = vfs_path_get_by_index (vpath, -1);
  161. id = vfs_getid (vpath);
  162. vfs_addstamp (path_element->class, id);
  163. }
  164. /* --------------------------------------------------------------------------------------------- */
  165. /**
  166. * Create a new timestamp item by VFS class and VFS id.
  167. */
  168. void
  169. vfs_stamp_create (struct vfs_class *vclass, vfsid id)
  170. {
  171. vfsid nvfsid;
  172. ev_vfs_stamp_create_t event_data = { vclass, id, FALSE };
  173. const vfs_path_t *vpath;
  174. const vfs_path_element_t *path_element;
  175. /* There are three directories we have to take care of: current_dir,
  176. current_panel->cwd and other_panel->cwd. Athough most of the time either
  177. current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
  178. same, it's possible that all three are different -- Norbert */
  179. if (!mc_event_present (MCEVENT_GROUP_CORE, "vfs_timestamp"))
  180. return;
  181. vpath = vfs_get_raw_current_dir ();
  182. path_element = vfs_path_get_by_index (vpath, -1);
  183. nvfsid = vfs_getid (vpath);
  184. vfs_rmstamp (path_element->class, nvfsid);
  185. if (!(id == NULL || (path_element->class == vclass && nvfsid == id)))
  186. {
  187. mc_event_raise (MCEVENT_GROUP_CORE, "vfs_timestamp", (gpointer) & event_data);
  188. if (!event_data.ret && vclass != NULL && vclass->nothingisopen != NULL
  189. && vclass->nothingisopen (id))
  190. vfs_addstamp (vclass, id);
  191. }
  192. }
  193. /* --------------------------------------------------------------------------------------------- */
  194. /** This is called from timeout handler with now = FALSE,
  195. or can be called with now = TRUE to force freeing all filesystems */
  196. void
  197. vfs_expire (gboolean now)
  198. {
  199. static gboolean locked = FALSE;
  200. struct timeval curr_time, exp_time;
  201. GSList *stamp;
  202. /* Avoid recursive invocation, e.g. when one of the free functions
  203. calls message */
  204. if (locked)
  205. return;
  206. locked = TRUE;
  207. gettimeofday (&curr_time, NULL);
  208. exp_time.tv_sec = curr_time.tv_sec - vfs_timeout;
  209. exp_time.tv_usec = curr_time.tv_usec;
  210. if (now)
  211. {
  212. /* reverse list to free nested VFSes at first */
  213. stamps = g_slist_reverse (stamps);
  214. }
  215. /* NULLize stamps that point to expired VFS */
  216. for (stamp = stamps; stamp != NULL; stamp = g_slist_next (stamp))
  217. {
  218. struct vfs_stamping *stamping = VFS_STAMPING (stamp->data);
  219. if (now)
  220. {
  221. /* free VFS forced */
  222. if (stamping->v->free != NULL)
  223. stamping->v->free (stamping->id);
  224. MC_PTR_FREE (stamp->data);
  225. }
  226. else if (timeoutcmp (&stamping->time, &exp_time))
  227. {
  228. /* update timestamp of VFS that is in use, or free unused VFS */
  229. if (stamping->v->nothingisopen != NULL && !stamping->v->nothingisopen (stamping->id))
  230. stamping->time = curr_time;
  231. else
  232. {
  233. if (stamping->v->free != NULL)
  234. stamping->v->free (stamping->id);
  235. MC_PTR_FREE (stamp->data);
  236. }
  237. }
  238. }
  239. /* then remove NULLized stamps */
  240. stamps = g_slist_remove_all (stamps, NULL);
  241. locked = FALSE;
  242. }
  243. /* --------------------------------------------------------------------------------------------- */
  244. /*
  245. * Return the number of seconds remaining to the vfs timeout.
  246. * FIXME: The code should be improved to actually return the number of
  247. * seconds until the next item times out.
  248. */
  249. int
  250. vfs_timeouts (void)
  251. {
  252. return stamps != NULL ? 10 : 0;
  253. }
  254. /* --------------------------------------------------------------------------------------------- */
  255. void
  256. vfs_timeout_handler (void)
  257. {
  258. vfs_expire (FALSE);
  259. }
  260. /* --------------------------------------------------------------------------------------------- */
  261. void
  262. vfs_release_path (const vfs_path_t * vpath)
  263. {
  264. const vfs_path_element_t *path_element;
  265. path_element = vfs_path_get_by_index (vpath, -1);
  266. vfs_stamp_create (path_element->class, vfs_getid (vpath));
  267. }
  268. /* --------------------------------------------------------------------------------------------- */
  269. /* Free all data */
  270. void
  271. vfs_gc_done (void)
  272. {
  273. vfs_expire (TRUE);
  274. }
  275. /* --------------------------------------------------------------------------------------------- */