From 459a4f330b369d4e9b753ee2e68c0479e1fdd31e Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sat, 8 Sep 2012 16:19:40 +0200 Subject: Add files and dirs manipulation functions. --- src/ninjacloud.go | 130 ++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 117 insertions(+), 13 deletions(-) diff --git a/src/ninjacloud.go b/src/ninjacloud.go index 2d3c2f0..57e939a 100644 --- a/src/ninjacloud.go +++ b/src/ninjacloud.go @@ -1,12 +1,16 @@ package main import ( + "encoding/json" + //"errors" "flag" + "io/ioutil" "log" "net/http" - "path" - //"io/ioutil" - //"os" + "os" + //"path" + "io" + "path/filepath" ) const APP_NAME = "Ninja Go Local Cloud" @@ -29,32 +33,121 @@ const statusPathLen = len(statusPath) //////// FILESYSTEM +func exist(path string) bool { + _, err := os.Stat(path) + if !os.IsNotExist(err) { + return true + } + return false +} + //// Files -func writeFile() { +func writeFile(path string, content []byte, overwrite bool) (err error) { + if !overwrite { + if exist(path) { + err = os.ErrExist + return + } + } + err = ioutil.WriteFile(path, content, 0600) + return +} + +func readFile(path string) (content []byte, err error) { + content, err = ioutil.ReadFile(path) + return } -func readFile() { +func removeFile(path string) (err error) { + err = os.Remove(path) + return } -func removeFile() { +func moveFile(source string, dest string) (err error) { + err = os.Rename(source, dest) + return } -func copyFile() { +func copyFile(source string, dest string) (err error) { + // from https://gist.github.com/2876519 + sf, err := os.Open(source) + if err != nil { + return err + } + defer sf.Close() + df, err := os.Create(dest) + if err != nil { + return err + } + defer df.Close() + _, err = io.Copy(df, sf) + if err == nil { + si, err := os.Stat(source) + if err != nil { + err = os.Chmod(dest, si.Mode()) + } + + } + return } //// Dirs -func createDir() { +func createDir(path string) (err error) { + err = os.MkdirAll(path, 0600) + return } -func removeDir() { +func removeDir(path string) (err error) { + err = os.RemoveAll(path) + return } -func listDir() { +func listDir(path string) (list []os.FileInfo, err error) { + list, err = ioutil.ReadDir(path) + return } -func copyDir() { +func moveDir(source string, dest string) (err error) { + err = os.Rename(source, dest) + return +} + +func copyDir(source string, dest string) (err error) { + // from https://gist.github.com/2876519 + fi, err := os.Stat(source) + if err != nil { + return + } + if !fi.IsDir() { + return os.ErrInvalid + } + _, err = os.Open(dest) + if !os.IsNotExist(err) { + return os.ErrExist + } + err = os.MkdirAll(dest, fi.Mode()) + if err != nil { + return + } + entries, err := ioutil.ReadDir(source) + for _, entry := range entries { + sfp := source + "/" + entry.Name() + dfp := dest + "/" + entry.Name() + if entry.IsDir() { + err = copyDir(sfp, dfp) + if err != nil { + return + } + } else { + err = copyFile(sfp, dfp) + if err != nil { + return + } + } + } + return } //////// REQUEST HANDLERS @@ -63,7 +156,7 @@ func copyDir() { func fileHandler(w http.ResponseWriter, r *http.Request) { p := r.URL.Path[filePathLen:] - path.Clean(p) + filepath.Clean(p) switch r.Method { case "POST": @@ -86,7 +179,7 @@ func fileHandler(w http.ResponseWriter, r *http.Request) { func dirHandler(w http.ResponseWriter, r *http.Request) { p := r.URL.Path[dirPathLen:] - path.Clean(p) + filepath.Clean(p) switch r.Method { case "POST": @@ -111,6 +204,17 @@ func getDataHandler(w http.ResponseWriter, r *http.Request) { // Get the cloud status JSON func getStatusHandler(w http.ResponseWriter, r *http.Request) { + cloudStatus := map[string]string{ + "name": APP_NAME, + "version": APP_VERSION, + "server-root": rootFlag, + "status": "running", + } + j, err := json.Marshal(cloudStatus) + if err != nil { + log.Println(err) + } + w.Write(j) } //////// INIT and MAIN -- cgit v1.2.3