Ejemplo n.º 1
0
int 
profibus_serial_open(const char *device,  unsigned int rate, GError **err)
{
  int fd = open(device, O_RDWR);
  if (fd < 0) {
    g_set_error(err, G_FILE_ERROR, g_file_error_from_errno(errno),
		"open failed: %s", strerror(errno));
    return -1;
  }
  if (!set_termios(fd, rate, err)) {
    close(fd);
    return -1;
  }
  return fd;
}
Ejemplo n.º 2
0
static gboolean
save_to_fd (const gchar *buf, gsize count, GError **error, gpointer data)
{
    int fd = GPOINTER_TO_INT (data);
    gssize written;
    
    written = write (fd, buf, count);
    if (written != (gssize) count) {
        g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno), 
                     "%s", g_strerror (errno));
        return FALSE;
    }
    
    return TRUE;
}
Ejemplo n.º 3
0
/* coverity[ -tainted_string_sink_content : arg-0 ] */
GsfInput *
gsf_input_stdio_new (char const *filename, GError **err)
{
	GsfInputStdio *input;
	struct stat st;
	FILE *file;
	gsf_off_t size;

	g_return_val_if_fail (filename != NULL, NULL);

	file = g_fopen (filename, "rb");
	if (file == NULL) {
		if (err) {
			int save_errno = errno;
			char *utf8name = g_filename_display_name (filename);
			g_set_error (err,
				     G_FILE_ERROR,
				     g_file_error_from_errno (save_errno),
				     "%s: %s",
				     utf8name, g_strerror (save_errno));
			g_free (utf8name);
		}
		return NULL;
	}

	if (fstat (fileno (file), &st) < 0 || !S_ISREG (st.st_mode)) {
		GsfInput *res = make_local_copy (file, filename, err);
		fclose (file);
		return res;
	}

	size = st.st_size;
	input = (GsfInputStdio *)g_object_new (GSF_INPUT_STDIO_TYPE, NULL);
	if (G_UNLIKELY (NULL == input)) {
		fclose (file);
		return NULL;
	}

	input->file = file;
	input->filename = g_strdup (filename);
	input->buf  = NULL;
	input->buf_size = 0;
	input->keep_open = FALSE;
	gsf_input_set_size (GSF_INPUT (input), size);
	gsf_input_set_name_from_filename (GSF_INPUT (input), filename);

	return GSF_INPUT (input);
}
/**
 * xfce_posix_signal_handler_set_handler:
 * @signal_id: A POSIX signal id number.
 * @handler: A callback function.
 * @user_data: Arbitrary data that will be passed to @handler.
 * @error: Location of a #GError to store any possible errors.
 *
 * Sets @handler to be called whenever @signal is caught by the
 * application.  The @user_data parameter will be passed as an argument
 * to @handler.
 *
 * Returns: %TRUE on success, %FALSE otherwise, in which case
 *          @error will be set.
 **/
gboolean
xfce_posix_signal_handler_set_handler(gint signal_id,
                                      XfcePosixSignalHandler handler,
                                      gpointer user_data,
                                      GError **error)
{
    XfcePosixSignalHandlerData *hdata;
    struct sigaction sa;

    if(G_UNLIKELY(!__inited)) {
        if(error) {
            g_set_error(error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                        _("xfce_posix_signal_handler_init() must be called first"));
        }
        return FALSE;
    }

    if(!handler) {
        g_warning("NULL signal handler supplied; removing existing handler");
        xfce_posix_signal_handler_restore_handler(signal_id);
        return TRUE;
    }

    if(g_hash_table_lookup(__handlers, GINT_TO_POINTER(signal_id)))
        xfce_posix_signal_handler_restore_handler(signal_id);

    hdata = g_new0(XfcePosixSignalHandlerData, 1);
    hdata->signal_id = signal_id;
    hdata->handler = handler;
    hdata->user_data = user_data;

    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = xfce_posix_signal_handler;
    sa.sa_flags = SA_RESTART;

    if(sigaction(signal_id, &sa, &hdata->old_sa)) {
        if(error) {
            g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno),
                        _("sigaction() failed: %s\n"), strerror(errno));
        }
        g_free(hdata);
        return FALSE;
    }

    g_hash_table_insert(__handlers, GINT_TO_POINTER(signal_id), hdata);

    return TRUE;
}
Ejemplo n.º 5
0
/* Creates the certificate directory if it doesn't exist,
 * returns TRUE if it's successful or it already exists,
 * returns FALSE if there was an error.
 */
