diff --git a/content/local/store.go b/content/local/store.go index 5503cb56f..efc58ea79 100644 --- a/content/local/store.go +++ b/content/local/store.go @@ -35,7 +35,6 @@ import ( "github.com/containerd/containerd/log" "github.com/sirupsen/logrus" - "github.com/containerd/continuity" digest "github.com/opencontainers/go-digest" ocispec "github.com/opencontainers/image-spec/specs-go/v1" "github.com/pkg/errors" @@ -661,6 +660,19 @@ func writeTimestampFile(p string, t time.Time) error { if err != nil { return err } - - return continuity.AtomicWriteFile(p, b, 0666) + return atomicWrite(p, b, 0666) +} + +func atomicWrite(path string, data []byte, mode os.FileMode) error { + tmp := fmt.Sprintf("%s.tmp", path) + f, err := os.OpenFile(tmp, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_SYNC, mode) + if err != nil { + return errors.Wrap(err, "create tmp file") + } + _, err = f.Write(data) + f.Close() + if err != nil { + return errors.Wrap(err, "write atomic data") + } + return os.Rename(tmp, path) } diff --git a/content/testsuite/testsuite.go b/content/testsuite/testsuite.go index bd7b8ed9f..dd3f1d36f 100644 --- a/content/testsuite/testsuite.go +++ b/content/testsuite/testsuite.go @@ -338,7 +338,7 @@ func checkRefNotAvailable(ctx context.Context, t *testing.T, cs content.Store, r w, err := cs.Writer(ctx, content.WithRef(ref)) if err == nil { - w.Close() + defer w.Close() t.Fatal("writer created with ref, expected to be in use") } if !errdefs.IsUnavailable(err) { @@ -402,6 +402,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store) } t.Fatalf("Unexpected error: %+v", err) } + w.Close() w, err = cs.Writer(ctx, content.WithRef(ref)) if err != nil { @@ -425,6 +426,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store) t.Errorf("Unexpected error: %+v", err) } + w.Close() w, err = cs.Writer(ctx, content.WithRef(ref)) if err != nil { t.Fatal(err) @@ -440,6 +442,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store) t.Errorf("Unexpected error: %+v", err) } + w.Close() w, err = cs.Writer(ctx, content.WithRef(ref)) if err != nil { t.Fatal(err) @@ -455,6 +458,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store) t.Fatalf("Failed to commit: %+v", err) } + w.Close() // Create another writer with same reference w, err = cs.Writer(ctx, content.WithRef(ref)) if err != nil { @@ -481,6 +485,7 @@ func checkCommitErrorState(ctx context.Context, t *testing.T, cs content.Store) t.Fatalf("Unexpected error: %+v", err) } + w.Close() w, err = cs.Writer(ctx, content.WithRef(ref)) if err != nil { t.Fatal(err)