Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 0 additions & 31 deletions plugins/snapshots/blockfile/blockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,13 @@ package blockfile
import (
"context"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"slices"

"github.com/containerd/containerd/v2/core/mount"
"github.com/containerd/containerd/v2/core/snapshots"
"github.com/containerd/containerd/v2/core/snapshots/storage"
"github.com/containerd/continuity/fs"
"github.com/containerd/log"
"github.com/containerd/plugin"
)
Expand Down Expand Up @@ -452,31 +449,3 @@ func (o *snapshotter) mounts(s storage.Snapshot) []mount.Mount {
func (o *snapshotter) Close() error {
return o.ms.Close()
}

func copyFileWithSync(target, source string) error {
// The Go stdlib does not seem to have an efficient os.File.ReadFrom
// routine for other platforms like it does on Linux with
// copy_file_range. For Darwin at least we can use clonefile
// in its place, otherwise if we have a sparse file we'd have
// a fun surprise waiting below.
//
// TODO: Enlighten other platforms (windows?)
if runtime.GOOS == "darwin" {
return fs.CopyFile(target, source)
}

src, err := os.Open(source)
if err != nil {
return fmt.Errorf("failed to open source %s: %w", source, err)
}
defer src.Close()
tgt, err := os.Create(target)
if err != nil {
return fmt.Errorf("failed to open target %s: %w", target, err)
}
defer tgt.Close()
defer tgt.Sync()

_, err = io.Copy(tgt, src)
return err
}
27 changes: 27 additions & 0 deletions plugins/snapshots/blockfile/blockfile_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package blockfile

import "github.com/containerd/continuity/fs"

func copyFileWithSync(target, source string) error {
// The Go stdlib does not seem to have an efficient os.File.ReadFrom
// routine for other platforms like it does on Linux with
// copy_file_range. For Darwin at least we can use clonefile
// in its place.
return fs.CopyFile(target, source)
}
46 changes: 46 additions & 0 deletions plugins/snapshots/blockfile/blockfile_default.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//go:build !linux && !darwin

/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package blockfile

import (
"fmt"
"io"
"os"
)

func copyFileWithSync(target, source string) error {
src, err := os.Open(source)
if err != nil {
return fmt.Errorf("failed to open source %s: %w", source, err)
}
defer src.Close()
tgt, err := os.Create(target)
if err != nil {
return fmt.Errorf("failed to open target %s: %w", target, err)
}
defer tgt.Close()
defer tgt.Sync()

_, err = io.Copy(tgt, src)
if err != nil {
return fmt.Errorf("failed to copy file to target %s: %w", target, err)
}

return err
}
114 changes: 114 additions & 0 deletions plugins/snapshots/blockfile/blockfile_linux.go

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have unit tests

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can, but I'd really prefer doing it in a seperate PR, @AkihiroSuda.

The reason for that is this PR just being a revival of #10197, which I was not the author.

If you don't mind, let's get this one merged and I will open a PR adding unit tests to it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds fair?

Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Copyright The containerd Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package blockfile

import (
"fmt"
"os"
"syscall"

"golang.org/x/sys/unix"
)

func copyFileWithSync(target, source string) error {
src, err := os.Open(source)
if err != nil {
return fmt.Errorf("failed to open source %s: %w", source, err)
}
defer src.Close()
tgt, err := os.Create(target)
if err != nil {
return fmt.Errorf("failed to open target %s: %w", target, err)
}
defer tgt.Close()
defer tgt.Sync()

info, err := src.Stat()
if err != nil {
return fmt.Errorf("failed to stat the source %s: %w", source, err)
}

srcSize := info.Size()
if err = tgt.Truncate(srcSize); err != nil {
return fmt.Errorf("failed to truncate the target %s: %w", target, err)
}

srcFd := int(src.Fd())
tgtFd := int(tgt.Fd())
offset := int64(0)
for offset < srcSize {
// search for the next hole segment
holeStart, err := unix.Seek(srcFd, offset, unix.SEEK_HOLE)
if err != nil {
if errno, ok := err.(syscall.Errno); ok && errno == syscall.ENXIO {
// no more holes found
// copy everything from the current offset till the end of the file
return doCopyFileRange(target, srcFd, tgtFd, &offset, int(srcSize-holeStart), 0)
}
return fmt.Errorf("failed to seek for holes in source %s: %w", source, err)
}

// this should never happen under normal conditions
// but better be safe than sorry, in case something goes VERY wrong
if holeStart < offset {
return fmt.Errorf("SEEK_HOLE returned a unexpected position, most likely due to an issue with the file (%s) or the filesystem themselves", source)
}

// a hole was found
if holeStart > offset {
// copy everything from the current offset till where the hole starts
if err := doCopyFileRange(target, srcFd, tgtFd, &offset, int(holeStart-offset), 0); err != nil {
return err
}
}

// search for the next data segment
dataStart, err := unix.Seek(srcFd, holeStart, unix.SEEK_DATA)
if err != nil {
if errno, ok := err.(syscall.Errno); ok && errno == syscall.ENXIO {
// no more data found, we reached the end of file for one of the two reasons:
// - we actually went through the whole file
// - the file's been created with `truncate -s ...` (or similar)
// - meaning that there are no data segements at all
return nil
}
return fmt.Errorf("failed to seek for next data segment in source %s: %w", source, err)
}

// this should never happen under normal conditions
// but, again, better be safe than sorry, in case something goes VERY wrong
if dataStart == offset {
return fmt.Errorf("no progress happened in this copy iteration for the file %s, indicating a unexpected error", source)
}

// update the offset and do everything again till we reach the end of the file
offset = dataStart
}
return nil

}

func doCopyFileRange(target string, srcFd, tgtFd int, offset *int64, length int, flags uint) error {
for length > 0 {
copied, err := unix.CopyFileRange(srcFd, offset, tgtFd, offset, length, 0)
Comment thread
fidencio marked this conversation as resolved.
if err != nil || copied == 0 {
return fmt.Errorf("failed to copy file to target %s: %w", target, err)
}
length -= copied
}
return nil
}
Loading