Esempio n. 1
0
gboolean
ev_attachment_save (EvAttachment *attachment,
		    GFile        *file,
		    GError      **error)
{
	GFileOutputStream *output_stream;
	GError *ioerror = NULL;
	gssize  written_bytes;

	g_return_val_if_fail (EV_IS_ATTACHMENT (attachment), FALSE);
	g_return_val_if_fail (G_IS_FILE (file), FALSE);

	output_stream = g_file_replace (file, NULL, FALSE, 0, NULL, &ioerror);
	if (output_stream == NULL) {
		char *uri;
		
		uri = g_file_get_uri (file);
		g_set_error (error,
			     EV_ATTACHMENT_ERROR, 
			     ioerror->code,
			     _("Couldn't save attachment “%s”: %s"),
			     uri, 
			     ioerror->message);

		g_error_free (ioerror);
		g_free (uri);
		
		return FALSE;
	}
	
	written_bytes = g_output_stream_write (G_OUTPUT_STREAM (output_stream),
					       attachment->priv->data,
					       attachment->priv->size,
					       NULL, &ioerror);
	if (written_bytes == -1) {
		char *uri;
		
		uri = g_file_get_uri (file);
		g_set_error (error,
			     EV_ATTACHMENT_ERROR,
			     ioerror->code,
			     _("Couldn't save attachment “%s”: %s"),
			     uri,
			     ioerror->message);
		
		g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, NULL);
		g_error_free (ioerror);
		g_free (uri);

		return FALSE;
	}

	g_output_stream_close (G_OUTPUT_STREAM (output_stream), NULL, NULL);

	return TRUE;
	
}
Esempio n. 2
0
gboolean
ot_util_variant_save (GFile *dest,
                      GVariant *variant,
                      GCancellable *cancellable,
                      GError  **error)
{
  gboolean ret = FALSE;
  gs_unref_object GOutputStream *out = NULL;
  gsize bytes_written;
  
  out = (GOutputStream*)g_file_replace (dest, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION,
                                        cancellable, error);
  if (!out)
    goto out;

  if (!g_output_stream_write_all (out,
                                  g_variant_get_data (variant),
                                  g_variant_get_size (variant),
                                  &bytes_written,
                                  cancellable,
                                  error))
    goto out;
  if (!g_output_stream_close (out, cancellable, error))
    goto out;

  ret = TRUE;
 out:
  return ret;
}
Esempio n. 3
0
static void
export_png (GtkButton     *button,
    ChamplainView *view)
{
  cairo_surface_t *surface;
  GdkPixbuf *pixbuf;
  GFileOutputStream *os;
  GFile *file;
  gint width, height;

  if (champlain_view_get_state (view) != CHAMPLAIN_STATE_DONE)
    return;

  surface = champlain_view_to_surface (view, TRUE);
  if (!surface)
    return;

  width = cairo_image_surface_get_width (surface);
  height = cairo_image_surface_get_height (surface);
  pixbuf = gdk_pixbuf_get_from_surface (surface, 0, 0, width, height);
  if (!pixbuf)
    return;

  file = g_file_new_for_path ("champlain-map.png");
  os = g_file_replace (file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, NULL);
  if (!os)
    {
      g_object_unref (pixbuf);
      return;
    }

  gdk_pixbuf_save_to_stream (pixbuf, G_OUTPUT_STREAM (os), "png", NULL, NULL, NULL);
  g_output_stream_close (G_OUTPUT_STREAM (os), NULL, NULL);
}
Esempio n. 4
0
static void
test_client_destroy (TestClient *client)
{
  GError *error = NULL;

  if (client->waiter)
    async_waiter_destroy (client->waiter);

  g_output_stream_close (G_OUTPUT_STREAM (client->in), NULL, &error);
  if (error)
    {
      g_warning ("Error closing client stdin: %s", error->message);
      g_clear_error (&error);
    }
  g_object_unref (client->in);

  g_input_stream_close (G_INPUT_STREAM (client->out), NULL, &error);
  if (error)
    {
      g_warning ("Error closing client stdout: %s", error->message);
      g_clear_error (&error);
    }
  g_object_unref (client->out);

  g_object_unref (client->cancellable);
  g_object_unref (client->subprocess);
  g_main_loop_unref (client->loop);
  g_free (client->id);
  g_free (client);
}
Esempio n. 5
0
static
gboolean spice_win_usb_driver_send_request(SpiceWinUsbDriver *self, guint16 op,
                                           guint16 vid, guint16 pid, GError **err)
{
    USBClerkDriverOp req;
    GOutputStream *ostream;
    SpiceWinUsbDriverPrivate *priv;
    gsize bytes;
    gboolean ret;

    SPICE_DEBUG("sending a request to usbclerk service (op=%d vid=0x%04x pid=0x%04x",
                op, vid, pid);

    g_return_val_if_fail(SPICE_IS_WIN_USB_DRIVER(self), FALSE);
    priv = self->priv;

    memset(&req, 0, sizeof(req));
    req.hdr.magic   = USB_CLERK_MAGIC;
    req.hdr.version = USB_CLERK_VERSION;
    req.hdr.type    = op;
    req.hdr.size    = sizeof(req);
    req.vid = vid;
    req.pid = pid;

    ostream = g_win32_output_stream_new(priv->handle, FALSE);

    ret = g_output_stream_write_all(ostream, &req, sizeof(req), &bytes, NULL, err);
    g_warn_if_fail(g_output_stream_close(ostream, NULL, NULL));
    g_object_unref(ostream);
    SPICE_DEBUG("write_all request returned %d written bytes %"G_GSIZE_FORMAT
                " expecting %"G_GSIZE_FORMAT,
                ret, bytes, sizeof(req));
    return ret;
}
Esempio n. 6
0
gboolean 
gitg_commit_add_ignore(GitgCommit *commit, GitgChangedFile *file, GError **error)
{
	g_return_if_fail(GITG_IS_COMMIT(commit));
	g_return_if_fail(GITG_IS_CHANGED_FILE(file));
	
	GFile *f = gitg_changed_file_get_file(file);
	gchar *path = gitg_repository_relative(commit->priv->repository, f);
	
	gchar *ignore = g_strdup_printf("%s/.gitignore", gitg_repository_get_path(commit->priv->repository));
	GFile *ig = g_file_new_for_path(ignore);
	
	GFileOutputStream *stream = g_file_append_to(ig, G_FILE_CREATE_NONE, NULL, error);
	gboolean ret = FALSE;
	
	if (stream)
	{
		gchar *line = g_strdup_printf("/%s\n", path);
		ret = g_output_stream_write_all(G_OUTPUT_STREAM(stream), line, strlen(line), NULL, NULL, error);
		g_output_stream_close(G_OUTPUT_STREAM(stream), NULL, NULL);

		g_object_unref(stream);
		g_free(line);
	}
	
	if (ret)
		remove_file(commit, file);

	g_object_unref(f);	
	g_free(ignore);
	g_free(path);
}
Esempio n. 7
0
static void write_output_file (gpointer data)
{
	g_assert (data != NULL);

	GError *error = NULL;
	GDownloadable *download		 = G_DOWNLOADABLE (data);
	SoupDownload  *soup_download = SOUP_DOWNLOAD (data);

	if (soup_download->priv->need_to_write) {
		GFileOutputStream *ostream = g_file_append_to (download->priv->local_file, G_FILE_CREATE_NONE, NULL, &error);
		handle_error (error);

		SoupBuffer *buffer = soup_message_body_flatten (soup_download->priv->message->response_body);
		g_output_stream_write (G_OUTPUT_STREAM (ostream), buffer->data, buffer->length, NULL, &error);
		handle_error (error);

		g_output_stream_close (G_OUTPUT_STREAM (ostream), NULL, &error);
		handle_error (error);

		download->priv->downloaded_size = soup_download->priv->prev_downloaded + buffer->length;
		soup_buffer_free (buffer);

		soup_download->priv->need_to_write = FALSE;
	}
}
Esempio n. 8
0
static gboolean
convert_var_to_tmpfiles_d (int            src_rootfs_dfd,
                           int            dest_rootfs_dfd,
                           GCancellable  *cancellable,
                           GError       **error)
{
    gboolean ret = FALSE;
    g_autoptr(GString) prefix = g_string_new ("/var");
    glnx_fd_close int tmpfiles_fd = -1;

    /* Append to an existing one for package layering */
    if ((tmpfiles_fd = TEMP_FAILURE_RETRY (openat (dest_rootfs_dfd, "usr/lib/tmpfiles.d/rpm-ostree-autovar.conf",
                                           O_WRONLY | O_CREAT | O_APPEND | O_NOCTTY, 0644))) == -1)
    {
        glnx_set_error_from_errno (error);
        goto out;
    }

    {   glnx_unref_object GOutputStream *tmpfiles_out =
            g_unix_output_stream_new (tmpfiles_fd, FALSE);

        if (!tmpfiles_out)
            goto out;

        if (!convert_var_to_tmpfiles_d_recurse (tmpfiles_out, src_rootfs_dfd, prefix, cancellable, error))
            goto out;

        if (!g_output_stream_close (tmpfiles_out, cancellable, error))
            goto out;
    }

    ret = TRUE;
out:
    return ret;
}
Esempio n. 9
0
static void
do_close_write (GVfsBackend *backend,
		GVfsJobCloseWrite *job,
		GVfsBackendHandle handle)
{
  GFileOutputStream *out;
  GError *error;
  char *etag;

  out = (GFileOutputStream *)handle;

  error = NULL;
  if (!g_output_stream_close (G_OUTPUT_STREAM (out),
			      G_VFS_JOB (job)->cancellable,
			      &error))
    {
      g_vfs_job_failed_from_error (G_VFS_JOB (job), error);
      g_error_free (error);
    }
  else
    {
      etag = g_file_output_stream_get_etag (out);

      if (etag)
	{
	  g_vfs_job_close_write_set_etag (job, etag);
	  g_free (etag);
	}
      
      g_vfs_job_succeeded (G_VFS_JOB (job));
    }
  
  g_object_unref (out);
}
Esempio n. 10
0
void
lsm_dom_document_save_to_memory	(LsmDomDocument *document, void **buffer, int *size, GError **error)
{
	GOutputStream *stream;

	if (buffer != NULL)
		*buffer = NULL;
	if (size != NULL)
		*size = 0;

	g_return_if_fail (document != NULL);
	g_return_if_fail (buffer != NULL);

	stream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
	if (stream == NULL) {
		*buffer = NULL;
		if (size != NULL)
			*size = 0;
		return;
	}

	lsm_dom_document_save_to_stream (document, G_OUTPUT_STREAM (stream), error);
	g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, error);

	if (size != NULL)
		*size = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (stream));
	*buffer = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (stream));

	g_object_unref (stream);
}
Esempio n. 11
0
static void
cleanup (FILE *f1, GFile *f2, GFileOutputStream *ostream, gchar *buf, gpgme_key_t *sk, gpgme_ctx_t *ctx)
{
    if (f1 != NULL) {
        fclose (f1);
    }

    if (f2 != NULL) {
        g_object_unref (f2);
    }

    if (ostream != NULL) {
        g_output_stream_close (G_OUTPUT_STREAM (ostream), NULL, NULL);
        g_object_unref (ostream);
    }

    if (buf != NULL) {
        g_free (buf);
    }

    if (sk != NULL) {
        gpgme_key_release (*sk);
    }

    if (ctx != NULL) {
        gpgme_release (*ctx);
    }

}
Esempio n. 12
0
void lllp_connected_cb(GObject *listener, GAsyncResult *res, gpointer pstore_library) {
	GtkListStore *store_library = GTK_LIST_STORE(pstore_library);
	GSocketConnection *connection;
	GSocketAddress *address;
	gchar *address_char;
	GInputStream *stream;
	GFile *output_file;
	GOutputStream *file_stream;
	gsize count;
	gboolean valid = FALSE;


	connection = g_socket_listener_accept_finish(G_SOCKET_LISTENER(listener), res, NULL, NULL);
	address = g_socket_connection_get_remote_address(connection, NULL);
	address_char = g_inet_address_to_string(g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(address)));
	printf("%s\n", address_char);

	output_file = g_file_new_for_path(address_char);
	g_file_delete(output_file, NULL, NULL);
	file_stream = (GOutputStream*)g_file_replace(output_file, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL, NULL);

	stream = g_io_stream_get_input_stream(G_IO_STREAM(connection));
	g_output_stream_splice(file_stream, stream, 
		G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE & G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET, NULL, NULL);
	g_output_stream_close(file_stream, NULL, NULL);

	convert_library_to_list_store(store_library, address_char);

	printf("%s CONNECTED\n", address_char);

	g_socket_listener_accept_async(G_SOCKET_LISTENER(listener), NULL, lllp_connected_cb, store_library);
}
static int
stream_close (CamelStream *stream)
{
	CamelStreamVFS *stream_vfs = CAMEL_STREAM_VFS (stream);
	GError *error = NULL;

	g_return_val_if_fail (CAMEL_IS_STREAM_VFS (stream) && stream_vfs != NULL, -1);
	g_return_val_if_fail (stream_vfs->stream != NULL, -1);
	g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream_vfs->stream) || G_IS_INPUT_STREAM (stream_vfs->stream), -1);

	if (G_IS_OUTPUT_STREAM (stream_vfs->stream))
		g_output_stream_close (G_OUTPUT_STREAM (stream_vfs->stream), NULL, &error);
	else
		g_input_stream_close (G_INPUT_STREAM (stream_vfs->stream), NULL, &error);

	if (error) {
		g_warning ("%s", error->message);
		g_error_free (error);
		return -1;
	}

	g_object_unref (stream_vfs->stream);
	stream_vfs->stream = NULL;

	return 0;
}
Esempio n. 14
0
/**
 * tmpl_template_expand_string:
 * @self: A #TmplTemplate.
 * @scope: (nullable): A #TmplScope or %NULL.
 * @error: A location for a #GError, or %NULL
 *
 * Expands the template and returns the result as a string.
 *
 * Returns: A newly allocated string, or %NULL upon failure.
 */
