Exemplo n.º 1
0
static void on_file_read_ready (GObject * object, GAsyncResult * res, gpointer data)
{
	GError *error = NULL;
	GDownloadable *download = G_DOWNLOADABLE (data);
	GioDownload   *gio_download = GIO_DOWNLOAD (data);
	g_assert (download != NULL);
	
	gio_download->priv->input = g_file_read_finish (G_FILE(object), res, &error); 
	handle_critical_error (error);
	
	if (gio_download->priv->input == NULL) {
		// TODO: Error details
		g_downloadable_set_status (download, G_DOWNLOADABLE_NETWORK_ERROR);
	} else {
		if (g_seekable_can_seek(G_SEEKABLE(gio_download->priv->input))
			&& g_file_query_exists (download->priv->local_file, NULL)) {
			//g_message ("appeding file ...\n");
			g_file_append_to_async (download->priv->local_file, 0,G_PRIORITY_DEFAULT,
									NULL, on_file_append_to_ready, download); 
		} else {
			//g_message ("replacing file ...\n");
			g_file_replace_async (download->priv->local_file, NULL, FALSE, 0, G_PRIORITY_DEFAULT,
								  NULL, on_file_replace_ready,  download);
		}
		g_downloadable_set_status (download, G_DOWNLOADABLE_DOWNLOADING);
	}
}
Exemplo n.º 2
0
static gboolean
g_resource_file_input_stream_can_seek (GFileInputStream *stream)
{
  GResourceFileInputStream *file = G_RESOURCE_FILE_INPUT_STREAM (stream);

  return G_IS_SEEKABLE (file->stream) && g_seekable_can_seek (G_SEEKABLE (file->stream));
}
Exemplo n.º 3
0
static gboolean
can_seek (GInputStream *stream)
{
    if (!G_IS_SEEKABLE (stream))
        return FALSE;

    return g_seekable_can_seek (G_SEEKABLE (stream));
}
Exemplo n.º 4
0
static void
ddb_gvfs_rewind (DB_FILE *stream)
{
  vfs_gvfs_data_t *data = (vfs_gvfs_data_t *) stream;
  g_return_if_fail (data != NULL);

  if (g_seekable_can_seek (G_SEEKABLE (data->handle)))
    g_seekable_seek (G_SEEKABLE (data->handle), 0, G_SEEK_SET, NULL, NULL);
}
Exemplo n.º 5
0
static gboolean
process(GeglOperation *operation,
        GeglBuffer *input,
        const GeglRectangle *result,
        int level)
{
  GeglProperties *o = GEGL_PROPERTIES(operation);
  Priv *p = g_new0(Priv, 1);
  gboolean status = TRUE;
  GError *error = NULL;

  g_assert(p != NULL);

  o->user_data = (void*) p;

  p->stream = gegl_gio_open_output_stream(NULL, o->path, &p->file, &error);
  if (p->stream != NULL && p->file != NULL)
    p->can_seek = g_seekable_can_seek(G_SEEKABLE(p->stream));
  if (p->stream == NULL)
    {
      status = FALSE;
      g_warning("%s", error->message);
      goto cleanup;
    }

  TIFFSetErrorHandler(error_handler);
  TIFFSetWarningHandler(warning_handler);

  p->tiff = TIFFClientOpen("GEGL-tiff-save", "w", (thandle_t) p,
                           read_from_stream, write_to_stream,
                           seek_in_stream, close_stream,
                           get_file_size, NULL, NULL);
  if (p->tiff == NULL)
    {
      status = FALSE;
      g_warning("failed to open TIFF from %s", o->path);
      goto cleanup;
    }

  if (export_tiff(operation, input, result))
    {
      status = FALSE;
      g_warning("could not export TIFF file");
      goto cleanup;
    }

cleanup:
  cleanup(operation);
  if (o->user_data != NULL)
    g_free(o->user_data);
  o->user_data = NULL;

  if (error != NULL)
    g_error_free(error);

  return status;
}
Exemplo n.º 6
0
/* Called from gpgme to seek a file */
static off_t
vfs_data_seek (void *handle, off_t offset, int whence)
{
    VfsAsyncHandle* ah = (VfsAsyncHandle*)handle;
    GSeekable *seekable = NULL;
    GSeekType wh;
    
    /* If the file isn't open yet, then do that now */
    if (!ah->ostream && !ah->istream && ah->state == VFS_ASYNC_READY)
        vfs_data_open_helper (ah);
        
    /* Just in case we have an operation, like open */
    if (!vfs_data_wait_results(ah, TRUE))
        return (off_t)-1;
           
    g_assert (ah->state == VFS_ASYNC_READY);
    
    if (ah->writer && G_IS_SEEKABLE (ah->ostream))
        seekable = G_SEEKABLE (ah->ostream);
    else
        seekable = G_SEEKABLE (ah->istream);
    
    if (!seekable || !g_seekable_can_seek (seekable)) {
        errno = ENOTSUP;
        return -1;
    }
    
    switch(whence)
    {
    case SEEK_SET:
        wh = G_SEEK_SET;
        break;
    case SEEK_CUR:
        wh = G_SEEK_CUR;
        break;
    case SEEK_END:
        wh = G_SEEK_END;
        break;
    default:
        g_assert_not_reached();
        break;
    }
    
    /* All seek operations are not async */
    g_clear_error (&ah->error);
    g_seekable_seek (seekable, (goffset)offset, wh, ah->cancellable, &ah->error);
    
    /* Start async operation */
    ah->state = VFS_ASYNC_READY;
    if (!vfs_data_wait_results (ah, TRUE))
        return -1;
    
    /* Return results */
    ah->state = VFS_ASYNC_READY;
    return offset;
}
Exemplo n.º 7
0
/* *** open_read () *** */
static void
open_for_read_ready (GObject      *source_object,
                     GAsyncResult *result,
                     gpointer      user_data)
{
  GInputStream *stream;
  GVfsJob      *job;
  SoupMessage  *msg;
  gboolean      res;
  gboolean      can_seek;
  GError       *error;

  stream = G_INPUT_STREAM (source_object);
  error  = NULL;
  job    = G_VFS_JOB (user_data);

  res = g_vfs_http_input_stream_send_finish (stream,
					     result,
					     &error);
  if (res == FALSE)
    {
      g_vfs_job_failed_literal (G_VFS_JOB (job),
                                error->domain,
                                error->code,
                                error->message);

      g_error_free (error);
      g_object_unref (stream);
      return;
    }

  msg = g_vfs_http_input_stream_get_message (stream);
  if (!SOUP_STATUS_IS_SUCCESSFUL (msg->status_code))
    {
      http_job_failed (G_VFS_JOB (job), msg);
      g_object_unref (msg);
      g_object_unref (stream);
      return;
    }
  g_object_unref (msg);

  can_seek = G_IS_SEEKABLE (stream) && g_seekable_can_seek (G_SEEKABLE (stream));

  g_vfs_job_open_for_read_set_can_seek (G_VFS_JOB_OPEN_FOR_READ (job), can_seek);
  g_vfs_job_open_for_read_set_handle (G_VFS_JOB_OPEN_FOR_READ (job), stream);
  g_vfs_job_succeeded (job);
}
Exemplo n.º 8
0
/*
 * et_ogg_seek_func:
 * @datasource: the Ogg parser state
 * @offset: the number of bytes to seek
 * @whence: either %SEEK_SET, %SEEK_CUR or %SEEK_END
 *
 * Seek in the currently-open Ogg file.
 *
 * Returns: 0 on success, -1 and sets errno on error
 */
