Skip to content

Instantly share code, notes, and snippets.

@BrunoBonacci
Created July 1, 2012 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrunoBonacci/3028607 to your computer and use it in GitHub Desktop.
Save BrunoBonacci/3028607 to your computer and use it in GitHub Desktop.
Vert.x Simple Telnet Server - command "cd"
////////////////////////////////////////////////////////////////////////// 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