Skip to content

Commit

Permalink
fix: round timestamps down by truncating them to seconds
Browse files Browse the repository at this point in the history
Otherwise, we'll write timestamps in the future.

fixes ipfs/kubo#8406


This commit was moved from ipfs/go-ipfs-files@3dadb7b
  • Loading branch information
Stebalien committed Sep 5, 2021
1 parent 1a66dc4 commit f2b9025
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions files/tarwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
Expand All @@ -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
})
}
Expand Down
5 changes: 5 additions & 0 deletions files/tarwriter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/tar"
"io"
"testing"
"time"
)

func TestTarWriter(t *testing.T) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit f2b9025

Please sign in to comment.