diff options
| author | Ophestra Umiker <cat@ophivana.moe> | 2024-07-15 01:20:52 +0900 |
|---|---|---|
| committer | Ophestra Umiker <cat@ophivana.moe> | 2024-07-15 01:20:52 +0900 |
| commit | 289e681c4181d7e5d9d63fb889ffc43545461248 (patch) | |
| tree | e0c698b88f1d74d8cc6521f34c2d57641f292262 | |
| parent | 190eb088bcf338a9489dfe409acc13d11de4039f (diff) | |
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>
| -rw-r--r-- | util.go | 34 |
1 files changed, 34 insertions, 0 deletions
@@ -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 +} |
