GnomeVFSResult mn_vfs_write_entire_file_uri (GnomeVFSURI *uri, gsize file_size, const char *file_contents, gboolean exclusive, unsigned int perms) { GnomeVFSHandle *handle; GnomeVFSResult result; GnomeVFSFileSize bytes_written = 0; result = gnome_vfs_create_uri(&handle, uri, GNOME_VFS_OPEN_WRITE | GNOME_VFS_OPEN_TRUNCATE, exclusive, perms); if (result != GNOME_VFS_OK) return result; while (bytes_written < file_size) { GnomeVFSFileSize this_bytes_written; result = gnome_vfs_write(handle, file_contents + bytes_written, file_size - bytes_written, &this_bytes_written); if (result != GNOME_VFS_OK) { gnome_vfs_close(handle); return result; } bytes_written += this_bytes_written; } return gnome_vfs_close(handle); }
gboolean file_copy(gchar *source, gchar *dest) { GnomeVFSHandle *read_handle, *write_handle; GnomeVFSFileSize bytes_read, bytes_written; guint buffer[BYTES_TO_PROCESS]; GnomeVFSResult result; gchar *OnDiEn_source, *OnDiEn_dest; OnDiEn_source = get_filename_on_disk_encoding(source); OnDiEn_dest = get_filename_on_disk_encoding(dest); result = gnome_vfs_open(&read_handle, OnDiEn_source, GNOME_VFS_OPEN_READ); g_free(OnDiEn_source); if (result != GNOME_VFS_OK) return FALSE; result = gnome_vfs_create(&write_handle, OnDiEn_dest, GNOME_VFS_OPEN_WRITE, FALSE, 0644); g_free(OnDiEn_dest); if (result != GNOME_VFS_OK) { gnome_vfs_close(read_handle); return FALSE; } result = gnome_vfs_read (read_handle, buffer, BYTES_TO_PROCESS, &bytes_read); while (result == GNOME_VFS_OK) { result = gnome_vfs_write (write_handle, buffer, bytes_read, &bytes_written); if (result != GNOME_VFS_OK || bytes_written != bytes_read) { DEBUG_MSG("file_copy, return FALSE, write result=%d, written=%ld, read=%ld\n",result,(long)bytes_written,(long)bytes_read); gnome_vfs_close(write_handle); gnome_vfs_close(read_handle); return FALSE; } result = gnome_vfs_read(read_handle, buffer, BYTES_TO_PROCESS, &bytes_read); } gnome_vfs_close(write_handle); gnome_vfs_close(read_handle); return TRUE; }
static GstFlowReturn gst_gnome_vfs_sink_render (GstBaseSink * basesink, GstBuffer * buf) { GnomeVFSFileSize written, cur_pos; GstGnomeVFSSink *sink; GnomeVFSResult result; GstFlowReturn ret; sink = GST_GNOME_VFS_SINK (basesink); if (gnome_vfs_tell (sink->handle, &cur_pos) == GNOME_VFS_OK) { /* bring up to date with current position for proper reporting */ sink->current_pos = cur_pos; } result = gnome_vfs_write (sink->handle, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf), &written); switch (result) { case GNOME_VFS_OK: { GST_DEBUG_OBJECT (sink, "wrote %" G_GINT64_FORMAT " bytes at %" G_GINT64_FORMAT, (gint64) written, (gint64) cur_pos); if (written < GST_BUFFER_SIZE (buf)) { /* FIXME: what to do here? (tpm) */ g_warning ("%s: %d bytes should be written, only %" G_GUINT64_FORMAT " bytes written", G_STRLOC, GST_BUFFER_SIZE (buf), written); } sink->current_pos += GST_BUFFER_SIZE (buf); ret = GST_FLOW_OK; break; } case GNOME_VFS_ERROR_NO_SPACE: { /* TODO: emit signal/send msg on out-of-diskspace and * handle this gracefully (see open bug) (tpm) */ GST_ELEMENT_ERROR (sink, RESOURCE, NO_SPACE_LEFT, (NULL), ("bufsize=%u, written=%u", GST_BUFFER_SIZE (buf), (guint) written)); ret = GST_FLOW_ERROR; break; } default: { gchar *filename = gnome_vfs_uri_to_string (sink->uri, GNOME_VFS_URI_HIDE_PASSWORD); GST_ELEMENT_ERROR (sink, RESOURCE, WRITE, (_("Error while writing to file \"%s\"."), filename), ("%s, bufsize=%u, written=%u", gnome_vfs_result_to_string (result), GST_BUFFER_SIZE (buf), (guint) written)); g_free (filename); ret = GST_FLOW_ERROR; break; } } return GST_FLOW_OK; }
int main (int argc, char **argv) { GnomeVFSResult result; GnomeVFSHandle *handle; gchar buffer[1024]; GnomeVFSFileSize bytes_read; GnomeVFSURI *uri; gchar *text_uri; if (argc != 2) { printf ("Usage: %s <uri>\n", argv[0]); return 1; } if (! gnome_vfs_init ()) { fprintf (stderr, "Cannot initialize gnome-vfs.\n"); return 1; } uri = gnome_vfs_uri_new (argv[1]); if (uri == NULL) { fprintf (stderr, "URI not valid.\n"); return 1; } text_uri = gnome_vfs_uri_to_string (uri, GNOME_VFS_URI_HIDE_NONE); result = gnome_vfs_open_uri (&handle, uri, GNOME_VFS_OPEN_WRITE); show_result (result, "open", text_uri); while( result==GNOME_VFS_OK && !feof(stdin)) { GnomeVFSFileSize temp; bytes_read = fread(buffer, 1, sizeof buffer - 1, stdin); if(!bytes_read) break; buffer[bytes_read] = 0; result = gnome_vfs_write (handle, buffer, bytes_read, &temp); show_result (result, "write", text_uri); } result = gnome_vfs_close (handle); show_result (result, "close", text_uri); g_free (text_uri); return 0; }
static gboolean copy_uri (const gchar *src_uri, const gchar *dest_uri, const gchar *copying_format_str, const gchar *copied_format_str, GError **error) { GnomeVFSHandle *read_handle; GnomeVFSHandle *write_handle; GnomeVFSFileInfo *src_info; GnomeVFSFileSize file_size = 0; GnomeVFSFileSize bytes_read = 0; guchar buffer[BUFSIZE]; GnomeVFSResult result; gchar *memsize; GTimeVal last_time = { 0, 0 }; gimp_progress_init (_("Connecting to server")); src_info = gnome_vfs_file_info_new (); result = gnome_vfs_get_file_info (src_uri, src_info, 0); /* ignore errors here, they will be noticed below */ if (result == GNOME_VFS_OK && (src_info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_SIZE)) { file_size = src_info->size; } gnome_vfs_file_info_unref (src_info); result = gnome_vfs_open (&read_handle, src_uri, GNOME_VFS_OPEN_READ); if (result != GNOME_VFS_OK) { g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Could not open '%s' for reading: %s"), src_uri, gnome_vfs_result_to_string (result)); return FALSE; } result = gnome_vfs_create (&write_handle, dest_uri, GNOME_VFS_OPEN_WRITE, FALSE, 0644); if (result != GNOME_VFS_OK) { g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Could not open '%s' for writing: %s"), dest_uri, gnome_vfs_result_to_string (result)); gnome_vfs_close (read_handle); return FALSE; } memsize = g_format_size_for_display (file_size); gimp_progress_init_printf (file_size > 0 ? copying_format_str : copied_format_str, memsize); g_free (memsize); while (TRUE) { GnomeVFSFileSize chunk_read; GnomeVFSFileSize chunk_written; GTimeVal now; result = gnome_vfs_read (read_handle, buffer, sizeof (buffer), &chunk_read); if (chunk_read == 0) { if (result != GNOME_VFS_ERROR_EOF) { memsize = g_format_size_for_display (sizeof (buffer)); g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Failed to read %s from '%s': %s"), memsize, src_uri, gnome_vfs_result_to_string (result)); g_free (memsize); gnome_vfs_close (read_handle); gnome_vfs_close (write_handle); return FALSE; } else { gimp_progress_update (1.0); break; } } bytes_read += chunk_read; /* update the progress only up to 10 times a second */ g_get_current_time (&now); if (((now.tv_sec - last_time.tv_sec) * 1000 + (now.tv_usec - last_time.tv_usec) / 1000) > 100) { if (file_size > 0) { gimp_progress_update ((gdouble) bytes_read / (gdouble) file_size); } else { memsize = g_format_size_for_display (bytes_read); gimp_progress_set_text_printf (copied_format_str, memsize); gimp_progress_pulse (); g_free (memsize); } last_time = now; } result = gnome_vfs_write (write_handle, buffer, chunk_read, &chunk_written); if (chunk_written < chunk_read) { memsize = g_format_size_for_display (chunk_read); g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED, _("Failed to write %s to '%s': %s"), memsize, dest_uri, gnome_vfs_result_to_string (result)); g_free (memsize); gnome_vfs_close (read_handle); gnome_vfs_close (write_handle); return FALSE; } } gnome_vfs_close (read_handle); gnome_vfs_close (write_handle); return TRUE; }
/*--------------------------------------------------------------------------*/ static void read_remote_file_callback( GnomeVFSAsyncHandle *handle, GnomeVFSResult result, gpointer buffer, GnomeVFSFileSize bytes_requested, GnomeVFSFileSize bytes_read, gpointer callback_data ) { DownloadCallbackData *data = (DownloadCallbackData *)callback_data; GnomeVFSFileSize bytes_written; gb_debug (DEBUG_UPDATE, "START"); if ( update_cancel_flag ) { gnome_vfs_async_close( handle, close_remote_file_callback, data ); gb_debug (DEBUG_UPDATE, "END -- CANCEL"); return; } switch (result) { case GNOME_VFS_OK: if ( bytes_read > 0 ) { data->total_bytes_read += bytes_read; data->file_bytes_read += bytes_read; update_progress_bar (GTK_PROGRESS_BAR(file_progress), data->file_bytes_read, data->file_bytes); update_progress_bar (GTK_PROGRESS_BAR(total_progress), data->total_bytes_read, data->total_bytes); gnome_vfs_write( data->local_handle, buffer, bytes_read, &bytes_written ); if ( bytes_written != bytes_read ) { g_warning( "Write failed: %d bytes written != %d bytes read", (gint)bytes_written, (gint)bytes_read ); } gnome_vfs_async_read( handle, data->buffer, data->buffer_length, read_remote_file_callback, data ); } else { gnome_vfs_async_close( handle, close_remote_file_callback, data ); gb_debug (DEBUG_UPDATE, "0 length read"); } break; case GNOME_VFS_ERROR_EOF: gb_debug (DEBUG_UPDATE, "EOF -- %" GNOME_VFS_OFFSET_FORMAT_STR, bytes_read); gnome_vfs_async_close( handle, close_remote_file_callback, data ); break; default: g_warning( "Read failed: %s", gnome_vfs_result_to_string(result) ); break; } gb_debug (DEBUG_UPDATE, "END"); }
static gboolean write_theme_to_disk (GnomeThemeMetaInfo *theme_info, const gchar *theme_name, const gchar *theme_description, gboolean save_background, GError **error) { gchar *dir, *theme_name_dir; GnomeVFSURI *uri; GnomeVFSURI *target_uri; GnomeVFSHandle *handle = NULL; GnomeVFSFileSize bytes_written; gchar *str, *current_background; GConfClient *client; const gchar *theme_header = "[Desktop Entry]\n" "Name=%s\n" "Type=X-GNOME-Metatheme\n" "Comment=%s\n" "\n" "[X-GNOME-Metatheme]\n" "GtkTheme=%s\n" "MetacityTheme=%s\n" "IconTheme=%s\n"; theme_name_dir = str_remove_slash (theme_name); dir = g_build_filename (g_get_home_dir (), ".themes", theme_name_dir, "index.theme~", NULL); g_free (theme_name_dir); uri = gnome_vfs_uri_new (dir); dir [strlen (dir) - 1] = '\000'; target_uri = gnome_vfs_uri_new (dir); g_free (dir); gnome_vfs_create_uri (&handle, uri, GNOME_VFS_OPEN_READ | GNOME_VFS_OPEN_WRITE, FALSE, 0644); gnome_vfs_truncate_handle (handle, 0); /* start making the theme file */ str = g_strdup_printf (theme_header, theme_name, theme_description, theme_info->gtk_theme_name, theme_info->metacity_theme_name, theme_info->icon_theme_name); gnome_vfs_write (handle, str, strlen (str), &bytes_written); g_free (str); if (theme_info->gtk_color_scheme) { gchar *a, *tmp; tmp = g_strdup (theme_info->gtk_color_scheme); for (a = tmp; *a != '\0'; a++) if (*a == '\n') *a = ','; str = g_strdup_printf ("GtkColorScheme=%s\n", tmp); gnome_vfs_write (handle, str, strlen (str), &bytes_written); g_free (str); g_free (tmp); } if (theme_info->cursor_theme_name) { #ifdef HAVE_XCURSOR str = g_strdup_printf ("CursorTheme=%s\n" "CursorSize=%i\n", theme_info->cursor_theme_name, theme_info->cursor_size); #else str = g_strdup_printf ("CursorFont=%s\n", theme_info->cursor_theme_name); #endif gnome_vfs_write (handle, str, strlen (str), &bytes_written); g_free (str); } if (save_background) { client = gconf_client_get_default (); current_background = gconf_client_get_string (client, BACKGROUND_KEY, NULL); if (current_background != NULL) { str = g_strdup_printf ("BackgroundImage=%s\n", current_background); gnome_vfs_write (handle, str, strlen (str), &bytes_written); g_free (current_background); g_free (str); } g_object_unref (client); } gnome_vfs_close (handle); gnome_vfs_move_uri (uri, target_uri, TRUE); gnome_vfs_uri_unref (uri); gnome_vfs_uri_unref (target_uri); return TRUE; }