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(p string) (files []string, err error) {
43 p = canonicalizePath(p)
45 return nil, errors.New("Path outside of jail")
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(p string) error {
74 p = canonicalizePath(p)
76 return errors.New("Path outside of jail")
78 return os.RemoveAll(p)
81 func MovePath(source string, target string) error {
82 source = canonicalizePath(source)
83 target = canonicalizePath(target)
84 if !checkInJail(source) {
85 return errors.New("Source outside of jail")
87 if !checkInJail(target) {
88 return errors.New("Target outside of jail")
90 return os.Rename(source, target)
93 func MakeDir(target string) error {
94 target = canonicalizePath(target)
95 if !checkInJail(target) {
96 return errors.New("Path outside of jail")
98 return os.Mkdir(target, 0755)