all: Add build constants for runtime.GOOS comparisons (#8442)

all: Add package runtimeos for runtime.GOOS comparisons

I grew tired of hand written string comparisons. This adds generated
constants for the GOOS values, and predefined Is$OS constants that can
be iffed on. In a couple of places I rewrote trivial switch:es to if:s,
and added Illumos where we checked for Solaris (because they are
effectively the same, and if we're going to target one of them that
would be Illumos...).
This commit is contained in:
Jakob Borg
2022-07-28 19:36:39 +02:00
committed by GitHub
parent f13d65262a
commit a3c724f2c3
51 changed files with 282 additions and 192 deletions
+2 -2
View File
@@ -9,8 +9,8 @@ package osutil
import (
"errors"
"path/filepath"
"runtime"
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/fs"
)
@@ -97,7 +97,7 @@ func (w *AtomicWriter) Close() error {
return infoErr
}
err := w.fs.Rename(w.next.Name(), w.path)
if runtime.GOOS == "windows" && fs.IsPermission(err) {
if build.IsWindows && fs.IsPermission(err) {
// On Windows, we might not be allowed to rename over the file
// because it's read-only. Get us some write permissions and try
// again.
+2 -2
View File
@@ -9,9 +9,9 @@ package osutil
import (
"path/filepath"
"runtime"
"strings"
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/sync"
)
@@ -90,7 +90,7 @@ func withPreparedTarget(filesystem fs.Filesystem, from, to string, f func() erro
}
// On Windows, make sure the destination file is writeable (or we can't delete it)
if runtime.GOOS == "windows" {
if build.IsWindows {
filesystem.Chmod(to, 0666)
if !strings.EqualFold(from, to) {
err := filesystem.Remove(to)
+3 -3
View File
@@ -10,10 +10,10 @@ import (
"io"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/osutil"
)
@@ -51,7 +51,7 @@ func TestIsDeleted(t *testing.T) {
}
fd.Close()
}
if runtime.GOOS != "windows" {
if !build.IsWindows {
// Can't create unreadable dir on windows
testFs.MkdirAll("inacc", 0777)
if err := testFs.Chmod("inacc", 0000); err == nil {
@@ -63,7 +63,7 @@ func TestIsDeleted(t *testing.T) {
}
for _, n := range []string{"Dir", "File", "Del"} {
if err := fs.DebugSymlinkForTestsOnly(testFs, testFs, strings.ToLower(n), "linkTo"+n); err != nil {
if runtime.GOOS == "windows" {
if build.IsWindows {
t.Skip("Symlinks aren't working")
}
t.Fatal(err)
+3 -2
View File
@@ -9,7 +9,8 @@ package osutil
import (
"bytes"
"io"
"runtime"
"github.com/syncthing/syncthing/lib/build"
)
type ReplacingWriter struct {
@@ -51,7 +52,7 @@ func (w ReplacingWriter) Write(bs []byte) (int, error) {
// LineEndingsWriter returns a writer that writes platform-appropriate line
// endings. (This is a no-op on non-Windows platforms.)
func LineEndingsWriter(w io.Writer) io.Writer {
if runtime.GOOS != "windows" {
if !build.IsWindows {
return w
}
return &ReplacingWriter{
+3 -2
View File
@@ -10,8 +10,9 @@
package osutil
import (
"runtime"
"syscall"
"github.com/syncthing/syncthing/lib/build"
)
const (
@@ -36,7 +37,7 @@ func MaximizeOpenFileLimit() (int, error) {
// macOS doesn't like a soft limit greater then OPEN_MAX
// See also: man setrlimit
if runtime.GOOS == "darwin" && lim.Max > darwinOpenMax {
if build.IsDarwin && lim.Max > darwinOpenMax {
lim.Max = darwinOpenMax
}
+3 -3
View File
@@ -9,9 +9,9 @@ package osutil_test
import (
"os"
"path/filepath"
"runtime"
"testing"
"github.com/syncthing/syncthing/lib/build"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/osutil"
)
@@ -22,7 +22,7 @@ func TestTraversesSymlink(t *testing.T) {
testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir)
testFs.MkdirAll("a/b/c", 0755)
if err := fs.DebugSymlinkForTestsOnly(testFs, testFs, filepath.Join("a", "b"), filepath.Join("a", "l")); err != nil {
if runtime.GOOS == "windows" {
if build.IsWindows {
t.Skip("Symlinks aren't working")
}
t.Fatal(err)
@@ -71,7 +71,7 @@ func TestIssue4875(t *testing.T) {
testFs := fs.NewFilesystem(fs.FilesystemTypeBasic, tmpDir)
testFs.MkdirAll(filepath.Join("a", "b", "c"), 0755)
if err := fs.DebugSymlinkForTestsOnly(testFs, testFs, filepath.Join("a", "b"), filepath.Join("a", "l")); err != nil {
if runtime.GOOS == "windows" {
if build.IsWindows {
t.Skip("Symlinks aren't working")
}
t.Fatal(err)