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.
20 var gConfig *config.Configuration
22 func SetConfig(aConfig *config.Configuration) {
26 func canonicalizePath(raw_path string) string {
27 raw_path = path.Join(gConfig.JailRoot, raw_path)
28 return path.Clean(raw_path)
31 func checkInJail(the_path string) bool {
32 if len(the_path) < len(gConfig.JailRoot) {
35 if the_path[0:len(gConfig.JailRoot)] != gConfig.JailRoot {
38 if strings.Index(the_path, "../") != -1 {
44 func IsValid(path string) (bool, string) {
45 path = canonicalizePath(path)
46 _, err := os.Lstat(path)
47 return err == nil && checkInJail(path), path
50 func List(the_path string) (files vector.StringVector, err os.Error) {
51 full_path := canonicalizePath(the_path)
52 if !checkInJail(full_path) {
53 return nil, os.NewError("Path outside of jail")
56 fd, file_error := os.Open(full_path)
57 if file_error != nil {
58 return nil, file_error
62 fileinfos, read_err := fd.Readdir(-1)
67 for _, info := range fileinfos {
69 if info.IsDirectory() {
72 if !gConfig.IncludeDotfiles && name[0] == '.' {
80 func Remove(the_path string) os.Error {
81 full_path := canonicalizePath(the_path)
82 if !checkInJail(full_path) {
83 return os.NewError("Path outside of jail")
85 return os.RemoveAll(full_path)
88 func Move(source string, target string) os.Error {
89 source = canonicalizePath(source)
90 target = canonicalizePath(target)
91 if !checkInJail(source) {
92 return os.NewError("Source outside of jail")
94 if !checkInJail(target) {
95 return os.NewError("Target outside of jail")
97 return os.Rename(source, target)
100 func MakeDir(target string) os.Error {
101 target = canonicalizePath(target)
102 if !checkInJail(target) {
103 return os.NewError("Path outside of jail")
105 return os.Mkdir(target, 0755)