mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-14 01:04:14 +00:00
3d8b4a42b7
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4228
56 lines
1.1 KiB
Go
56 lines
1.1 KiB
Go
// Copyright (C) 2016 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 (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
var errNoHome = errors.New("no home directory found - set $HOME (or the platform equivalent)")
|
|
|
|
func ExpandTilde(path string) (string, error) {
|
|
if path == "~" {
|
|
return getHomeDir()
|
|
}
|
|
|
|
path = filepath.FromSlash(path)
|
|
if !strings.HasPrefix(path, fmt.Sprintf("~%c", PathSeparator)) {
|
|
return path, nil
|
|
}
|
|
|
|
home, err := getHomeDir()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return filepath.Join(home, path[2:]), nil
|
|
}
|
|
|
|
func getHomeDir() (string, error) {
|
|
var home string
|
|
|
|
switch runtime.GOOS {
|
|
case "windows":
|
|
home = filepath.Join(os.Getenv("HomeDrive"), os.Getenv("HomePath"))
|
|
if home == "" {
|
|
home = os.Getenv("UserProfile")
|
|
}
|
|
default:
|
|
home = os.Getenv("HOME")
|
|
}
|
|
|
|
if home == "" {
|
|
return "", errNoHome
|
|
}
|
|
|
|
return home, nil
|
|
}
|