From 1de0f655b9d6c1bdf3a3ddb676cb2768b544adc5 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 13 Oct 2012 19:02:13 -0400 Subject: [PATCH] Clean up paths.go to be more idiomatic. --- server/paths.go | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/server/paths.go b/server/paths.go index c8371dd..83f0308 100644 --- a/server/paths.go +++ b/server/paths.go @@ -16,45 +16,44 @@ import ( "strings" ) -func canonicalizePath(raw_path string) string { - raw_path = path.Join(gConfig.JailRoot, raw_path) - return path.Clean(raw_path) +func canonicalizePath(raw string) string { + return path.Clean(path.Join(gConfig.JailRoot, raw)) } -func checkInJail(the_path string) bool { - if len(the_path) < len(gConfig.JailRoot) { +func checkInJail(p string) bool { + if len(p) < len(gConfig.JailRoot) { return false } - if the_path[0:len(gConfig.JailRoot)] != gConfig.JailRoot { + if p[0:len(gConfig.JailRoot)] != gConfig.JailRoot { return false } - if strings.Index(the_path, "../") != -1 { + if strings.Index(p, "../") != -1 { return false } return true } -func IsValidPath(path string) (bool, string) { - path = canonicalizePath(path) - _, err := os.Lstat(path) - return err == nil && checkInJail(path), path +func IsValidPath(p string) (bool, string) { + p = canonicalizePath(p) + _, err := os.Lstat(p) + return err == nil && checkInJail(p), p } -func ListPath(the_path string) (files []string, err error) { - full_path := canonicalizePath(the_path) - if !checkInJail(full_path) { +func ListPath(p string) (files []string, err error) { + p = canonicalizePath(p) + if !checkInJail(p) { return nil, errors.New("Path outside of jail") } - fd, file_error := os.Open(full_path) - if file_error != nil { - return nil, file_error + f, err := os.Open(p) + if err != nil { + return } - defer fd.Close() + defer f.Close() - fileinfos, read_err := fd.Readdir(-1) - if read_err != nil { - return nil, read_err + fileinfos, err := f.Readdir(-1) + if err != nil { + return } files = make([]string, 0) @@ -71,12 +70,12 @@ func ListPath(the_path string) (files []string, err error) { return files, nil } -func RemovePath(the_path string) error { - full_path := canonicalizePath(the_path) - if !checkInJail(full_path) { +func RemovePath(p string) error { + p = canonicalizePath(p) + if !checkInJail(p) { return errors.New("Path outside of jail") } - return os.RemoveAll(full_path) + return os.RemoveAll(p) } func MovePath(source string, target string) error { -- 2.43.5