Skip to content

Errno is lost #3

@tbodt

Description

@tbodt

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions