From f2b9025d0c6974f9e50fe559bbdbd12416ff8b60 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Sun, 5 Sep 2021 21:52:38 +0200 Subject: [PATCH] fix: round timestamps down by truncating them to seconds Otherwise, we'll write timestamps in the future. fixes https://github.com/ipfs/go-ipfs/issues/8406 This commit was moved from ipfs/go-ipfs-files@3dadb7b27487873743ac6908295ff1959617d34c --- files/tarwriter.go | 4 ++-- files/tarwriter_test.go | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/files/tarwriter.go b/files/tarwriter.go index 6d062726a..4f4ee4e73 100644 --- a/files/tarwriter.go +++ b/files/tarwriter.go @@ -74,7 +74,7 @@ func writeDirHeader(w *tar.Writer, fpath string) error { Name: fpath, Typeflag: tar.TypeDir, Mode: 0777, - ModTime: time.Now(), + ModTime: time.Now().Truncate(time.Second), // TODO: set mode, dates, etc. when added to unixFS }) } @@ -85,7 +85,7 @@ func writeFileHeader(w *tar.Writer, fpath string, size uint64) error { Size: int64(size), Typeflag: tar.TypeReg, Mode: 0644, - ModTime: time.Now(), + ModTime: time.Now().Truncate(time.Second), // TODO: set mode, dates, etc. when added to unixFS }) } diff --git a/files/tarwriter_test.go b/files/tarwriter_test.go index 02686b567..f66d03549 100644 --- a/files/tarwriter_test.go +++ b/files/tarwriter_test.go @@ -4,6 +4,7 @@ import ( "archive/tar" "io" "testing" + "time" ) func TestTarWriter(t *testing.T) { @@ -42,6 +43,10 @@ func TestTarWriter(t *testing.T) { if cur.Size != size { t.Errorf("got wrong size: %d != %d", cur.Size, size) } + now := time.Now() + if cur.ModTime.After(now) { + t.Errorf("wrote timestamp in the future: %s (now) < %s", now, cur.ModTime) + } } if cur, err = tr.Next(); err != nil {