bugfix: updatedAt timestamp file may be empty

Signed-off-by: Michael Wan <zirenwan@gmail.com>
This commit is contained in:
Michael Wan 2018-08-29 07:32:01 -04:00
parent 93d3f065ac
commit 92243ff72a
5 changed files with 32 additions and 6 deletions

View File

@ -33,6 +33,8 @@ import (
"github.com/containerd/containerd/errdefs"
"github.com/containerd/containerd/filters"
"github.com/containerd/containerd/log"
"github.com/containerd/continuity"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
@ -651,5 +653,5 @@ func writeTimestampFile(p string, t time.Time) error {
return err
}
return ioutil.WriteFile(p, b, 0666)
return continuity.AtomicWriteFile(p, b, 0666)
}

View File

@ -36,6 +36,7 @@ import (
"github.com/containerd/containerd/content"
"github.com/containerd/containerd/content/testsuite"
"github.com/containerd/containerd/pkg/testutil"
"github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"gotest.tools/assert"
@ -392,3 +393,24 @@ func setupIncompleteWrite(ctx context.Context, t *testing.T, cs content.Store, r
assert.NilError(t, writer.Close())
}
func TestWriteReadEmptyFileTimestamp(t *testing.T) {
root, err := ioutil.TempDir("", "test-write-read-file-timestamp")
if err != nil {
t.Errorf("failed to create a tmp dir: %v", err)
}
defer os.RemoveAll(root)
emptyFile := filepath.Join(root, "updatedat")
if err := writeTimestampFile(emptyFile, time.Time{}); err != nil {
t.Errorf("failed to write Zero Time to file: %v", err)
}
timestamp, err := readFileTimestamp(emptyFile)
if err != nil {
t.Errorf("read empty timestamp file should success, but got error: %v", err)
}
if !timestamp.IsZero() {
t.Errorf("read empty timestamp file should return time.Time{}, but got: %v", timestamp)
}
}

View File

@ -33,6 +33,7 @@ import (
"github.com/containerd/containerd/snapshots"
"github.com/containerd/containerd/snapshots/storage"
"github.com/containerd/continuity/fs"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
@ -171,7 +172,7 @@ func (b *snapshotter) usage(ctx context.Context, key string) (snapshots.Usage, e
if parentID != "" {
du, err = fs.DiffUsage(ctx, filepath.Join(b.root, "snapshots", parentID), p)
} else {
du, err = fs.DiskUsage(p)
du, err = fs.DiskUsage(ctx, p)
}
if err != nil {
// TODO(stevvooe): Consider not reporting an error in this case.

View File

@ -28,6 +28,7 @@ import (
"github.com/containerd/containerd/plugin"
"github.com/containerd/containerd/snapshots"
"github.com/containerd/containerd/snapshots/storage"
"github.com/containerd/continuity/fs"
"github.com/pkg/errors"
)
@ -120,7 +121,7 @@ func (o *snapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, e
}
if info.Kind == snapshots.KindActive {
du, err := fs.DiskUsage(o.getSnapshotDir(id))
du, err := fs.DiskUsage(ctx, o.getSnapshotDir(id))
if err != nil {
return snapshots.Usage{}, err
}
@ -166,7 +167,7 @@ func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
return err
}
usage, err := fs.DiskUsage(o.getSnapshotDir(id))
usage, err := fs.DiskUsage(ctx, o.getSnapshotDir(id))
if err != nil {
return err
}

View File

@ -168,7 +168,7 @@ func (o *snapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, e
upperPath := o.upperPath(id)
if info.Kind == snapshots.KindActive {
du, err := fs.DiskUsage(upperPath)
du, err := fs.DiskUsage(ctx, upperPath)
if err != nil {
// TODO(stevvooe): Consider not reporting an error in this case.
return snapshots.Usage{}, err
@ -225,7 +225,7 @@ func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
return err
}
usage, err := fs.DiskUsage(o.upperPath(id))
usage, err := fs.DiskUsage(ctx, o.upperPath(id))
if err != nil {
return err
}