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...).
60 lines
1.6 KiB
Go
60 lines
1.6 KiB
Go
// Copyright (C) 2015 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 fs
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
|
"github.com/syncthing/syncthing/lib/sha256"
|
|
)
|
|
|
|
const (
|
|
WindowsTempPrefix = "~syncthing~"
|
|
UnixTempPrefix = ".syncthing."
|
|
)
|
|
|
|
func tempPrefix() string {
|
|
if build.IsWindows {
|
|
return WindowsTempPrefix
|
|
} else {
|
|
return UnixTempPrefix
|
|
}
|
|
}
|
|
|
|
// Real filesystems usually handle 255 bytes. encfs has varying and
|
|
// confusing file name limits. We take a safe way out and switch to hashing
|
|
// quite early.
|
|
const maxFilenameLength = 160 - len(UnixTempPrefix) - len(".tmp")
|
|
|
|
// IsTemporary is true if the file name has the temporary prefix. Regardless
|
|
// of the normally used prefix, the standard Windows and Unix temp prefixes
|
|
// are always recognized as temp files.
|
|
func IsTemporary(name string) bool {
|
|
name = filepath.Base(name)
|
|
return strings.HasPrefix(name, WindowsTempPrefix) ||
|
|
strings.HasPrefix(name, UnixTempPrefix)
|
|
}
|
|
|
|
func TempNameWithPrefix(name, prefix string) string {
|
|
tdir := filepath.Dir(name)
|
|
tbase := filepath.Base(name)
|
|
var tname string
|
|
if len(tbase) > maxFilenameLength {
|
|
tname = fmt.Sprintf("%s%x.tmp", prefix, sha256.Sum256([]byte(tbase)))
|
|
} else {
|
|
tname = prefix + tbase + ".tmp"
|
|
}
|
|
return filepath.Join(tdir, tname)
|
|
}
|
|
|
|
func TempName(name string) string {
|
|
return TempNameWithPrefix(name, tempPrefix())
|
|
}
|