try to automatically find shared data dirs in system-wide paths

Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
Nicola Murino
2022-05-05 11:27:19 +02:00
parent 61947e67ae
commit 80da2dc722
16 changed files with 63 additions and 53 deletions

View File

@@ -346,6 +346,32 @@ func IsFileInputValid(fileInput string) bool {
return true
}
// FindSharedDataPath searches for the specified directory name in searchDir
// and in system-wide shared data directories.
// If name is an absolute path it is returned unmodified.
func FindSharedDataPath(name, searchDir string) string {
if !IsFileInputValid(name) {
return ""
}
if name != "" && !filepath.IsAbs(name) {
searchList := []string{searchDir}
if runtime.GOOS != osWindows {
searchList = append(searchList, "/usr/share/sftpgo")
searchList = append(searchList, "/usr/local/share/sftpgo")
}
for _, basePath := range searchList {
res := filepath.Join(basePath, name)
_, err := os.Stat(res)
if err == nil {
logger.Debug(logSender, "", "found share data path for name %#v: %#v", name, res)
return res
}
}
return filepath.Join(searchDir, name)
}
return name
}
// CleanDirInput sanitizes user input for directories.
// On Windows it removes any trailing `"`.
// We try to help windows users that set an invalid path such as "C:\ProgramData\SFTPGO\".