gchar *
tmpl_template_expand_string (TmplTemplate  *self,
                             TmplScope     *scope,
                             GError       **error)
{
  GOutputStream *stream;
  gchar zero = 0;
  gchar *ret;

  g_return_val_if_fail (TMPL_IS_TEMPLATE (self), NULL);

  stream = g_memory_output_stream_new (NULL, 0, g_realloc, g_free);

  if (!tmpl_template_expand (self, stream, scope, NULL, error) ||
      !g_output_stream_write_all (stream, &zero, 1, NULL, NULL, error) ||
      !g_output_stream_close (stream, NULL, error))

    {
      g_object_unref (stream);
      return NULL;
    }

  ret = g_memory_output_stream_steal_data (G_MEMORY_OUTPUT_STREAM (stream));

  g_object_unref (stream);

  return ret;
}
Esempio n. 15
0
static int gio_fclose (VFSFile * file)
{
    FileData * data = vfs_get_handle (file);
    GError * error = 0;

    if (data->iostream)
    {
        g_io_stream_close (data->iostream, 0, & error);
        g_object_unref (data->iostream);
        CHECK_ERROR ("close", vfs_get_filename (file));
    }
    else if (data->istream)
    {
        g_input_stream_close (data->istream, 0, & error);
        g_object_unref (data->istream);
        CHECK_ERROR ("close", vfs_get_filename (file));
    }
    else if (data->ostream)
    {
        g_output_stream_close (data->ostream, 0, & error);
        g_object_unref (data->ostream);
        CHECK_ERROR ("close", vfs_get_filename (file));
    }

    if (data->file)
        g_object_unref (data->file);

    return 0;

FAILED:
    if (data->file)
        g_object_unref (data->file);

    return -1;
}
Esempio n. 16
0
static GFile *
create_file (GFile *base_dir)
{
  GFile         *scratch_file;
  gchar         *scratch_name;
  GOutputStream *output_stream;
  gint           pid;
  GError        *error = NULL;
  gchar          buffer [BUFFER_SIZE];
  gint           i;

  pid = getpid ();
  scratch_name = g_strdup_printf ("gvfs-benchmark-scratch-%d", pid);
  scratch_file = g_file_resolve_relative_path (base_dir, scratch_name);
  g_free (scratch_name);

  if (!scratch_file)
    return NULL;

  output_stream = G_OUTPUT_STREAM (g_file_replace (scratch_file, NULL, FALSE, G_FILE_CREATE_NONE, NULL, &error));
  if (!output_stream)
    {
      g_printerr ("Failed to create scratch file: %s\n", error->message);
      g_object_unref (scratch_file);
      return NULL;
    }

  memset (buffer, 0xaa, BUFFER_SIZE);

  for (i = 0; i < FILE_SIZE; i += BUFFER_SIZE)
    {
      if (g_output_stream_write (output_stream, buffer, BUFFER_SIZE, NULL, &error) < BUFFER_SIZE)
        {
          g_printerr ("Failed to populate scratch file: %s\n", error->message);
          g_output_stream_close (output_stream, NULL, NULL);
          g_object_unref (output_stream);
          g_object_unref (scratch_file);
          return NULL;
        }
    }

  g_output_stream_close (output_stream, NULL, NULL);
  g_object_unref (output_stream);
  return scratch_file;
}
Esempio n. 17
0
static gboolean
gimp_config_writer_close_output (GimpConfigWriter  *writer,
                                 GError           **error)
{
  g_return_val_if_fail (writer->output != NULL, FALSE);

  if (writer->error)
    {
      GCancellable *cancellable = g_cancellable_new ();

      /* Cancel the overwrite initiated by g_file_replace(). */
      g_cancellable_cancel (cancellable);
      g_output_stream_close (writer->output, cancellable, NULL);
      g_object_unref (cancellable);

      g_object_unref (writer->output);
      writer->output = NULL;

      return FALSE;
    }

  if (writer->file)
    {
      GError *my_error = NULL;

      if (! g_output_stream_close (writer->output, NULL, &my_error))
        {
          g_set_error (error, GIMP_CONFIG_ERROR, GIMP_CONFIG_ERROR_WRITE,
                       _("Error writing '%s': %s"),
                       gimp_file_get_utf8_name (writer->file),
                       my_error->message);
          g_clear_error (&my_error);

          g_object_unref (writer->output);
          writer->output = NULL;

          return FALSE;
        }
    }

  g_object_unref (writer->output);
  writer->output = NULL;

  return TRUE;
}
Esempio n. 18
0
static GFile *
get_g_file_with_encrypted_data (GFileInputStream *in_stream, goffset file_size)
{
    GError *err = NULL;
    GFileIOStream *ostream;
    gssize read_len;
    guchar *buf;
    gsize len_file_data = file_size - SHA512_DIGEST_SIZE;
    gsize done_size = 0;

    GFile *tmp_encrypted_file = g_file_new_tmp (NULL, &ostream, &err);
    if (tmp_encrypted_file == NULL) {
        g_printerr ("%s\n", err->message);
        // TODO
        return NULL;
    }

    GFileOutputStream *out_enc_stream = g_file_append_to (tmp_encrypted_file, G_FILE_CREATE_NONE, NULL, &err);
    if (out_enc_stream == NULL) {
        g_printerr ("%s\n", err->message);
        // TODO
        return NULL;
    }

    if (len_file_data < FILE_BUFFER) {
        buf = g_malloc (len_file_data);
        g_input_stream_read (G_INPUT_STREAM (in_stream), buf, len_file_data, NULL, &err);
        g_output_stream_write (G_OUTPUT_STREAM (out_enc_stream), buf, len_file_data, NULL, &err);
    }
    else {
        buf = g_malloc (FILE_BUFFER);
        while (done_size < len_file_data) {
            if ((len_file_data - done_size) > FILE_BUFFER) {
                read_len = g_input_stream_read (G_INPUT_STREAM (in_stream), buf, FILE_BUFFER, NULL, &err);
            }
            else {
                read_len = g_input_stream_read (G_INPUT_STREAM (in_stream), buf, len_file_data - done_size, NULL, &err);
            }
            if (read_len == -1) {
                g_printerr ("%s\n", err->message);
                // TODO
                return NULL;
            }
            g_output_stream_write (G_OUTPUT_STREAM (out_enc_stream), buf, read_len, NULL, &err);
            done_size += read_len;
            memset (buf, 0, FILE_BUFFER);
        }
    }

    g_input_stream_close (G_INPUT_STREAM (in_stream), NULL, NULL);
    g_output_stream_close (G_OUTPUT_STREAM (out_enc_stream), NULL, NULL);
    g_object_unref (out_enc_stream);
    g_free (buf);

    return tmp_encrypted_file;
}
Esempio n. 19
0
void sc_local_storage_manager_free(ScLocalStorageManager *manager) {
  GError *error = NULL;
  g_output_stream_close(G_OUTPUT_STREAM(manager->ostream), NULL, &error);
  if(error) {
    g_critical(G_STRLOC ": %d: %s", error->code, error->message);
    g_error_free(error);
  }
  g_object_unref(manager->file);
  g_free(manager);
}
Esempio n. 20
0
 arrow::Status Close() override {
   GError *error = NULL;
   if (g_output_stream_close(output_stream_, NULL, &error)) {
     return arrow::Status::OK();
   } else {
     return garrow_error_to_status(error,
                                   arrow::StatusCode::IOError,
                                   "[gio-output-stream][close]");
   }
 }
