gc.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. Virtual File System garbage collection code
  3. Copyright (C) 2003-2014
  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. /*** global variables ****************************************************************************/
  41. int vfs_timeout = 60; /* VFS timeout in seconds */
  42. /*** file scope macro definitions ****************************************************************/
  43. /*** file scope type declarations ****************************************************************/
  44. /*** file scope variables ************************************************************************/
  45. static struct vfs_stamping *stamps;
  46. /*** file scope functions ************************************************************************/
  47. /* --------------------------------------------------------------------------------------------- */
  48. static void
  49. vfs_addstamp (struct vfs_class *v, vfsid id)
  50. {
  51. if (!(v->flags & VFSF_LOCAL) && id != NULL)
  52. {
  53. struct vfs_stamping *stamp;
  54. struct vfs_stamping *last_stamp = NULL;
  55. for (stamp = stamps; stamp != NULL; stamp = stamp->next)
  56. {
  57. if (stamp->v == v && stamp->id == id)
  58. {
  59. gettimeofday (&(stamp->time), NULL);
  60. return;
  61. }
  62. last_stamp = stamp;
  63. }
  64. stamp = g_new (struct vfs_stamping, 1);
  65. stamp->v = v;
  66. stamp->id = id;
  67. gettimeofday (&(stamp->time), NULL);
  68. stamp->next = 0;
  69. if (stamps)
  70. {
  71. /* Add to the end */
  72. last_stamp->next = stamp;
  73. }
  74. else
  75. {
  76. /* Add first element */
  77. stamps = stamp;
  78. }
  79. }
  80. }
  81. /* --------------------------------------------------------------------------------------------- */
  82. /** Compare two timeval structures. Return 0 is t1 is less than t2. */
  83. static inline int
  84. timeoutcmp (struct timeval *t1, struct timeval *t2)
  85. {
  86. return ((t1->tv_sec < t2->tv_sec)
  87. || ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec <= t2->tv_usec)));
  88. }
  89. /* --------------------------------------------------------------------------------------------- */
  90. /*** public functions ****************************************************************************/
  91. /* --------------------------------------------------------------------------------------------- */
  92. void
  93. vfs_stamp (struct vfs_class *v, vfsid id)
  94. {
  95. struct vfs_stamping *stamp;
  96. for (stamp = stamps; stamp != NULL; stamp = stamp->next)
  97. if (stamp->v == v && stamp->id == id)
  98. {
  99. gettimeofday (&(stamp->time), NULL);
  100. return;
  101. }
  102. }
  103. /* --------------------------------------------------------------------------------------------- */
  104. void
  105. vfs_rmstamp (struct vfs_class *v, vfsid id)
  106. {
  107. struct vfs_stamping *stamp, *st1;
  108. for (stamp = stamps, st1 = NULL; stamp != NULL; st1 = stamp, stamp = stamp->next)
  109. if (stamp->v == v && stamp->id == id)
  110. {
  111. if (st1 == NULL)
  112. {
  113. stamps = stamp->next;
  114. }
  115. else
  116. {
  117. st1->next = stamp->next;
  118. }
  119. g_free (stamp);
  120. return;
  121. }
  122. }
  123. /* --------------------------------------------------------------------------------------------- */
  124. void
  125. vfs_stamp_path (const char *path)
  126. {
  127. vfsid id;
  128. vfs_path_t *vpath;
  129. const vfs_path_element_t *path_element;
  130. vpath = vfs_path_from_str (path);
  131. path_element = vfs_path_get_by_index (vpath, -1);
  132. id = vfs_getid (vpath);
  133. vfs_addstamp (path_element->class, id);
  134. vfs_path_free (vpath);
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. /**
  138. * Create a new timestamp item by VFS class and VFS id.
  139. */
  140. void
  141. vfs_stamp_create (struct vfs_class *vclass, vfsid id)
  142. {
  143. vfsid nvfsid;
  144. ev_vfs_stamp_create_t event_data = { vclass, id, FALSE };
  145. const vfs_path_t *vpath;
  146. const vfs_path_element_t *path_element;
  147. /* There are three directories we have to take care of: current_dir,
  148. current_panel->cwd and other_panel->cwd. Athough most of the time either
  149. current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
  150. same, it's possible that all three are different -- Norbert */
  151. if (!mc_event_present (MCEVENT_GROUP_CORE, "vfs_timestamp"))
  152. return;
  153. vpath = vfs_get_raw_current_dir ();
  154. path_element = vfs_path_get_by_index (vpath, -1);
  155. nvfsid = vfs_getid (vpath);
  156. vfs_rmstamp (path_element->class, nvfsid);
  157. if (!(id == NULL || (path_element->class == vclass && nvfsid == id)))
  158. {
  159. mc_event_raise (MCEVENT_GROUP_CORE, "vfs_timestamp", (gpointer) & event_data);
  160. if (!event_data.ret && vclass != NULL && vclass->nothingisopen != NULL
  161. && vclass->nothingisopen (id) != 0)
  162. vfs_addstamp (vclass, id);
  163. }
  164. }
  165. /* --------------------------------------------------------------------------------------------- */
  166. /** This is called from timeout handler with now = 0, or can be called
  167. with now = 1 to force freeing all filesystems that are not in use */
  168. void
  169. vfs_expire (gboolean now)
  170. {
  171. static gboolean locked = FALSE;
  172. struct timeval lc_time;
  173. struct vfs_stamping *stamp, *st;
  174. /* Avoid recursive invocation, e.g. when one of the free functions
  175. calls message */
  176. if (locked)
  177. return;
  178. locked = TRUE;
  179. gettimeofday (&lc_time, NULL);
  180. lc_time.tv_sec -= vfs_timeout;
  181. for (stamp = stamps; stamp != NULL;)
  182. {
  183. if (now || (timeoutcmp (&stamp->time, &lc_time)))
  184. {
  185. st = stamp->next;
  186. if (stamp->v->free)
  187. (*stamp->v->free) (stamp->id);
  188. vfs_rmstamp (stamp->v, stamp->id);
  189. stamp = st;
  190. }
  191. else
  192. stamp = stamp->next;
  193. }
  194. locked = FALSE;
  195. }
  196. /* --------------------------------------------------------------------------------------------- */
  197. /*
  198. * Return the number of seconds remaining to the vfs timeout.
  199. * FIXME: The code should be improved to actually return the number of
  200. * seconds until the next item times out.
  201. */
  202. int
  203. vfs_timeouts (void)
  204. {
  205. return stamps ? 10 : 0;
  206. }
  207. /* --------------------------------------------------------------------------------------------- */
  208. void
  209. vfs_timeout_handler (void)
  210. {
  211. vfs_expire (FALSE);
  212. }
  213. /* --------------------------------------------------------------------------------------------- */
  214. void
  215. vfs_release_path (const vfs_path_t * vpath)
  216. {
  217. const vfs_path_element_t *path_element;
  218. path_element = vfs_path_get_by_index (vpath, -1);
  219. vfs_stamp_create (path_element->class, vfs_getid (vpath));
  220. }
  221. /* --------------------------------------------------------------------------------------------- */
  222. /* Free all data */
  223. void
  224. vfs_gc_done (void)
  225. {
  226. struct vfs_stamping *stamp, *st;
  227. for (stamp = stamps, stamps = 0; stamp != NULL;)
  228. {
  229. if (stamp->v->free)
  230. (*stamp->v->free) (stamp->id);
  231. st = stamp->next;
  232. g_free (stamp);
  233. stamp = st;
  234. }
  235. if (stamps)
  236. vfs_rmstamp (stamps->v, stamps->id);
  237. }
  238. /* --------------------------------------------------------------------------------------------- */