2 // Armadillo File Manager
3 // Copyright (c) 2010-2012, Robert Sesek <http://www.bluestatic.org>
5 // This program is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free Software
7 // Foundation, either version 3 of the License, or any later version.
19 func canonicalizePath(raw string) string {
20 return path.Clean(path.Join(gConfig.JailRoot, raw))
23 func checkInJail(p string) bool {
24 if len(p) < len(gConfig.JailRoot) {
27 if p[0:len(gConfig.JailRoot)] != gConfig.JailRoot {
30 if strings.Index(p, "../") != -1 {
36 func IsValidPath(p string) (bool, string) {
37 p = canonicalizePath(p)
39 return err == nil && checkInJail(p), p
42 func ListPath(op string) (files []string, err error) {
43 p := canonicalizePath(op)
45 return nil, fmt.Errorf("Path outside of jail: %q", op)
54 fileinfos, err := f.Readdir(-1)
59 files = make([]string, 0)
60 for _, info := range fileinfos {
65 if !gConfig.IncludeDotfiles && name[0] == '.' {
68 files = append(files, name)
73 func RemovePath(op string) error {
74 p := canonicalizePath(op)
76 return fmt.Errorf("Path outside of jail: %q", op)
78 return os.RemoveAll(p)
81 func MovePath(oSource string, oTarget string) error {
82 source := canonicalizePath(oSource)
83 target := canonicalizePath(oTarget)
84 if !checkInJail(source) {
85 return fmt.Errorf("Source outside of jail: %q", oSource)
87 if !checkInJail(target) {
88 return fmt.Errorf("Target outside of jail: %q", oTarget)
90 return os.Rename(source, target)
93 func MakeDir(oTarget string) error {
94 target := canonicalizePath(oTarget)
95 if !checkInJail(target) {
96 return fmt.Errorf("Path outside of jail: %q", oTarget)
98 return os.Mkdir(target, 0755)