mirror of
https://github.com/syncthing/syncthing.git
synced 2024-11-16 10:28:49 -07:00
a3c724f2c3
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...).
64 lines
1.2 KiB
Go
64 lines
1.2 KiB
Go
// Copyright (C) 2014 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package osutil
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
|
)
|
|
|
|
type ReplacingWriter struct {
|
|
Writer io.Writer
|
|
From byte
|
|
To []byte
|
|
}
|
|
|
|
func (w ReplacingWriter) Write(bs []byte) (int, error) {
|
|
var n, written int
|
|
var err error
|
|
|
|
newlineIdx := bytes.IndexByte(bs, w.From)
|
|
for newlineIdx >= 0 {
|
|
n, err = w.Writer.Write(bs[:newlineIdx])
|
|
written += n
|
|
if err != nil {
|
|
break
|
|
}
|
|
if len(w.To) > 0 {
|
|
n, err := w.Writer.Write(w.To)
|
|
if n == len(w.To) {
|
|
written++
|
|
}
|
|
if err != nil {
|
|
break
|
|
}
|
|
}
|
|
bs = bs[newlineIdx+1:]
|
|
newlineIdx = bytes.IndexByte(bs, w.From)
|
|
}
|
|
|
|
n, err = w.Writer.Write(bs)
|
|
written += n
|
|
|
|
return written, err
|
|
}
|
|
|
|
// 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 !build.IsWindows {
|
|
return w
|
|
}
|
|
return &ReplacingWriter{
|
|
Writer: w,
|
|
From: '\n',
|
|
To: []byte{'\r', '\n'},
|
|
}
|
|
}
|