feature: unix directory permission improvements

master
amorozov 2024-06-30 00:51:26 +03:00
parent 1fe9bae942
commit cf8adf9059
7 changed files with 74 additions and 7 deletions

View File

@ -0,0 +1,22 @@
package files
import (
"path/filepath"
"runtime"
"strings"
)
func IsFileLocatedIn(fileToCheck string, dirInWhichLocated string) bool {
fileToCheck = TryAbs(fileToCheck)
dirInWhichLocated = TryAbs(dirInWhichLocated)
if runtime.GOOS == "windows" {
fileToCheck = strings.ToLower(fileToCheck)
dirInWhichLocated = strings.ToLower(dirInWhichLocated)
}
fileToCheck = filepath.ToSlash(fileToCheck)
dirInWhichLocated = filepath.ToSlash(dirInWhichLocated)
return strings.HasPrefix(fileToCheck, dirInWhichLocated+"/")
}

View File

@ -60,3 +60,14 @@ func ReplaceAllRestrictedCharacters(filePath string) string {
return filePath return filePath
} }
func TryAbs(filePath string) string {
if !filepath.IsAbs(filePath) {
abs, err := filepath.Abs(filePath)
if err == nil {
return abs
}
}
return filePath
}

View File

@ -0,0 +1,19 @@
package file_perm
import (
"os"
"git.tswf.io/incredible-go/incredible-go-core/pkg/io/files"
)
func GetPermForDir(dirLocation string) os.FileMode {
userHome, err := os.UserHomeDir()
if err == nil {
if files.IsFileLocatedIn(dirLocation, userHome) {
return DirUserPermitted
}
}
return DirAllPermitted
}

View File

@ -0,0 +1,8 @@
package file_perm
import "os"
const (
DirUserPermitted os.FileMode = 0755
DirAllPermitted = os.ModePerm
)

View File

@ -1,11 +1,15 @@
package files package files
import "os" import (
"os"
"git.tswf.io/incredible-go/incredible-go-core/pkg/io/files/file_perm"
)
func MkdirParent(fileLocation string) error { func MkdirParent(fileLocation string) error {
parentDir := GetParentDir(fileLocation) parentDir := GetParentDir(fileLocation)
if parentDir != "" { if parentDir != "" {
parentDirCreationErr := os.MkdirAll(parentDir, 0755) parentDirCreationErr := os.MkdirAll(parentDir, file_perm.GetPermForDir(parentDir))
if parentDirCreationErr != nil { if parentDirCreationErr != nil {
return parentDirCreationErr return parentDirCreationErr

View File

@ -1,9 +1,12 @@
package files package files
import "strings" import (
"path/filepath"
"strings"
)
func GetParentDir(fileLocation string) string { func GetParentDir(fileLocation string) string {
fileLocation = strings.ReplaceAll(fileLocation, "\\", "/") fileLocation = filepath.ToSlash(fileLocation)
indexOfSlash := strings.LastIndex(fileLocation, "/") indexOfSlash := strings.LastIndex(fileLocation, "/")
if indexOfSlash > 0 { if indexOfSlash > 0 {
return fileLocation[:indexOfSlash] return fileLocation[:indexOfSlash]

View File

@ -45,15 +45,15 @@ func ListFiles(dirPath string) (*array_list.ArrayList[os.FileInfo], error) {
return result, nil return result, nil
} }
func FindSubfilesByFilter(rootPath string, filter FileFilter) (*array_list.ArrayList[os.FileInfo], error) { func FindSubFilesByFilter(rootPath string, filter FileFilter) (*array_list.ArrayList[os.FileInfo], error) {
result := array_list.NewArrayList[os.FileInfo]() result := array_list.NewArrayList[os.FileInfo]()
err := filepath.Walk(rootPath, func(path string, info fs.FileInfo, err error) error { err := filepath.Walk(rootPath, func(path string, info fs.FileInfo, err error) error {
if err != nil && filter(info, path) { if err == nil && filter(info, path) {
result.Add(info) result.Add(info)
} }
return nil return err
}) })
if err != nil { if err != nil {