Ejemplo n.º 1
0
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;
}
Ejemplo n.º 2
0
void
bookmarks_write_to_disk (Bookmarks *bookmarks)
{
	GnomeVFSResult  result;
	GnomeVFSHandle *handle;
	char           *uri;
	int             lines;
	GList          *scan;

	g_return_if_fail (bookmarks != NULL);

	if (bookmarks->rc_filename == NULL)
		return;

	uri = g_strconcat (get_home_uri (),
			   "/",
			   bookmarks->rc_filename,
			   NULL);
	result = gnome_vfs_create (&handle, uri, GNOME_VFS_OPEN_WRITE, FALSE, FILE_PERMISSIONS);
	g_free (uri);

	if (result != GNOME_VFS_OK)
		return;

	/* write the file list. */

	lines = 0;
	scan = bookmarks->list;
	while (((bookmarks->max_lines < 0) || (lines < bookmarks->max_lines))
	       && (scan != NULL)) {
		if (_gnome_vfs_write_line (handle,
					   "\"%s\"",
					   (char*) scan->data) != GNOME_VFS_OK) {
			g_print ("ERROR saving to bookmark file\n");
			break;
		}
		lines++;
		scan = scan->next;
	}

	gnome_vfs_close (handle);
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
/*--------------------------------------------------------------------------*/
static void open_remote_file_callback( GnomeVFSAsyncHandle *handle,
				       GnomeVFSResult result,
				       gpointer callback_data )
{
	DownloadCallbackData *data = (DownloadCallbackData *)callback_data;
	GnomeVFSFileInfo     *info = (GnomeVFSFileInfo *)data->p->data;
	gchar                *local_path, *local_text_uri, *local_name;
	GnomeVFSHandle       *local_handle;
	gchar                *status_string;
	GnomeVFSResult        ret;

	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:
		data->file_bytes_read = 0;
		data->file_bytes      = info->size;

		status_string = g_strdup_printf( _("File: \"%s\" (%d of %d)"),
						 info->name, (data->i+1), data->n );
		gtk_label_set_text( GTK_LABEL(file_label), status_string );
		g_free( status_string );

		update_progress_bar (GTK_PROGRESS_BAR(file_progress),
				     0,
				     data->file_bytes);
		update_progress_bar (GTK_PROGRESS_BAR(total_progress),
				     data->total_bytes_read,
				     data->total_bytes);
    
		local_name = hash_filename( info->name );
		local_path = g_build_filename (gb_util_get_home_data_dir(),
					       local_name,
					       NULL);
		local_text_uri = gnome_vfs_get_uri_from_local_path( local_path );
		ret = gnome_vfs_create( &local_handle, local_text_uri,
					GNOME_VFS_OPEN_WRITE, FALSE, 0664 );
		if ( ret != GNOME_VFS_OK ) {
			g_warning( "error opening local file %s, %s\n", local_path,
				   gnome_vfs_result_to_string(ret) );
		}
		else {
			data->local_handle = local_handle;
			gnome_vfs_async_read( handle, data->buffer, data->buffer_length,
					      read_remote_file_callback, data );
		}
		g_free( local_name );
		g_free( local_path );
		g_free( local_text_uri );
		break;

	default:
		g_warning( "Open failed: %s.\n", gnome_vfs_result_to_string(result) );
		break;

	}

	gb_debug (DEBUG_UPDATE, "END");
}