copyfile() returns a byte count, with errno cleared, when the copy
fails after the first block. movefile() across file systems then
removes the source and leaves a truncated copy.
The copy loop adds the result of write() to the total without checking
it, takes a failed read() for end of file, and the result of close() on
the destination is dropped.
Return -1 with errno set on any of them, and retry short writes.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
copyfile() passes the source mode to open(2), which applies the umask,
so a 0644 file copied with umask 0027 ends up 0640.
Apply the source mode with fchmod() after creating the destination.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>