diff options
Diffstat (limited to 'forged/internal/common')
-rw-r--r-- | forged/internal/common/misc/deploy.go | 22 | ||||
-rw-r--r-- | forged/internal/common/misc/net.go | 18 | ||||
-rw-r--r-- | forged/internal/common/misc/panic.go | 19 | ||||
-rw-r--r-- | forged/internal/common/misc/trivial.go | 4 | ||||
-rw-r--r-- | forged/internal/common/misc/unsafe.go | 4 |
5 files changed, 15 insertions, 52 deletions
diff --git a/forged/internal/common/misc/deploy.go b/forged/internal/common/misc/deploy.go deleted file mode 100644 index 3ee5f92..0000000 --- a/forged/internal/common/misc/deploy.go +++ /dev/null @@ -1,22 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> - -package misc - -import ( - "io" - "io/fs" - "os" -) - -// DeployBinary copies the contents of a binary file to the target destination path. -// The destination file is created with executable permissions. -func DeployBinary(src fs.File, dst string) (err error) { - var dstFile *os.File - if dstFile, err = os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755); err != nil { - return err - } - defer dstFile.Close() - _, err = io.Copy(dstFile, src) - return err -} diff --git a/forged/internal/common/misc/net.go b/forged/internal/common/misc/net.go index 2c6b0a5..967ea77 100644 --- a/forged/internal/common/misc/net.go +++ b/forged/internal/common/misc/net.go @@ -1,20 +1,23 @@ package misc import ( + "context" "errors" "fmt" "net" "syscall" ) -func ListenUnixSocket(path string) (listener net.Listener, replaced bool, err error) { - listener, err = net.Listen("unix", path) +func ListenUnixSocket(ctx context.Context, path string) (listener net.Listener, replaced bool, err error) { + listenConfig := net.ListenConfig{} //exhaustruct:ignore + listener, err = listenConfig.Listen(ctx, "unix", path) if errors.Is(err, syscall.EADDRINUSE) { replaced = true - if unlinkErr := syscall.Unlink(path); unlinkErr != nil { + unlinkErr := syscall.Unlink(path) + if unlinkErr != nil { return listener, false, fmt.Errorf("remove existing socket %q: %w", path, unlinkErr) } - listener, err = net.Listen("unix", path) + listener, err = listenConfig.Listen(ctx, "unix", path) } if err != nil { return listener, replaced, fmt.Errorf("listen on unix socket %q: %w", path, err) @@ -22,14 +25,15 @@ func ListenUnixSocket(path string) (listener net.Listener, replaced bool, err er return listener, replaced, nil } -func Listen(net_, addr string) (listener net.Listener, err error) { +func Listen(ctx context.Context, net_, addr string) (listener net.Listener, err error) { if net_ == "unix" { - listener, _, err = ListenUnixSocket(addr) + listener, _, err = ListenUnixSocket(ctx, addr) if err != nil { return listener, fmt.Errorf("listen unix socket for web: %w", err) } } else { - listener, err = net.Listen(net_, addr) + listenConfig := net.ListenConfig{} //exhaustruct:ignore + listener, err = listenConfig.Listen(ctx, net_, addr) if err != nil { return listener, fmt.Errorf("listen %s for web: %w", net_, err) } diff --git a/forged/internal/common/misc/panic.go b/forged/internal/common/misc/panic.go deleted file mode 100644 index 34c49c5..0000000 --- a/forged/internal/common/misc/panic.go +++ /dev/null @@ -1,19 +0,0 @@ -// SPDX-License-Identifier: AGPL-3.0-only -// SPDX-FileCopyrightText: Copyright (c) 2025 Runxi Yu <https://runxiyu.org> - -package misc - -// FirstOrPanic returns the value or panics if the error is non-nil. -func FirstOrPanic[T any](v T, err error) T { - if err != nil { - panic(err) - } - return v -} - -// NoneOrPanic panics if the provided error is non-nil. -func NoneOrPanic(err error) { - if err != nil { - panic(err) - } -} diff --git a/forged/internal/common/misc/trivial.go b/forged/internal/common/misc/trivial.go index e59c17e..83901e0 100644 --- a/forged/internal/common/misc/trivial.go +++ b/forged/internal/common/misc/trivial.go @@ -28,13 +28,13 @@ func QueryEscape(s string) string { } // Dereference dereferences a pointer. -func Dereference[T any](p *T) T { +func Dereference[T any](p *T) T { //nolint:ireturn return *p } // DereferenceOrZero dereferences a pointer. If the pointer is nil, the zero // value of its associated type is returned instead. -func DereferenceOrZero[T any](p *T) T { +func DereferenceOrZero[T any](p *T) T { //nolint:ireturn if p != nil { return *p } diff --git a/forged/internal/common/misc/unsafe.go b/forged/internal/common/misc/unsafe.go index 6c2192f..d827e7f 100644 --- a/forged/internal/common/misc/unsafe.go +++ b/forged/internal/common/misc/unsafe.go @@ -9,12 +9,12 @@ import "unsafe" // Memory is borrowed from the string. // The resulting byte slice must not be modified in any form. func StringToBytes(s string) (bytes []byte) { - return unsafe.Slice(unsafe.StringData(s), len(s)) + return unsafe.Slice(unsafe.StringData(s), len(s)) //#nosec G103 } // BytesToString converts a byte slice to a string without copying the bytes. // Memory is borrowed from the byte slice. // The source byte slice must not be modified. func BytesToString(b []byte) string { - return unsafe.String(unsafe.SliceData(b), len(b)) + return unsafe.String(unsafe.SliceData(b), len(b)) //#nosec G103 } |