Esempio n. 21
0
static void 
get_callback (SoupSession *session, SoupMessage *msg, SeahorseHKPOperation *hop) 
{
    GError *err = NULL;
    const gchar *start;
    const gchar *end;
    GOutputStream *output;
    const gchar *text;
    gboolean ret;
    guint len;
    gsize written;
    
    if (hop->cancelling)
        return;
    
    if (SOUP_MESSAGE_IS_ERROR (msg)) {
        fail_hkp_operation (hop, msg, NULL);
        return;
    }
    
    end = text = msg->response_body->data;
    len = msg->response_body->length;
    
    for (;;) {

        len -= end - text;
        text = end;
        
        if(!detect_key (text, len, &start, &end))
            break;
        
		/* Any key blocks get written to our result data */
		output = seahorse_operation_get_result (SEAHORSE_OPERATION (hop));
		g_return_if_fail (G_IS_OUTPUT_STREAM (output));

		ret = g_output_stream_write_all (output, start, end - start, &written, NULL, &err) &&
		      g_output_stream_write_all (output, "\n", 1, &written, NULL, &err) &&
		      g_output_stream_flush (output, NULL, &err);

		if (!ret) {
			seahorse_operation_mark_done (SEAHORSE_OPERATION (hop), FALSE, err);
			return;
		}
    	}
        
    	if (--hop->requests <= 0) {
    		output = seahorse_operation_get_result (SEAHORSE_OPERATION (hop));
    		g_return_if_fail (G_IS_OUTPUT_STREAM (output));
    		g_output_stream_close (output, NULL, &err);
    		seahorse_operation_mark_done (SEAHORSE_OPERATION (hop), FALSE, err);
    	} else {
    		seahorse_operation_mark_progress_full (SEAHORSE_OPERATION (hop), _("Retrieving keys..."), 
    		                                       hop->requests, hop->total);
    	}
}
static void
create_one_file (GFile *file)
{
        GFileOutputStream* stream;

        stream = g_file_create (file, G_FILE_CREATE_NONE, NULL, NULL);
        if (stream != NULL) {
                g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, NULL);
                g_object_unref (stream);
        }
}
Esempio n. 23
0
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;
}
Esempio n. 24
0
/**
* ly_log_logger_add:
* @logger: the logger
* @str: the string data to be write to log file.
*
* Add a log item to log file.
*/
void	ly_log_logger_add		(LyLogLogger *logger, gchar *str)
{
	LyLogLoggerPrivate *priv=LY_LOG_LOGGER_GET_PRIVATE(LY_LOG_LOGGER(logger));
	GFileOutputStream *ostream;
	ostream=g_file_append_to(G_FILE(priv->file), G_FILE_CREATE_NONE, \
		NULL, NULL);
	g_output_stream_write(G_OUTPUT_STREAM(ostream), (const void *)str, \
		strlen(str), NULL, NULL);
	g_output_stream_close(G_OUTPUT_STREAM(ostream), NULL, NULL);
	g_object_unref(ostream);
}
Esempio n. 25
0
/**
 * uncompress_bzip2:
 * @name: The name of the file to attempt to decompress
 *
 * Returns: The name of the uncompressed file (in a temporary location) or NULL
 *   free the returned name after use.
 *
 * Also see: http://www.bzip.org/1.0.5/bzip2-manual-1.0.5.html
 */
