[前][次][番号順一覧][スレッド一覧]

ruby-changes:58527

From: Alan <ko1@a...>
Date: Thu, 31 Oct 2019 22:25:04 +0900 (JST)
Subject: [ruby-changes:58527] 0aaa15f636 (master): [ruby/zlib] Fix setting mtime to zero in GzipWriter

https://git.ruby-lang.org/ruby.git/commit/?id=0aaa15f636

From 0aaa15f6362e307c9ef636e9625027b96e577dfb Mon Sep 17 00:00:00 2001
From: Alan Wu <XrXr@u...>
Date: Fri, 25 Oct 2019 19:15:24 -0400
Subject: [ruby/zlib] Fix setting mtime to zero in GzipWriter

Before this change, it was not possible to write out zero for the
timestamp part of a Gzip file's header, as calling GzipWriter#mtime with
zero was ignored.

Judging from the docs for `GzipWriter#mtime=`, it should be possible to
indicate that no timestamp is available by calling the method with zero.

https://github.com/ruby/zlib/commit/310be39cac

diff --git a/ext/zlib/zlib.c b/ext/zlib/zlib.c
index 1d32581..a7d0bc1 100644
--- a/ext/zlib/zlib.c
+++ b/ext/zlib/zlib.c
@@ -2230,6 +2230,7 @@ struct gzfile { https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L2230
 #define GZFILE_FLAG_SYNC             ZSTREAM_FLAG_UNUSED
 #define GZFILE_FLAG_HEADER_FINISHED  (ZSTREAM_FLAG_UNUSED << 1)
 #define GZFILE_FLAG_FOOTER_FINISHED  (ZSTREAM_FLAG_UNUSED << 2)
+#define GZFILE_FLAG_MTIME_IS_SET     (ZSTREAM_FLAG_UNUSED << 3)
 
 #define GZFILE_IS_FINISHED(gz) \
     (ZSTREAM_IS_FINISHED(&(gz)->z) && ZSTREAM_BUF_FILLED(&(gz)->z) == 0)
@@ -2516,7 +2517,7 @@ gzfile_make_header(struct gzfile *gz) https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L2517
     if (!NIL_P(gz->comment)) {
 	flags |= GZ_FLAG_COMMENT;
     }
-    if (gz->mtime == 0) {
+    if (!(gz->z.flags & GZFILE_FLAG_MTIME_IS_SET)) {
 	gz->mtime = time(0);
     }
 
@@ -3246,6 +3247,7 @@ rb_gzfile_set_mtime(VALUE obj, VALUE mtime) https://github.com/ruby/ruby/blob/trunk/ext/zlib/zlib.c#L3247
 
     val = rb_Integer(mtime);
     gz->mtime = NUM2UINT(val);
+    gz->z.flags |= GZFILE_FLAG_MTIME_IS_SET;
 
     return mtime;
 }
diff --git a/test/zlib/test_zlib.rb b/test/zlib/test_zlib.rb
index 7df53bd..7d703d1 100644
--- a/test/zlib/test_zlib.rb
+++ b/test/zlib/test_zlib.rb
@@ -488,6 +488,17 @@ if defined? Zlib https://github.com/ruby/ruby/blob/trunk/test/zlib/test_zlib.rb#L488
       }
     end
 
+    def test_zero_mtime
+      sio = StringIO.new
+      gz = Zlib::GzipWriter.new(sio)
+      gz.mtime = 0
+      gz.write("Hi")
+      gz.close
+      reading_io = StringIO.new(sio.string)
+      reader = Zlib::GzipReader.new(reading_io)
+      assert_equal(0, reader.mtime.to_i)
+    end
+
     def test_level
       Tempfile.create("test_zlib_gzip_file_level") {|t|
         t.close
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]