static GTlsCertificateFlags g_tls_database_openssl_verify_chain (GTlsDatabase *database, GTlsCertificate *chain, const gchar *purpose, GSocketConnectable *identity, GTlsInteraction *interaction, GTlsDatabaseVerifyFlags flags, GCancellable *cancellable, GError **error) { GTlsDatabaseOpenssl *self = G_TLS_DATABASE_OPENSSL (database); GTlsDatabaseOpensslPrivate *priv; STACK_OF(X509) *certs; X509_STORE_CTX *csc; X509 *x; GTlsCertificateFlags result = 0; g_return_val_if_fail (G_IS_TLS_CERTIFICATE_OPENSSL (chain), G_TLS_CERTIFICATE_GENERIC_ERROR); priv = g_tls_database_openssl_get_instance_private (self); if (g_cancellable_set_error_if_cancelled (cancellable, error)) return G_TLS_CERTIFICATE_GENERIC_ERROR; certs = convert_certificate_chain_to_openssl (G_TLS_CERTIFICATE_OPENSSL (chain)); csc = X509_STORE_CTX_new (); x = g_tls_certificate_openssl_get_cert (G_TLS_CERTIFICATE_OPENSSL (chain)); if (!X509_STORE_CTX_init (csc, priv->store, x, certs)) { X509_STORE_CTX_free (csc); sk_X509_free (certs); return G_TLS_CERTIFICATE_GENERIC_ERROR; } if (X509_verify_cert (csc) <= 0) result = g_tls_certificate_openssl_convert_error (X509_STORE_CTX_get_error (csc)); X509_STORE_CTX_free (csc); sk_X509_free (certs); if (g_cancellable_set_error_if_cancelled (cancellable, error)) return G_TLS_CERTIFICATE_GENERIC_ERROR; /* We have to check these ourselves since openssl * does not give us flags and UNKNOWN_CA will take priority. */ result |= double_check_before_after_dates (G_TLS_CERTIFICATE_OPENSSL (chain)); if (identity) result |= g_tls_certificate_openssl_verify_identity (G_TLS_CERTIFICATE_OPENSSL (chain), identity); return result; }
static gboolean g_tls_database_openssl_initable_init (GInitable *initable, GCancellable *cancellable, GError **error) { GTlsDatabaseOpenssl *self = G_TLS_DATABASE_OPENSSL (initable); GTlsDatabaseOpensslPrivate *priv; X509_STORE *store; gboolean result = TRUE; priv = g_tls_database_openssl_get_instance_private (self); if (g_cancellable_set_error_if_cancelled (cancellable, error)) return FALSE; store = X509_STORE_new (); if (store == NULL) { g_set_error_literal (error, G_TLS_ERROR, G_TLS_ERROR_MISC, _("Could not create CA store")); result = FALSE; goto out; } g_assert (G_TLS_DATABASE_OPENSSL_GET_CLASS (self)->populate_trust_list); if (!G_TLS_DATABASE_OPENSSL_GET_CLASS (self)->populate_trust_list (self, store, error)) { result = FALSE; goto out; } if (g_cancellable_set_error_if_cancelled (cancellable, error)) result = FALSE; if (result) { g_mutex_lock (&priv->mutex); if (!priv->store) { priv->store = store; store = NULL; } g_mutex_unlock (&priv->mutex); } out: if (store != NULL) X509_STORE_free (store); return result; }
/* returns FALSE if cancelled, in which case the lock is not held */ gboolean e_mapi_cancellable_rec_mutex_lock (EMapiCancellableRecMutex *rec_mutex, GCancellable *cancellable, GError **error) { gulong handler_id; gboolean res = TRUE; g_return_val_if_fail (rec_mutex != NULL, FALSE); g_mutex_lock (&rec_mutex->cond_mutex); if (!cancellable) { g_mutex_unlock (&rec_mutex->cond_mutex); g_rec_mutex_lock (&rec_mutex->rec_mutex); return TRUE; } if (g_cancellable_is_cancelled (cancellable)) { if (error && !*error) { /* coverity[unchecked_value] */ g_cancellable_set_error_if_cancelled (cancellable, error); } g_mutex_unlock (&rec_mutex->cond_mutex); return FALSE; } handler_id = g_signal_connect (cancellable, "cancelled", G_CALLBACK (cancellable_rec_mutex_cancelled_cb), rec_mutex); while (!g_rec_mutex_trylock (&rec_mutex->rec_mutex)) { /* recheck once per 10 seconds, just in case */ g_cond_wait_until (&rec_mutex->cond, &rec_mutex->cond_mutex, g_get_monotonic_time () + (10 * G_TIME_SPAN_SECOND)); if (g_cancellable_is_cancelled (cancellable)) { if (error && !*error) g_cancellable_set_error_if_cancelled (cancellable, error); res = FALSE; break; } } g_signal_handler_disconnect (cancellable, handler_id); g_mutex_unlock (&rec_mutex->cond_mutex); return res; }
static void e_webdav_discover_content_trust_prompt_done_cb (GObject *source_object, GAsyncResult *result, gpointer user_data) { ETrustPromptResponse response = E_TRUST_PROMPT_RESPONSE_UNKNOWN; ESource *source; RefreshData *rd = user_data; GError *local_error = NULL; g_return_if_fail (E_IS_SOURCE (source_object)); g_return_if_fail (rd != NULL); source = E_SOURCE (source_object); if (!e_trust_prompt_run_for_source_finish (source, result, &response, &local_error)) { g_simple_async_result_take_error (rd->simple, local_error); local_error = NULL; g_simple_async_result_complete (rd->simple); refresh_data_free (rd); } else if (response == E_TRUST_PROMPT_RESPONSE_ACCEPT || response == E_TRUST_PROMPT_RESPONSE_ACCEPT_TEMPORARILY) { /* Use NULL credentials to reuse those from the last time. */ e_webdav_discover_sources (source, rd->base_url, E_WEBDAV_DISCOVER_SUPPORTS_NONE, rd->credentials, rd->cancellable, e_webdav_discover_content_refresh_done_cb, rd); } else { g_cancellable_cancel (rd->cancellable); g_cancellable_set_error_if_cancelled (rd->cancellable, &local_error); g_simple_async_result_take_error (rd->simple, local_error); local_error = NULL; g_simple_async_result_complete (rd->simple); refresh_data_free (rd); } g_clear_error (&local_error); }
static void send_async_finished (GInputStream *stream) { SoupInputStreamPrivate *priv = SOUP_INPUT_STREAM_GET_PRIVATE (stream); GSimpleAsyncResult *result; GError *error = NULL; if (!g_cancellable_set_error_if_cancelled (priv->cancellable, &error)) set_error_if_http_failed (priv->msg, &error); priv->got_headers_cb = NULL; priv->finished_cb = NULL; soup_input_stream_done_io (stream); result = priv->result; priv->result = NULL; g_simple_async_result_set_op_res_gboolean (result, error == NULL); if (error) { g_simple_async_result_set_from_error (result, error); g_error_free (error); } g_simple_async_result_complete (result); }
static void read_async_done (GInputStream *stream) { SoupInputStreamPrivate *priv = SOUP_INPUT_STREAM_GET_PRIVATE (stream); GSimpleAsyncResult *result; GError *error = NULL; result = priv->result; priv->result = NULL; if (g_cancellable_set_error_if_cancelled (priv->cancellable, &error) || set_error_if_http_failed (priv->msg, &error)) { g_simple_async_result_set_from_error (result, error); g_error_free (error); } else g_simple_async_result_set_op_res_gssize (result, priv->caller_nread); priv->got_chunk_cb = NULL; priv->finished_cb = NULL; priv->cancelled_cb = NULL; soup_input_stream_done_io (stream); g_simple_async_result_complete (result); g_object_unref (result); }
static gssize soup_input_stream_read (GInputStream *stream, void *buffer, gsize count, GCancellable *cancellable, GError **error) { SoupInputStreamPrivate *priv = SOUP_INPUT_STREAM_GET_PRIVATE (stream); if (priv->finished) return 0; /* If there is data leftover from a previous read, return it. */ if (priv->leftover_bufsize) return read_from_leftover (priv, buffer, count); /* No leftover data, accept one chunk from the network */ soup_input_stream_prepare_for_io (stream, cancellable, buffer, count); while (!priv->finished && priv->caller_nread == 0 && !g_cancellable_is_cancelled (cancellable)) g_main_context_iteration (priv->async_context, TRUE); soup_input_stream_done_io (stream); if (priv->caller_nread > 0) return priv->caller_nread; if (g_cancellable_set_error_if_cancelled (cancellable, error)) return -1; else if (set_error_if_http_failed (priv->msg, error)) return -1; else return 0; }
static gssize ostree_chain_input_stream_read (GInputStream *stream, void *buffer, gsize count, GCancellable *cancellable, GError **error) { OstreeChainInputStream *self = (OstreeChainInputStream*) stream; GInputStream *child; gssize res = -1; if (g_cancellable_set_error_if_cancelled (cancellable, error)) return -1; if (self->priv->index >= self->priv->streams->len) return 0; res = 0; while (res == 0 && self->priv->index < self->priv->streams->len) { child = self->priv->streams->pdata[self->priv->index]; res = g_input_stream_read (child, buffer, count, cancellable, error); if (res == 0) self->priv->index++; } return res; }
/** * gmw_probe_device_data_restore: **/ static gboolean gmw_probe_device_data_restore (GmwProbeDevice *dev, GCancellable *cancellable, GError **error) { GmwProbeBlock *item; guint i; for (i = 0; i < dev->data_save->len; i++) { item = g_ptr_array_index (dev->data_save, i); if (!item->valid) continue; item->bytes_wrote = gmw_probe_device_write (dev, item->address + item->offset, item->data_old, ONE_BLOCK); if (item->bytes_wrote != ONE_BLOCK) break; if (g_cancellable_set_error_if_cancelled (cancellable, error)) return FALSE; } return TRUE; }
static gssize g_local_file_output_stream_write (GOutputStream *stream, const void *buffer, gsize count, GCancellable *cancellable, GError **error) { GLocalFileOutputStream *file; gssize res; file = G_LOCAL_FILE_OUTPUT_STREAM (stream); while (1) { if (g_cancellable_set_error_if_cancelled (cancellable, error)) return -1; res = write (file->priv->fd, buffer, count); if (res == -1) { int errsv = errno; if (errsv == EINTR) continue; g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Error writing to file: %s"), g_strerror (errsv)); } break; } return res; }
static gboolean run_io_scheduler_job (GIOSchedulerJob *io_scheduler_job, GCancellable *cancellable, gpointer user_data) { StorageThreadedJob *job = STORAGE_THREADED_JOB (user_data); /* TODO: probably want to create a GMainContext dedicated to the thread */ g_assert (!job->job_result); g_assert_no_error (job->job_error); if (!g_cancellable_set_error_if_cancelled (cancellable, &job->job_error)) { job->job_result = job->job_func (cancellable, job->user_data, &job->job_error); } g_io_scheduler_job_send_to_mainloop (io_scheduler_job, job_complete, job, NULL); return FALSE; /* job is complete (or cancelled) */ }
/** * gnlp_client_get_available_languages: * @cancellable: (nullable): a #GCancellable * @error: (nullable): return location for a #GError, or %NULL * * Retrieves the list of available languages. * * Returns: (transfer container): a #GPtrArray that may be empty or not. * Free with @g_ptr_array_free. * * Since: 0.1.0 */ GPtrArray* gnlp_get_available_languages_sync (GCancellable *cancellable, GError **error) { GPtrArray *languages; gboolean cancelled; gchar **locales; guint i; cancelled = FALSE; languages = g_ptr_array_new_with_free_func (g_object_unref); locales = gnome_get_all_locales (); for (i = 0; locales[i]; i++) { if (g_cancellable_set_error_if_cancelled (cancellable, error)) { cancelled = TRUE; break; } g_ptr_array_add (languages, gnlp_language_new (locales[i])); } g_strfreev (locales); if (cancelled) g_clear_pointer (&languages, g_ptr_array_unref); return languages; }
/** * glnx_dirfd_iterator_next_dent: * @dfd_iter: A directory iterator * @out_dent: (out) (transfer none): Pointer to dirent; do not free * @cancellable: Cancellable * @error: Error * * Read the next value from @dfd_iter, causing @out_dent to be * updated. If end of stream is reached, @out_dent will be set * to %NULL, and %TRUE will be returned. */ gboolean glnx_dirfd_iterator_next_dent (GLnxDirFdIterator *dfd_iter, struct dirent **out_dent, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; GLnxRealDirfdIterator *real_dfd_iter = (GLnxRealDirfdIterator*) dfd_iter; g_return_val_if_fail (out_dent, FALSE); if (g_cancellable_set_error_if_cancelled (cancellable, error)) goto out; do { errno = 0; *out_dent = readdir (real_dfd_iter->d); if (*out_dent == NULL && errno != 0) { glnx_set_prefix_error_from_errno (error, "%s", "fdopendir"); goto out; } } while (*out_dent && (strcmp ((*out_dent)->d_name, ".") == 0 || strcmp ((*out_dent)->d_name, "..") == 0)); ret = TRUE; out: return ret; }
static void close_async_done (GOutputStream *stream) { SoupOutputStreamPrivate *priv = SOUP_OUTPUT_STREAM_GET_PRIVATE (stream); GSimpleAsyncResult *result; GError *error = NULL; result = priv->result; priv->result = NULL; if (g_cancellable_set_error_if_cancelled (priv->cancellable, &error) || set_error_if_http_failed (priv->msg, &error)) { g_simple_async_result_set_from_error (result, error); g_error_free (error); } else g_simple_async_result_set_op_res_gboolean (result, TRUE); priv->finished_cb = NULL; priv->cancelled_cb = NULL; soup_output_stream_done_io (stream); g_simple_async_result_complete (result); g_object_unref (result); }
/** * gs_file_read_noatime: * @file: a #GFile * @cancellable: a #GCancellable * @error: a #GError * * Like g_file_read(), but try to avoid updating the file's * access time. This should be used by background scanning * components such as search indexers, antivirus programs, etc. * * Returns: (transfer full): A new input stream, or %NULL on error */ GInputStream * gs_file_read_noatime (GFile *file, GCancellable *cancellable, GError **error) { const char *path = NULL; int fd; if (g_cancellable_set_error_if_cancelled (cancellable, error)) return NULL; path = gs_file_get_path_cached (file); if (path == NULL) { char *uri; uri = g_file_get_uri (file); g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_NOENT, "%s has no associated path", uri); g_free (uri); return NULL; } if (!gs_file_openat_noatime (AT_FDCWD, path, &fd, cancellable, error)) return NULL; return g_unix_input_stream_new (fd, TRUE); }
/** * gs_file_chmod: * @path: Path to file * @mode: UNIX mode * @cancellable: a #GCancellable * @error: a #GError * * Merely wraps UNIX chmod(). * * Returns: %TRUE on success, %FALSE on error */ gboolean gs_file_chmod (GFile *path, guint mode, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; int res; if (g_cancellable_set_error_if_cancelled (cancellable, error)) return FALSE; do res = chmod (gs_file_get_path_cached (path), mode); while (G_UNLIKELY (res != 0 && errno == EINTR)); if (res < 0) { gs_set_prefix_error_from_errno (error, errno, "chmod"); goto out; } ret = TRUE; out: return ret; }
static gboolean chown_internal (GFile *path, gboolean dereference_links, guint32 owner, guint32 group, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; int res; if (g_cancellable_set_error_if_cancelled (cancellable, error)) return FALSE; do if (dereference_links) res = chown (gs_file_get_path_cached (path), owner, group); else res = lchown (gs_file_get_path_cached (path), owner, group); while (G_UNLIKELY (res != 0 && errno == EINTR)); if (res < 0) { gs_set_prefix_error_from_errno (error, errno, "chown"); goto out; } ret = TRUE; out: return ret; }
gboolean xdg_cache_cache_write_thumbnail_info (const gchar *filename, const gchar *uri, guint64 mtime, GCancellable *cancellable, GError **error) { GdkPixbuf *pixbuf; GError *err = NULL; gchar *mtime_str; g_return_val_if_fail (filename != NULL, FALSE); g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); if (g_cancellable_set_error_if_cancelled (cancellable, error)) return FALSE; pixbuf = gdk_pixbuf_new_from_file (filename, &err); if (pixbuf != NULL) { if (!g_cancellable_set_error_if_cancelled (cancellable, &err)) { mtime_str = g_strdup_printf ("%" G_GUINT64_FORMAT, mtime); gdk_pixbuf_save (pixbuf, filename, "png", &err, "tEXt::Thumb::URI", uri, "tEXt::Thumb::MTime", mtime_str, NULL); g_free (mtime_str); } g_object_unref (pixbuf); } if (err != NULL) { g_propagate_error (error, err); return FALSE; } else { return TRUE; } }
/* * g_socket_condition_timed_wait: * * Provides g_socket_condition_timed_wait function for older * glib versions. It's a simplified version of the glib one * that should work with all glib version from glib-2.22. */ gboolean g_socket_condition_timed_wait(GSocket *socket,GIOCondition condition,gint64 timeout,GCancellable *cancellable,GError **error) { gint64 start_time; GPollFD poll_fd[2]; gint result; gint num; g_return_val_if_fail(G_IS_SOCKET(socket),FALSE); if(g_cancellable_set_error_if_cancelled(cancellable,error)) return FALSE; if(timeout != -1) timeout /= 1000; start_time = g_get_monotonic_time(); poll_fd[0].fd = g_socket_get_fd(socket); poll_fd[0].events = condition; num = 1; if(g_cancellable_make_pollfd(cancellable,&poll_fd[1])) num++; while(TRUE){ result = g_poll(poll_fd,num,timeout); if(result != -1 || errno != EINTR) break; if(timeout != -1){ timeout -= (g_get_monotonic_time () - start_time) * 1000; if(timeout < 0) timeout = 0; } } if(num > 1) g_cancellable_release_fd(cancellable); if(result == 0){ g_set_error_literal(error,G_IO_ERROR,G_IO_ERROR_TIMED_OUT, "Socket I/O timed out"); return FALSE; } return !g_cancellable_set_error_if_cancelled(cancellable,error); }
static gssize g_unix_output_stream_write (GOutputStream *stream, const void *buffer, gsize count, GCancellable *cancellable, GError **error) { GUnixOutputStream *unix_stream; gssize res; GPollFD poll_fds[2]; int poll_ret; unix_stream = G_UNIX_OUTPUT_STREAM (stream); if (g_cancellable_make_pollfd (cancellable, &poll_fds[1])) { poll_fds[0].fd = unix_stream->priv->fd; poll_fds[0].events = G_IO_OUT; do poll_ret = g_poll (poll_fds, 2, -1); while (poll_ret == -1 && errno == EINTR); g_cancellable_release_fd (cancellable); if (poll_ret == -1) { int errsv = errno; g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Error writing to unix: %s"), g_strerror (errsv)); return -1; } } while (1) { if (g_cancellable_set_error_if_cancelled (cancellable, error)) return -1; res = write (unix_stream->priv->fd, buffer, count); if (res == -1) { int errsv = errno; if (errsv == EINTR) continue; g_set_error (error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Error writing to unix: %s"), g_strerror (errsv)); } break; } return res; }
/** * gdata_access_handler_update_rule: * @self: a #GDataAccessHandler * @service: a #GDataService * @rule: the #GDataAccessRule to update * @cancellable: optional #GCancellable object, or %NULL * @error: a #GError, or %NULL * * Updates @rule in the access control list of the #GDataAccessHandler. * * The service will return an updated version of the rule, which is the return value of this function on success. * * If @cancellable is not %NULL, then the operation can be cancelled by triggering the @cancellable object from another thread. * If the operation was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. * * If there is an error updating the rule, a %GDATA_SERVICE_ERROR_WITH_UPDATE error will be returned. * * Return value: an updated #GDataAccessRule, or %NULL * * Since: 0.3.0 **/ GDataAccessRule * gdata_access_handler_update_rule (GDataAccessHandler *self, GDataService *service, GDataAccessRule *rule, GCancellable *cancellable, GError **error) { GDataServiceClass *klass; GDataAccessRule *updated_rule; SoupMessage *message; gchar *upload_data; guint status; g_return_val_if_fail (GDATA_IS_ENTRY (self), NULL); g_return_val_if_fail (GDATA_IS_SERVICE (service), NULL); g_return_val_if_fail (GDATA_IS_ACCESS_RULE (rule), NULL); message = get_soup_message (self, rule, SOUP_METHOD_PUT); /* Make sure subclasses set their headers */ klass = GDATA_SERVICE_GET_CLASS (service); if (klass->append_query_headers != NULL) klass->append_query_headers (service, message); /* Looks like ACLs don't support ETags */ /* Append the data */ upload_data = gdata_entry_get_xml (GDATA_ENTRY (rule)); soup_message_set_request (message, "application/atom+xml", SOUP_MEMORY_TAKE, upload_data, strlen (upload_data)); /* Send the message */ status = _gdata_service_send_message (service, message, error); if (status == SOUP_STATUS_NONE) { g_object_unref (message); return NULL; } /* Check for cancellation */ if (g_cancellable_set_error_if_cancelled (cancellable, error) == TRUE) { g_object_unref (message); return NULL; } if (status != 200) { /* Error */ g_assert (klass->parse_error_response != NULL); klass->parse_error_response (service, GDATA_SERVICE_ERROR_WITH_UPDATE, status, message->reason_phrase, message->response_body->data, message->response_body->length, error); g_object_unref (message); return NULL; } /* Build the updated entry */ g_assert (message->response_body->data != NULL); /* Parse the XML; create and return a new GDataEntry of the same type as @entry */ updated_rule = GDATA_ACCESS_RULE (_gdata_entry_new_from_xml (G_OBJECT_TYPE (rule), message->response_body->data, message->response_body->length, error)); g_object_unref (message); return updated_rule; }
static void deactivate_step (DeactivateContext *ctx) { NMModemPrivate *priv = NM_MODEM_GET_PRIVATE (ctx->self); GError *error = NULL; /* Check cancellable in each step */ if (g_cancellable_set_error_if_cancelled (ctx->cancellable, &error)) { g_simple_async_result_take_error (ctx->result, error); deactivate_context_complete (ctx); return; } switch (ctx->step) { case DEACTIVATE_CONTEXT_STEP_FIRST: ctx->step++; /* Fall down */ case DEACTIVATE_CONTEXT_STEP_CLEANUP: /* Make sure we keep a ref to the PPP manager if there is one */ if (priv->ppp_manager) ctx->ppp_manager = g_object_ref (priv->ppp_manager); /* Run cleanup */ NM_MODEM_GET_CLASS (ctx->self)->deactivate_cleanup (ctx->self, ctx->device); ctx->step++; /* Fall down */ case DEACTIVATE_CONTEXT_STEP_PPP_MANAGER_STOP: /* If we have a PPP manager, stop it */ if (ctx->ppp_manager) { nm_ppp_manager_stop (ctx->ppp_manager, ctx->cancellable, (GAsyncReadyCallback) ppp_manager_stop_ready, ctx); return; } ctx->step++; /* Fall down */ case DEACTIVATE_CONTEXT_STEP_MM_DISCONNECT: /* Disconnect asynchronously */ NM_MODEM_GET_CLASS (ctx->self)->disconnect (ctx->self, FALSE, ctx->cancellable, (GAsyncReadyCallback) disconnect_ready, ctx); return; case DEACTIVATE_CONTEXT_STEP_LAST: nm_log_dbg (LOGD_MB, "(%s): modem deactivation finished", nm_modem_get_uid (ctx->self)); deactivate_context_complete (ctx); return; } g_assert_not_reached (); }
/** * gdata_access_handler_delete_rule: * @self: a #GDataAccessHandler * @service: a #GDataService * @rule: the #GDataAccessRule to delete * @cancellable: optional #GCancellable object, or %NULL * @error: a #GError, or %NULL * * Deletes @rule from the access control list of the #GDataAccessHandler. * * If @cancellable is not %NULL, then the operation can be cancelled by triggering the @cancellable object from another thread. * If the operation was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. * * If there is an error deleting the rule, a %GDATA_SERVICE_ERROR_WITH_DELETION error will be returned, unless the @rule was the owner's * rule; in which case, %GDATA_SERVICE_ERROR_FORBIDDEN will be returned without any network activity. * * Return value: %TRUE on success, %FALSE otherwise * * Since: 0.3.0 **/ gboolean gdata_access_handler_delete_rule (GDataAccessHandler *self, GDataService *service, GDataAccessRule *rule, GCancellable *cancellable, GError **error) { GDataServiceClass *klass; GDataAccessHandlerIface *iface; SoupMessage *message; guint status; g_return_val_if_fail (GDATA_IS_ENTRY (self), FALSE); g_return_val_if_fail (GDATA_IS_SERVICE (service), FALSE); g_return_val_if_fail (GDATA_IS_ACCESS_RULE (rule), FALSE); /* The owner of the access handler can't be deleted */ iface = GDATA_ACCESS_HANDLER_GET_IFACE (self); g_assert (iface->is_owner_rule != NULL); if (iface->is_owner_rule (rule) == TRUE) { g_set_error_literal (error, GDATA_SERVICE_ERROR, GDATA_SERVICE_ERROR_FORBIDDEN, _("The owner's rule may not be deleted.")); return FALSE; } message = get_soup_message (self, rule, SOUP_METHOD_DELETE); /* Make sure subclasses set their headers */ klass = GDATA_SERVICE_GET_CLASS (service); if (klass->append_query_headers != NULL) klass->append_query_headers (service, message); /* Looks like ACLs don't support ETags */ /* Send the message */ status = _gdata_service_send_message (service, message, error); if (status == SOUP_STATUS_NONE) { g_object_unref (message); return FALSE; } /* Check for cancellation */ if (g_cancellable_set_error_if_cancelled (cancellable, error) == TRUE) { g_object_unref (message); return FALSE; } if (status != 200) { /* Error */ g_assert (klass->parse_error_response != NULL); klass->parse_error_response (service, GDATA_SERVICE_ERROR_WITH_DELETION, status, message->reason_phrase, message->response_body->data, message->response_body->length, error); g_object_unref (message); return FALSE; } g_object_unref (message); return TRUE; }
/** * gdata_access_handler_get_rules: * @self: a #GDataAccessHandler * @service: a #GDataService * @cancellable: optional #GCancellable object, or %NULL * @progress_callback: a #GDataQueryProgressCallback to call when a rule is loaded, or %NULL * @progress_user_data: data to pass to the @progress_callback function * @error: a #GError, or %NULL * * Retrieves a #GDataFeed containing all the access rules which apply to the given #GDataAccessHandler. Only the owner of a #GDataAccessHandler may * view its rule feed. * * If @cancellable is not %NULL, then the operation can be cancelled by triggering the @cancellable object from another thread. * If the operation was cancelled, the error %G_IO_ERROR_CANCELLED will be returned. * * A %GDATA_SERVICE_ERROR_WITH_QUERY will be returned if the server indicates there is a problem with the query. * * For each rule in the response feed, @progress_callback will be called in the main thread. If there was an error parsing the XML response, * a #GDataParserError will be returned. * * Return value: a #GDataFeed of access control rules, or %NULL; unref with g_object_unref() * * Since: 0.3.0 **/ GDataFeed * gdata_access_handler_get_rules (GDataAccessHandler *self, GDataService *service, GCancellable *cancellable, GDataQueryProgressCallback progress_callback, gpointer progress_user_data, GError **error) { GDataServiceClass *klass; GDataFeed *feed; GDataLink *link; SoupMessage *message; guint status; /* TODO: async version */ g_return_val_if_fail (GDATA_IS_ENTRY (self), NULL); g_return_val_if_fail (GDATA_IS_SERVICE (service), NULL); /* Get the ACL URI */ link = gdata_entry_look_up_link (GDATA_ENTRY (self), "http://schemas.google.com/acl/2007#accessControlList"); g_assert (link != NULL); message = soup_message_new (SOUP_METHOD_GET, link->href); /* Make sure subclasses set their headers */ klass = GDATA_SERVICE_GET_CLASS (service); if (klass->append_query_headers != NULL) klass->append_query_headers (service, message); /* Send the message */ status = _gdata_service_send_message (service, message, error); if (status == SOUP_STATUS_NONE) { g_object_unref (message); return NULL; } /* Check for cancellation */ if (g_cancellable_set_error_if_cancelled (cancellable, error) == TRUE) { g_object_unref (message); return NULL; } if (status != 200) { /* Error */ g_assert (klass->parse_error_response != NULL); klass->parse_error_response (service, GDATA_SERVICE_ERROR_WITH_QUERY, status, message->reason_phrase, message->response_body->data, message->response_body->length, error); g_object_unref (message); return NULL; } g_assert (message->response_body->data != NULL); feed = _gdata_feed_new_from_xml (GDATA_TYPE_FEED, message->response_body->data, message->response_body->length, GDATA_TYPE_ACCESS_RULE, progress_callback, progress_user_data, error); g_object_unref (message); return feed; }
static gboolean fetch_uri_contents_utf8_sync (OtPullData *pull_data, SoupURI *uri, char **out_contents, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; const guint8 nulchar = 0; gs_free char *ret_contents = NULL; gs_unref_object GMemoryOutputStream *buf = NULL; OstreeFetchUriSyncData fetch_data = { 0, }; if (g_cancellable_set_error_if_cancelled (cancellable, error)) return FALSE; fetch_data.pull_data = pull_data; pull_data->fetching_sync_uri = uri; ostree_fetcher_stream_uri_async (pull_data->fetcher, uri, cancellable, fetch_uri_sync_on_complete, &fetch_data); run_mainloop_monitor_fetcher (pull_data); if (!fetch_data.result_stream) goto out; buf = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free); if (g_output_stream_splice ((GOutputStream*)buf, fetch_data.result_stream, G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE, cancellable, error) < 0) goto out; /* Add trailing NUL */ if (!g_output_stream_write ((GOutputStream*)buf, &nulchar, 1, cancellable, error)) goto out; if (!g_output_stream_close ((GOutputStream*)buf, cancellable, error)) goto out; ret_contents = g_memory_output_stream_steal_data (buf); if (!g_utf8_validate (ret_contents, -1, NULL)) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Invalid UTF-8"); goto out; } ret = TRUE; ot_transfer_out_value (out_contents, &ret_contents); out: g_clear_object (&(fetch_data.result_stream)); return ret; }
static gboolean stop_context_complete_if_cancelled (StopContext *ctx) { GError *error = NULL; if (g_cancellable_set_error_if_cancelled (ctx->cancellable, &error)) { g_simple_async_result_take_error (ctx->result, error); stop_context_complete (ctx); return TRUE; } return FALSE; }
/* called in the thread where @cancellable was cancelled */ static void on_cancelled (GCancellable *cancellable, gpointer user_data) { UlSpawnedJob *self = UL_SPAWNED_JOB (user_data); GError *error; error = NULL; g_warn_if_fail (g_cancellable_set_error_if_cancelled (cancellable, &error)); emit_completed_with_error_in_idle (self, error); g_error_free (error); }
static void mail_printer_load_status_cb (WebKitWebView *web_view, GParamSpec *pspec, GSimpleAsyncResult *simple) { AsyncContext *async_context; WebKitLoadStatus load_status; GCancellable *cancellable; GError *error = NULL; /* Note: we disregard WEBKIT_LOAD_FAILED and print what we can. */ load_status = webkit_web_view_get_load_status (web_view); if (load_status != WEBKIT_LOAD_FINISHED) return; /* Signal handlers are holding the only GSimpleAsyncResult * references. This is to avoid finalizing it prematurely. */ g_object_ref (simple); async_context = g_simple_async_result_get_op_res_gpointer (simple); cancellable = async_context->cancellable; /* WebKit reloads the page once more right before starting to print, * so disconnect this handler after the first time to avoid starting * another print operation. */ g_signal_handler_disconnect ( async_context->web_view, async_context->load_status_handler_id); async_context->load_status_handler_id = 0; /* Check if we've been cancelled. */ if (g_cancellable_set_error_if_cancelled (cancellable, &error)) { g_simple_async_result_take_error (simple, error); g_simple_async_result_complete_in_idle (simple); /* Give WebKit some time to perform layouting and rendering before * we start printing. 500ms should be enough in most cases. */ } else { GSource *timeout_source; timeout_source = g_timeout_source_new (500); g_source_set_callback ( timeout_source, mail_printer_print_timeout_cb, g_object_ref (simple), (GDestroyNotify) g_object_unref); g_source_attach ( timeout_source, async_context->main_context); g_source_unref (timeout_source); } g_object_unref (simple); }
static gboolean read_async_cb (ReadAsyncData *data, GIOCondition condition, int fd) { GSimpleAsyncResult *simple; GError *error = NULL; gssize count_read; /* We know that we can read from fd once without blocking */ while (1) { if (g_cancellable_set_error_if_cancelled (data->cancellable, &error)) { count_read = -1; break; } count_read = read (data->stream->priv->fd, data->buffer, data->count); if (count_read == -1) { int errsv = errno; if (errsv == EINTR) continue; g_set_error (&error, G_IO_ERROR, g_io_error_from_errno (errsv), _("Error reading from unix: %s"), g_strerror (errsv)); } break; } simple = g_simple_async_result_new (G_OBJECT (data->stream), data->callback, data->user_data, g_unix_input_stream_read_async); g_simple_async_result_set_op_res_gssize (simple, count_read); if (count_read == -1) { g_simple_async_result_set_from_error (simple, error); g_error_free (error); } /* Complete immediately, not in idle, since we're already in a mainloop callout */ g_simple_async_result_complete (simple); g_object_unref (simple); return FALSE; }
/** * json_parser_load_from_stream: * @parser: a #JsonParser * @stream: an open #GInputStream * @cancellable: (allow-none): a #GCancellable, or %NULL * @error: the return location for a #GError, or %NULL * * Loads the contents of an input stream and parses them. * * If @cancellable is not %NULL, then the operation can be cancelled by * triggering the @cancellable object from another thread. If the * operation was cancelled, the error %G_IO_ERROR_CANCELLED will be set * on the passed @error. * * Return value: %TRUE if the data stream was successfully read and * parsed, and %FALSE otherwise * * Since: 0.12 */ gboolean json_parser_load_from_stream (JsonParser *parser, GInputStream *stream, GCancellable *cancellable, GError **error) { GByteArray *content; gsize pos; gssize res; gboolean retval = FALSE; GError *internal_error; g_return_val_if_fail (JSON_IS_PARSER (parser), FALSE); g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE); g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), FALSE); if (g_cancellable_set_error_if_cancelled (cancellable, error)) return FALSE; content = g_byte_array_new (); pos = 0; g_byte_array_set_size (content, pos + GET_DATA_BLOCK_SIZE + 1); while ((res = g_input_stream_read (stream, content->data + pos, GET_DATA_BLOCK_SIZE, cancellable, error)) > 0) { pos += res; g_byte_array_set_size (content, pos + GET_DATA_BLOCK_SIZE + 1); } if (res < 0) { /* error has already been set */ retval = FALSE; goto out; } /* zero-terminate the content; we allocated an extra byte for this */ content->data[pos] = 0; internal_error = NULL; retval = json_parser_load (parser, (const gchar *) content->data, content->len, &internal_error); if (internal_error != NULL) g_propagate_error (error, internal_error); out: g_byte_array_free (content, TRUE); return retval; }