static int
et_ogg_seek_func (void *datasource, ogg_int64_t offset, int whence)
{
    EtOggState *state = (EtOggState *)datasource;
    GSeekType seektype;

    if (!g_seekable_can_seek (G_SEEKABLE (state->istream)))
    {
        return -1;
    }
    else
    {
        switch (whence)
        {
            case SEEK_SET:
                seektype = G_SEEK_SET;
                break;
            case SEEK_CUR:
                seektype = G_SEEK_CUR;
                break;
            case SEEK_END:
                seektype = G_SEEK_END;
                break;
            default:
                errno = EINVAL;
                return -1;
        }

        if (g_seekable_seek (G_SEEKABLE (state->istream), offset, seektype,
                             NULL, &state->error))
        {
            return 0;
        }
        else
        {
            errno = EBADF;
            return -1;
        }
    }
}
Exemplo n.º 9
0
static int
ddb_gvfs_seek (DB_FILE *stream, int64_t offset, int whence)
{
  vfs_gvfs_data_t *data = (vfs_gvfs_data_t *) stream;

  g_return_val_if_fail (data != NULL, -1);

  if (!g_seekable_can_seek (G_SEEKABLE (data->handle)))
    return -1;

  g_return_val_if_fail (! g_input_stream_is_closed (data->handle), -1);

  GSeekType seektype;
  switch (whence) {
    case SEEK_CUR:
      seektype = G_SEEK_CUR;
      break;

    case SEEK_END:
      seektype = G_SEEK_END;
      break;

    default:
      seektype = G_SEEK_SET;
      break;
  }

  GError *error = NULL;

  if (g_seekable_seek (G_SEEKABLE (data->handle), offset, seektype, NULL, &error))
    return 0;

  g_warning ("Could not seek: %s", error->message);
  g_error_free (error);

  return -1;
}
Exemplo n.º 10
0
static int64_t gio_fsize (VFSFile * file)
{
    FileData * data = vfs_get_handle (file);
    GError * error = 0;

    /* Audacious core expects one of two cases:
     *  1) File size is known and file is seekable.
     *  2) File size is unknown and file is not seekable.
     * Therefore, we return -1 for size if file is not seekable. */
    if (! g_seekable_can_seek (data->seekable))
        return -1;

    GFileInfo * info = g_file_query_info (data->file,
     G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, 0, & error);
    CHECK_ERROR ("get size of", vfs_get_filename (file));

    int64_t size = g_file_info_get_attribute_uint64 (info, G_FILE_ATTRIBUTE_STANDARD_SIZE);

    g_object_unref (info);
    return size;

FAILED:
    return -1;
}
static void
gth_metadata_provider_image_read (GthMetadataProvider *self,
				  GthFileData         *file_data,
				  const char          *attributes,
				  GCancellable        *cancellable)
{
	gboolean          format_recognized;
	GFileInputStream *stream;
	char             *description = NULL;
	int               width;
	int               height;
	const char       *mime_type = NULL;

	format_recognized = FALSE;

	stream = g_file_read (file_data->file, cancellable, NULL);
	if (stream != NULL) {
		int     buffer_size;
		guchar *buffer;
		gssize  size;

		buffer_size = BUFFER_SIZE;
		buffer = g_new (guchar, buffer_size);
		size = g_input_stream_read (G_INPUT_STREAM (stream),
					    buffer,
					    buffer_size,
					    cancellable,
					    NULL);
		if (size >= 0) {
			if ((size >= 24)

			    /* PNG signature */

			    && (buffer[0] == 0x89)
			    && (buffer[1] == 0x50)
			    && (buffer[2] == 0x4E)
			    && (buffer[3] == 0x47)
			    && (buffer[4] == 0x0D)
			    && (buffer[5] == 0x0A)
			    && (buffer[6] == 0x1A)
			    && (buffer[7] == 0x0A)

			    /* IHDR Image header */

			    && (buffer[12] == 0x49)
    			    && (buffer[13] == 0x48)
    			    && (buffer[14] == 0x44)
    			    && (buffer[15] == 0x52))
			{
				/* PNG */

				width  = (buffer[16] << 24) + (buffer[17] << 16) + (buffer[18] << 8) + buffer[19];
				height = (buffer[20] << 24) + (buffer[21] << 16) + (buffer[22] << 8) + buffer[23];
				description = _("PNG");
				mime_type = "image/png";
				format_recognized = TRUE;
			}

#if HAVE_LIBJPEG
			else if ((size >= 4)
				 && (buffer[0] == 0xff)
				 && (buffer[1] == 0xd8)
				 && (buffer[2] == 0xff))
			{
				/* JPEG */

				GthTransform orientation;

				if (g_seekable_can_seek (G_SEEKABLE (stream))) {
					g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, cancellable, NULL);
				}
				else {
					g_object_unref (stream);
					stream = g_file_read (file_data->file, cancellable, NULL);
				}

				if (_jpeg_get_image_info (G_INPUT_STREAM (stream),
							  &width,
							  &height,
							  &orientation,
							  cancellable,
							  NULL))
				{
					description = _("JPEG");
					mime_type = "image/jpeg";
					format_recognized = TRUE;

					if ((orientation == GTH_TRANSFORM_ROTATE_90)
					     ||	(orientation == GTH_TRANSFORM_ROTATE_270)
					     ||	(orientation == GTH_TRANSFORM_TRANSPOSE)
					     ||	(orientation == GTH_TRANSFORM_TRANSVERSE))
					{
						int tmp = width;
						width = height;
						height = tmp;
					}
				}
			}
#endif /* HAVE_LIBJPEG */

#if HAVE_LIBWEBP
			else if ((size > 15) && (memcmp (buffer + 8, "WEBPVP8", 7) == 0)) {
				WebPDecoderConfig config;

				if (WebPInitDecoderConfig (&config)) {
					if (WebPGetFeatures (buffer, buffer_size, &config.input) == VP8_STATUS_OK) {
						width = config.input.width;
						height = config.input.height;
						description = _("WebP");
						mime_type = "image/webp";
						format_recognized = TRUE;
					}
					WebPFreeDecBuffer (&config.output);
				}
			}
#endif /* HAVE_LIBWEBP */

			else if ((size >= 26)
				 && (strncmp ((char *) buffer, "gimp xcf ", 9) == 0))
			{
				/* XCF */

				GInputStream      *mem_stream;
				GDataInputStream  *data_stream;

				mem_stream = g_memory_input_stream_new_from_data (buffer, BUFFER_SIZE, NULL);
				data_stream = g_data_input_stream_new (mem_stream);
				g_data_input_stream_set_byte_order (data_stream, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);

				if (g_seekable_seek (G_SEEKABLE (data_stream), 14, G_SEEK_SET, NULL, NULL)) {
					int base_type;

					width  = g_data_input_stream_read_uint32 (data_stream, NULL, NULL);
					height = g_data_input_stream_read_uint32 (data_stream, NULL, NULL);
					base_type = g_data_input_stream_read_uint32 (data_stream, NULL, NULL);
					if (base_type == 0)
						description = "XCF RGB";
					else if (base_type == 1)
						description = "XCF grayscale";
					else if (base_type == 2)
						description = "XCF indexed";
					else
						description = "XCF";
					mime_type = "image/x-xcf";
					format_recognized = TRUE;
				}

				g_object_unref (data_stream);
				g_object_unref (mem_stream);
			}
		}

		g_free (buffer);
		g_object_unref (stream);
	}

	if (! format_recognized) { /* use gdk_pixbuf_get_file_info */
		char *filename;

		filename = g_file_get_path (file_data->file);
		if (filename != NULL) {
			GdkPixbufFormat  *format;

			format = gdk_pixbuf_get_file_info (filename, &width, &height);
			if (format != NULL) {
				format_recognized = TRUE;
				description = gdk_pixbuf_format_get_description (format);
			}

			g_free (filename);
		}
	}

	if (format_recognized) {
		char *size;

		g_file_info_set_attribute_string (file_data->info, "general::format", description);

		g_file_info_set_attribute_int32 (file_data->info, "image::width", width);
		g_file_info_set_attribute_int32 (file_data->info, "image::height", height);
		g_file_info_set_attribute_int32 (file_data->info, "frame::width", width);
		g_file_info_set_attribute_int32 (file_data->info, "frame::height", height);

		if (mime_type != NULL)
			gth_file_data_set_mime_type (file_data, mime_type);

		size = g_strdup_printf (_("%d × %d"), width, height);
		g_file_info_set_attribute_string (file_data->info, "general::dimensions", size);

		g_free (size);
	}
}
Exemplo n.º 12
0
static VALUE
rg_can_seek_p(VALUE self)
{
        return CBOOL2RVAL(g_seekable_can_seek(_SELF(self)));
}
Exemplo n.º 13
0
    bool verify_and_strip_signatures()
    {
        g_debug( "CHECKING SIGNATURE(S)" );

        Signature::Info::List signatures;

        gsize signature_length = 0;

        // This means that the signatures are invalid or we had a problem
        // dealing with them. If there are no signatures, this will return
        // true and give us an empty list.

        if ( ! Signature::get_signatures( info.source_file.c_str(), signatures, &signature_length ) )
        {
            return false;
        }

        unsigned int found = 0;

        if ( signatures.empty() )
        {
            g_debug( "  NOT SIGNED" );
        }
        else
        {
            for ( Signature::Info::List::const_iterator it = signatures.begin(); it != signatures.end(); ++it )
            {
                info.fingerprints.insert( it->fingerprint );
            }

            // Now, see how many of the required ones are found in the signatures

            for ( StringSet::const_iterator it = info.required_fingerprints.begin(); it != info.required_fingerprints.end(); ++it )
            {
                found += info.fingerprints.count( *it );
            }
        }

        if ( found != info.required_fingerprints.size() )
        {
            // Signature(s) missing

            g_warning( "APP IS MISSING AT LEAST ONE REQUIRED SIGNATURE" );

            return false;
        }

        // If there were no signatures, we are done

        if ( signatures.empty() )
        {
            return true;
        }

        // Otherwise, we need to strip the signature block from the file

        bool result = false;

        GFile * file = g_file_new_for_path( info.source_file.c_str() );

        if ( file )
        {

            GFileIOStream * stream = g_file_open_readwrite( file, NULL, NULL );

            if ( stream )
            {
                GSeekable * seekable = G_SEEKABLE( stream );

                if ( g_seekable_can_seek( seekable ) && g_seekable_can_truncate( seekable ) )
                {
                    if ( g_seekable_seek( seekable, 0, G_SEEK_END, NULL, NULL ) )
                    {
                        goffset file_size = g_seekable_tell( seekable );

                        file_size -= signature_length;

                        if ( g_seekable_truncate( seekable, file_size, NULL, NULL ) )
                        {
                            g_debug( "TRUNCATING FILE TO %" G_GOFFSET_FORMAT " BYTES", file_size );
                            result = true;
                        }
                    }
                }

                g_io_stream_close( G_IO_STREAM( stream ), NULL, NULL );

                g_object_unref( G_OBJECT( stream ) );
            }

            g_object_unref( G_OBJECT( file ) );
        }

        if ( ! result )
        {
            g_warning( "FAILED TO STRIP SIGNATURE(S)" );
        }

        return result;
    }