apfs icon indicating copy to clipboard operation
apfs copied to clipboard

Errno is lost

Open tbodt opened this issue 9 months ago • 0 comments

The return value of libc functions like copyfile is not errno, it is a sentinel of either 0 or -1. errno is a separate global variable.

It turns out cgo supports automatically converting the errno into a go error if you call the function with a two-variable assignment like so:

if status, err := C.copyfile(C.CString(src), C.CString(dst), state, C.copyfile_flags_t(flag)); status != 0 {
	return false, fmt.Errorf("couldn't copy from %s to %s: %w", src, dst, err)
}

This produces correct error output like couldn't copy from /foo to /bar: file exists, instead of losing the error code such as couldn't copy from /foo to /bar: errno -1.

tbodt avatar Jan 20 '25 08:01 tbodt