Skip to content

Instantly share code, notes, and snippets.

@kazuki-ma
Created August 1, 2019 00:58
Show Gist options
  • Save kazuki-ma/fab0199a83c84302d1c9daa723742873 to your computer and use it in GitHub Desktop.
Save kazuki-ma/fab0199a83c84302d1c9daa723742873 to your computer and use it in GitHub Desktop.
// +build darwin
package tools
import (
"fmt"
"io"
"os"
"strconv"
"strings"
"unsafe"
"golang.org/x/sys/unix"
)
/*
#include <fcntl.h>
#include <errno.h>
#include <sys/clonefile.h>
*/
import "C"
var cloneFileSupported bool
func init() {
cloneFileSupported = checkCloneFileSupported()
}
// checkCloneFileSupported return iff Mac OS version is greater or equal to 10.12.x Sierra.
//
// clonefile is supported since Mac OS X 10.12
// https://www.manpagez.com/man/2/clonefile/
//
// kern.osrelease mapping
// 17.x.x. macOS 10.13.x High Sierra.
// 16.x.x macOS 10.12.x Sierra.
// 15.x.x OS X 10.11.x El Capitan.
func checkCloneFileSupported() bool {
bytes, err := unix.Sysctl("kern.osrelease")
if err != nil {
return false
}
versionString := strings.Split(string(bytes), ".") // major.minor.patch
if len(versionString) < 2 {
return false
}
major, err := strconv.Atoi(versionString[0])
if err != nil {
return false
}
return major >= 16
}
type CloneFileError struct {
Unsupported bool
errorString string
}
func (c *CloneFileError) Error() string {
return c.errorString
}
func CloneFile(writer io.Writer, reader io.Reader) (bool, error) {
if !cloneFileSupported {
return false, &CloneFileError{Unsupported: true, errorString: "clonefile is not supported"}
}
dst, dstIsFile := writer.(*os.File)
src, srcIsFile := reader.(*os.File)
if !dstIsFile || !srcIsFile {
return false, &CloneFileError{errorString: "either one is not file"}
}
// Setup: Delete file
if err := os.Remove(dst.Name()); err != nil {
return false, err
}
// Do
if err := cloneFileSyscall(dst, src); err != nil {
recoveryDeletedFileAndFD(dst)
return false, err
}
// Finally: Reopen and recovery file descriptor.
if newFd, err := os.Open(dst.Name()); err != nil {
return false, err
} else {
// Seek end of file.
if _, err := newFd.Seek(0, io.SeekEnd); err != nil {
return false, err
}
*dst = *newFd
}
return true, nil
}
func recoveryDeletedFileAndFD(file *os.File) {
_, err := file.Seek(0, io.SeekStart)
if err != nil {
panic(err)
}
state, err := file.Stat()
if err != nil {
panic(err)
}
recovered, err := os.Create(file.Name())
if err != nil {
panic(err)
}
io.Copy(recovered, file)
recovered.Chmod(state.Mode())
recovered.Seek(0, io.SeekStart)
*file = *recovered
}
func cloneFileSyscall(dst, src *os.File) *CloneFileError {
srcCString, err := unix.BytePtrFromString(src.Name())
if err != nil {
return &CloneFileError{errorString: err.Error()}
}
dstCString, err := unix.BytePtrFromString(dst.Name())
if err != nil {
return &CloneFileError{errorString: err.Error()}
}
atFDCwd := C.AT_FDCWD // current directory.
_, _, errNo := unix.Syscall6(
unix.SYS_CLONEFILEAT,
uintptr(atFDCwd),
uintptr(unsafe.Pointer(srcCString)),
uintptr(atFDCwd),
uintptr(unsafe.Pointer(dstCString)),
uintptr(C.CLONE_NOFOLLOW),
0,
)
if errNo != 0 {
return &CloneFileError{
Unsupported: errNo == C.ENOTSUP,
errorString: fmt.Sprintf("%s. from %v to %v", unix.ErrnoName(errNo), src.Name(), dst.Name()),
}
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment