Bug 561807 – inotify_sub.c :: dup_dirname() fails to remove trailing '/'
[glib.git] / gio / gfileoutputstream.c
blob7dea32cd122a5f93c24a42b1d1936762bbf33644
1 /* GIO - GLib Input, Output and Streaming Library
2 *
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
23 #include "config.h"
25 #include <glib.h>
26 #include <gfileoutputstream.h>
27 #include <gseekable.h>
28 #include "gsimpleasyncresult.h"
29 #include "gasyncresult.h"
30 #include "gcancellable.h"
31 #include "gioerror.h"
32 #include "glibintl.h"
34 #include "gioalias.h"
36 /**
37 * SECTION:gfileoutputstream
38 * @short_description: File output streaming operations
39 * @include: gio/gio.h
40 * @see_also: #GOutputStream, #GDataOutputStream, #GSeekable
42 * GFileOutputStream provides output streams that write their
43 * content to a file.
45 * GFileOutputStream implements #GSeekable, which allows the output
46 * stream to jump to arbitrary positions in the file and to truncate
47 * the file, provided the filesystem of the file supports these
48 * operations. In addition to the generic g_seekable_ API,
49 * GFileOutputStream has its own API for seeking and positioning.
50 * To find the position of a file output stream, use
51 * g_file_output_stream_tell(). To find out if a file output
52 * stream supports seeking, use g_file_output_stream_can_seek().
53 * To position a file output stream, use g_file_output_stream_seek().
54 * To find out if a file output stream supports truncating, use
55 * g_file_output_stream_can_truncate(). To truncate a file output
56 * stream, use g_file_output_stream_truncate().
57 **/
59 static void g_file_output_stream_seekable_iface_init (GSeekableIface *iface);
60 static goffset g_file_output_stream_seekable_tell (GSeekable *seekable);
61 static gboolean g_file_output_stream_seekable_can_seek (GSeekable *seekable);
62 static gboolean g_file_output_stream_seekable_seek (GSeekable *seekable,
63 goffset offset,
64 GSeekType type,
65 GCancellable *cancellable,
66 GError **error);
67 static gboolean g_file_output_stream_seekable_can_truncate (GSeekable *seekable);
68 static gboolean g_file_output_stream_seekable_truncate (GSeekable *seekable,
69 goffset offset,
70 GCancellable *cancellable,
71 GError **error);
72 static void g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
73 char *attributes,
74 int io_priority,
75 GCancellable *cancellable,
76 GAsyncReadyCallback callback,
77 gpointer user_data);
78 static GFileInfo *g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
79 GAsyncResult *result,
80 GError **error);
82 G_DEFINE_TYPE_WITH_CODE (GFileOutputStream, g_file_output_stream, G_TYPE_OUTPUT_STREAM,
83 G_IMPLEMENT_INTERFACE (G_TYPE_SEEKABLE,
84 g_file_output_stream_seekable_iface_init));
86 struct _GFileOutputStreamPrivate {
87 GAsyncReadyCallback outstanding_callback;
90 static void
91 g_file_output_stream_class_init (GFileOutputStreamClass *klass)
93 g_type_class_add_private (klass, sizeof (GFileOutputStreamPrivate));
95 klass->query_info_async = g_file_output_stream_real_query_info_async;
96 klass->query_info_finish = g_file_output_stream_real_query_info_finish;
99 static void
100 g_file_output_stream_seekable_iface_init (GSeekableIface *iface)
102 iface->tell = g_file_output_stream_seekable_tell;
103 iface->can_seek = g_file_output_stream_seekable_can_seek;
104 iface->seek = g_file_output_stream_seekable_seek;
105 iface->can_truncate = g_file_output_stream_seekable_can_truncate;
106 iface->truncate_fn = g_file_output_stream_seekable_truncate;
109 static void
110 g_file_output_stream_init (GFileOutputStream *stream)
112 stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (stream,
113 G_TYPE_FILE_OUTPUT_STREAM,
114 GFileOutputStreamPrivate);
118 * g_file_output_stream_query_info:
119 * @stream: a #GFileOutputStream.
120 * @attributes: a file attribute query string.
121 * @cancellable: optional #GCancellable object, %NULL to ignore.
122 * @error: a #GError, %NULL to ignore.
124 * Queries a file output stream for the given @attributes.
125 * This function blocks while querying the stream. For the asynchronous
126 * version of this function, see g_file_output_stream_query_info_async().
127 * While the stream is blocked, the stream will set the pending flag
128 * internally, and any other operations on the stream will fail with
129 * %G_IO_ERROR_PENDING.
131 * Can fail if the stream was already closed (with @error being set to
132 * %G_IO_ERROR_CLOSED), the stream has pending operations (with @error being
133 * set to %G_IO_ERROR_PENDING), or if querying info is not supported for
134 * the stream's interface (with @error being set to %G_IO_ERROR_NOT_SUPPORTED). In
135 * all cases of failure, %NULL will be returned.
137 * If @cancellable is not %NULL, then the operation can be cancelled by
138 * triggering the cancellable object from another thread. If the operation
139 * was cancelled, the error %G_IO_ERROR_CANCELLED will be set, and %NULL will
140 * be returned.
142 * Returns: a #GFileInfo for the @stream, or %NULL on error.
144 GFileInfo *
145 g_file_output_stream_query_info (GFileOutputStream *stream,
146 char *attributes,
147 GCancellable *cancellable,
148 GError **error)
150 GFileOutputStreamClass *class;
151 GOutputStream *output_stream;
152 GFileInfo *info;
154 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
156 output_stream = G_OUTPUT_STREAM (stream);
158 if (!g_output_stream_set_pending (output_stream, error))
159 return NULL;
161 info = NULL;
163 if (cancellable)
164 g_cancellable_push_current (cancellable);
166 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
167 if (class->query_info)
168 info = class->query_info (stream, attributes, cancellable, error);
169 else
170 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
171 _("Stream doesn't support query_info"));
173 if (cancellable)
174 g_cancellable_pop_current (cancellable);
176 g_output_stream_clear_pending (output_stream);
178 return info;
181 static void
182 async_ready_callback_wrapper (GObject *source_object,
183 GAsyncResult *res,
184 gpointer user_data)
186 GFileOutputStream *stream = G_FILE_OUTPUT_STREAM (source_object);
188 g_output_stream_clear_pending (G_OUTPUT_STREAM (stream));
189 if (stream->priv->outstanding_callback)
190 (*stream->priv->outstanding_callback) (source_object, res, user_data);
191 g_object_unref (stream);
195 * g_file_output_stream_query_info_async:
196 * @stream: a #GFileOutputStream.
197 * @attributes: a file attribute query string.
198 * @io_priority: the <link linkend="gio-GIOScheduler">I/O priority</link>
199 * of the request.
200 * @cancellable: optional #GCancellable object, %NULL to ignore.
201 * @callback: callback to call when the request is satisfied
202 * @user_data: the data to pass to callback function
204 * Asynchronously queries the @stream for a #GFileInfo. When completed,
205 * @callback will be called with a #GAsyncResult which can be used to
206 * finish the operation with g_file_output_stream_query_info_finish().
208 * For the synchronous version of this function, see
209 * g_file_output_stream_query_info().
212 void
213 g_file_output_stream_query_info_async (GFileOutputStream *stream,
214 char *attributes,
215 int io_priority,
216 GCancellable *cancellable,
217 GAsyncReadyCallback callback,
218 gpointer user_data)
220 GFileOutputStreamClass *klass;
221 GOutputStream *output_stream;
222 GError *error = NULL;
224 g_return_if_fail (G_IS_FILE_OUTPUT_STREAM (stream));
226 output_stream = G_OUTPUT_STREAM (stream);
228 if (!g_output_stream_set_pending (output_stream, &error))
230 g_simple_async_report_gerror_in_idle (G_OBJECT (stream),
231 callback,
232 user_data,
233 error);
234 g_error_free (error);
235 return;
238 klass = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
240 stream->priv->outstanding_callback = callback;
241 g_object_ref (stream);
242 klass->query_info_async (stream, attributes, io_priority, cancellable,
243 async_ready_callback_wrapper, user_data);
247 * g_file_output_stream_query_info_finish:
248 * @stream: a #GFileOutputStream.
249 * @result: a #GAsyncResult.
250 * @error: a #GError, %NULL to ignore.
252 * Finalizes the asynchronous query started
253 * by g_file_output_stream_query_info_async().
255 * Returns: A #GFileInfo for the finished query.
257 GFileInfo *
258 g_file_output_stream_query_info_finish (GFileOutputStream *stream,
259 GAsyncResult *result,
260 GError **error)
262 GSimpleAsyncResult *simple;
263 GFileOutputStreamClass *class;
265 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
266 g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
268 if (G_IS_SIMPLE_ASYNC_RESULT (result))
270 simple = G_SIMPLE_ASYNC_RESULT (result);
271 if (g_simple_async_result_propagate_error (simple, error))
272 return NULL;
275 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
276 return class->query_info_finish (stream, result, error);
280 * g_file_output_stream_get_etag:
281 * @stream: a #GFileOutputStream.
283 * Gets the entity tag for the file when it has been written.
284 * This must be called after the stream has been written
285 * and closed, as the etag can change while writing.
287 * Returns: the entity tag for the stream.
289 char *
290 g_file_output_stream_get_etag (GFileOutputStream *stream)
292 GFileOutputStreamClass *class;
293 GOutputStream *output_stream;
294 char *etag;
296 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), NULL);
298 output_stream = G_OUTPUT_STREAM (stream);
300 if (!g_output_stream_is_closed (output_stream))
302 g_warning ("stream is not closed yet, can't get etag");
303 return NULL;
306 etag = NULL;
308 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
309 if (class->get_etag)
310 etag = class->get_etag (stream);
312 return etag;
315 static goffset
316 g_file_output_stream_tell (GFileOutputStream *stream)
318 GFileOutputStreamClass *class;
319 goffset offset;
321 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), 0);
323 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
325 offset = 0;
326 if (class->tell)
327 offset = class->tell (stream);
329 return offset;
332 static goffset
333 g_file_output_stream_seekable_tell (GSeekable *seekable)
335 return g_file_output_stream_tell (G_FILE_OUTPUT_STREAM (seekable));
338 static gboolean
339 g_file_output_stream_can_seek (GFileOutputStream *stream)
341 GFileOutputStreamClass *class;
342 gboolean can_seek;
344 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
346 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
348 can_seek = FALSE;
349 if (class->seek)
351 can_seek = TRUE;
352 if (class->can_seek)
353 can_seek = class->can_seek (stream);
356 return can_seek;
359 static gboolean
360 g_file_output_stream_seekable_can_seek (GSeekable *seekable)
362 return g_file_output_stream_can_seek (G_FILE_OUTPUT_STREAM (seekable));
365 static gboolean
366 g_file_output_stream_seek (GFileOutputStream *stream,
367 goffset offset,
368 GSeekType type,
369 GCancellable *cancellable,
370 GError **error)
372 GFileOutputStreamClass *class;
373 GOutputStream *output_stream;
374 gboolean res;
376 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
378 output_stream = G_OUTPUT_STREAM (stream);
379 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
381 if (!class->seek)
383 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
384 _("Seek not supported on stream"));
385 return FALSE;
388 if (!g_output_stream_set_pending (output_stream, error))
389 return FALSE;
391 if (cancellable)
392 g_cancellable_push_current (cancellable);
394 res = class->seek (stream, offset, type, cancellable, error);
396 if (cancellable)
397 g_cancellable_pop_current (cancellable);
399 g_output_stream_clear_pending (output_stream);
401 return res;
404 static gboolean
405 g_file_output_stream_seekable_seek (GSeekable *seekable,
406 goffset offset,
407 GSeekType type,
408 GCancellable *cancellable,
409 GError **error)
411 return g_file_output_stream_seek (G_FILE_OUTPUT_STREAM (seekable),
412 offset, type, cancellable, error);
415 static gboolean
416 g_file_output_stream_can_truncate (GFileOutputStream *stream)
418 GFileOutputStreamClass *class;
419 gboolean can_truncate;
421 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
423 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
425 can_truncate = FALSE;
426 if (class->truncate_fn)
428 can_truncate = TRUE;
429 if (class->can_truncate)
430 can_truncate = class->can_truncate (stream);
433 return can_truncate;
436 static gboolean
437 g_file_output_stream_seekable_can_truncate (GSeekable *seekable)
439 return g_file_output_stream_can_truncate (G_FILE_OUTPUT_STREAM (seekable));
442 static gboolean
443 g_file_output_stream_truncate (GFileOutputStream *stream,
444 goffset size,
445 GCancellable *cancellable,
446 GError **error)
448 GFileOutputStreamClass *class;
449 GOutputStream *output_stream;
450 gboolean res;
452 g_return_val_if_fail (G_IS_FILE_OUTPUT_STREAM (stream), FALSE);
454 output_stream = G_OUTPUT_STREAM (stream);
455 class = G_FILE_OUTPUT_STREAM_GET_CLASS (stream);
457 if (!class->truncate_fn)
459 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
460 _("Truncate not supported on stream"));
461 return FALSE;
464 if (!g_output_stream_set_pending (output_stream, error))
465 return FALSE;
467 if (cancellable)
468 g_cancellable_push_current (cancellable);
470 res = class->truncate_fn (stream, size, cancellable, error);
472 if (cancellable)
473 g_cancellable_pop_current (cancellable);
475 g_output_stream_clear_pending (output_stream);
477 return res;
480 static gboolean
481 g_file_output_stream_seekable_truncate (GSeekable *seekable,
482 goffset size,
483 GCancellable *cancellable,
484 GError **error)
486 return g_file_output_stream_truncate (G_FILE_OUTPUT_STREAM (seekable),
487 size, cancellable, error);
489 /********************************************
490 * Default implementation of async ops *
491 ********************************************/
493 typedef struct {
494 char *attributes;
495 GFileInfo *info;
496 } QueryInfoAsyncData;
498 static void
499 query_info_data_free (QueryInfoAsyncData *data)
501 if (data->info)
502 g_object_unref (data->info);
503 g_free (data->attributes);
504 g_free (data);
507 static void
508 query_info_async_thread (GSimpleAsyncResult *res,
509 GObject *object,
510 GCancellable *cancellable)
512 GFileOutputStreamClass *class;
513 GError *error = NULL;
514 QueryInfoAsyncData *data;
515 GFileInfo *info;
517 data = g_simple_async_result_get_op_res_gpointer (res);
519 info = NULL;
521 class = G_FILE_OUTPUT_STREAM_GET_CLASS (object);
522 if (class->query_info)
523 info = class->query_info (G_FILE_OUTPUT_STREAM (object), data->attributes, cancellable, &error);
524 else
525 g_set_error_literal (&error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
526 _("Stream doesn't support query_info"));
528 if (info == NULL)
530 g_simple_async_result_set_from_error (res, error);
531 g_error_free (error);
533 else
534 data->info = info;
537 static void
538 g_file_output_stream_real_query_info_async (GFileOutputStream *stream,
539 char *attributes,
540 int io_priority,
541 GCancellable *cancellable,
542 GAsyncReadyCallback callback,
543 gpointer user_data)
545 GSimpleAsyncResult *res;
546 QueryInfoAsyncData *data;
548 data = g_new0 (QueryInfoAsyncData, 1);
549 data->attributes = g_strdup (attributes);
551 res = g_simple_async_result_new (G_OBJECT (stream), callback, user_data, g_file_output_stream_real_query_info_async);
552 g_simple_async_result_set_op_res_gpointer (res, data, (GDestroyNotify)query_info_data_free);
554 g_simple_async_result_run_in_thread (res, query_info_async_thread, io_priority, cancellable);
555 g_object_unref (res);
558 static GFileInfo *
559 g_file_output_stream_real_query_info_finish (GFileOutputStream *stream,
560 GAsyncResult *res,
561 GError **error)
563 GSimpleAsyncResult *simple = G_SIMPLE_ASYNC_RESULT (res);
564 QueryInfoAsyncData *data;
566 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_file_output_stream_real_query_info_async);
568 data = g_simple_async_result_get_op_res_gpointer (simple);
569 if (data->info)
570 return g_object_ref (data->info);
572 return NULL;
575 #define __G_FILE_OUTPUT_STREAM_C__
576 #include "gioaliasdef.c"