static gboolean
ensure_certificate_dir(GError **error)
{
	gchar *dir = make_certificate_path(NULL);
	gboolean ret = TRUE;

	if (purple_build_dir(dir, 0700) != 0) {
		g_set_error_literal(error, G_FILE_ERROR,
				g_file_error_from_errno(errno),
				g_strerror(errno));
		ret = FALSE;
	}

	g_free(dir);
	return ret;
}
Ejemplo n.º 6
0
/**
 * xfae_model_edit:
 * @model       : a #XfaeModel.
 * @iter        : the #GtkTreeIter referring to the item that should be removed.
 * @name        : the user visible name of the new item.
 * @description : the description for the new item.
 * @command     : the command for the new item.
 * @error       : return locations for errors or %NULL.
 *
 * Attempts to edit an item with the given parameters
 * to @model.
 *
 * Return value: %TRUE if successfull, else %FALSE.
 **/
gboolean
xfae_model_edit (XfaeModel   *model,
                 GtkTreeIter *iter,
                 const gchar *name,
                 const gchar *description,
                 const gchar *command,
                 GError     **error)
{
  GtkTreePath *path;
  XfaeItem    *item;
  XfceRc      *rc;
  GList       *lp;

  g_return_val_if_fail (XFAE_IS_MODEL (model), FALSE);
  g_return_val_if_fail (iter->stamp == model->stamp, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  lp = iter->user_data;
  item = lp->data;

  /* try to open the resource config */
  rc = xfce_rc_config_open (XFCE_RESOURCE_CONFIG, item->relpath, FALSE);
  if (G_UNLIKELY (rc == NULL))
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (EIO),
                   _("Failed to open %s for writing"), item->relpath);
      return FALSE;
    }

  item->name = g_strdup (name);
  item->comment = g_strdup (description);

  /* write the result */
  xfce_rc_set_group (rc, "Desktop Entry");
  xfce_rc_write_entry (rc, "Name", name);
  xfce_rc_write_entry (rc, "Comment", description);
  xfce_rc_write_entry (rc, "Exec", command);
  xfce_rc_close (rc);

  /* tell the view that we have most probably a new state */
  path = gtk_tree_path_new_from_indices (g_list_position (model->items, lp), -1);
  gtk_tree_model_row_changed (GTK_TREE_MODEL (model), path, iter);
  gtk_tree_path_free (path);

  return TRUE;
}
Ejemplo n.º 7
0
/**
 * xfae_model_get:
 * @model       : a #XfaeModel.
 * @name        : the user visible name of the new item.
 * @description : the description for the new item.
 * @command     : the command for the new item.
 * @error       : return locations for errors or %NULL.
 *
 * Attempts to add a new item with the given parameters
 * to @model.
 *
 * Return value: %TRUE if successfull, else %FALSE.
 **/
