1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* gnome-vfs-cancellation.c - Cancellation handling for the GNOME Virtual File |
---|
3 | System access methods. |
---|
4 | |
---|
5 | Copyright (C) 1999 Free Software Foundation |
---|
6 | |
---|
7 | The Gnome Library is free software; you can redistribute it and/or |
---|
8 | modify it under the terms of the GNU Library General Public License as |
---|
9 | published by the Free Software Foundation; either version 2 of the |
---|
10 | License, or (at your option) any later version. |
---|
11 | |
---|
12 | The Gnome Library is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
15 | Library General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Library General Public |
---|
18 | License along with the Gnome Library; see the file COPYING.LIB. If not, |
---|
19 | write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
---|
20 | Boston, MA 02111-1307, USA. |
---|
21 | |
---|
22 | Author: Ettore Perazzoli <ettore@gnu.org> */ |
---|
23 | |
---|
24 | #ifdef HAVE_CONFIG_H |
---|
25 | #include <config.h> |
---|
26 | #endif |
---|
27 | |
---|
28 | #include <unistd.h> |
---|
29 | |
---|
30 | #include "gnome-vfs.h" |
---|
31 | #include "gnome-vfs-private.h" |
---|
32 | |
---|
33 | /* WARNING: this code is not general-purpose. It is supposed to make the two |
---|
34 | sides of the VFS (i.e. the main process/thread and its asynchronous slave) |
---|
35 | talk in a simple way. For this reason, only the main process/thread should |
---|
36 | be allowed to call `gnome_vfs_cancellation_cancel()'. *All* the code is |
---|
37 | based on this assumption. */ |
---|
38 | |
---|
39 | |
---|
40 | struct GnomeVFSCancellation { |
---|
41 | gboolean cancelled; |
---|
42 | gint pipe_in; |
---|
43 | gint pipe_out; |
---|
44 | }; |
---|
45 | |
---|
46 | |
---|
47 | /** |
---|
48 | * gnome_vfs_cancellation_new: |
---|
49 | * |
---|
50 | * Create a new GnomeVFSCancellation object for reporting cancellation to a |
---|
51 | * GNOME VFS module. |
---|
52 | * |
---|
53 | * Return value: A pointer to the new GnomeVFSCancellation object. |
---|
54 | **/ |
---|
55 | GnomeVFSCancellation * |
---|
56 | gnome_vfs_cancellation_new (void) |
---|
57 | { |
---|
58 | GnomeVFSCancellation *new; |
---|
59 | gint pipefd[2]; |
---|
60 | |
---|
61 | if (pipe (pipefd) == -1) |
---|
62 | return NULL; |
---|
63 | |
---|
64 | new = g_new (GnomeVFSCancellation, 1); |
---|
65 | new->cancelled = FALSE; |
---|
66 | new->pipe_in = pipefd[0]; |
---|
67 | new->pipe_out = pipefd[1]; |
---|
68 | |
---|
69 | return new; |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * gnome_vfs_cancellation_destroy: |
---|
74 | * @cancellation: A GnomeVFSCancellation object |
---|
75 | * |
---|
76 | * Destroy @cancellation. |
---|
77 | **/ |
---|
78 | void |
---|
79 | gnome_vfs_cancellation_destroy (GnomeVFSCancellation *cancellation) |
---|
80 | { |
---|
81 | g_return_if_fail (cancellation != NULL); |
---|
82 | |
---|
83 | close (cancellation->pipe_in); |
---|
84 | close (cancellation->pipe_out); |
---|
85 | g_free (cancellation); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * gnome_vfs_cancellation_cancel: |
---|
90 | * @cancellation: A GnomeVFSCancellation object |
---|
91 | * |
---|
92 | * Send a cancellation request through @cancellation. |
---|
93 | **/ |
---|
94 | void |
---|
95 | gnome_vfs_cancellation_cancel (GnomeVFSCancellation *cancellation) |
---|
96 | { |
---|
97 | g_return_if_fail (cancellation != NULL); |
---|
98 | |
---|
99 | if (cancellation->cancelled) |
---|
100 | return; |
---|
101 | |
---|
102 | write (cancellation->pipe_out, "c", 1); |
---|
103 | cancellation->cancelled = TRUE; |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * gnome_vfs_cancellation_check: |
---|
108 | * @cancellation: A GnomeVFSCancellation object |
---|
109 | * |
---|
110 | * Check for pending cancellation. |
---|
111 | * |
---|
112 | * Return value: %TRUE if the operation should be interrupted. |
---|
113 | **/ |
---|
114 | gboolean |
---|
115 | gnome_vfs_cancellation_check (GnomeVFSCancellation *cancellation) |
---|
116 | { |
---|
117 | if (cancellation == NULL) |
---|
118 | return FALSE; |
---|
119 | |
---|
120 | return cancellation->cancelled; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * gnome_vfs_cancellation_ack: |
---|
125 | * @cancellation: A GnomeVFSCancellation object |
---|
126 | * |
---|
127 | * Acknowledge a cancellation. This should be called if |
---|
128 | * `gnome_vfs_cancellation_check()' returns %TRUE or if `select()' reports that |
---|
129 | * input is available on the file descriptor returned by |
---|
130 | * `gnome_vfs_cancellation_get_fd()'. |
---|
131 | **/ |
---|
132 | void |
---|
133 | gnome_vfs_cancellation_ack (GnomeVFSCancellation *cancellation) |
---|
134 | { |
---|
135 | gchar c; |
---|
136 | |
---|
137 | if (cancellation == NULL) |
---|
138 | return; |
---|
139 | |
---|
140 | read (cancellation->pipe_in, &c, 1); |
---|
141 | cancellation->cancelled = FALSE; |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * gnome_vfs_cancellation_get_fd: |
---|
146 | * @cancellation: A GnomeVFSCancellation object |
---|
147 | * |
---|
148 | * Get a file descriptor -based notificator for @cancellation. When |
---|
149 | * @cancellation receives a cancellation request, a character will be made |
---|
150 | * available on the returned file descriptor for input. |
---|
151 | * |
---|
152 | * This is very useful for detecting cancellation during I/O operations: you |
---|
153 | * can use the `select()' call to check for available input/output on the file |
---|
154 | * you are reading/writing, and on the notificator's file descriptor at the |
---|
155 | * same time. If a data is available on the notificator's file descriptor, you |
---|
156 | * know you have to cancel the read/write operation. |
---|
157 | * |
---|
158 | * Return value: the notificator's file descriptor. |
---|
159 | **/ |
---|
160 | gint |
---|
161 | gnome_vfs_cancellation_get_fd (GnomeVFSCancellation *cancellation) |
---|
162 | { |
---|
163 | g_return_val_if_fail (cancellation != NULL, -1); |
---|
164 | |
---|
165 | return cancellation->pipe_in; |
---|
166 | } |
---|