]>
src.bluestatic.org Git - armadillo.git/blob - src/paths.go
2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, 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 // Verifies that the path is in the jail and returns the cleaned-up path. Will
45 // return nil on error.
46 func Verify(thePath
string) *string {
47 fullPath
:= canonicalizePath(thePath
)
48 if !checkInJail(fullPath
) {
54 func List(the_path
string) (files vector
.StringVector
, err os
.Error
) {
55 full_path
:= canonicalizePath(the_path
)
56 if !checkInJail(full_path
) {
57 return nil, os
.NewError("Path outside of jail")
60 fd
, file_error
:= os
.Open(full_path
, os
.O_RDONLY
, 0)
61 if file_error
!= nil {
62 return nil, file_error
65 fileinfos
, read_err
:= fd
.Readdir(-1)
70 for _
, info
:= range fileinfos
{
72 if info
.IsDirectory() {
75 if !gConfig
.IncludeDotfiles
&& name
[0] == '.' {
83 func Remove(the_path
string) os
.Error
{
84 full_path
:= canonicalizePath(the_path
)
85 if !checkInJail(full_path
) {
86 return os
.NewError("Path outside of jail")
88 return os
.RemoveAll(full_path
)
91 func Move(source
string, target
string) os
.Error
{
92 source
= canonicalizePath(source
)
93 target
= canonicalizePath(target
)
94 if !checkInJail(source
) {
95 return os
.NewError("Source outside of jail")
97 if !checkInJail(target
) {
98 return os
.NewError("Target outside of jail")
100 return os
.Rename(source
, target
)