diff --git a/pkg/io/files/file_located_in_checker.go b/pkg/io/files/file_located_in_checker.go new file mode 100644 index 0000000..5cfd0aa --- /dev/null +++ b/pkg/io/files/file_located_in_checker.go @@ -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+"/") +} diff --git a/pkg/io/files/file_name_utils.go b/pkg/io/files/file_name_utils.go index 989214c..03f22f6 100644 --- a/pkg/io/files/file_name_utils.go +++ b/pkg/io/files/file_name_utils.go @@ -60,3 +60,14 @@ func ReplaceAllRestrictedCharacters(filePath string) string { return filePath } + +func TryAbs(filePath string) string { + if !filepath.IsAbs(filePath) { + abs, err := filepath.Abs(filePath) + if err == nil { + return abs + } + } + + return filePath +} diff --git a/pkg/io/files/file_perm/auto_file_perm.go b/pkg/io/files/file_perm/auto_file_perm.go new file mode 100644 index 0000000..ccf7b2b --- /dev/null +++ b/pkg/io/files/file_perm/auto_file_perm.go @@ -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 +} diff --git a/pkg/io/files/file_perm/file_perm.go b/pkg/io/files/file_perm/file_perm.go new file mode 100644 index 0000000..8639eaa --- /dev/null +++ b/pkg/io/files/file_perm/file_perm.go @@ -0,0 +1,8 @@ +package file_perm + +import "os" + +const ( + DirUserPermitted os.FileMode = 0755 + DirAllPermitted = os.ModePerm +) diff --git a/pkg/io/files/parent_dir_creator.go b/pkg/io/files/parent_dir_creator.go index 562ab97..efab2ec 100644 --- a/pkg/io/files/parent_dir_creator.go +++ b/pkg/io/files/parent_dir_creator.go @@ -1,11 +1,15 @@ package files -import "os" +import ( + "os" + + "git.tswf.io/incredible-go/incredible-go-core/pkg/io/files/file_perm" +) func MkdirParent(fileLocation string) error { parentDir := GetParentDir(fileLocation) if parentDir != "" { - parentDirCreationErr := os.MkdirAll(parentDir, 0755) + parentDirCreationErr := os.MkdirAll(parentDir, file_perm.GetPermForDir(parentDir)) if parentDirCreationErr != nil { return parentDirCreationErr diff --git a/pkg/io/files/parent_dir_utils.go b/pkg/io/files/parent_dir_utils.go index d31eeea..fa134cb 100644 --- a/pkg/io/files/parent_dir_utils.go +++ b/pkg/io/files/parent_dir_utils.go @@ -1,9 +1,12 @@ package files -import "strings" +import ( + "path/filepath" + "strings" +) func GetParentDir(fileLocation string) string { - fileLocation = strings.ReplaceAll(fileLocation, "\\", "/") + fileLocation = filepath.ToSlash(fileLocation) indexOfSlash := strings.LastIndex(fileLocation, "/") if indexOfSlash > 0 { return fileLocation[:indexOfSlash] diff --git a/pkg/io/files/subfile_finder.go b/pkg/io/files/subfile_finder.go index b957f1f..e586aaa 100644 --- a/pkg/io/files/subfile_finder.go +++ b/pkg/io/files/subfile_finder.go @@ -45,15 +45,15 @@ func ListFiles(dirPath string) (*array_list.ArrayList[os.FileInfo], error) { 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]() 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) } - return nil + return err }) if err != nil {