Skip to content

Commit 310be39

Browse files
XrXrnobu
authored andcommitted
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.
1 parent 2462a96 commit 310be39

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

ext/zlib/zlib.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -2230,6 +2230,7 @@ struct gzfile {
22302230
#define GZFILE_FLAG_SYNC ZSTREAM_FLAG_UNUSED
22312231
#define GZFILE_FLAG_HEADER_FINISHED (ZSTREAM_FLAG_UNUSED << 1)
22322232
#define GZFILE_FLAG_FOOTER_FINISHED (ZSTREAM_FLAG_UNUSED << 2)
2233+
#define GZFILE_FLAG_MTIME_IS_SET (ZSTREAM_FLAG_UNUSED << 3)
22332234

22342235
#define GZFILE_IS_FINISHED(gz) \
22352236
(ZSTREAM_IS_FINISHED(&(gz)->z) && ZSTREAM_BUF_FILLED(&(gz)->z) == 0)
@@ -2516,7 +2517,7 @@ gzfile_make_header(struct gzfile *gz)
25162517
if (!NIL_P(gz->comment)) {
25172518
flags |= GZ_FLAG_COMMENT;
25182519
}
2519-
if (gz->mtime == 0) {
2520+
if (!(gz->z.flags & GZFILE_FLAG_MTIME_IS_SET)) {
25202521
gz->mtime = time(0);
25212522
}
25222523

@@ -3246,6 +3247,7 @@ rb_gzfile_set_mtime(VALUE obj, VALUE mtime)
32463247

32473248
val = rb_Integer(mtime);
32483249
gz->mtime = NUM2UINT(val);
3250+
gz->z.flags |= GZFILE_FLAG_MTIME_IS_SET;
32493251

32503252
return mtime;
32513253
}

test/zlib/test_zlib.rb

+11
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,17 @@ def test_mtime
488488
}
489489
end
490490

491+
def test_zero_mtime
492+
sio = StringIO.new
493+
gz = Zlib::GzipWriter.new(sio)
494+
gz.mtime = 0
495+
gz.write("Hi")
496+
gz.close
497+
reading_io = StringIO.new(sio.string)
498+
reader = Zlib::GzipReader.new(reading_io)
499+
assert_equal(0, reader.mtime.to_i)
500+
end
501+
491502
def test_level
492503
Tempfile.create("test_zlib_gzip_file_level") {|t|
493504
t.close

0 commit comments

Comments
 (0)