feature: unix directory permission improvements
parent
1fe9bae942
commit
cf8adf9059
|
@ -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+"/")
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package file_perm
|
||||
|
||||
import "os"
|
||||
|
||||
const (
|
||||
DirUserPermitted os.FileMode = 0755
|
||||
DirAllPermitted = os.ModePerm
|
||||
)
|
|
@ -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
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue