2017-10-29 16:59:03 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
// The Path module should work with the stack by providing functions that update the stack
|
|
|
|
// with information that helps keeping track of the directories that the clients use.
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PATH is the constant which should contain the fixed path where the simpleFTP server will run
|
2017-11-09 20:39:58 +00:00
|
|
|
// This will act like a root cage. It must end with a forward slash!
|
2017-11-16 09:19:01 +00:00
|
|
|
var BasePath = ""
|
2017-10-29 16:59:03 +00:00
|
|
|
|
|
|
|
// MakePathFromStringStack gets a StringStack and makes a path.
|
|
|
|
func MakePathFromStringStack(stack *StringStack) string {
|
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
|
|
|
buffer.WriteString(BasePath)
|
|
|
|
if BasePath[len(BasePath)-1] != '/' {
|
|
|
|
buffer.WriteString("/")
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < stack.Size(); i++ {
|
|
|
|
buffer.WriteString(stack.Items()[i] + "/")
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffer.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeDirectory changes the current working directory with respect to BasePath
|
|
|
|
func ChangeDirectory(stack *StringStack, directory string) error {
|
|
|
|
stack.Push(directory)
|
|
|
|
|
|
|
|
path := MakePathFromStringStack(stack)
|
|
|
|
fileInfo, err := os.Stat(path)
|
|
|
|
if err != nil {
|
2017-11-09 20:39:58 +00:00
|
|
|
stack.Pop()
|
2017-11-09 21:02:11 +00:00
|
|
|
return PathError{err}
|
2017-10-29 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if fileInfo.IsDir() == false {
|
2017-11-09 20:39:58 +00:00
|
|
|
stack.Pop()
|
2017-10-29 16:59:03 +00:00
|
|
|
return ErrNotADirectory
|
|
|
|
}
|
|
|
|
|
|
|
|
// The last 9 bits represent the Unix perms format rwxrwxrwx
|
|
|
|
perms := fileInfo.Mode().Perm()
|
|
|
|
|
|
|
|
// The user has permission to view the directory
|
|
|
|
if (perms&1 != 0) && (perms&(1<<2) != 0) || (perms&(1<<6) != 0) && (perms&(1<<8) != 0) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
stack.Pop()
|
2017-11-09 21:02:11 +00:00
|
|
|
return PathError{err}
|
2017-10-29 16:59:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChangeDirectoryToPrevious changes the current working directory to the previous one,
|
|
|
|
// doesn't go past the BasePath
|
|
|
|
func ChangeDirectoryToPrevious(stack *StringStack) error {
|
|
|
|
if stack.IsEmpty() {
|
|
|
|
return ErrAlreadyAtBaseDirectory
|
|
|
|
}
|
|
|
|
stack.Pop()
|
|
|
|
return nil
|
|
|
|
}
|