Skip to content

Commit

Permalink
Improved working with paths in fsutil
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Feb 5, 2016
1 parent de687ab commit 5d5f0b8
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### v1.6.2

* `[fsutil]` Improved working with paths
* `[fsutil]` Added method ProperPath to windows stub

#### v1.6.1
Expand Down
33 changes: 33 additions & 0 deletions fsutil/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"syscall"
"time"

PATH "pkg.re/essentialkaos/ek.v1/path"
"pkg.re/essentialkaos/ek.v1/system"
)

Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -289,6 +307,8 @@ func IsNonEmpty(path string) bool {
return false
}

path = PATH.Clean(path)

return GetSize(path) != 0
}

Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -332,20 +356,26 @@ 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
}

// GetCTime return time of creation
func GetCTime(path string) (time.Time, error) {
path = PATH.Clean(path)

_, _, ctime, err := GetTimes(path)

return ctime, err
}

// GetMTime return time of modification
func GetMTime(path string) (time.Time, error) {
path = PATH.Clean(path)

_, mtime, _, err := GetTimes(path)

return mtime, err
Expand All @@ -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)
Expand All @@ -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)
}

Expand Down
6 changes: 6 additions & 0 deletions fsutil/fs_time_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"errors"
"syscall"
"time"

PATH "pkg.re/essentialkaos/ek.v1/path"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions fsutil/fs_time_freebsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"errors"
"syscall"
"time"

PATH "pkg.re/essentialkaos/ek.v1/path"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions fsutil/fs_time_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"errors"
"syscall"
"time"

PATH "pkg.re/essentialkaos/ek.v1/path"
)

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 5d5f0b8

Please sign in to comment.