gc.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /* Virtual File System garbage collection code
  2. Copyright (C) 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
  3. Written by: 1995 Miguel de Icaza
  4. 1995 Jakub Jelinek
  5. 1998 Pavel Machek
  6. 2003 Pavel Roskin
  7. This program is free software; you can redistribute it and/or
  8. modify it under the terms of the GNU Library General Public License
  9. as published by the Free Software Foundation; either version 2 of
  10. the License, or (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU Library General Public License for more details.
  15. You should have received a copy of the GNU Library General Public
  16. License along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  18. /**
  19. * \file
  20. * \brief Source: Virtual File System: garbage collection code
  21. * \author Miguel de Icaza
  22. * \author Jakub Jelinek
  23. * \author Pavel Machek
  24. * \author Pavel Roskin
  25. * \date 1995, 1998, 2003
  26. */
  27. #include <config.h>
  28. #include <stdio.h>
  29. #include <stdlib.h> /* For atol() */
  30. #include <stdarg.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #include <sys/types.h>
  34. #include <ctype.h> /* is_digit() */
  35. #include <sys/time.h> /* gettimeofday() */
  36. #include "lib/global.h"
  37. #include "src/filemanager/midnight.h" /* current_panel */
  38. #include "src/filemanager/layout.h" /* get_current_type(), get_other_type() */
  39. #include "vfs-impl.h"
  40. #include "utilvfs.h"
  41. #include "gc.h"
  42. /*** global variables ****************************************************************************/
  43. int vfs_timeout = 60; /* VFS timeout in seconds */
  44. /*** file scope macro definitions ****************************************************************/
  45. /*** file scope type declarations ****************************************************************/
  46. /*** file scope variables ************************************************************************/
  47. static struct vfs_stamping *stamps;
  48. /*** file scope functions ************************************************************************/
  49. /* --------------------------------------------------------------------------------------------- */
  50. static void
  51. vfs_addstamp (struct vfs_class *v, vfsid id)
  52. {
  53. if (!(v->flags & VFSF_LOCAL) && id != NULL)
  54. {
  55. struct vfs_stamping *stamp;
  56. struct vfs_stamping *last_stamp = NULL;
  57. for (stamp = stamps; stamp != NULL; stamp = stamp->next)
  58. {
  59. if (stamp->v == v && stamp->id == id)
  60. {
  61. gettimeofday (&(stamp->time), NULL);
  62. return;
  63. }
  64. last_stamp = stamp;
  65. }
  66. stamp = g_new (struct vfs_stamping, 1);
  67. stamp->v = v;
  68. stamp->id = id;
  69. gettimeofday (&(stamp->time), NULL);
  70. stamp->next = 0;
  71. if (stamps)
  72. {
  73. /* Add to the end */
  74. last_stamp->next = stamp;
  75. }
  76. else
  77. {
  78. /* Add first element */
  79. stamps = stamp;
  80. }
  81. }
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. /** Compare two timeval structures. Return 0 is t1 is less than t2. */
  85. static inline int
  86. timeoutcmp (struct timeval *t1, struct timeval *t2)
  87. {
  88. return ((t1->tv_sec < t2->tv_sec)
  89. || ((t1->tv_sec == t2->tv_sec) && (t1->tv_usec <= t2->tv_usec)));
  90. }
  91. /* --------------------------------------------------------------------------------------------- */
  92. /*** public functions ****************************************************************************/
  93. /* --------------------------------------------------------------------------------------------- */
  94. void
  95. vfs_stamp (struct vfs_class *v, vfsid id)
  96. {
  97. struct vfs_stamping *stamp;
  98. for (stamp = stamps; stamp != NULL; stamp = stamp->next)
  99. if (stamp->v == v && stamp->id == id)
  100. {
  101. gettimeofday (&(stamp->time), NULL);
  102. return;
  103. }
  104. }
  105. /* --------------------------------------------------------------------------------------------- */
  106. void
  107. vfs_rmstamp (struct vfs_class *v, vfsid id)
  108. {
  109. struct vfs_stamping *stamp, *st1;
  110. for (stamp = stamps, st1 = NULL; stamp != NULL; st1 = stamp, stamp = stamp->next)
  111. if (stamp->v == v && stamp->id == id)
  112. {
  113. if (st1 == NULL)
  114. {
  115. stamps = stamp->next;
  116. }
  117. else
  118. {
  119. st1->next = stamp->next;
  120. }
  121. g_free (stamp);
  122. return;
  123. }
  124. }
  125. /* --------------------------------------------------------------------------------------------- */
  126. void
  127. vfs_stamp_path (const char *path)
  128. {
  129. struct vfs_class *vfs;
  130. vfsid id;
  131. vfs = vfs_get_class (path);
  132. id = vfs_getid (vfs, path);
  133. vfs_addstamp (vfs, id);
  134. }
  135. /* --------------------------------------------------------------------------------------------- */
  136. /**
  137. * Create a new timestamp item by VFS class and VFS id.
  138. */
  139. void
  140. vfs_stamp_create (struct vfs_class *oldvfs, vfsid oldvfsid)
  141. {
  142. struct vfs_class *nvfs, *n2vfs, *n3vfs;
  143. vfsid nvfsid, n2vfsid, n3vfsid;
  144. /* There are three directories we have to take care of: current_dir,
  145. current_panel->cwd and other_panel->cwd. Athough most of the time either
  146. current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
  147. same, it's possible that all three are different -- Norbert */
  148. if (current_panel == NULL)
  149. return;
  150. nvfs = vfs_get_class (vfs_get_current_dir ());
  151. nvfsid = vfs_getid (nvfs, vfs_get_current_dir ());
  152. vfs_rmstamp (nvfs, nvfsid);
  153. if ((nvfs == oldvfs && nvfsid == oldvfsid) || oldvfsid == NULL)
  154. {
  155. return;
  156. }
  157. if (get_current_type () == view_listing)
  158. {
  159. n2vfs = vfs_get_class (current_panel->cwd);
  160. n2vfsid = vfs_getid (n2vfs, current_panel->cwd);
  161. if (n2vfs == oldvfs && n2vfsid == oldvfsid)
  162. return;
  163. }
  164. else
  165. {
  166. n2vfs = NULL;
  167. n2vfsid = NULL;
  168. }
  169. if (get_other_type () == view_listing)
  170. {
  171. n3vfs = vfs_get_class (other_panel->cwd);
  172. n3vfsid = vfs_getid (n3vfs, other_panel->cwd);
  173. if (n3vfs == oldvfs && n3vfsid == oldvfsid)
  174. return;
  175. }
  176. else
  177. {
  178. n3vfs = NULL;
  179. n3vfsid = NULL;
  180. }
  181. if (!oldvfs->nothingisopen || !(*oldvfs->nothingisopen) (oldvfsid))
  182. return;
  183. vfs_addstamp (oldvfs, oldvfsid);
  184. }
  185. /* --------------------------------------------------------------------------------------------- */
  186. /** This is called from timeout handler with now = 0, or can be called
  187. with now = 1 to force freeing all filesystems that are not in use */
  188. void
  189. vfs_expire (int now)
  190. {
  191. static int locked = 0;
  192. struct timeval lc_time;
  193. struct vfs_stamping *stamp, *st;
  194. /* Avoid recursive invocation, e.g. when one of the free functions
  195. calls message */
  196. if (locked)
  197. return;
  198. locked = 1;
  199. gettimeofday (&lc_time, NULL);
  200. lc_time.tv_sec -= vfs_timeout;
  201. for (stamp = stamps; stamp != NULL;)
  202. {
  203. if (now || (timeoutcmp (&stamp->time, &lc_time)))
  204. {
  205. st = stamp->next;
  206. if (stamp->v->free)
  207. (*stamp->v->free) (stamp->id);
  208. vfs_rmstamp (stamp->v, stamp->id);
  209. stamp = st;
  210. }
  211. else
  212. stamp = stamp->next;
  213. }
  214. locked = 0;
  215. }
  216. /* --------------------------------------------------------------------------------------------- */
  217. /*
  218. * Return the number of seconds remaining to the vfs timeout.
  219. * FIXME: The code should be improved to actually return the number of
  220. * seconds until the next item times out.
  221. */
  222. int
  223. vfs_timeouts ()
  224. {
  225. return stamps ? 10 : 0;
  226. }
  227. /* --------------------------------------------------------------------------------------------- */
  228. void
  229. vfs_timeout_handler (void)
  230. {
  231. vfs_expire (0);
  232. }
  233. /* --------------------------------------------------------------------------------------------- */
  234. void
  235. vfs_release_path (const char *dir)
  236. {
  237. struct vfs_class *oldvfs;
  238. vfsid oldvfsid;
  239. oldvfs = vfs_get_class (dir);
  240. oldvfsid = vfs_getid (oldvfs, dir);
  241. vfs_stamp_create (oldvfs, oldvfsid);
  242. }
  243. /* --------------------------------------------------------------------------------------------- */
  244. /* Free all data */
  245. void
  246. vfs_gc_done (void)
  247. {
  248. struct vfs_stamping *stamp, *st;
  249. for (stamp = stamps, stamps = 0; stamp != NULL;)
  250. {
  251. if (stamp->v->free)
  252. (*stamp->v->free) (stamp->id);
  253. st = stamp->next;
  254. g_free (stamp);
  255. stamp = st;
  256. }
  257. if (stamps)
  258. vfs_rmstamp (stamps->v, stamps->id);
  259. }
  260. /* --------------------------------------------------------------------------------------------- */