Remove a few calls do document.createElement
[armadillo.git] / src / paths.go
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, Robert Sesek <http://www.bluestatic.org>
4 //
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.
8 //
9
10 package paths
11
12 import (
13 "container/vector"
14 "os"
15 "path"
16 "strings"
17 "./config"
18 )
19
20 var gConfig *config.Configuration
21
22 func SetConfig(aConfig *config.Configuration) {
23 gConfig = aConfig
24 }
25
26 func canonicalizePath(raw_path string) string {
27 raw_path = path.Join(gConfig.JailRoot, raw_path)
28 return path.Clean(raw_path)
29 }
30
31 func checkInJail(the_path string) bool {
32 if len(the_path) < len(gConfig.JailRoot) {
33 return false
34 }
35 if the_path[0:len(gConfig.JailRoot)] != gConfig.JailRoot {
36 return false
37 }
38 if strings.Index(the_path, "../") != -1 {
39 return false
40 }
41 return true
42 }
43
44 func IsValid(path string) (bool, string) {
45 path = canonicalizePath(path)
46 _, err := os.Lstat(path)
47 return err == nil && checkInJail(path), path
48 }
49
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")
54 }
55
56 fd, file_error := os.Open(full_path)
57 if file_error != nil {
58 return nil, file_error
59 }
60
61 fileinfos, read_err := fd.Readdir(-1)
62 if read_err != nil {
63 return nil, read_err
64 }
65
66 for _, info := range fileinfos {
67 name := info.Name
68 if info.IsDirectory() {
69 name += "/"
70 }
71 if !gConfig.IncludeDotfiles && name[0] == '.' {
72 continue
73 }
74 files.Push(name)
75 }
76 return files, nil
77 }
78
79 func Remove(the_path string) os.Error {
80 full_path := canonicalizePath(the_path)
81 if !checkInJail(full_path) {
82 return os.NewError("Path outside of jail")
83 }
84 return os.RemoveAll(full_path)
85 }
86
87 func Move(source string, target string) os.Error {
88 source = canonicalizePath(source)
89 target = canonicalizePath(target)
90 if !checkInJail(source) {
91 return os.NewError("Source outside of jail")
92 }
93 if !checkInJail(target) {
94 return os.NewError("Target outside of jail")
95 }
96 return os.Rename(source, target)
97 }
98
99 func MakeDir(target string) os.Error {
100 target = canonicalizePath(target)
101 if !checkInJail(target) {
102 return os.NewError("Path outside of jail")
103 }
104 return os.Mkdir(target, 0755)
105 }