| 1 |
//go:build !windows |
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"cmp" |
| 7 |
"fmt" |
| 8 |
"log" |
| 9 |
"os" |
| 10 |
"os/exec" |
| 11 |
"os/user" |
| 12 |
"path/filepath" |
| 13 |
"runtime" |
| 14 |
"strconv" |
| 15 |
"strings" |
| 16 |
"syscall" |
| 17 |
|
| 18 |
"golang.org/x/sys/unix" |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
envOpener = os.Getenv("OPENER") |
| 23 |
envEditor = os.Getenv("VISUAL") |
| 24 |
envPager = os.Getenv("PAGER") |
| 25 |
envShell = os.Getenv("SHELL") |
| 26 |
) |
| 27 |
|
| 28 |
var ( |
| 29 |
gDefaultShell = "sh" |
| 30 |
gDefaultShellFlag = "-c" |
| 31 |
gDefaultSocketPath string |
| 32 |
gDefaultHiddenFiles = []string{".*"} |
| 33 |
) |
| 34 |
|
| 35 |
var ( |
| 36 |
gUser *user.User |
| 37 |
gConfigPaths []string |
| 38 |
gColorsPaths []string |
| 39 |
gIconsPaths []string |
| 40 |
gFilesPath string |
| 41 |
gMarksPath string |
| 42 |
gTagsPath string |
| 43 |
gHistoryPath string |
| 44 |
) |
| 45 |
|
| 46 |
func init() { |
| 47 |
if envOpener == "" { |
| 48 |
if runtime.GOOS == "darwin" { |
| 49 |
envOpener = "open" |
| 50 |
} else { |
| 51 |
envOpener = "xdg-open" |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if envEditor == "" { |
| 56 |
envEditor = cmp.Or(os.Getenv("EDITOR"), "vi") |
| 57 |
} |
| 58 |
|
| 59 |
if envPager == "" { |
| 60 |
envPager = "less" |
| 61 |
} |
| 62 |
|
| 63 |
if envShell == "" { |
| 64 |
envShell = "sh" |
| 65 |
} |
| 66 |
|
| 67 |
u, err := user.Current() |
| 68 |
if err != nil { |
| 69 |
// When the user is not in /etc/passwd (for e.g. LDAP) and CGO_ENABLED=1 in go env, |
| 70 |
// the cgo implementation of user.Current() fails even when HOME and USER are set. |
| 71 |
|
| 72 |
log.Printf("user: %s", err) |
| 73 |
if os.Getenv("HOME") == "" { |
| 74 |
panic("$HOME variable is empty or not set") |
| 75 |
} |
| 76 |
if os.Getenv("USER") == "" { |
| 77 |
panic("$USER variable is empty or not set") |
| 78 |
} |
| 79 |
u = &user.User{ |
| 80 |
Username: os.Getenv("USER"), |
| 81 |
HomeDir: os.Getenv("HOME"), |
| 82 |
} |
| 83 |
} |
| 84 |
gUser = u |
| 85 |
|
| 86 |
config := cmp.Or( |
| 87 |
os.Getenv("LF_CONFIG_HOME"), |
| 88 |
os.Getenv("XDG_CONFIG_HOME"), |
| 89 |
filepath.Join(gUser.HomeDir, ".config"), |
| 90 |
) |
| 91 |
|
| 92 |
gConfigPaths = []string{ |
| 93 |
filepath.Join("/etc", "lf", "lfrc"), |
| 94 |
filepath.Join(config, "lf", "lfrc"), |
| 95 |
} |
| 96 |
|
| 97 |
gColorsPaths = []string{ |
| 98 |
filepath.Join("/etc", "lf", "colors"), |
| 99 |
filepath.Join(config, "lf", "colors"), |
| 100 |
} |
| 101 |
|
| 102 |
gIconsPaths = []string{ |
| 103 |
filepath.Join("/etc", "lf", "icons"), |
| 104 |
filepath.Join(config, "lf", "icons"), |
| 105 |
} |
| 106 |
|
| 107 |
data := cmp.Or( |
| 108 |
os.Getenv("LF_DATA_HOME"), |
| 109 |
os.Getenv("XDG_DATA_HOME"), |
| 110 |
filepath.Join(gUser.HomeDir, ".local", "share"), |
| 111 |
) |
| 112 |
|
| 113 |
gFilesPath = filepath.Join(data, "lf", "files") |
| 114 |
gMarksPath = filepath.Join(data, "lf", "marks") |
| 115 |
gTagsPath = filepath.Join(data, "lf", "tags") |
| 116 |
gHistoryPath = filepath.Join(data, "lf", "history") |
| 117 |
|
| 118 |
// Use a private per-user dir when XDG_RUNTIME_DIR is unset |
| 119 |
runtimeDir := os.Getenv("XDG_RUNTIME_DIR") |
| 120 |
if runtimeDir == "" { |
| 121 |
runtimeDir = filepath.Join(os.TempDir(), fmt.Sprintf("lf-%s", gUser.Uid)) |
| 122 |
if err := os.MkdirAll(runtimeDir, 0o700); err != nil { |
| 123 |
log.Fatalf("creating runtime dir: %s", err) |
| 124 |
} |
| 125 |
st, err := os.Stat(runtimeDir) |
| 126 |
if err != nil { |
| 127 |
log.Fatalf("stat runtime dir: %s", err) |
| 128 |
} |
| 129 |
if stat, ok := st.Sys().(*syscall.Stat_t); !ok || stat.Uid != uint32(os.Geteuid()) { |
| 130 |
log.Fatalf("runtime dir not owned by current user: %s", runtimeDir) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
gDefaultSocketPath = filepath.Join(runtimeDir, "lf.sock") |
| 135 |
} |
| 136 |
|
| 137 |
func detachedCommand(name string, arg ...string) *exec.Cmd { |
| 138 |
cmd := exec.Command(name, arg...) |
| 139 |
cmd.SysProcAttr = &unix.SysProcAttr{Setsid: true} |
| 140 |
return cmd |
| 141 |
} |
| 142 |
|
| 143 |
func shellCommand(s string, args []string) *exec.Cmd { |
| 144 |
if len(gOpts.ifs) != 0 { |
| 145 |
s = fmt.Sprintf("IFS='%s'; %s", gOpts.ifs, s) |
| 146 |
} |
| 147 |
|
| 148 |
args = append([]string{gOpts.shellflag, s, "--"}, args...) |
| 149 |
|
| 150 |
args = append(gOpts.shellopts, args...) |
| 151 |
|
| 152 |
return exec.Command(gOpts.shell, args...) |
| 153 |
} |
| 154 |
|
| 155 |
func shellSetPG(cmd *exec.Cmd) { |
| 156 |
cmd.SysProcAttr = &unix.SysProcAttr{Setpgid: true} |
| 157 |
} |
| 158 |
|
| 159 |
func shellKill(cmd *exec.Cmd) error { |
| 160 |
pgid, err := unix.Getpgid(cmd.Process.Pid) |
| 161 |
if err == nil && cmd.Process.Pid == pgid { |
| 162 |
// kill the process group |
| 163 |
err = unix.Kill(-pgid, syscall.SIGTERM) |
| 164 |
if err == nil { |
| 165 |
return nil |
| 166 |
} |
| 167 |
} |
| 168 |
return cmd.Process.Kill() |
| 169 |
} |
| 170 |
|
| 171 |
func setDefaults() { |
| 172 |
gOpts.cmds["open"] = &execExpr{"&", `$OPENER "$f"`} |
| 173 |
gOpts.nkeys["e"] = &execExpr{"$", `$EDITOR "$f"`} |
| 174 |
gOpts.vkeys["e"] = &execExpr{"$", `$EDITOR "$f"`} |
| 175 |
gOpts.nkeys["i"] = &execExpr{"$", `$PAGER "$f"`} |
| 176 |
gOpts.vkeys["i"] = &execExpr{"$", `$PAGER "$f"`} |
| 177 |
gOpts.nkeys["w"] = &execExpr{"$", "$SHELL"} |
| 178 |
gOpts.vkeys["w"] = &execExpr{"$", "$SHELL"} |
| 179 |
|
| 180 |
gOpts.cmds["help"] = &execExpr{"$", `"$lf" -doc | $PAGER`} |
| 181 |
gOpts.nkeys["<f-1>"] = &callExpr{"help", nil, 1} |
| 182 |
gOpts.vkeys["<f-1>"] = &callExpr{"help", nil, 1} |
| 183 |
|
| 184 |
gOpts.cmds["maps"] = &execExpr{"$", `"$lf" -remote "query $id maps" | $PAGER`} |
| 185 |
gOpts.cmds["nmaps"] = &execExpr{"$", `"$lf" -remote "query $id nmaps" | $PAGER`} |
| 186 |
gOpts.cmds["vmaps"] = &execExpr{"$", `"$lf" -remote "query $id vmaps" | $PAGER`} |
| 187 |
gOpts.cmds["cmaps"] = &execExpr{"$", `"$lf" -remote "query $id cmaps" | $PAGER`} |
| 188 |
gOpts.cmds["cmds"] = &execExpr{"$", `"$lf" -remote "query $id cmds" | $PAGER`} |
| 189 |
} |
| 190 |
|
| 191 |
func setUserUmask() { |
| 192 |
unix.Umask(0o077) |
| 193 |
} |
| 194 |
|
| 195 |
func isExecutable(f os.FileInfo) bool { |
| 196 |
return f.Mode()&0o111 != 0 |
| 197 |
} |
| 198 |
|
| 199 |
func isHidden(f os.FileInfo, path string, hiddenfiles []string) bool { |
| 200 |
hidden := false |
| 201 |
for _, pattern := range hiddenfiles { |
| 202 |
if matchPattern(strings.TrimPrefix(pattern, "!"), f.Name(), path) { |
| 203 |
hidden = !strings.HasPrefix(pattern, "!") |
| 204 |
} |
| 205 |
} |
| 206 |
return hidden |
| 207 |
} |
| 208 |
|
| 209 |
func userName(f os.FileInfo) string { |
| 210 |
if stat, ok := f.Sys().(*syscall.Stat_t); ok { |
| 211 |
uid := strconv.FormatUint(uint64(stat.Uid), 10) |
| 212 |
if u, err := user.LookupId(uid); err == nil { |
| 213 |
return u.Username |
| 214 |
} |
| 215 |
return uid |
| 216 |
} |
| 217 |
return "" |
| 218 |
} |
| 219 |
|
| 220 |
func groupName(f os.FileInfo) string { |
| 221 |
if stat, ok := f.Sys().(*syscall.Stat_t); ok { |
| 222 |
gid := strconv.FormatUint(uint64(stat.Gid), 10) |
| 223 |
if g, err := user.LookupGroupId(gid); err == nil { |
| 224 |
return g.Name |
| 225 |
} |
| 226 |
return gid |
| 227 |
} |
| 228 |
return "" |
| 229 |
} |
| 230 |
|
| 231 |
func linkCount(f os.FileInfo) string { |
| 232 |
if stat, ok := f.Sys().(*syscall.Stat_t); ok { |
| 233 |
return strconv.FormatUint(uint64(stat.Nlink), 10) |
| 234 |
} |
| 235 |
return "" |
| 236 |
} |
| 237 |
|
| 238 |
func errCrossDevice(err error) bool { |
| 239 |
return err.(*os.LinkError).Err.(unix.Errno) == unix.EXDEV |
| 240 |
} |
| 241 |
|
| 242 |
func quoteString(s string) string { |
| 243 |
return s |
| 244 |
} |
| 245 |
|
| 246 |
func shellEscape(s string) string { |
| 247 |
buf := make([]rune, 0, len(s)) |
| 248 |
for _, r := range s { |
| 249 |
if strings.ContainsRune(" !\"$&'()*,:;<=>?@[\\]^`{|}", r) { |
| 250 |
buf = append(buf, '\\') |
| 251 |
} |
| 252 |
buf = append(buf, r) |
| 253 |
} |
| 254 |
return string(buf) |
| 255 |
} |
| 256 |
|
| 257 |
func shellUnescape(s string) string { |
| 258 |
esc := false |
| 259 |
buf := make([]rune, 0, len(s)) |
| 260 |
for _, r := range s { |
| 261 |
if r == '\\' && !esc { |
| 262 |
esc = true |
| 263 |
continue |
| 264 |
} |
| 265 |
esc = false |
| 266 |
buf = append(buf, r) |
| 267 |
} |
| 268 |
if esc { |
| 269 |
buf = append(buf, '\\') |
| 270 |
} |
| 271 |
return string(buf) |
| 272 |
} |