Created
July 1, 2012 14:34
-
-
Save BrunoBonacci/3028607 to your computer and use it in GitHub Desktop.
Vert.x Simple Telnet Server - command "cd"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
////////////////////////////////////////////////////////////////////////// cd | |
def cd(def path, String cid) { | |
path = path == "" ? null : path | |
def newPath = virtual2real(path, cid); | |
if (!newPath.exists() || !newPath.isDirectory()) | |
return [status: "ERROR", message: "### ERROR: $path: no such directory!".toString()] | |
// changing directory | |
_connections[cid].curDir = new File(newPath.canonicalPath); | |
return [status: "OK", message: ""]; | |
} | |
File virtual2real(def path, def cid) { | |
def newPath = new File(BASE_DIR); | |
if (path != null) { | |
if (path.startsWith("/")) | |
newPath = new File("$BASE_DIR$path"); | |
else | |
newPath = new File("${_connections[cid].curDir}/$path"); | |
} | |
// security: in case goes above root like: /../../ | |
if (!newPath.canonicalPath.startsWith(BASE_DIR)) | |
newPath = new File(BASE_DIR); | |
return newPath; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment