gc.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 "../src/global.h"
  37. #include "../src/wtools.h" /* message() */
  38. #include "../src/main.h" /* print_vfs_message */
  39. #include "../src/panel.h" /* get_current_panel() */
  40. #include "../src/layout.h" /* get_current_type() */
  41. #include "utilvfs.h"
  42. #include "vfs-impl.h"
  43. #include "vfs.h"
  44. #include "gc.h"
  45. int vfs_timeout = 60; /* VFS timeout in seconds */
  46. static struct vfs_stamping *stamps;
  47. static void
  48. vfs_addstamp (struct vfs_class *v, vfsid id)
  49. {
  50. if (!(v->flags & VFSF_LOCAL) && id != NULL) {
  51. struct vfs_stamping *stamp;
  52. struct vfs_stamping *last_stamp = NULL;
  53. for (stamp = stamps; stamp != NULL; stamp = stamp->next) {
  54. if (stamp->v == v && stamp->id == id) {
  55. gettimeofday (&(stamp->time), NULL);
  56. return;
  57. }
  58. last_stamp = stamp;
  59. }
  60. stamp = g_new (struct vfs_stamping, 1);
  61. stamp->v = v;
  62. stamp->id = id;
  63. gettimeofday (&(stamp->time), NULL);
  64. stamp->next = 0;
  65. if (stamps) {
  66. /* Add to the end */
  67. last_stamp->next = stamp;
  68. } else {
  69. /* Add first element */
  70. stamps = stamp;
  71. }
  72. }
  73. }
  74. void
  75. vfs_stamp (struct vfs_class *v, vfsid id)
  76. {
  77. struct vfs_stamping *stamp;
  78. for (stamp = stamps; stamp != NULL; stamp = stamp->next)
  79. if (stamp->v == v && stamp->id == id) {
  80. gettimeofday (&(stamp->time), NULL);
  81. return;
  82. }
  83. }
  84. void
  85. vfs_rmstamp (struct vfs_class *v, vfsid id)
  86. {
  87. struct vfs_stamping *stamp, *st1;
  88. for (stamp = stamps, st1 = NULL; stamp != NULL;
  89. st1 = stamp, stamp = stamp->next)
  90. if (stamp->v == v && stamp->id == id) {
  91. if (st1 == NULL) {
  92. stamps = stamp->next;
  93. } else {
  94. st1->next = stamp->next;
  95. }
  96. g_free (stamp);
  97. return;
  98. }
  99. }
  100. /* Find VFS id for given directory name */
  101. vfsid
  102. vfs_getid (struct vfs_class *vclass, const char *dir)
  103. {
  104. char *dir1;
  105. vfsid id = NULL;
  106. /* append slash if needed */
  107. dir1 = concat_dir_and_file (dir, "");
  108. if (vclass->getid)
  109. id = (*vclass->getid) (vclass, dir1);
  110. g_free (dir1);
  111. return id;
  112. }
  113. static void
  114. vfs_stamp_path (char *path)
  115. {
  116. struct vfs_class *vfs;
  117. vfsid id;
  118. vfs = vfs_get_class (path);
  119. id = vfs_getid (vfs, path);
  120. vfs_addstamp (vfs, id);
  121. }
  122. /*
  123. * Create a new timestamp item by VFS class and VFS id.
  124. */
  125. void
  126. vfs_stamp_create (struct vfs_class *oldvfs, vfsid oldvfsid)
  127. {
  128. struct vfs_class *nvfs, *n2vfs, *n3vfs;
  129. vfsid nvfsid, n2vfsid, n3vfsid;
  130. /* There are three directories we have to take care of: current_dir,
  131. current_panel->cwd and other_panel->cwd. Athough most of the time either
  132. current_dir and current_panel->cwd or current_dir and other_panel->cwd are the
  133. same, it's possible that all three are different -- Norbert */
  134. if (!current_panel)
  135. return;
  136. nvfs = vfs_get_class (vfs_get_current_dir ());
  137. nvfsid = vfs_getid (nvfs, vfs_get_current_dir ());
  138. vfs_rmstamp (nvfs, nvfsid);
  139. if ((nvfs == oldvfs && nvfsid == oldvfsid) || oldvfsid == NULL) {
  140. return;
  141. }
  142. if (get_current_type () == view_listing) {
  143. n2vfs = vfs_get_class (current_panel->cwd);
  144. n2vfsid = vfs_getid (n2vfs, current_panel->cwd);
  145. if (n2vfs == oldvfs && n2vfsid == oldvfsid)
  146. return;
  147. } else {
  148. n2vfs = NULL;
  149. n2vfsid = NULL;
  150. }
  151. if (get_other_type () == view_listing) {
  152. n3vfs = vfs_get_class (other_panel->cwd);
  153. n3vfsid = vfs_getid (n3vfs, other_panel->cwd);
  154. if (n3vfs == oldvfs && n3vfsid == oldvfsid)
  155. return;
  156. } else {
  157. n3vfs = NULL;
  158. n3vfsid = NULL;
  159. }
  160. if (!oldvfs->nothingisopen || !(*oldvfs->nothingisopen) (oldvfsid))
  161. return;
  162. vfs_addstamp (oldvfs, oldvfsid);
  163. }
  164. void
  165. vfs_add_current_stamps (void)
  166. {
  167. vfs_stamp_path (vfs_get_current_dir ());
  168. if (current_panel) {
  169. if (get_current_type () == view_listing)
  170. vfs_stamp_path (current_panel->cwd);
  171. }
  172. if (other_panel) {
  173. if (get_other_type () == view_listing)
  174. vfs_stamp_path (other_panel->cwd);
  175. }
  176. }
  177. /* Compare two timeval structures. Return 0 is t1 is less than t2. */
  178. static inline int
  179. timeoutcmp (struct timeval *t1, struct timeval *t2)
  180. {
  181. return ((t1->tv_sec < t2->tv_sec)
  182. || ((t1->tv_sec == t2->tv_sec)
  183. && (t1->tv_usec <= t2->tv_usec)));
  184. }
  185. /* This is called from timeout handler with now = 0, or can be called
  186. with now = 1 to force freeing all filesystems that are not in use */
  187. void
  188. vfs_expire (int now)
  189. {
  190. static int locked = 0;
  191. struct timeval time;
  192. struct vfs_stamping *stamp, *st;
  193. /* Avoid recursive invocation, e.g. when one of the free functions
  194. calls message */
  195. if (locked)
  196. return;
  197. locked = 1;
  198. gettimeofday (&time, NULL);
  199. time.tv_sec -= vfs_timeout;
  200. for (stamp = stamps; stamp != NULL;) {
  201. if (now || (timeoutcmp (&stamp->time, &time))) {
  202. st = stamp->next;
  203. if (stamp->v->free)
  204. (*stamp->v->free) (stamp->id);
  205. vfs_rmstamp (stamp->v, stamp->id);
  206. stamp = st;
  207. } else
  208. stamp = stamp->next;
  209. }
  210. locked = 0;
  211. }
  212. /*
  213. * Return the number of seconds remaining to the vfs timeout.
  214. * FIXME: The code should be improved to actually return the number of
  215. * seconds until the next item times out.
  216. */
  217. int
  218. vfs_timeouts ()
  219. {
  220. return stamps ? 10 : 0;
  221. }
  222. void
  223. vfs_timeout_handler (void)
  224. {
  225. vfs_expire (0);
  226. }
  227. void
  228. vfs_release_path (const char *dir)
  229. {
  230. struct vfs_class *oldvfs;
  231. vfsid oldvfsid;
  232. oldvfs = vfs_get_class (dir);
  233. oldvfsid = vfs_getid (oldvfs, dir);
  234. vfs_stamp_create (oldvfs, oldvfsid);
  235. }
  236. /* Free all data */
  237. void
  238. vfs_gc_done (void)
  239. {
  240. struct vfs_stamping *stamp, *st;
  241. for (stamp = stamps, stamps = 0; stamp != NULL;) {
  242. if (stamp->v->free)
  243. (*stamp->v->free) (stamp->id);
  244. st = stamp->next;
  245. g_free (stamp);
  246. stamp = st;
  247. }
  248. if (stamps)
  249. vfs_rmstamp (stamps->v, stamps->id);
  250. }