[gcab/wip/hughsie/future: 12/18] Add gcab_file_set_date()
- From: Richard Hughes <rhughes src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gcab/wip/hughsie/future: 12/18] Add gcab_file_set_date()
- Date: Wed, 13 Dec 2017 23:03:22 +0000 (UTC)
commit db3d53df9cd6311534da4559b411b88b00ae29cb
Author: Richard Hughes <richard hughsie com>
Date: Wed Dec 13 15:13:59 2017 +0000
Add gcab_file_set_date()
This allows us to override the modification time from the GFile and allows us
to create reproducable archives without manually setting the mtime on the
source files.
gcab.c | 2 +-
libgcab.syms | 1 +
libgcab/gcab-file.c | 62 +++++++++++++++++++++++++++++++-------------------
libgcab/gcab-file.h | 3 +-
4 files changed, 42 insertions(+), 26 deletions(-)
---
diff --git a/gcab.c b/gcab.c
index 4d04ae3..e94fa97 100644
--- a/gcab.c
+++ b/gcab.c
@@ -195,7 +195,7 @@ individual files from the archive.\
if (list_details) {
gchar date[32];
struct tm *tm;
- GTimeVal tv;
+ GTimeVal tv = {0};
gcab_file_get_date (GCAB_FILE (l->data), &tv);
tm = localtime (&tv.tv_sec);
diff --git a/libgcab.syms b/libgcab.syms
index 56cb370..1914261 100644
--- a/libgcab.syms
+++ b/libgcab.syms
@@ -43,5 +43,6 @@ LIBGCAB1_0.6 {
} LIBGCAB1_0.5;
LIBGCAB1_0.8 {
+ gcab_file_set_date;
gcab_folder_get_comptype;
} LIBGCAB1_0.6;
diff --git a/libgcab/gcab-file.c b/libgcab/gcab-file.c
index 11e2368..25ebaa4 100644
--- a/libgcab/gcab-file.c
+++ b/libgcab/gcab-file.c
@@ -154,25 +154,40 @@ gcab_file_class_init (GCabFileClass *klass)
G_PARAM_STATIC_STRINGS));
}
+/**
+ * gcab_file_set_date:
+ * @file: a #GCabFile
+ * @tv: a #GTimeVal
+ *
+ * Sets the file modification date, instead of the value provided by the GFile.
+ *
+ * Since: 0.8
+ **/
+void
+gcab_file_set_date (GCabFile *self, const GTimeVal *tv)
+{
+ g_autoptr(GDateTime) dt = g_date_time_new_from_timeval_utc (tv);
+ self->cfile->date = ((g_date_time_get_year (dt) - 1980 ) << 9 ) +
+ ((g_date_time_get_month (dt)) << 5) +
+ (g_date_time_get_day_of_month (dt));
+ self->cfile->time = ((g_date_time_get_hour (dt)) << 11) +
+ (g_date_time_get_minute (dt) << 5) +
+ (g_date_time_get_second (dt) / 2);
+}
+
G_GNUC_INTERNAL gboolean
gcab_file_update_info (GCabFile *self, GFileInfo *info)
{
GTimeVal tv;
- time_t time;
- struct tm *m; // Use GDateTime when 2.26 in RHEL6
g_return_val_if_fail (GCAB_IS_FILE (self), FALSE);
g_return_val_if_fail (G_IS_FILE_INFO (info), FALSE);
g_file_info_get_modification_time (info, &tv);
- time = tv.tv_sec;
- m = gmtime (&time);
-
+ if (self->cfile->date == 0)
+ gcab_file_set_date (self, &tv);
self->cfile->usize = g_file_info_get_size (info);
self->cfile->fattr = GCAB_FILE_ATTRIBUTE_ARCH;
- self->cfile->date = ((m->tm_year + 1900 - 1980 ) << 9 ) +
- ((m->tm_mon+1) << 5 ) + (m->tm_mday);
- self->cfile->time = (m->tm_hour << 11) + (m->tm_min << 5) + (m->tm_sec / 2);
return TRUE;
}
@@ -247,30 +262,29 @@ gcab_file_get_size (GCabFile *self)
* Get the file date, in @result.
*
* Since: 0.6
+ *
+ * Returns: %TRUE if @tv was set
**/
-void
+gboolean
gcab_file_get_date (GCabFile *self, GTimeVal *tv)
{
- struct tm tm;
guint16 date, time;
+ g_autoptr(GDateTime) dt = NULL;
- g_return_if_fail (GCAB_IS_FILE (self));
- g_return_if_fail (tv != NULL);
+ g_return_val_if_fail (GCAB_IS_FILE (self), FALSE);
+ g_return_val_if_fail (tv != NULL, FALSE);
date = self->cfile->date;
time = self->cfile->time;
-
- tm.tm_isdst = -1;
- tm.tm_year = ((date >> 9) + 1980) - 1900;
- tm.tm_mon = ((date >> 5) & 0xf) - 1;
- tm.tm_mday = (date & 0x1f) - 1;
-
- tm.tm_hour = (time >> 11);
- tm.tm_min = ((time >> 5) & 0x3f);
- tm.tm_sec = (time & 0x1f) * 2;
-
- tv->tv_sec = mktime(&tm);
- tv->tv_usec = 0;
+ dt = g_date_time_new_utc ((date >> 9) + 1980,
+ (date >> 5) & 0xf,
+ (date & 0x1f),
+ (time >> 11),
+ (time >> 5) & 0x3f,
+ (time & 0x1f) * 2);
+ if (dt == NULL)
+ return FALSE;
+ return g_date_time_to_timeval (dt, tv);
}
/**
diff --git a/libgcab/gcab-file.h b/libgcab/gcab-file.h
index 60e59d1..9a3a134 100644
--- a/libgcab/gcab-file.h
+++ b/libgcab/gcab-file.h
@@ -70,9 +70,10 @@ GFile * gcab_file_get_file (GCabFile *file);
const gchar * gcab_file_get_name (GCabFile *file);
guint32 gcab_file_get_size (GCabFile *file);
guint32 gcab_file_get_attributes (GCabFile *file);
-void gcab_file_get_date (GCabFile *file, GTimeVal *result);
+gboolean gcab_file_get_date (GCabFile *file, GTimeVal *result);
const gchar * gcab_file_get_extract_name (GCabFile *file);
void gcab_file_set_extract_name (GCabFile *file, const gchar *name);
+void gcab_file_set_date (GCabFile *file, const GTimeVal *tv);
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]