gchar* uncompress_bzip2 ( gchar *name )
{
#ifdef HAVE_BZLIB_H
	FILE *ff = g_fopen ( name, "r" );
	if ( !ff )
		return NULL;

	int     bzerror;
	BZFILE* bf = BZ2_bzReadOpen ( &bzerror, ff, 0, 0, NULL, 0 ); // This should take care of the bz2 file header
	if ( bzerror != BZ_OK ) {
		BZ2_bzReadClose ( &bzerror, bf );
		// handle error
		return NULL;
	}

	GFileIOStream *gios;
	GError *error = NULL;
	GFile *gf = g_file_new_tmp ( "vik-bz2-tmp.XXXXXX", &gios, &error );
	gchar *tmpname = g_file_get_path (gf);

	GOutputStream *gos = g_io_stream_get_output_stream ( G_IO_STREAM(gios) );

	// Process in arbitary sized chunks
	char buf[4096];
	bzerror = BZ_OK;
	int nBuf;
	// Now process the actual compression data
	while ( bzerror == BZ_OK ) {
		nBuf = BZ2_bzRead ( &bzerror, bf, buf, 4096 );
		if ( bzerror == BZ_OK || bzerror == BZ_STREAM_END) {
			// do something with buf[0 .. nBuf-1]
			if ( g_output_stream_write ( gos, buf, nBuf, NULL, &error ) < 0 ) {
				g_critical ( "Couldn't write bz2 tmp %s file due to %s", tmpname, error->message );
				g_error_free (error);
				BZ2_bzReadClose ( &bzerror, bf );
				goto end;
			}
		}
	}
	if ( bzerror != BZ_STREAM_END ) {
		BZ2_bzReadClose ( &bzerror, bf );
		// handle error...
		goto end;
	} else {
		BZ2_bzReadClose ( &bzerror, bf );
		g_output_stream_close ( gos, NULL, &error );
	}

 end:
	return tmpname;
#else
	return NULL;
#endif
}
Esempio n. 26
0
/**
* ly_log_logger_clear:
* @logger: the logger
*
* Clear all the logs written before. This function always attempts to
* create a temp file whose filename is ended up with "~" and store all
* the old logs into it.
*/
void	ly_log_logger_clear		(LyLogLogger *logger)
{
	LyLogLoggerPrivate *priv=LY_LOG_LOGGER_GET_PRIVATE(LY_LOG_LOGGER(logger));
	GFileOutputStream *ostream;
	gchar str[64]="LOG BEGIN >>\n";	
	ostream=g_file_replace(G_FILE(priv->file), NULL, TRUE, \
		G_FILE_CREATE_NONE, NULL, NULL);
	g_output_stream_write(G_OUTPUT_STREAM(ostream), str, strlen(str), \
		NULL, NULL);
	g_output_stream_close(G_OUTPUT_STREAM(ostream), NULL, NULL);
	g_object_unref(ostream);
}
Esempio n. 27
0
static void
g_output_stream_dispose (GObject *object)
{
  GOutputStream *stream;

  stream = G_OUTPUT_STREAM (object);
  
  if (!stream->priv->closed)
    g_output_stream_close (stream, NULL, NULL);

  G_OBJECT_CLASS (g_output_stream_parent_class)->dispose (object);
}
Esempio n. 28
0
static int
close_stream(thandle_t handle)
{
  Priv *p = (Priv*) handle;
  GError *error = NULL;
  gsize total = 0;
  gboolean closed = FALSE;

  g_assert(p->stream);

  /* !can_seek: file content is now fully cached, time to write it down. */
  if (!p->can_seek && p->buffer != NULL && p->allocated > 0)
    {
      while (total < p->allocated)
        {
          gssize written;

          written = g_output_stream_write(G_OUTPUT_STREAM(p->stream),
                                          (void *) (p->buffer + total),
                                          p->allocated - total,
                                          NULL, &error);
          if (written < 0)
            {
                  g_warning("%s", error->message);
                  g_error_free(error);
                  break;
            }

          total += written;
        }
    }

  closed = g_output_stream_close(G_OUTPUT_STREAM(p->stream),
                                 NULL, &error);
  if (!closed)
    {
      g_warning("%s", error->message);
      g_error_free(error);
    }

  g_clear_object(&p->stream);

  p->position = 0;

  if (p->buffer != NULL)
    g_free(p->buffer);
  p->buffer = NULL;

  p->allocated = 0;

  return (closed) ? 0 : -1;
}
Esempio n. 29
0
static void
gdk_clipboard_read_local_write_done (GObject      *clipboard,
                                     GAsyncResult *result,
                                     gpointer      stream)
{
  /* we don't care about the error, we just want to clean up */
  gdk_clipboard_write_finish (GDK_CLIPBOARD (clipboard), result, NULL);

  /* XXX: Do we need to close_async() here? */
  g_output_stream_close (stream, NULL, NULL);

  g_object_unref (stream);
}
Esempio n. 30
0
GDataInputStream *
http_data_input_stream(GInputStream * stream,gsize * length,GCancellable * cancellable,GError ** error)
{
	GMemoryOutputStream
	* out = G_MEMORY_OUTPUT_STREAM(g_memory_output_stream_new_resizable());
	gchar * content = NULL;
	guint8 byte = 0;
	while(g_input_stream_read(stream,&byte,1,cancellable,error))
	{
		if(g_memory_output_stream_get_data_size(out) > 2048)
		{
			g_output_stream_close(G_OUTPUT_STREAM(out),NULL,NULL);
			g_free(g_memory_output_stream_steal_data(out));
			g_object_unref(out);
			return NULL;
		}
		g_output_stream_write_all(G_OUTPUT_STREAM(out),&byte,1,NULL,NULL,NULL);
		if(g_memory_output_stream_get_data_size(out) >= 4)
		{
			content = g_memory_output_stream_get_data(out);
			if(strncmp((content + g_memory_output_stream_get_data_size(out) - 4),"\r\n\r\n",4) == 0)
				break;
		}
	}
	g_output_stream_close(G_OUTPUT_STREAM(out),NULL,NULL);
	GMemoryInputStream
	* result = G_MEMORY_INPUT_STREAM(g_memory_input_stream_new_from_data(
				g_memory_output_stream_steal_data(out),
				g_memory_output_stream_get_data_size(out),
				g_free
			));
	g_object_unref(out);
	GDataInputStream
	* dis = g_data_input_stream_new(G_INPUT_STREAM(result));
	g_data_input_stream_set_newline_type(dis,G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
	return dis;
}