gc.c 6.8 KB

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