Repository navigation
Expand file tree
/
Copy pathopen.go
More file actions
78 lines (72 loc) · 2.14 KB
/
Copy pathopen.go
File metadata and controls
78 lines (72 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
var (
errNotFound = errors.New("path does not exist")
errBadPath = errors.New("bad path")
errAccessDenied = errors.New("access denied")
)
// openPath validates the path and opens it in the system file browser.
// Directories open directly; files are revealed (selected) in their parent
// folder. Unlike Windows Explorer's default behavior, a missing path is a
// real error — we never silently open a fallback location.
//
// smb:// URLs are resolved to a local path first (mounting the share on
// demand where the platform needs it) and then opened like any other path.
func openPath(path string) (action string, err error) {
path = strings.TrimSpace(path)
if path == "" {
return "", fmt.Errorf("%w: empty path", errBadPath)
}
if isSMBURL(path) {
if path, err = resolveSMB(path); err != nil {
return "", err
}
}
if !filepath.IsAbs(path) {
return "", fmt.Errorf("%w: path must be absolute: %q", errBadPath, path)
}
path = filepath.Clean(path)
info, err := os.Stat(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return "", fmt.Errorf("%w: %q", errNotFound, path)
}
if errors.Is(err, os.ErrPermission) {
// This process's identity can be narrower than the desktop
// user's — e.g. an elevated or IT-assisted install launched the
// server as the installer, whose network-share access differs
// from the signed-in user's. openDenied (per-platform) decides
// whether the file browser, which DOES run as the desktop user,
// can still handle the path.
if action, ok := openDenied(path); ok {
return action, nil
}
return "", fmt.Errorf("%w: %q", errAccessDenied, path)
}
return "", fmt.Errorf("stat %q: %w", path, err)
}
if info.IsDir() {
if err := openDir(path); err != nil {
return "", err
}
return "opened", nil
}
if err := revealFile(path); err != nil {
return "", err
}
return "revealed", nil
}
func runQuiet(cmd *exec.Cmd) error {
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%s: %w: %s", cmd.Args[0], err, strings.TrimSpace(string(out)))
}
return nil
}