From d67a085b9cd6e5d0f796d24961c5b11902e5263a Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 30 Aug 2010 14:08:23 -0400 Subject: [PATCH] Add path listing service. --- build.py | 1 + src/paths.go | 40 ++++++++++++++++++++++++++++++++++++++++ src/server.go | 8 +++++++- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/paths.go diff --git a/build.py b/build.py index ac125bf..1f2328f 100755 --- a/build.py +++ b/build.py @@ -16,6 +16,7 @@ CLOSURE_COMPILER = os.path.join(ROOT, 'closure-compiler.jar') CLOSURE_CALCDEPS = os.path.join(CLOSURE_DEST, 'closure', 'bin', 'calcdeps.py') SOURCES = [ + 'paths.go', 'server.go', 'main.go' ] diff --git a/src/paths.go b/src/paths.go new file mode 100644 index 0000000..896b5cd --- /dev/null +++ b/src/paths.go @@ -0,0 +1,40 @@ +package paths + +import ( + "container/vector" + "os" + "path" +) + +const kJailRoot = "/Users/rsesek/Downloads" + +func canonicalizePath(raw_path string) string { + raw_path = path.Join(kJailRoot, raw_path) + return path.Clean(raw_path) +} + +func checkInJail(path string) bool { + return true +} + +func List(the_path string) (files vector.StringVector, err os.Error) { + full_path := canonicalizePath(the_path) + if !checkInJail(full_path) { + return nil, os.NewError("path outside of jail") + } + + fd, file_error := os.Open(full_path, os.O_RDONLY, 0) + if file_error != nil { + return nil, file_error + } + + fileinfos, read_err := fd.Readdir(-1) + if read_err != nil { + return nil, read_err + } + + for _, info := range fileinfos { + files.Push(info.Name) + } + return files, nil +} diff --git a/src/server.go b/src/server.go index 294c0fa..026f78d 100644 --- a/src/server.go +++ b/src/server.go @@ -7,6 +7,7 @@ import ( "json" "os" "path" + "./paths" ) const kFrontEndFiles = "/Users/rsesek/Projects/armadillo/out/fe/" @@ -28,7 +29,12 @@ func serviceHandler(connection *http.Conn, request *http.Request) { switch request.FormValue("action") { case "list": - okResponse(connection, "Request received") + files, err := paths.List("./") + if err != nil { + errorResponse(connection, err.String()) + } else { + okResponse(connection, files) + } return } -- 2.43.5