util: file copy and exec.LookPath wrapper
Add convenience functions for copying files to owner readable targets and LookPath comma ok wrapper. Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
This commit is contained in:
parent
190eb088bc
commit
289e681c41
34
util.go
34
util.go
@ -3,8 +3,10 @@ package main
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/fs"
|
||||
"os"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -26,3 +28,35 @@ func sdBooted() bool {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func which(file string) (string, bool) {
|
||||
path, err := exec.LookPath(file)
|
||||
return path, err == nil
|
||||
}
|
||||
|
||||
func copyFile(dst, src string) error {
|
||||
srcD, err := os.Open(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if srcD.Close() != nil {
|
||||
// unreachable
|
||||
panic("src file closed prematurely")
|
||||
}
|
||||
}()
|
||||
|
||||
dstD, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if dstD.Close() != nil {
|
||||
// unreachable
|
||||
panic("dst file closed prematurely")
|
||||
}
|
||||
}()
|
||||
|
||||
_, err = io.Copy(dstD, srcD)
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user