Skip to content

Commit 92243ff

Browse files
committed
bugfix: updatedAt timestamp file may be empty
Signed-off-by: Michael Wan <zirenwan@gmail.com>
1 parent 93d3f06 commit 92243ff

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed

content/local/store.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
"github.com/containerd/containerd/errdefs"
3434
"github.com/containerd/containerd/filters"
3535
"github.com/containerd/containerd/log"
36+
37+
"github.com/containerd/continuity"
3638
digest "github.com/opencontainers/go-digest"
3739
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3840
"github.com/pkg/errors"
@@ -651,5 +653,5 @@ func writeTimestampFile(p string, t time.Time) error {
651653
return err
652654
}
653655

654-
return ioutil.WriteFile(p, b, 0666)
656+
return continuity.AtomicWriteFile(p, b, 0666)
655657
}

content/local/store_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/containerd/containerd/content"
3737
"github.com/containerd/containerd/content/testsuite"
3838
"github.com/containerd/containerd/pkg/testutil"
39+
3940
"github.com/opencontainers/go-digest"
4041
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
4142
"gotest.tools/assert"
@@ -392,3 +393,24 @@ func setupIncompleteWrite(ctx context.Context, t *testing.T, cs content.Store, r
392393

393394
assert.NilError(t, writer.Close())
394395
}
396+
397+
func TestWriteReadEmptyFileTimestamp(t *testing.T) {
398+
root, err := ioutil.TempDir("", "test-write-read-file-timestamp")
399+
if err != nil {
400+
t.Errorf("failed to create a tmp dir: %v", err)
401+
}
402+
defer os.RemoveAll(root)
403+
404+
emptyFile := filepath.Join(root, "updatedat")
405+
if err := writeTimestampFile(emptyFile, time.Time{}); err != nil {
406+
t.Errorf("failed to write Zero Time to file: %v", err)
407+
}
408+
409+
timestamp, err := readFileTimestamp(emptyFile)
410+
if err != nil {
411+
t.Errorf("read empty timestamp file should success, but got error: %v", err)
412+
}
413+
if !timestamp.IsZero() {
414+
t.Errorf("read empty timestamp file should return time.Time{}, but got: %v", timestamp)
415+
}
416+
}

snapshots/btrfs/btrfs.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/containerd/containerd/snapshots"
3434
"github.com/containerd/containerd/snapshots/storage"
3535
"github.com/containerd/continuity/fs"
36+
3637
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3738
"github.com/pkg/errors"
3839
"github.com/sirupsen/logrus"
@@ -171,7 +172,7 @@ func (b *snapshotter) usage(ctx context.Context, key string) (snapshots.Usage, e
171172
if parentID != "" {
172173
du, err = fs.DiffUsage(ctx, filepath.Join(b.root, "snapshots", parentID), p)
173174
} else {
174-
du, err = fs.DiskUsage(p)
175+
du, err = fs.DiskUsage(ctx, p)
175176
}
176177
if err != nil {
177178
// TODO(stevvooe): Consider not reporting an error in this case.

snapshots/native/native.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"github.com/containerd/containerd/plugin"
2929
"github.com/containerd/containerd/snapshots"
3030
"github.com/containerd/containerd/snapshots/storage"
31+
3132
"github.com/containerd/continuity/fs"
3233
"github.com/pkg/errors"
3334
)
@@ -120,7 +121,7 @@ func (o *snapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, e
120121
}
121122

122123
if info.Kind == snapshots.KindActive {
123-
du, err := fs.DiskUsage(o.getSnapshotDir(id))
124+
du, err := fs.DiskUsage(ctx, o.getSnapshotDir(id))
124125
if err != nil {
125126
return snapshots.Usage{}, err
126127
}
@@ -166,7 +167,7 @@ func (o *snapshotter) Commit(ctx context.Context, name, key string, opts ...snap
166167
return err
167168
}
168169

169-
usage, err := fs.DiskUsage(o.getSnapshotDir(id))
170+
usage, err := fs.DiskUsage(ctx, o.getSnapshotDir(id))
170171
if err != nil {
171172
return err
172173
}

snapshots/overlay/overlay.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ func (o *snapshotter) Usage(ctx context.Context, key string) (snapshots.Usage, e
168168
upperPath := o.upperPath(id)
169169

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

228-
usage, err := fs.DiskUsage(o.upperPath(id))
228+
usage, err := fs.DiskUsage(ctx, o.upperPath(id))
229229
if err != nil {
230230
return err
231231
}

0 commit comments

Comments
 (0)