gc.c 9.8 KB

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