GZlibCompressor
GZlibCompressorFormat
g_zlib_compressor_new
+g_zlib_compressor_get_file_info
+g_zlib_compressor_set_file_info
<SUBSECTION Standard>
GZlibCompressorClass
G_TYPE_ZLIB_COMPRESSOR
<TITLE>GZlibDecompressor</TITLE>
GZlibDecompressor
g_zlib_decompressor_new
+g_zlib_decompressor_get_file_info
<SUBSECTION Standard>
GZlibDecompressorClass
G_TYPE_ZLIB_DECOMPRESSOR
#if IN_FILE(__G_ZLIB_COMPRESSOR_C__)
g_zlib_compressor_get_type G_GNUC_CONST
g_zlib_compressor_new
+g_zlib_compressor_get_file_info
+g_zlib_compressor_set_file_info
#endif
#endif
#if IN_FILE(__G_ZLIB_DECOMPRESSOR_C__)
g_zlib_decompressor_get_type G_GNUC_CONST
g_zlib_decompressor_new
+g_zlib_decompressor_get_file_info
#endif
#endif
#include <zlib.h>
#include <string.h>
+#include "gfileinfo.h"
#include "gioerror.h"
#include "gioenums.h"
#include "gioenumtypes.h"
enum {
PROP_0,
PROP_FORMAT,
- PROP_LEVEL
+ PROP_LEVEL,
+ PROP_FILE_INFO
};
/**
GZlibCompressorFormat format;
int level;
z_stream zstream;
+ gz_header gzheader;
+ GFileInfo *file_info;
};
+static void
+g_zlib_compressor_set_gzheader (GZlibCompressor *compressor)
+{
+ const gchar *filename;
+
+ if (compressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP ||
+ compressor->file_info == NULL)
+ return;
+
+ memset (&compressor->gzheader, 0, sizeof (gz_header));
+ compressor->gzheader.os = 0x03; /* Unix */
+
+ filename = g_file_info_get_name (compressor->file_info);
+ compressor->gzheader.name = (Bytef *) filename;
+ compressor->gzheader.name_max = filename ? strlen (filename) + 1 : 0;
+
+ compressor->gzheader.time =
+ (uLong) g_file_info_get_attribute_uint64 (compressor->file_info,
+ G_FILE_ATTRIBUTE_TIME_MODIFIED);
+
+ if (deflateSetHeader (&compressor->zstream, &compressor->gzheader) != Z_OK)
+ g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
+}
+
G_DEFINE_TYPE_WITH_CODE (GZlibCompressor, g_zlib_compressor, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
g_zlib_compressor_iface_init))
deflateEnd (&compressor->zstream);
+ if (compressor->file_info)
+ g_object_unref (compressor->file_info);
+
G_OBJECT_CLASS (g_zlib_compressor_parent_class)->finalize (object);
}
compressor->level = g_value_get_int (value);
break;
+ case PROP_FILE_INFO:
+ g_zlib_compressor_set_file_info (compressor, g_value_get_object (value));
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
g_value_set_int (value, compressor->level);
break;
+ case PROP_FILE_INFO:
+ g_value_set_object (value, compressor->file_info);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
if (res != Z_OK)
g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
+
+ g_zlib_compressor_set_gzheader (compressor);
}
static void
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
+
+ /**
+ * GZlibCompressor:file-info:
+ *
+ * If set to a non-%NULL #GFileInfo object, and #GZlibCompressor:format is
+ * %G_ZLIB_COMPRESSOR_FORMAT_GZIP, the compressor will write the file name
+ * and modification time from the file info to the the GZIP header.
+ *
+ * Since: 2.26
+ */
+ g_object_class_install_property (gobject_class,
+ PROP_FILE_INFO,
+ g_param_spec_object ("file-info",
+ P_("file info"),
+ P_("File info"),
+ G_TYPE_FILE_INFO,
+ G_PARAM_READWRITE |
+ G_PARAM_STATIC_STRINGS));
}
/**
return compressor;
}
+/**
+ * g_zlib_compressor_get_file_info:
+ * @compressor: a #GZlibCompressor
+ *
+ * Returns the #GZlibCompressor:file-info property.
+ *
+ * Returns: (transfer none): a #GFileInfo, or %NULL
+ *
+ * Since: 2.26
+ */
+GFileInfo *
+g_zlib_compressor_get_file_info (GZlibCompressor *compressor)
+{
+ g_return_val_if_fail (G_IS_ZLIB_COMPRESSOR (compressor), NULL);
+
+ return compressor->file_info;
+}
+
+/**
+ * g_zlib_compressor_set_file_info:
+ * @compressor: a #GZlibCompressor
+ * @file_info: (allow-none): a #GFileInfo
+ *
+ * Sets @file_info in @compressor. If non-%NULL, and @compressor's
+ * #GZlibCompressor:format property is %G_ZLIB_COMPRESSOR_FORMAT_GZIP,
+ * it will be used to set the file name and modification time in
+ * the GZIP header of the compressed data.
+ *
+ * Note: it is an error to call this function while a compression is in
+ * progress; it may only be called immediately after creation of @compressor,
+ * or after resetting it with g_converter_reset().
+ *
+ * Since: 2.26
+ */
+void
+g_zlib_compressor_set_file_info (GZlibCompressor *compressor,
+ GFileInfo *file_info)
+{
+ g_return_if_fail (G_IS_ZLIB_COMPRESSOR (compressor));
+
+ if (file_info == compressor->file_info)
+ return;
+
+ if (compressor->file_info)
+ g_object_unref (compressor->file_info);
+ if (file_info)
+ g_object_ref (file_info);
+ compressor->file_info = file_info;
+ g_object_notify (G_OBJECT (compressor), "file-info");
+
+ g_zlib_compressor_set_gzheader (compressor);
+}
+
static void
g_zlib_compressor_reset (GConverter *converter)
{
res = deflateReset (&compressor->zstream);
if (res != Z_OK)
g_warning ("unexpected zlib error: %s\n", compressor->zstream.msg);
+
+ /* deflateReset reset the header too, so re-set it */
+ g_zlib_compressor_set_gzheader (compressor);
}
static GConverterResult
#define __G_ZLIB_COMPRESSOR_H__
#include <gio/gconverter.h>
+#include <gio/gfileinfo.h>
G_BEGIN_DECLS
GZlibCompressor *g_zlib_compressor_new (GZlibCompressorFormat format,
int level);
+GFileInfo *g_zlib_compressor_get_file_info (GZlibCompressor *compressor);
+void g_zlib_compressor_set_file_info (GZlibCompressor *compressor,
+ GFileInfo *file_info);
+
G_END_DECLS
#endif /* __G_ZLIB_COMPRESSOR_H__ */
#include <zlib.h>
#include <string.h>
+#include "gfileinfo.h"
#include "gioerror.h"
#include "gioenums.h"
#include "gioenumtypes.h"
enum {
PROP_0,
- PROP_FORMAT
+ PROP_FORMAT,
+ PROP_FILE_INFO
};
/**
static void g_zlib_decompressor_iface_init (GConverterIface *iface);
+typedef struct {
+ gz_header gzheader;
+ char filename[257];
+ GFileInfo *file_info;
+} HeaderData;
+
/**
* GZlibDecompressor:
*
GZlibCompressorFormat format;
z_stream zstream;
+ HeaderData *header_data;
};
+static void
+g_zlib_decompressor_set_gzheader (GZlibDecompressor *decompressor)
+{
+ if (decompressor->format != G_ZLIB_COMPRESSOR_FORMAT_GZIP)
+ return;
+
+ if (decompressor->header_data != NULL)
+ {
+ if (decompressor->header_data->file_info)
+ g_object_unref (decompressor->header_data->file_info);
+
+ memset (decompressor->header_data, 0, sizeof (HeaderData));
+ }
+ else
+ {
+ decompressor->header_data = g_new0 (HeaderData, 1);
+ }
+
+ decompressor->header_data->gzheader.name = (Bytef*) &decompressor->header_data->filename;
+ /* We keep one byte to guarantee the string is 0-terminated */
+ decompressor->header_data->gzheader.name_max = 256;
+
+ if (inflateGetHeader (&decompressor->zstream, &decompressor->header_data->gzheader) != Z_OK)
+ g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
+}
+
G_DEFINE_TYPE_WITH_CODE (GZlibDecompressor, g_zlib_decompressor, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
g_zlib_decompressor_iface_init))
inflateEnd (&decompressor->zstream);
+ if (decompressor->header_data != NULL)
+ {
+ if (decompressor->header_data->file_info)
+ g_object_unref (decompressor->header_data->file_info);
+ g_free (decompressor->header_data);
+ }
+
G_OBJECT_CLASS (g_zlib_decompressor_parent_class)->finalize (object);
}
g_value_set_enum (value, decompressor->format);
break;
+ case PROP_FILE_INFO:
+ if (decompressor->header_data)
+ g_value_set_object (value, decompressor->header_data->file_info);
+ else
+ g_value_set_object (value, NULL);
+ break;
+
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
if (res != Z_OK)
g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
+
+ g_zlib_decompressor_set_gzheader (decompressor);
}
static void
G_ZLIB_COMPRESSOR_FORMAT_ZLIB,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS));
+
+ /**
+ * GZlibDecompressor:file-info:
+ *
+ * A #GFileInfo containing the information found in the GZIP header
+ * of the data stream processed, or %NULL if the header was not yet
+ * fully processed, is not present at all, or the compressor's
+ * #GZlibDecompressor:format property is not %G_ZLIB_COMPRESSOR_FORMAT_GZIP.
+ *
+ * Since: 2.26
+ */
+ g_object_class_install_property (gobject_class,
+ PROP_FILE_INFO,
+ g_param_spec_object ("file-info",
+ P_("file info"),
+ P_("File info"),
+ G_TYPE_FILE_INFO,
+ G_PARAM_READABLE |
+ G_PARAM_STATIC_STRINGS));
}
/**
return decompressor;
}
+/**
+ * g_zlib_decompressor_get_file_info:
+ * @decompressor: a #GZlibDecompressor
+ *
+ * Retrieves the #GFileInfo constructed from the GZIP header data
+ * of compressed data processed by @compressor, or %NULL if @decompressor's
+ * #GZlibDecompressor:format property is not %G_ZLIB_COMPRESSOR_FORMAT_GZIP,
+ * or the header data was not fully processed yet, or it not present in the
+ * data stream at all.
+ *
+ * Returns: (transfer none): a #GFileInfo, or %NULL
+ *
+ * Since: 2.26
+ */
+GFileInfo *
+g_zlib_decompressor_get_file_info (GZlibDecompressor *decompressor)
+{
+ g_return_val_if_fail (G_IS_ZLIB_DECOMPRESSOR (decompressor), NULL);
+
+ if (decompressor->header_data)
+ return decompressor->header_data->file_info;
+
+ return NULL;
+}
+
static void
g_zlib_decompressor_reset (GConverter *converter)
{
res = inflateReset (&decompressor->zstream);
if (res != Z_OK)
g_warning ("unexpected zlib error: %s\n", decompressor->zstream.msg);
+
+ g_zlib_decompressor_set_gzheader (decompressor);
}
static GConverterResult
return G_CONVERTER_ERROR;
}
- if (res == Z_OK || res == Z_STREAM_END)
+ g_assert (res == Z_OK || res == Z_STREAM_END);
+
+ *bytes_read = inbuf_size - decompressor->zstream.avail_in;
+ *bytes_written = outbuf_size - decompressor->zstream.avail_out;
+
+ if (decompressor->header_data != NULL &&
+ decompressor->header_data->gzheader.done == 1)
{
- *bytes_read = inbuf_size - decompressor->zstream.avail_in;
- *bytes_written = outbuf_size - decompressor->zstream.avail_out;
+ HeaderData *data = decompressor->header_data;
+
+ /* So we don't notify again */
+ data->gzheader.done = 2;
+
+ data->file_info = g_file_info_new ();
+ g_file_info_set_attribute_uint64 (data->file_info,
+ G_FILE_ATTRIBUTE_TIME_MODIFIED,
+ data->gzheader.time);
+ g_file_info_set_attribute_uint32 (data->file_info,
+ G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
+ 0);
+
+ if (data->filename[0] != '\0')
+ g_file_info_set_attribute_byte_string (data->file_info,
+ G_FILE_ATTRIBUTE_STANDARD_NAME,
+ data->filename);
- if (res == Z_STREAM_END)
- return G_CONVERTER_FINISHED;
- return G_CONVERTER_CONVERTED;
+ g_object_notify (G_OBJECT (decompressor), "file-info");
}
- g_assert_not_reached ();
+ if (res == Z_STREAM_END)
+ return G_CONVERTER_FINISHED;
+ return G_CONVERTER_CONVERTED;
}
static void
#define __G_ZLIB_DECOMPRESSOR_H__
#include <gio/gconverter.h>
+#include <gio/gfileinfo.h>
G_BEGIN_DECLS
GZlibDecompressor *g_zlib_decompressor_new (GZlibCompressorFormat format);
+GFileInfo *g_zlib_decompressor_get_file_info (GZlibDecompressor *decompressor);
+
G_END_DECLS
#endif /* __G_ZLIB_DECOMPRESSOR_H__ */
};
static void
+decompressor_file_info_notify_cb (GZlibDecompressor *decompressor,
+ GParamSpec *pspec,
+ gpointer data)
+{
+ GFileInfo *file_info;
+ const gchar *filename;
+
+ file_info = g_zlib_decompressor_get_file_info (decompressor);
+ if (file_info == NULL)
+ return;
+
+ filename = g_file_info_get_name (file_info);
+ if (filename)
+ g_printerr ("Decompressor filename: %s\n", filename);
+}
+
+static void
cat (GFile * file)
{
GInputStream *in;
conv = (GConverter *)g_zlib_decompressor_new (gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB);
old = in;
in = (GInputStream *) g_converter_input_stream_new (in, conv);
+ g_signal_connect (conv, "notify::file-info", G_CALLBACK (decompressor_file_info_notify_cb), NULL);
g_object_unref (conv);
g_object_unref (old);
}
if (compress)
{
GInputStream *old;
- conv = (GConverter *)g_zlib_compressor_new (gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB, -1);
+ GFileInfo *in_file_info;
+
+ in_file_info = g_file_query_info (file,
+ G_FILE_ATTRIBUTE_STANDARD_NAME ","
+ G_FILE_ATTRIBUTE_TIME_MODIFIED,
+ G_FILE_QUERY_INFO_NONE,
+ NULL,
+ &error);
+ if (in_file_info == NULL)
+ {
+ g_printerr ("%s: %s: error reading file info: %s\n",
+ g_get_prgname (), g_file_get_uri (file), error->message);
+ g_error_free (error);
+ return;
+ }
+
+ conv = (GConverter *)g_zlib_compressor_new(gzip?G_ZLIB_COMPRESSOR_FORMAT_GZIP:G_ZLIB_COMPRESSOR_FORMAT_ZLIB, -1);
+ g_zlib_compressor_set_file_info (G_ZLIB_COMPRESSOR (conv), in_file_info);
old = in;
in = (GInputStream *) g_converter_input_stream_new (in, conv);
g_object_unref (conv);
g_object_unref (old);
+ g_object_unref (in_file_info);
}
while (1)