static gboolean do_hash_job (GIOSchedulerJob *job, GCancellable *cancellable, gpointer user_data) { HashingData *hash_data = user_data; gssize bytes_read; GError *error = NULL; again: if (hash_data->buffer == NULL) hash_data->buffer = g_malloc0 (BUFFER_SIZE); bytes_read = g_input_stream_read (hash_data->stream, hash_data->buffer, BUFFER_SIZE, cancellable, &error); if (error != NULL) goto out; hash_data->total_read += bytes_read; /* we now have the chunk */ if (bytes_read > 0) { g_checksum_update (hash_data->checksum, hash_data->buffer, bytes_read); #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" g_io_scheduler_job_send_to_mainloop_async (job, emit_hashing_progress, hash_data, NULL); #pragma GCC diagnostic pop g_free (hash_data->buffer); hash_data->buffer = NULL; goto again; } else { g_input_stream_close (hash_data->stream, cancellable, &error); } out: if (error != NULL) hash_data->error = error; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done, hash_data, NULL); #pragma GCC diagnostic pop return FALSE; }
static gboolean do_hash_job_incoming (GIOSchedulerJob *job, GCancellable *cancellable, gpointer user_data) { HashingData *hash_data = user_data; EmpathyFTHandler *handler = hash_data->handler; EmpathyFTHandlerPriv *priv = GET_PRIV (handler); GError *error = NULL; DEBUG ("checking integrity for incoming handler"); /* need to get the stream first */ hash_data->stream = G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error)); if (error != NULL) { hash_data->error = error; g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done, hash_data, NULL); return FALSE; } return do_hash_job (job, cancellable, user_data); }
static gboolean trash_empty_job (GIOSchedulerJob *job, GCancellable *cancellable, gpointer user_data) { gsize deleted; GFile *trash; trash = g_file_new_for_uri ("trash:///"); /* first do a dry run to count the number of files */ deleted = 0; trash_empty_delete_contents (job, cancellable, trash, FALSE, &deleted); trash_empty_total_files = deleted; /* now do the real thing */ deleted = 0; trash_empty_delete_contents (job, cancellable, trash, TRUE, &deleted); /* done */ g_object_unref (trash); g_io_scheduler_job_send_to_mainloop_async (job, trash_empty_done, NULL, NULL); return FALSE; }
static gboolean do_hash_job_incoming (GIOSchedulerJob *job, GCancellable *cancellable, gpointer user_data) { HashingData *hash_data = user_data; EmpathyFTHandler *handler = hash_data->handler; EmpathyFTHandlerPriv *priv = handler->priv; GError *error = NULL; DEBUG ("checking integrity for incoming handler"); /* need to get the stream first */ hash_data->stream = G_INPUT_STREAM (g_file_read (priv->gfile, cancellable, &error)); if (error != NULL) { hash_data->error = error; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdeprecated-declarations" g_io_scheduler_job_send_to_mainloop_async (job, hash_job_done, hash_data, NULL); #pragma GCC diagnostic pop return FALSE; } return do_hash_job (job, cancellable, user_data); }
static gboolean do_read_new_lines (GIOSchedulerJob *io_job, GCancellable *cancellable, gpointer user_data) { /* this runs in a separate thread */ NewLinesJob *job = user_data; LogviewLog *log = job->log; char *line; GError *err = NULL; GPtrArray *lines; g_assert (LOGVIEW_IS_LOG (log)); g_assert (log->priv->stream != NULL); if (!log->priv->lines) { log->priv->lines = g_ptr_array_new (); g_ptr_array_add (log->priv->lines, NULL); } lines = log->priv->lines; /* remove the NULL-terminator */ g_ptr_array_remove_index (lines, lines->len - 1); while ((line = g_data_input_stream_read_line (log->priv->stream, NULL, NULL, &err)) != NULL) { g_ptr_array_add (lines, (gpointer) line); } if (err) { job->err = err; goto out; } /* NULL-terminate the array again */ g_ptr_array_add (lines, NULL); log->priv->has_new_lines = FALSE; /* we'll return only the new lines in the callback */ line = g_ptr_array_index (lines, log->priv->lines_no); job->lines = (const char **) lines->pdata + log->priv->lines_no; /* save the new number of days and lines */ job->new_days = add_new_days_to_cache (log, job->lines, log->priv->lines_no); log->priv->lines_no = (lines->len - 1); out: g_io_scheduler_job_send_to_mainloop_async (io_job, new_lines_job_done, job, NULL); return FALSE; }
static VALUE rg_send_to_mainloop_async(VALUE self) { VALUE block; block = rb_block_proc(); SAVE_BLOCK(block); g_io_scheduler_job_send_to_mainloop_async(_SELF(self), ioscheduler_source_callback, (gpointer)block, ioscheduler_source_callback_free); return self; }
static gboolean start_job (GIOSchedulerJob *job, GCancellable *cancellable, gpointer user_data) { if (backup_op) backup (bk_file, cancellable); else if (restore_op) restore (res_file, cancellable); else if (check_op) check (chk_file, NULL); /* not cancellable */ g_io_scheduler_job_send_to_mainloop_async ( job, finish_job, NULL, (GDestroyNotify) NULL); return FALSE; }
static gboolean _empty_trash_job (GIOSchedulerJob *io_job, GCancellable* cancellable, gpointer user_data) { GList* trash_list = (GList*) user_data; GList* l; for (l = trash_list; l != NULL; l = l->next) _delete_trash_file (l->data, FALSE, TRUE); g_io_scheduler_job_send_to_mainloop_async (io_job, _empty_trash_job_done, user_data, NULL); return FALSE; }
/* =============== worker thread code begins here =============== */ static void trash_empty_maybe_schedule_update (GIOSchedulerJob *job, GFile *file, gsize deleted) { if (!trash_empty_update_pending) { g_assert (trash_empty_current_file == NULL); trash_empty_current_file = g_object_ref (file); trash_empty_deleted_files = deleted; trash_empty_update_pending = TRUE; g_io_scheduler_job_send_to_mainloop_async (job, trash_empty_update_dialog, NULL, NULL); } }
static gboolean font_load_job (GIOSchedulerJob *sched_job, GCancellable *cancellable, gpointer user_data) { FontLoadJob *job = user_data; GError *error = NULL; font_load_job_do_load (job, &error); if (error != NULL) g_simple_async_result_take_error (job->result, error); g_io_scheduler_job_send_to_mainloop_async (sched_job, font_load_job_callback, job, NULL); return FALSE; }
static gboolean build_gnutls_ca_and_crl_lists(GIOSchedulerJob *job, GCancellable *cancellable, gpointer user_data) { HevImpathyTLSVerifier *self = HEV_IMPATHY_TLS_VERIFIER(user_data); HevImpathyTLSVerifierPrivate *priv = HEV_IMPATHY_TLS_VERIFIER_GET_PRIVATE(self); gint idx = 0; gchar *user_certs_dir = NULL; GDir *dir = NULL; GError *error = NULL; g_debug("%s:%d[%s]", __FILE__, __LINE__, __FUNCTION__); priv->trusted_ca_list = g_ptr_array_new_with_free_func( (GDestroyNotify)gnutls_x509_crt_deinit); for(idx=0; idx<(gint)G_N_ELEMENTS(system_ca_paths)-1; idx++) { const gchar *path = NULL; gchar *contents = NULL; gsize length = 0; gint res = 0, n_certs = 0; gnutls_x509_crt_t *cert_list = NULL; gnutls_datum_t datum = { NULL, 0 }; gnutls_x509_crt_fmt_t format = 0; path = system_ca_paths[idx]; g_file_get_contents(path, &contents, &length, &error); if(NULL != error) { g_clear_error(&error); continue; } datum.data = (guchar *)contents; datum.size = length; n_certs = get_number_and_type_of_certificates(&datum, &format); if(0 > n_certs) { g_free(contents); continue; } cert_list = g_malloc0(sizeof(gnutls_x509_crt_t) * n_certs); res = gnutls_x509_crt_list_import(cert_list, (guint *)&n_certs, &datum, format, 0); if(0 > res) { g_free(contents); continue; } for(idx=0; idx<n_certs; idx++) g_ptr_array_add(priv->trusted_ca_list, cert_list[idx]); g_free(contents); g_free(cert_list); } /* user certs */ user_certs_dir = g_build_filename(g_get_user_config_dir(), "telepathy", "certs", NULL); dir = g_dir_open(user_certs_dir, 0, &error); if(NULL == error) { const gchar *cert_name = NULL; while(NULL != (cert_name=g_dir_read_name(dir))) { gchar *contents = NULL, *cert_path = NULL; gsize length = 0; gint res = 0; gnutls_datum_t datum = { NULL, 0 }; gnutls_x509_crt_t cert = 0; cert_path = g_build_filename(user_certs_dir, cert_name, NULL); g_file_get_contents(cert_path, &contents, &length, &error); if(NULL != error) { g_clear_error(&error); g_free(cert_path); continue; } datum.data = (guchar *)contents; datum.size = length; gnutls_x509_crt_init(&cert); res = gnutls_x509_crt_import(cert, &datum, GNUTLS_X509_FMT_PEM); if(GNUTLS_E_SUCCESS == res) g_ptr_array_add(priv->trusted_ca_list, cert); g_free(contents); g_free(cert_path); } g_dir_close(dir); } else { g_error_free(error); } g_free(user_certs_dir); g_io_scheduler_job_send_to_mainloop_async(job, start_verification, self, NULL); return FALSE; }
static gboolean try_check_file (GIOSchedulerJob *io_job, GCancellable *cancellable, gpointer data) { AsyncExistenceJob *job = data; GFile *file; GFileInfo *info; GError *error; char *uri; retry: error = NULL; uri = build_uri (job); file = g_file_new_for_uri (uri); info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, G_FILE_QUERY_INFO_NONE, cancellable, &error); if (info != NULL) { /* file already exists, iterate again */ g_object_unref (info); g_object_unref (file); g_free (uri); (job->iteration)++; goto retry; } else { /* see the error to check whether the location is not accessible * or the file does not exist. */ if (error->code == G_IO_ERROR_NOT_FOUND) { GFile *parent; /* if the parent directory doesn't exist as well, forget the saved * directory and treat this as a generic error. */ parent = g_file_get_parent (file); if (!g_file_query_exists (parent, NULL)) { (job->type)++; job->iteration = 0; g_object_unref (file); g_object_unref (parent); goto retry; } else { job->retval = uri; g_object_unref (parent); goto out; } } else { /* another kind of error, assume this location is not * accessible. */ g_free (uri); if (job->type == TEST_TMP) { job->retval = NULL; goto out; } else { (job->type)++; job->iteration = 0; g_error_free (error); g_object_unref (file); goto retry; } } } out: g_error_free (error); g_object_unref (file); g_io_scheduler_job_send_to_mainloop_async (io_job, check_file_done, job, NULL); return FALSE; }
static gboolean ensure_thumbnail_job (GIOSchedulerJob *job, GCancellable *cancellable, gpointer user_data) { LoadThumbnailData *data = user_data; gboolean thumb_failed; const gchar *thumb_path; GError *error = NULL; GFile *thumb_file = NULL; GFileInputStream *is = NULL; GFileInfo *info = NULL; info = g_file_query_info (data->font_file, ATTRIBUTES_FOR_EXISTING_THUMBNAIL, G_FILE_QUERY_INFO_NONE, NULL, &error); if (error != NULL) { g_debug ("Can't query info for file %s: %s\n", data->font_path, error->message); goto out; } thumb_failed = g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED); if (thumb_failed) goto out; thumb_path = g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH); if (thumb_path != NULL) { thumb_file = g_file_new_for_path (thumb_path); is = g_file_read (thumb_file, NULL, &error); if (error != NULL) { g_debug ("Can't read file %s: %s\n", thumb_path, error->message); goto out; } data->pixbuf = gdk_pixbuf_new_from_stream_at_scale (G_INPUT_STREAM (is), 128, 128, TRUE, NULL, &error); if (error != NULL) { g_debug ("Can't read thumbnail pixbuf %s: %s\n", thumb_path, error->message); goto out; } } else { data->pixbuf = create_thumbnail (data); } out: g_clear_error (&error); g_clear_object (&is); g_clear_object (&thumb_file); g_clear_object (&info); g_io_scheduler_job_send_to_mainloop_async (job, ensure_thumbnail_job_done, data, NULL); return FALSE; }
static gboolean fetch_uri_job (GIOSchedulerJob *sched_job, GCancellable *cancellable, gpointer user_data) { FetchUriJob *job = user_data; Mb5Metadata metadata; Mb5Query query; Mb5Release release; Mb5ReleaseList release_list; gchar *retval = NULL; gchar **param_names = NULL; gchar **param_values = NULL; query = mb5_query_new ("sushi", NULL, 0); param_names = g_new (gchar*, 3); param_values = g_new (gchar*, 3); param_names[0] = g_strdup ("query"); param_values[0] = g_strdup_printf ("artist:\"%s\" AND release:\"%s\"", job->artist, job->album); param_names[1] = g_strdup ("limit"); param_values[1] = g_strdup ("10"); param_names[2] = NULL; param_values[2] = NULL; metadata = mb5_query_query (query, "release", "", "", 2, param_names, param_values); mb5_query_delete (query); if (metadata) { release_list = mb5_metadata_get_releaselist (metadata); int i; int release_list_length = mb5_release_list_size (release_list); for (i = 0; i < release_list_length; i++) { gchar asin[255]; release = mb5_release_list_item (release_list, i); mb5_release_get_asin (release, asin, 255); if (asin != NULL && asin[0] != '\0') { retval = g_strdup (asin); break; } } } mb5_metadata_delete (metadata); if (retval == NULL) { /* FIXME: do we need a better error? */ g_simple_async_result_set_error (job->result, G_IO_ERROR, 0, "%s", "Error getting the ASIN from MusicBrainz"); } else { g_simple_async_result_set_op_res_gpointer (job->result, retval, NULL); } g_io_scheduler_job_send_to_mainloop_async (sched_job, fetch_uri_job_callback, job, NULL); g_strfreev (param_names); g_strfreev (param_values); return FALSE; }
static gboolean log_load (GIOSchedulerJob *io_job, GCancellable *cancellable, gpointer user_data) { /* this runs in a separate i/o thread */ LoadJob *job = user_data; LogviewLog *log = job->log; GFile *f = log->priv->file; GFileInfo *info; GInputStream *is; const char *peeked_buffer; const char * parse_data[2]; GSList *days; const char *content_type; GFileType type; GError *err = NULL; GTimeVal timeval; gboolean is_archive, can_read; info = g_file_query_info (f, G_FILE_ATTRIBUTE_ACCESS_CAN_READ "," G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE "," G_FILE_ATTRIBUTE_STANDARD_DISPLAY_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE "," G_FILE_ATTRIBUTE_STANDARD_SIZE "," G_FILE_ATTRIBUTE_TIME_MODIFIED ",", 0, NULL, &err); if (err) { if (err->code == G_IO_ERROR_PERMISSION_DENIED) { /* TODO: PolicyKit integration */ } goto out; } can_read = g_file_info_get_attribute_boolean (info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ); if (!can_read) { /* TODO: PolicyKit integration */ err = g_error_new_literal (LOGVIEW_ERROR_QUARK, LOGVIEW_ERROR_PERMISSION_DENIED, _("You don't have enough permissions to read the file.")); g_object_unref (info); goto out; } type = g_file_info_get_file_type (info); content_type = g_file_info_get_content_type (info); is_archive = g_content_type_equals (content_type, "application/x-gzip"); if (type != (G_FILE_TYPE_REGULAR || G_FILE_TYPE_SYMBOLIC_LINK) || (!g_content_type_is_a (content_type, "text/plain") && !is_archive)) { err = g_error_new_literal (LOGVIEW_ERROR_QUARK, LOGVIEW_ERROR_NOT_A_LOG, _("The file is not a regular file or is not a text file.")); g_object_unref (info); goto out; } log->priv->file_size = g_file_info_get_size (info); g_file_info_get_modification_time (info, &timeval); log->priv->file_time = timeval.tv_sec; log->priv->display_name = g_strdup (g_file_info_get_display_name (info)); g_object_unref (info); /* initialize the stream */ is = G_INPUT_STREAM (g_file_read (f, NULL, &err)); if (err) { if (err->code == G_IO_ERROR_PERMISSION_DENIED) { /* TODO: PolicyKit integration */ } goto out; } if (is_archive) { #ifdef HAVE_ZLIB GZHandle *gz; gboolean res; guchar * buffer; gsize bytes_read; GInputStream *real_is; time_t mtime; /* seconds */ /* this also skips the header from |is| */ res = read_gzip_header (is, &mtime); if (!res) { g_object_unref (is); err = create_zlib_error (); goto out; } log->priv->file_time = mtime; gz = gz_handle_new (f, is); res = gz_handle_init (gz); if (!res) { g_object_unref (is); gz_handle_free (gz); err = create_zlib_error (); goto out; } real_is = g_memory_input_stream_new (); do { buffer = g_malloc (1024); res = gz_handle_read (gz, buffer, 1024, &bytes_read); g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (real_is), buffer, bytes_read, g_free); } while (res == TRUE && bytes_read > 0); if (!res) { gz_handle_free (gz); g_object_unref (real_is); g_object_unref (is); err = create_zlib_error (); goto out; } g_object_unref (is); is = real_is; gz_handle_free (gz); #else /* HAVE_ZLIB */ g_object_unref (is); err = g_error_new_literal (LOGVIEW_ERROR_QUARK, LOGVIEW_ERROR_NOT_SUPPORTED, _("This version of System Log does not support GZipped logs.")); goto out; #endif /* HAVE_ZLIB */ } log->priv->stream = g_data_input_stream_new (is); /* sniff into the stream for a timestamped line */ g_buffered_input_stream_fill (G_BUFFERED_INPUT_STREAM (log->priv->stream), (gssize) g_buffered_input_stream_get_buffer_size (G_BUFFERED_INPUT_STREAM (log->priv->stream)), NULL, &err); if (err == NULL) { peeked_buffer = g_buffered_input_stream_peek_buffer (G_BUFFERED_INPUT_STREAM (log->priv->stream), NULL); parse_data[0] = peeked_buffer; parse_data[1] = NULL; if ((days = log_read_dates (parse_data, time (NULL))) != NULL) { log->priv->has_days = TRUE; g_slist_foreach (days, (GFunc) logview_utils_day_free, NULL); g_slist_free (days); } else { log->priv->has_days = FALSE; } } else { log->priv->has_days = FALSE; g_clear_error (&err); } g_object_unref (is); out: if (err) { job->err = err; } g_io_scheduler_job_send_to_mainloop_async (io_job, log_load_done, job, NULL); return FALSE; }