Files
kopia/internal/ospath/ospath.go
Jarek Kowalski 191a51b278 ui: fixed snapshotting UNC roots (#1401)
This was caused by additional resolution of path names only done in UI,
which caused \\hostname\share to be treated as relative and resolved
against the home directory.

Fixes #1385
Fixes #1362
2021-10-17 13:25:12 -07:00

62 lines
1.3 KiB
Go

// Package ospath provides discovery of OS-dependent paths.
package ospath
import (
"os"
"path/filepath"
"runtime"
"strings"
)
var (
userSettingsDir string
userLogsDir string
)
// ConfigDir returns the directory where configuration data (possibly roaming) needs to be stored.
func ConfigDir() string {
return filepath.Join(userSettingsDir, "kopia")
}
// LogsDir returns the directory where per-user logs should be written.
func LogsDir() string {
return filepath.Join(userLogsDir, "kopia")
}
// IsAbs determines if a given path is absolute, in particular treating treating \\hostname\share as absolute on Windows.
func IsAbs(s string) bool {
// nolint:forbidigo
if filepath.IsAbs(s) {
return true
}
// On Windows, treat \\hostname\share as absolute paths, which is not what filepath.IsAbs() does.
if runtime.GOOS == "windows" {
if strings.HasPrefix(s, "\\\\") {
parts := strings.Split(s[2:], "\\")
return len(parts) > 1 && len(parts[1]) > 0
}
}
return false
}
// ResolveUserFriendlyPath replaces ~ in a path with a home directory.
func ResolveUserFriendlyPath(path string, relativeToHome bool) string {
home, _ := os.UserHomeDir()
if home != "" && strings.HasPrefix(path, "~") {
return home + path[1:]
}
if IsAbs(path) {
return path
}
if relativeToHome {
return filepath.Join(home, path)
}
return path
}