From de687abbc200b035d9ef95ae0240a4cd304edb1a Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Fri, 5 Feb 2016 10:50:01 +0100 Subject: [PATCH 1/2] Added method fsutil.ProperPath to windows stub --- changelog.md | 4 ++++ fsutil/fs_windows.go | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/changelog.md b/changelog.md index 485b4281..7ca2760e 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,9 @@ ## Changelog +#### v1.6.2 + +* `[fsutil]` Added method ProperPath to windows stub + #### v1.6.1 * `[path]` Fixed windows stub diff --git a/fsutil/fs_windows.go b/fsutil/fs_windows.go index 6a44bc50..10b9cd65 100644 --- a/fsutil/fs_windows.go +++ b/fsutil/fs_windows.go @@ -48,6 +48,10 @@ func CheckPerms(perms, path string) bool { return false } +func ProperPath(props string, paths []string) string { + return "" +} + func IsExist(path string) bool { return false } From 5d5f0b870554c9fe20e32df899b1e70510490431 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Fri, 5 Feb 2016 11:21:20 +0100 Subject: [PATCH 2/2] Improved working with paths in fsutil --- changelog.md | 1 + fsutil/fs.go | 33 +++++++++++++++++++++++++++++++++ fsutil/fs_time_darwin.go | 6 ++++++ fsutil/fs_time_freebsd.go | 6 ++++++ fsutil/fs_time_linux.go | 6 ++++++ 5 files changed, 52 insertions(+) diff --git a/changelog.md b/changelog.md index 7ca2760e..cfca3426 100644 --- a/changelog.md +++ b/changelog.md @@ -2,6 +2,7 @@ #### v1.6.2 +* `[fsutil]` Improved working with paths * `[fsutil]` Added method ProperPath to windows stub #### v1.6.1 diff --git a/fsutil/fs.go b/fsutil/fs.go index 524fb1a0..43e4ebcc 100644 --- a/fsutil/fs.go +++ b/fsutil/fs.go @@ -17,6 +17,7 @@ import ( "syscall" "time" + PATH "pkg.re/essentialkaos/ek.v1/path" "pkg.re/essentialkaos/ek.v1/system" ) @@ -49,6 +50,7 @@ func CheckPerms(props, path string) bool { return false } + path = PATH.Clean(path) props = strings.ToUpper(props) var stat = &syscall.Stat_t{} @@ -131,6 +133,8 @@ func CheckPerms(props, path string) bool { // ProperPath return first proper path from given slice func ProperPath(props string, paths []string) string { for _, path := range paths { + path = PATH.Clean(path) + if CheckPerms(props, path) { return path } @@ -145,11 +149,14 @@ func IsExist(path string) bool { return false } + path = PATH.Clean(path) + return syscall.Access(path, syscall.F_OK) == nil } // IsRegular check if target is regular file or not func IsRegular(path string) bool { + path = PATH.Clean(path) mode := getMode(path) if mode == 0 { @@ -161,6 +168,7 @@ func IsRegular(path string) bool { // IsSocket check if target is socket or not func IsSocket(path string) bool { + path = PATH.Clean(path) mode := getMode(path) if mode == 0 { @@ -172,6 +180,7 @@ func IsSocket(path string) bool { // IsBlockDevice check if target is block device or not func IsBlockDevice(path string) bool { + path = PATH.Clean(path) mode := getMode(path) if mode == 0 { @@ -183,6 +192,7 @@ func IsBlockDevice(path string) bool { // IsCharacterDevice check if target is character device or not func IsCharacterDevice(path string) bool { + path = PATH.Clean(path) mode := getMode(path) if mode == 0 { @@ -194,6 +204,7 @@ func IsCharacterDevice(path string) bool { // IsDir check if target is directory or not func IsDir(path string) bool { + path = PATH.Clean(path) mode := getMode(path) if mode == 0 { @@ -205,6 +216,7 @@ func IsDir(path string) bool { // IsLink check if file is link or not func IsLink(path string) bool { + path = PATH.Clean(path) mode := getMode(path) if mode == 0 { @@ -220,6 +232,8 @@ func IsReadable(path string) bool { return false } + path = PATH.Clean(path) + var stat = &syscall.Stat_t{} err := syscall.Stat(path, stat) @@ -243,6 +257,8 @@ func IsWritable(path string) bool { return false } + path = PATH.Clean(path) + var stat = &syscall.Stat_t{} err := syscall.Stat(path, stat) @@ -266,6 +282,8 @@ func IsExecutable(path string) bool { return false } + path = PATH.Clean(path) + var stat = &syscall.Stat_t{} err := syscall.Stat(path, stat) @@ -289,6 +307,8 @@ func IsNonEmpty(path string) bool { return false } + path = PATH.Clean(path) + return GetSize(path) != 0 } @@ -298,6 +318,8 @@ func IsEmptyDir(path string) bool { return false } + path = PATH.Clean(path) + fd, err := syscall.Open(path, syscall.O_RDONLY, 0) if err != nil { @@ -319,6 +341,8 @@ func GetOwner(path string) (int, int, error) { return -1, -1, errors.New("Path is empty") } + path = PATH.Clean(path) + var stat = &syscall.Stat_t{} err := syscall.Stat(path, stat) @@ -332,6 +356,8 @@ func GetOwner(path string) (int, int, error) { // GetATime return time of last access func GetATime(path string) (time.Time, error) { + path = PATH.Clean(path) + atime, _, _, err := GetTimes(path) return atime, err @@ -339,6 +365,8 @@ func GetATime(path string) (time.Time, error) { // GetCTime return time of creation func GetCTime(path string) (time.Time, error) { + path = PATH.Clean(path) + _, _, ctime, err := GetTimes(path) return ctime, err @@ -346,6 +374,8 @@ func GetCTime(path string) (time.Time, error) { // GetMTime return time of modification func GetMTime(path string) (time.Time, error) { + path = PATH.Clean(path) + _, mtime, _, err := GetTimes(path) return mtime, err @@ -357,6 +387,8 @@ func GetSize(path string) int64 { return 0 } + path = PATH.Clean(path) + var stat = &syscall.Stat_t{} err := syscall.Stat(path, stat) @@ -370,6 +402,7 @@ func GetSize(path string) int64 { // GetPerm return file permissions func GetPerm(path string) os.FileMode { + path = PATH.Clean(path) return os.FileMode(getMode(path) & 0777) } diff --git a/fsutil/fs_time_darwin.go b/fsutil/fs_time_darwin.go index 935348c1..f164c663 100644 --- a/fsutil/fs_time_darwin.go +++ b/fsutil/fs_time_darwin.go @@ -13,6 +13,8 @@ import ( "errors" "syscall" "time" + + PATH "pkg.re/essentialkaos/ek.v1/path" ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -23,6 +25,8 @@ func GetTimes(path string) (time.Time, time.Time, time.Time, error) { return time.Time{}, time.Time{}, time.Time{}, errors.New("Path is empty") } + path = PATH.Clean(path) + var stat = &syscall.Stat_t{} err := syscall.Stat(path, stat) @@ -43,6 +47,8 @@ func GetTimestamps(path string) (int64, int64, int64, error) { return -1, -1, -1, errors.New("Path is empty") } + path = PATH.Clean(path) + var stat = &syscall.Stat_t{} err := syscall.Stat(path, stat) diff --git a/fsutil/fs_time_freebsd.go b/fsutil/fs_time_freebsd.go index 70b08176..e69b9c67 100644 --- a/fsutil/fs_time_freebsd.go +++ b/fsutil/fs_time_freebsd.go @@ -13,6 +13,8 @@ import ( "errors" "syscall" "time" + + PATH "pkg.re/essentialkaos/ek.v1/path" ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -23,6 +25,8 @@ func GetTimes(path string) (time.Time, time.Time, time.Time, error) { return time.Time{}, time.Time{}, time.Time{}, errors.New("Path is empty") } + path = PATH.Clean(path) + var stat = &syscall.Stat_t{} err := syscall.Stat(path, stat) @@ -43,6 +47,8 @@ func GetTimestamps(path string) (int64, int64, int64, error) { return -1, -1, -1, errors.New("Path is empty") } + path = PATH.Clean(path) + var stat = &syscall.Stat_t{} err := syscall.Stat(path, stat) diff --git a/fsutil/fs_time_linux.go b/fsutil/fs_time_linux.go index 252f575d..39626ece 100644 --- a/fsutil/fs_time_linux.go +++ b/fsutil/fs_time_linux.go @@ -13,6 +13,8 @@ import ( "errors" "syscall" "time" + + PATH "pkg.re/essentialkaos/ek.v1/path" ) // ////////////////////////////////////////////////////////////////////////////////// // @@ -23,6 +25,8 @@ func GetTimes(path string) (time.Time, time.Time, time.Time, error) { return time.Time{}, time.Time{}, time.Time{}, errors.New("Path is empty") } + path = PATH.Clean(path) + var stat = &syscall.Stat_t{} err := syscall.Stat(path, stat) @@ -43,6 +47,8 @@ func GetTimestamps(path string) (int64, int64, int64, error) { return -1, -1, -1, errors.New("Path is empty") } + path = PATH.Clean(path) + var stat = &syscall.Stat_t{} err := syscall.Stat(path, stat)