sg: update binary correctly instead of renaming file
Created by: mrnugget
Linux users on Slack reported that sg update
produces this error:
> sg update
error: rename /tmp/sg1226370089/sg /home/fkling/.local/bin/sg: invalid cross-device link
As Geoffrey pointed out:
rename only works within the same filesystem. I think the implementation should just copy / delete the file instead.
And the man page for rename says:
oldpath and newpath are not on the same mounted file system. (Linux permits a file system to be mounted at multiple points, but rename() does not work across different mount points, even if the same file system is mounted on both.)
So what this here does is to use code from gitserver
that does an
in-place update of a file safele. I extracted the code into an
internal/fileutil
package (couldn't think of a better name).
The code avoids the across-filesystem problem by creating a temp file next to the destination file. And it also does more things, like making sure the changes are synced to the filesystem.
Test plan
In ./dev/sg
:
$ go build -o ~/bin/sg . && sg version && sg update && sg version
dev
✅ sg has been updated!
To see what's new, run 'sg version changelog'.
d8176d40f3ddf54d47317c18f9d1532293eb6c23