Ejemplo n.º 1
0
static gchar *
_load_uri (const gchar *uri)
{
    GnomeVFSHandle   *handle = NULL;
    GnomeVFSFileSize  bytes_read;

        gsize bytesRead = 0;
        gsize bytesWritten = 0;
        GError* error = NULL;
        gchar* uri_local = g_filename_from_utf8( uri, -1, &bytesRead, &bytesWritten, &error);

        if ( uri_local == NULL ) {
            g_warning( "Error converting filename to locale encoding.");
        }

    GnomeVFSResult result = gnome_vfs_open (&handle, uri_local, GNOME_VFS_OPEN_READ);

    if (result != GNOME_VFS_OK) {
        g_warning("%s", gnome_vfs_result_to_string(result));
    }

    std::vector<gchar> doc;
    while (result == GNOME_VFS_OK) {
        gchar buffer[BUF_SIZE];
        result = gnome_vfs_read (handle, buffer, BUF_SIZE, &bytes_read);
        doc.insert(doc.end(), buffer, buffer+bytes_read);
    }

    return g_strndup(&doc[0], doc.size());
}
Ejemplo n.º 2
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.º 3
0
gboolean
parse_ram (const gchar *path)
{
    gchar **lines, **alternatives;
    guint i, nalt;
    GnomeVFSHandle *h;
    char buf[4];
    GnomeVFSFileSize read, left;

    g_message ("parsing ram: %s", path);

    if (gnome_vfs_open (&h, path, GNOME_VFS_OPEN_READ))
        return FALSE;

    left = 4;
    while (left &&
           gnome_vfs_read (h, buf + (4 - left), left, &read) == GNOME_VFS_OK)
        left -= read;

    gnome_vfs_close (h);

    if (left) return FALSE;

    /* from gxine:
       if this is true, then its an actual stream, not a playlist file */
    if (buf[0] == '.' && buf[1] == 'R' && buf[2] == 'M' && buf[3] == 'F')
        return TRUE;

    if ((lines = read_file (path))) {

        alternatives = g_new (gchar*, 1);
        alternatives[0] = NULL;
        nalt = 0;

        for (i = 0; lines[i]; ++i) {
            lines[i] = g_strstrip (lines[i]);
            if (strlen (lines[i])) {

                /* comment */
                if (lines[i][0] == '#') continue;

                /* --stop-- lines */
                if (strstr (lines[i], "--stop--")) break;

                /* from gxine:
                   Either it's a rtsp, or a pnm mrl, but we also match http
                   mrls here.
                */
                if (g_str_has_prefix (lines[i], "rtsp://") ||
                    g_str_has_prefix (lines[i], "pnm://") ||
                    g_str_has_prefix (lines[i], "http://"))
                {
                    alternatives = g_renew (gchar*, alternatives, nalt+2);
                    alternatives[nalt] = g_strdup (lines[i]);
                    alternatives[nalt+1] = NULL;
                    ++nalt;
                }
            }
        }
Ejemplo n.º 4
0
void
bookmarks_load_from_disk (Bookmarks *bookmarks)
{
	GnomeVFSResult  result;
	GnomeVFSHandle *handle;
	char           *uri;
	char            line [MAX_LINE_LENGTH];

	g_return_if_fail (bookmarks != NULL);

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

	uri = g_strconcat (get_home_uri (),
			   "/",
			   bookmarks->rc_filename,
			   NULL);
	result = gnome_vfs_open (&handle, uri, GNOME_VFS_OPEN_READ);
	g_free (uri);

	if (result != GNOME_VFS_OK)
		return;

	while (_gnome_vfs_read_line (handle,
				     line,
				     MAX_LINE_LENGTH,
				     NULL) == GNOME_VFS_OK) {
		char *path;

		if (line[0] != '"')
			continue;

		line[strlen (line) - 1] = 0;
		path = line + 1;

		bookmarks->list = g_list_prepend (bookmarks->list, g_strdup (path));
		my_insert (bookmarks->names,
			   path,
			   get_uri_display_name (path));
		my_insert (bookmarks->tips,
			   path,
			   get_menu_item_tip (path));
	}

	gnome_vfs_close (handle);

	bookmarks->list = g_list_reverse (bookmarks->list);
}
Ejemplo n.º 5
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;
}
GnomeVFSResult
nsGnomeVFSInputStream::DoOpen()
{
  GnomeVFSResult rv;

  NS_ASSERTION(mHandle == nsnull, "already open");

  // Push a callback handler on the stack for this thread, so we can intercept
  // authentication requests from GnomeVFS.  We'll use the channel to get a
  // nsIAuthPrompt instance.

  gnome_vfs_module_callback_push(GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION,
                                 AuthCallback, mChannel, NULL);

  // Query the mime type first (this could return NULL). 
  //
  // XXX We need to do this up-front in order to determine how to open the URI.
  //     Unfortunately, the error code GNOME_VFS_ERROR_IS_DIRECTORY is not
  //     always returned by gnome_vfs_open when we pass it a URI to a directory!
  //     Otherwise, we could have used that as a way to failover to opening the
  //     URI as a directory.  Also, it would have been ideal if
  //     gnome_vfs_get_file_info_from_handle were actually implemented by the
  //     smb:// module, since that would have allowed us to potentially save a
  //     round trip to the server to discover the mime type of the document in
  //     the case where gnome_vfs_open would have been used.  (Oh well!  /me
  //     throws hands up in the air and moves on...)

  GnomeVFSFileInfo info = {0};
  rv = gnome_vfs_get_file_info(mSpec.get(), &info, GnomeVFSFileInfoOptions(
                               GNOME_VFS_FILE_INFO_DEFAULT |
                               GNOME_VFS_FILE_INFO_FOLLOW_LINKS));
  if (rv == GNOME_VFS_OK)
  {
    if (info.type == GNOME_VFS_FILE_TYPE_DIRECTORY)
    {
      rv = gnome_vfs_directory_list_load(&mDirList, mSpec.get(),
                                         GNOME_VFS_FILE_INFO_DEFAULT);

      LOG(("gnomevfs: gnome_vfs_directory_list_load returned %d (%s) [spec=\"%s\"]\n",
          rv, gnome_vfs_result_to_string(rv), mSpec.get()));
    }
    else
    {
      rv = gnome_vfs_open(&mHandle, mSpec.get(), GNOME_VFS_OPEN_READ);

      LOG(("gnomevfs: gnome_vfs_open returned %d (%s) [spec=\"%s\"]\n",
          rv, gnome_vfs_result_to_string(rv), mSpec.get()));
    }
  }

  gnome_vfs_module_callback_pop(GNOME_VFS_MODULE_CALLBACK_AUTHENTICATION);

  if (rv == GNOME_VFS_OK)
  {
    if (mHandle)
    {
      // Here we set the content type of the channel to the value of the mime
      // type determined by GnomeVFS.  However, if GnomeVFS is telling us that
      // the document is binary, we'll ignore that and keep the channel's
      // content type unspecified.  That will enable our content type sniffing
      // algorithms.  This should provide more consistent mime type handling.

      if (info.mime_type && (strcmp(info.mime_type, APPLICATION_OCTET_STREAM) != 0))
        SetContentTypeOfChannel(info.mime_type);

      mBytesRemaining = info.size;

      // Update the content length attribute on the channel.  We do this
      // synchronously without proxying.  This hack is not as bad as it looks!
      if (mBytesRemaining != PRUint64(-1))
        mChannel->SetContentLength(mBytesRemaining);
    }
    else
    {
      mDirOpen = PR_TRUE;

      // Sort mDirList
      mDirList = g_list_sort(mDirList, FileInfoComparator);
      mDirListPtr = mDirList;

      // Write base URL (make sure it ends with a '/')
      mDirBuf.Append("300: ");
      mDirBuf.Append(mSpec);
      if (mSpec.get()[mSpec.Length() - 1] != '/')
        mDirBuf.Append('/');
      mDirBuf.Append('\n');

      // Write column names
      mDirBuf.Append("200: filename content-length last-modified file-type\n");

      // Write charset (assume UTF-8)
      // XXX is this correct?
      mDirBuf.Append("301: UTF-8\n");

      SetContentTypeOfChannel(APPLICATION_HTTP_INDEX_FORMAT);
    }
  }

  gnome_vfs_file_info_clear(&info);
  return rv;
}