Skip to content

Commit

Permalink
Use time.Time not string for LastModified
Browse files Browse the repository at this point in the history
Signed-off-by: Anders F Björklund <anders.f.bjorklund@gmail.com>
  • Loading branch information
afbjorklund committed Jun 2, 2024
1 parent a5a885f commit 0beac38
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 4 deletions.
21 changes: 17 additions & 4 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import (
"path"
"path/filepath"
"strings"
"time"

"github.com/cheggaaa/pb/v3"
"github.com/containerd/continuity/fs"
"github.com/lima-vm/lima/pkg/httpclientutil"
"github.com/lima-vm/lima/pkg/localpathutil"
"github.com/lima-vm/lima/pkg/progressbar"
"github.com/lima-vm/lima/pkg/timeutil"
"github.com/opencontainers/go-digest"
"github.com/sirupsen/logrus"
)
Expand All @@ -44,7 +46,7 @@ const (
type Result struct {
Status Status
CachePath string // "/Users/foo/Library/Caches/lima/download/by-url-sha256/<SHA256_OF_URL>/data"
LastModified string
LastModified time.Time
ContentType string
ValidatedDigest bool
}
Expand Down Expand Up @@ -131,6 +133,17 @@ func readFile(path string) string {
return string(b)
}

func readTime(path string) time.Time {
if _, err := os.Stat(path); err != nil {
return time.Time{}
}
t, err := timeutil.ReadTime(path)
if err != nil {
return time.Time{}
}
return t
}

// Download downloads the remote resource into the local path.
//
// Download caches the remote resource if WithCache or WithCacheDir option is specified.
Expand Down Expand Up @@ -225,7 +238,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
res := &Result{
Status: StatusUsedCache,
CachePath: shadData,
LastModified: readFile(shadTime),
LastModified: readTime(shadTime),
ContentType: readFile(shadType),
ValidatedDigest: o.expectedDigest != "",
}
Expand Down Expand Up @@ -256,7 +269,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
res := &Result{
Status: StatusDownloaded,
CachePath: shadData,
LastModified: readFile(shadTime),
LastModified: readTime(shadTime),
ContentType: readFile(shadType),
ValidatedDigest: o.expectedDigest != "",
}
Expand Down Expand Up @@ -306,7 +319,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
res := &Result{
Status: StatusUsedCache,
CachePath: shadData,
LastModified: readFile(shadTime),
LastModified: readTime(shadTime),
ContentType: readFile(shadType),
ValidatedDigest: o.expectedDigest != "",
}
Expand Down
22 changes: 22 additions & 0 deletions pkg/timeutil/timeutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package timeutil

import (
"os"
"time"
)

// same format as HTTP headers
const timeFormat = time.RFC1123

func ReadTime(name string) (time.Time, error) {
data, err := os.ReadFile(name)
if err != nil {
return time.Time{}, err
}
return time.Parse(timeFormat, string(data))
}

func WriteTime(name string, tm time.Time, perm os.FileMode) error {
data := tm.Format(timeFormat)
return os.WriteFile(name, []byte(data), perm)
}
33 changes: 33 additions & 0 deletions pkg/timeutil/timeutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package timeutil

import (
"os"
"path/filepath"
"testing"
"time"

"gotest.tools/v3/assert"
)

var testTime time.Time
var testData = testTime.Format(timeFormat)

func TestReadTime(t *testing.T) {
tmpFile := filepath.Join(t.TempDir(), "time")
err := os.WriteFile(tmpFile, []byte(testData), 0o644)
assert.NilError(t, err)

tm, err := ReadTime(tmpFile)
assert.NilError(t, err)
assert.Equal(t, tm, testTime)
}

func TestWriteTime(t *testing.T) {
tmpFile := filepath.Join(t.TempDir(), "time")
err := WriteTime(tmpFile, testTime, 0o644)
assert.NilError(t, err)

b, err := os.ReadFile(tmpFile)
assert.NilError(t, err)
assert.Equal(t, string(b), testData)
}

0 comments on commit 0beac38

Please sign in to comment.