Repository: containerd/containerd
File: plugins/content/local/writer.go (Go)

Code before:
		target, _ = w.s.blobPath(dgst) // ignore error because we calculated this dgst
	)

	// make sure parent directories of blob exist
	if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
		return err
	}

	if _, err := os.Stat(target); err == nil {
		// collision with the target file!
		if err := os.RemoveAll(w.path); err != nil {
			log.G(ctx).WithField("ref", w.ref).WithField("path", w.path).Error("failed to remove ingest directory")
		}
		return fmt.Errorf("content %v: %w", dgst, errdefs.ErrAlreadyExists)
	}

	if err := os.Rename(ingest, target); err != nil {
		return err
	}

	targetDir := filepath.Dir(target)
	dirF, err := os.Open(targetDir)
	if err != nil {
		return fmt.Errorf("failed to open dir %s: %w", targetDir, err)
	}
	err = dirF.Sync()
	dirF.Close()
	if err != nil {
		return fmt.Errorf("failed to sync dir %s: %w", targetDir, err)
	}

	// Enable content blob integrity verification if supported

	if w.s.integritySupported {
		if err := fsverity.Enable(target); err != nil {
			log.G(ctx).Warnf("failed to enable integrity for blob %v: %s", target, err.Error())
		}
	}

	// Ingest has now been made available in the content store, attempt to complete
	// setting metadata but errors should only be logged and not returned since
	// the content store cannot be cleanly rolled back.

	commitTime := time.Now()
	if err := os.Chtimes(target, commitTime, commitTime); err != nil {
		log.G(ctx).WithField("digest", dgst).Error("failed to change file time to commit time")
	}

	// clean up!!
	if err := os.RemoveAll(w.path); err != nil {
		log.G(ctx).WithField("ref", w.ref).WithField("path", w.path).Error("failed to remove ingest directory")

Review comment (bug):
```
    store_test.go:346: failed commit on ref "sha256:5da5f435603db04757df01edcd28bab836f0e142b92ad59de635e7356d7ee19a": failed to sync dir C:\Users\RUNNER~1\AppData\Local\Temp\TestWalkBlobs927362987\001\blobs\sha256: sync C:\Users\RUNNER~1\AppData\Local\Temp\TestWalkBlobs927362987\001\blobs\sha256: Access is denied.
```

hmm. it's unable to sync dir in windows...

Code after:
		target, _ = w.s.blobPath(dgst) // ignore error because we calculated this dgst
	)

	// make sure parent directories of blob exist
	if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
		return err
	}

	if _, err := os.Stat(target); err == nil {
		// collision with the target file!
		if err := os.RemoveAll(w.path); err != nil {
			log.G(ctx).WithField("ref", w.ref).WithField("path", w.path).Error("failed to remove ingest directory")
		}
		return fmt.Errorf("content %v: %w", dgst, errdefs.ErrAlreadyExists)
	}

	if err := os.Rename(ingest, target); err != nil {
		return err
	}

	if err := syncDir(filepath.Dir(target)); err != nil {
		return err
	}
	// Enable content blob integrity verification if supported

	if w.s.integritySupported {
		if err := fsverity.Enable(target); err != nil {
			log.G(ctx).Warnf("failed to enable integrity for blob %v: %s", target, err.Error())
		}
	}

	// Ingest has now been made available in the content store, attempt to complete
	// setting metadata but errors should only be logged and not returned since
	// the content store cannot be cleanly rolled back.

	commitTime := time.Now()
	if err := os.Chtimes(target, commitTime, commitTime); err != nil {
		log.G(ctx).WithField("digest", dgst).Error("failed to change file time to commit time")
	}

	// clean up!!
	if err := os.RemoveAll(w.path); err != nil {
		log.G(ctx).WithField("ref", w.ref).WithField("path", w.path).Error("failed to remove ingest directory")
	}

	if w.s.ls != nil && base.Labels != nil {
		if err := w.s.ls.Set(dgst, base.Labels); err != nil {
			log.G(ctx).WithField("digest", dgst).Error("failed to set labels")
		}
	}