mirror of
https://github.com/netbirdio/netbird.git
synced 2024-11-27 10:33:11 +01:00
59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
//go:build windows || (linux && !android) || (darwin && !ios) || freebsd
|
|
|
|
package system
|
|
|
|
import (
|
|
"os"
|
|
"slices"
|
|
|
|
"github.com/shirou/gopsutil/v3/process"
|
|
)
|
|
|
|
// getRunningProcesses returns a list of running process paths.
|
|
func getRunningProcesses() ([]string, error) {
|
|
processes, err := process.Processes()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
processMap := make(map[string]bool)
|
|
for _, p := range processes {
|
|
path, _ := p.Exe()
|
|
if path != "" {
|
|
processMap[path] = true
|
|
}
|
|
}
|
|
|
|
uniqueProcesses := make([]string, 0, len(processMap))
|
|
for p := range processMap {
|
|
uniqueProcesses = append(uniqueProcesses, p)
|
|
}
|
|
|
|
return uniqueProcesses, nil
|
|
}
|
|
|
|
// checkFileAndProcess checks if the file path exists and if a process is running at that path.
|
|
func checkFileAndProcess(paths []string) ([]File, error) {
|
|
files := make([]File, len(paths))
|
|
if len(paths) == 0 {
|
|
return files, nil
|
|
}
|
|
|
|
runningProcesses, err := getRunningProcesses()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
for i, path := range paths {
|
|
file := File{Path: path}
|
|
|
|
_, err := os.Stat(path)
|
|
file.Exist = !os.IsNotExist(err)
|
|
|
|
file.ProcessIsRunning = slices.Contains(runningProcesses, path)
|
|
files[i] = file
|
|
}
|
|
|
|
return files, nil
|
|
}
|