gboolean
xfae_model_get (XfaeModel    *model,
                GtkTreeIter  *iter,
                gchar       **name,
                gchar       **description,
                gchar       **command,
                GError      **error)
{
  XfaeItem    *item;
  GList       *lp;
  XfceRc      *rc;
  const gchar *value;

  g_return_val_if_fail (XFAE_IS_MODEL (model), FALSE);
  g_return_val_if_fail (iter->stamp == model->stamp, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  lp = iter->user_data;
  item = lp->data;

  /* try to open the resource config */
  rc = xfce_rc_config_open (XFCE_RESOURCE_CONFIG, item->relpath, FALSE);
  if (G_UNLIKELY (rc == NULL))
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (EIO),
                   _("Failed to open %s for reading"), item->relpath);
      return FALSE;
    }

  /* read the resource config */
  value = xfce_rc_read_entry (rc, "Name", NULL);
  if (name != NULL)
   *name = g_strdup (value);

  value = xfce_rc_read_entry (rc, "Comment", NULL);
  if (description != NULL)
    *description = g_strdup (value);

  value = xfce_rc_read_entry (rc, "Exec", NULL);
  if (command != NULL)
    *command = g_strdup (value);

  xfce_rc_close (rc);

  return TRUE;
}
Ejemplo n.º 8
0
GList *
gimp_palette_load_riff (const gchar  *filename,
                        GError      **error)
{
  GimpPalette *palette;
  gchar       *palette_name;
  gint         fd;
  guchar       color_bytes[4];

  g_return_val_if_fail (filename != NULL, NULL);
  g_return_val_if_fail (g_path_is_absolute (filename), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  fd = g_open (filename, O_RDONLY | _O_BINARY, 0);
  if (! fd)
    {
      g_set_error (error,
                   G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not open '%s' for reading: %s"),
                   gimp_filename_to_utf8 (filename), g_strerror (errno));
      return NULL;
    }

  palette_name = g_filename_display_basename (filename);
  palette = GIMP_PALETTE (gimp_palette_new (palette_name));
  g_free (palette_name);

  lseek (fd, 28, SEEK_SET);
  while (read (fd,
               color_bytes, sizeof (color_bytes)) == sizeof (color_bytes))
    {
      GimpRGB color;

      gimp_rgba_set_uchar (&color,
                           color_bytes[0],
                           color_bytes[1],
                           color_bytes[2],
                           255);
      gimp_palette_add_entry (palette, -1, NULL, &color);
    }

  close (fd);

  return g_list_prepend (NULL, palette);
}
Ejemplo n.º 9
0
/* Utility function */
void
psd_set_error (const gboolean   file_eof,
               const gint       err_no,
               GError         **error)
{
  if (file_eof)
    {
      g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                   "%s", _("Unexpected end of file"));
    }
  else
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (err_no),
                   "%s", g_strerror (err_no));
    }

  return;
}
Ejemplo n.º 10
0
LJEntry*
lj_entry_new_from_filename(const char *filename, LJEntryFileType type,
                           LJEntryFileType *typeret, GError **err) {
	LJEntry *entry;
	FILE *f;

	f = fopen(filename, "r");
	if (!f) {
		g_set_error(err, G_FILE_ERROR, g_file_error_from_errno(errno),
				_("Error reading file '%s': %s"), filename, g_strerror(errno));
		return NULL;
	}

	entry = lj_entry_new_from_file(f, type, typeret, err);
	fclose(f);

	return entry;
}
Ejemplo n.º 11
0
static GFile *
create_snapshot_file (EMsgComposer *composer,
                      GError **error)
{
	GFile *snapshot_file;
	const gchar *user_data_dir;
	gchar *path;
	gint fd;

	snapshot_file = e_composer_get_snapshot_file (composer);

	if (G_IS_FILE (snapshot_file))
		return snapshot_file;

	user_data_dir = e_get_user_data_dir ();
	path = g_build_filename (user_data_dir, SNAPSHOT_FILE_SEED, NULL);

	/* g_mkstemp() modifies the XXXXXX part of the
	 * template string to form the actual filename. */
	errno = 0;
	fd = g_mkstemp (path);
	if (fd == -1) {
		g_set_error (
			error, G_FILE_ERROR,
			g_file_error_from_errno (errno),
			"%s", g_strerror (errno));
		g_free (path);
		return NULL;
	}

	close (fd);

	snapshot_file = g_file_new_for_path (path);

	/* Save the GFile for subsequent snapshots. */
	g_object_set_data_full (
		G_OBJECT (composer),
		SNAPSHOT_FILE_KEY, snapshot_file,
		(GDestroyNotify) delete_snapshot_file);

	g_free (path);

	return snapshot_file;
}
Ejemplo n.º 12
0
static GdkPixbuf *
icns_image_load (FILE *f, GError ** error)
{
  GByteArray *data;
  GdkPixbuf *pixbuf = NULL;
  guint i;

  data = g_byte_array_new ();
  while (!feof (f))
    {
      gint save_errno;
      guchar buf[4096];
      gsize bytes;

      bytes = fread (buf, 1, sizeof (buf), f);
      save_errno = errno;
      data = g_byte_array_append (data, buf, bytes);

      if (ferror (f))
        {
	  g_set_error (error,
		       G_FILE_ERROR,
		       g_file_error_from_errno (save_errno),
		       _("Error reading ICNS image: %s"),
		       g_strerror (save_errno));

	  g_byte_array_free (data, TRUE);

	  return NULL;
	}
    }

  for (i = 0; i < G_N_ELEMENTS(sizes) && !pixbuf; i++)
    pixbuf = load_icon (sizes[i], data->data, data->len);

  g_byte_array_free (data, TRUE);

  if (!pixbuf)
    g_set_error_literal (error, GDK_PIXBUF_ERROR,
                         GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                         _("Could not decode ICNS file"));

  return pixbuf;
}
Ejemplo n.º 13
0
gboolean
xcf_seek_pos (XcfInfo  *info,
              guint     pos,
              GError  **error)
{
  if (info->cp != pos)
    {
      info->cp = pos;
      if (fseek (info->fp, info->cp, SEEK_SET) == -1)
        {
          g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                       _("Could not seek in XCF file: %s"),
                       g_strerror (errno));

          return FALSE;
        }
    }

  return TRUE;
}
Ejemplo n.º 14
0
static gboolean
clutter_md2_data_seek (FILE *file, size_t offset,
                       const gchar *display_name, GError **error)
{
  if (fseek (file, offset, SEEK_SET))
    {
      gint save_errno = errno;

      g_set_error (error,
                   G_FILE_ERROR,
                   g_file_error_from_errno (save_errno),
                   "Error reading '%s': %s",
                   display_name,
                   g_strerror (save_errno));

      return FALSE;
    }
  else
    return TRUE;
}
Ejemplo n.º 15
0
static GIOStatus
g_io_win32_set_flags (GIOChannel     *channel,
                      GIOFlags        flags,
                      GError        **err)
{
  GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;

  if (win32_channel->debug)
    {
      g_print ("g_io_win32_set_flags: ");
      g_win32_print_gioflags (flags);
      g_print ("\n");
    }

  g_set_error (err, 
               G_IO_CHANNEL_ERROR, 
               g_file_error_from_errno (EACCES), 
               _("Channel set flags unsupported"));
  return G_IO_STATUS_ERROR;
}
Ejemplo n.º 16
0
gboolean
conf_rename_host(JamHost *host, const char *newname, GError **err) {
	char *oldpath, *newpath;

	/* disallow:
	 *   [empty string]
	 *   .
	 *   ..
	 *   ./../foo
	 *   /foo
	 * allow:
	 *   .lAmE.sErVeR.
	 */
	if ((newname[0] == 0) ||
			(newname[0] == '.' &&
				(newname[1] == '.' || newname[1] == '/' || newname[1] == 0)) ||
			(newname[0] == '/')) {
		g_set_error(err, 0, 0, _("new host name is invalid"));
		return FALSE;
	}
	oldpath = g_build_filename(app.conf_dir, "servers", host->name, NULL);
	if (!g_file_test(oldpath, G_FILE_TEST_EXISTS)) {
		string_replace(&host->name, g_strdup(newname));
		g_free(oldpath);
		return TRUE;
	}
	newpath = g_build_filename(app.conf_dir, "servers", newname, NULL);
	if (rename(oldpath, newpath) < 0) {
		g_set_error(err, G_FILE_ERROR, g_file_error_from_errno(errno),
				_("renaming '%s' to '%s': %s"), oldpath, newpath,
				g_strerror(errno));
		g_free(oldpath);
		g_free(newpath);
		return FALSE;
	}
	string_replace(&host->name, g_strdup(newname));
	g_free(oldpath);
	g_free(newpath);
	return TRUE;
}
Ejemplo n.º 17
0
Archivo: io-xpm.c Proyecto: chipx86/gtk
static gboolean
gdk_pixbuf__xpm_image_load_increment (gpointer data,
                                      const guchar *buf,
                                      guint    size,
                                      GError **error)
{
       XPMContext *context = (XPMContext *) data;

       g_return_val_if_fail (data != NULL, FALSE);

       if (fwrite (buf, sizeof (guchar), size, context->file) != size) {
	       gint save_errno = errno;
               context->all_okay = FALSE;
               g_set_error_literal (error,
                                    G_FILE_ERROR,
                                    g_file_error_from_errno (save_errno),
                                    _("Failed to write to temporary file when loading XPM image"));
               return FALSE;
       }

       return TRUE;
}
Ejemplo n.º 18
0
gboolean
gdip_save_to_file_callback (const gchar *buf,
                            gsize        count,
                            GError     **error,
                            gpointer     data)
{
  FILE *filehandle = data;
  gsize n;
  
  n = fwrite (buf, 1, count, filehandle);
  if (n != count) {
    gint save_errno = errno;
    g_set_error (error,
                 G_FILE_ERROR,
                 g_file_error_from_errno (save_errno),
                 _("Error writing to image file: %s"),
                 g_strerror (save_errno));
    return FALSE;
  }
  
  return TRUE;
}
Ejemplo n.º 19
0
static gchar *
create_temp_file (const gchar *directory,
                  const gchar *templ,
                  GError **error)
{
  gchar *path;
  gint fd;

  path = g_build_filename (directory, templ, NULL);
  fd = g_mkstemp (path);
  if (fd < 0)
    {
      g_set_error (error, G_FILE_ERROR,
                   g_file_error_from_errno (errno),
                   "Couldn't create temporary file: %s: %m", path);
      g_free (path);
      return NULL;
    }

  close (fd);
  return path;
}
Ejemplo n.º 20
0
gboolean
gimp_contexts_clear (Gimp    *gimp,
                     GError **error)
{
  gchar    *filename;
  gboolean  success = TRUE;

  g_return_val_if_fail (GIMP_IS_GIMP (gimp), FALSE);

  filename = gimp_personal_rc_file ("contextrc");

  if (g_unlink (filename) != 0 && errno != ENOENT)
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Deleting \"%s\" failed: %s"),
                   gimp_filename_to_utf8 (filename), g_strerror (errno));
      success = FALSE;
    }

  g_free (filename);

  return success;
}
Ejemplo n.º 21
0
gboolean
purple_tls_certificate_distrust(const gchar *id, GError **error)
{
	gchar *path;
	gboolean ret = TRUE;

	g_return_val_if_fail(id != NULL && id[0] != '\0', FALSE);

	/* Delete certificate file if it exists */

	path = make_certificate_path(id);

	if (g_unlink(path) != 0) {
		g_set_error_literal(error, G_FILE_ERROR,
				g_file_error_from_errno(errno),
				g_strerror(errno));
		ret = FALSE;
	}

	g_free(path);

	return ret;
}
Ejemplo n.º 22
0
static gboolean
composer_autosave_state_open (AutosaveState *state,
                              GError **error)
{
	if (state->filename != NULL)
		return TRUE;

	state->filename = g_build_filename (
		e_get_user_data_dir (), AUTOSAVE_SEED, NULL);

	errno = 0;
	if ((state->fd = g_mkstemp (state->filename)) >= 0)
		return TRUE;

	g_set_error (
		error, G_FILE_ERROR,
		g_file_error_from_errno (errno),
		"%s: %s", state->filename, g_strerror (errno));

	g_free (state->filename);
	state->filename = NULL;

	return FALSE;
}
Ejemplo n.º 23
0
GDir *
g_dir_open (const gchar *path, guint flags, GError **gerror)
{
	GDir *dir;

	g_return_val_if_fail (path != NULL, NULL);
	g_return_val_if_fail (gerror == NULL || *gerror == NULL, NULL);

	(void) flags; /* this is not used */
	dir = g_new (GDir, 1);
	dir->dir = opendir (path);
	if (dir->dir == NULL) {
		if (gerror) {
			gint err = errno;
			*gerror = g_error_new (G_LOG_DOMAIN, g_file_error_from_errno (err), strerror (err));
		}
		g_free (dir);
		return NULL;
	}
#ifndef HAVE_REWINDDIR
	dir->path = g_strdup (path);
#endif
	return dir;
}
Ejemplo n.º 24
0
static gboolean
xfae_item_remove (XfaeItem *item,
                  GError  **error)
{
  gboolean succeed = TRUE;
  gchar  **files;
  guint    n;

  /* remove all files that belong to this item */
  files = xfce_resource_lookup_all (XFCE_RESOURCE_CONFIG, item->relpath);
  for (n = 0; files[n] != NULL; ++n)
    {
      if (unlink (files[n]) < 0 && errno != ENOENT)
        {
          g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                       _("Failed to unlink %s: %s"), files[n], g_strerror (errno));
          succeed = FALSE;
          break;
        }
    }
  g_strfreev (files);

  return succeed;
}
Ejemplo n.º 25
0
static gboolean
get_contents_posix (const gchar *filename,
                    gchar      **contents,
                    gsize       *length,
                    GError     **error)
{
  struct stat stat_buf;
  gint fd;
  gchar *display_filename = g_filename_display_name (filename);

  /* O_BINARY useful on Cygwin */
  fd = open (filename, O_RDONLY|O_BINARY);

  if (fd < 0)
    {
      int save_errno = errno;

      g_set_error (error,
                   G_FILE_ERROR,
                   g_file_error_from_errno (save_errno),
                   _("Failed to open file '%s': %s"),
                   display_filename, 
		   g_strerror (save_errno));
      g_free (display_filename);

      return FALSE;
    }

  /* I don't think this will ever fail, aside from ENOMEM, but. */
  if (fstat (fd, &stat_buf) < 0)
    {
      int save_errno = errno;

      close (fd);
      g_set_error (error,
                   G_FILE_ERROR,
                   g_file_error_from_errno (save_errno),
                   _("Failed to get attributes of file '%s': fstat() failed: %s"),
                   display_filename, 
		   g_strerror (save_errno));
      g_free (display_filename);

      return FALSE;
    }

  if (stat_buf.st_size > 0 && S_ISREG (stat_buf.st_mode))
    {
      gboolean retval = get_contents_regfile (display_filename,
					      &stat_buf,
					      fd,
					      contents,
					      length,
					      error);
      g_free (display_filename);

      return retval;
    }
  else
    {
      FILE *f;
      gboolean retval;

      f = fdopen (fd, "r");
      
      if (f == NULL)
        {
	  int save_errno = errno;

          g_set_error (error,
                       G_FILE_ERROR,
                       g_file_error_from_errno (save_errno),
                       _("Failed to open file '%s': fdopen() failed: %s"),
                       display_filename, 
		       g_strerror (save_errno));
          g_free (display_filename);

          return FALSE;
        }
  
      retval = get_contents_stdio (display_filename, f, contents, length, error);
      g_free (display_filename);

      return retval;
    }
}
Ejemplo n.º 26
0
static gboolean
get_contents_regfile (const gchar *display_filename,
                      struct stat *stat_buf,
                      gint         fd,
                      gchar      **contents,
                      gsize       *length,
                      GError     **error)
{
  gchar *buf;
  gsize bytes_read;
  gsize size;
  gsize alloc_size;
  
  size = stat_buf->st_size;

  alloc_size = size + 1;
  buf = g_try_malloc (alloc_size);

  if (buf == NULL)
    {
      g_set_error (error,
                   G_FILE_ERROR,
                   G_FILE_ERROR_NOMEM,
                   _("Could not allocate %lu bytes to read file \"%s\""),
                   (gulong) alloc_size, 
		   display_filename);

      goto error;
    }
  
  bytes_read = 0;
  while (bytes_read < size)
    {
      gssize rc;
          
      rc = read (fd, buf + bytes_read, size - bytes_read);

      if (rc < 0)
        {
          if (errno != EINTR) 
            {
	      int save_errno = errno;

              g_free (buf);
              g_set_error (error,
                           G_FILE_ERROR,
                           g_file_error_from_errno (save_errno),
                           _("Failed to read from file '%s': %s"),
                           display_filename, 
			   g_strerror (save_errno));

	      goto error;
            }
        }
      else if (rc == 0)
        break;
      else
        bytes_read += rc;
    }
      
  buf[bytes_read] = '\0';

  if (length)
    *length = bytes_read;
  
  *contents = buf;

  close (fd);

  return TRUE;

 error:

  close (fd);
  
  return FALSE;
}
Ejemplo n.º 27
0
static gboolean
get_contents_stdio (const gchar *display_filename,
                    FILE        *f,
                    gchar      **contents,
                    gsize       *length,
                    GError     **error)
{
  gchar buf[4096];
  gsize bytes;
  gchar *str = NULL;
  gsize total_bytes = 0;
  gsize total_allocated = 0;
  gchar *tmp;

  g_assert (f != NULL);

  while (!feof (f))
    {
      gint save_errno;

      bytes = fread (buf, 1, sizeof (buf), f);
      save_errno = errno;

      while ((total_bytes + bytes + 1) > total_allocated)
        {
          if (str)
            total_allocated *= 2;
          else
            total_allocated = MIN (bytes + 1, sizeof (buf));

          tmp = g_try_realloc (str, total_allocated);

          if (tmp == NULL)
            {
              g_set_error (error,
                           G_FILE_ERROR,
                           G_FILE_ERROR_NOMEM,
                           _("Could not allocate %lu bytes to read file \"%s\""),
                           (gulong) total_allocated,
			   display_filename);

              goto error;
            }

	  str = tmp;
        }

      if (ferror (f))
        {
          g_set_error (error,
                       G_FILE_ERROR,
                       g_file_error_from_errno (save_errno),
                       _("Error reading file '%s': %s"),
                       display_filename,
		       g_strerror (save_errno));

          goto error;
        }

      memcpy (str + total_bytes, buf, bytes);
      total_bytes += bytes;
    }

  fclose (f);

  if (total_allocated == 0)
    str = g_new (gchar, 1);

  str[total_bytes] = '\0';

  if (length)
    *length = total_bytes;

  *contents = str;

  return TRUE;

 error:

  g_free (str);
  fclose (f);

  return FALSE;
}
Ejemplo n.º 28
0
static GimpValueArray *
xcf_save_invoker (GimpProcedure         *procedure,
                  Gimp                  *gimp,
                  GimpContext           *context,
                  GimpProgress          *progress,
                  const GimpValueArray  *args,
                  GError               **error)
{
  XcfInfo         info;
  GimpValueArray *return_vals;
  GimpImage      *image;
  const gchar    *filename;
  gboolean        success = FALSE;

  gimp_set_busy (gimp);

  image    = gimp_value_get_image (gimp_value_array_index (args, 1), gimp);
  filename = g_value_get_string (gimp_value_array_index (args, 3));

  info.fp = g_fopen (filename, "wb");

  if (info.fp)
    {
      info.gimp                  = gimp;
      info.progress              = progress;
      info.cp                    = 0;
      info.filename              = filename;
      info.active_layer          = NULL;
      info.active_channel        = NULL;
      info.floating_sel_drawable = NULL;
      info.floating_sel          = NULL;
      info.floating_sel_offset   = 0;
      info.swap_num              = 0;
      info.ref_count             = NULL;
      info.compression           = COMPRESS_RLE;

      if (progress)
        {
          gchar *name = g_filename_display_name (filename);
          gchar *msg  = g_strdup_printf (_("Saving '%s'"), name);

          gimp_progress_start (progress, msg, FALSE);

          g_free (msg);
          g_free (name);
        }

      xcf_save_choose_format (&info, image);

      success = xcf_save_image (&info, image, error);

      if (success)
        {
          if (fclose (info.fp) == EOF)
            {
              int save_errno = errno;

              g_set_error (error, G_FILE_ERROR,
                           g_file_error_from_errno (save_errno),
                            _("Error saving XCF file: %s"),
                           g_strerror (save_errno));

              success = FALSE;
            }
        }
      else
        {
          fclose (info.fp);
        }

      if (progress)
        gimp_progress_end (progress);
    }
  else
    {
      int save_errno = errno;

      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (save_errno),
                   _("Could not open '%s' for writing: %s"),
                   gimp_filename_to_utf8 (filename), g_strerror (save_errno));
    }

  return_vals = gimp_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  gimp_unset_busy (gimp);

  return return_vals;
}
Ejemplo n.º 29
0
static GimpValueArray *
xcf_load_invoker (GimpProcedure         *procedure,
                  Gimp                  *gimp,
                  GimpContext           *context,
                  GimpProgress          *progress,
                  const GimpValueArray  *args,
                  GError               **error)
{
  XcfInfo         info;
  GimpValueArray *return_vals;
  GimpImage      *image   = NULL;
  const gchar    *filename;
  gboolean        success = FALSE;
  gchar           id[14];

  gimp_set_busy (gimp);

  filename = g_value_get_string (gimp_value_array_index (args, 1));

  info.fp = g_fopen (filename, "rb");

  if (info.fp)
    {
      info.gimp                  = gimp;
      info.progress              = progress;
      info.cp                    = 0;
      info.filename              = filename;
      info.tattoo_state          = 0;
      info.active_layer          = NULL;
      info.active_channel        = NULL;
      info.floating_sel_drawable = NULL;
      info.floating_sel          = NULL;
      info.floating_sel_offset   = 0;
      info.swap_num              = 0;
      info.ref_count             = NULL;
      info.compression           = COMPRESS_NONE;

      if (progress)
        {
          gchar *name = g_filename_display_name (filename);
          gchar *msg  = g_strdup_printf (_("Opening '%s'"), name);

          gimp_progress_start (progress, msg, FALSE);

          g_free (msg);
          g_free (name);
        }

      success = TRUE;

      info.cp += xcf_read_int8 (info.fp, (guint8 *) id, 14);

      if (! g_str_has_prefix (id, "gimp xcf "))
        {
          success = FALSE;
        }
      else if (strcmp (id + 9, "file") == 0)
        {
          info.file_version = 0;
        }
      else if (id[9] == 'v')
        {
          info.file_version = atoi (id + 10);
        }
      else
        {
          success = FALSE;
        }

      if (success)
        {
          if (info.file_version >= 0 &&
              info.file_version < G_N_ELEMENTS (xcf_loaders))
            {
              image = (*(xcf_loaders[info.file_version])) (gimp, &info, error);

              if (! image)
                success = FALSE;
            }
          else
            {
              g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
                           _("XCF error: unsupported XCF file version %d "
                             "encountered"), info.file_version);
              success = FALSE;
            }
        }

      fclose (info.fp);

      if (progress)
        gimp_progress_end (progress);
    }
  else
    {
      int save_errno = errno;

      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (save_errno),
                   _("Could not open '%s' for reading: %s"),
                   gimp_filename_to_utf8 (filename), g_strerror (save_errno));
    }

  return_vals = gimp_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    gimp_value_set_image (gimp_value_array_index (return_vals, 1), image);

  gimp_unset_busy (gimp);

  return return_vals;
}
Ejemplo n.º 30
0
bool XojPopplerDocument::load(const char * filename, const char * password, GError ** error) {
	XOJ_CHECK_TYPE(XojPopplerDocument);

	PDFDoc * newDoc;
	GooString * filename_g;
	GooString * password_g;

	if (!globalParams) {
		globalParams = new GlobalParams();
	}

	if (!filename) {
		return false;
	}

	password_g = NULL;
	if (password != NULL) {
		if (g_utf8_validate(password, -1, NULL)) {
			gchar *password_latin;

			password_latin = g_convert(password, -1, "ISO-8859-1", "UTF-8", NULL, NULL, NULL);
			password_g = new GooString(password_latin);
			g_free(password_latin);
		} else {
			password_g = new GooString(password);
		}
	}

#ifdef G_OS_WIN32
	wchar_t *filenameW;
	int wlen;

	wlen = MultiByteToWideChar(CP_UTF8, 0, filename, -1, NULL, 0);

	filenameW = new WCHAR[wlen];
	if (!filenameW)
	return NULL;

	wlen = MultiByteToWideChar(CP_UTF8, 0, filename, -1, filenameW, wlen);

	newDoc = new PDFDoc(filenameW, wlen, password_g, password_g);
	delete filenameW;
#else
	filename_g = new GooString(filename);
	newDoc = new PDFDoc(filename_g, password_g, password_g);
#endif
	delete password_g;

	if (!newDoc->isOk()) {
		int fopen_errno;
		switch (newDoc->getErrorCode()) {
		case errOpenFile:
			// If there was an error opening the file, count it as a G_FILE_ERROR
			// and set the GError parameters accordingly. (this assumes that the
			// only way to get an errOpenFile error is if newDoc was created using
			// a filename and thus fopen was called, which right now is true.
			fopen_errno = newDoc->getFopenErrno();
			g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(fopen_errno), "%s", g_strerror(fopen_errno));
			break;
		case errBadCatalog:
			g_set_error(error, 0, 0, "Failed to read the document catalog");
			break;
		case errDamaged:
			g_set_error(error, 0, 0, "PDF document is damaged");
			break;
		case errEncrypted:
			g_set_error(error, 0, 0, "Document is encrypted");
			break;
		default:
			g_set_error(error, 0, 0, "Failed to load document");
		}

		delete newDoc;
		return false;
	}

	if (this->data) {
		this->data->unreference();
	}
	this->data = new _IntPopplerDocument(newDoc);

	return true;
}