diff options
author | Pacien TRAN-GIRARD | 2012-09-08 16:19:40 +0200 |
---|---|---|
committer | Pacien | 2015-12-07 22:48:22 +0100 |
commit | 459a4f330b369d4e9b753ee2e68c0479e1fdd31e (patch) | |
tree | 93ba50dde29c31fc2f4e81eaa26c383fdc95757a /src | |
parent | c5a9744b87354f67e219ee0069180a94ed27c3ee (diff) | |
download | ninja-go-local-cloud-459a4f330b369d4e9b753ee2e68c0479e1fdd31e.tar.gz |
Add files and dirs manipulation functions.
Diffstat (limited to 'src')
-rw-r--r-- | src/ninjacloud.go | 130 |
1 files 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 @@ | |||
1 | package main | 1 | package main |
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "encoding/json" | ||
5 | //"errors" | ||
4 | "flag" | 6 | "flag" |
7 | "io/ioutil" | ||
5 | "log" | 8 | "log" |
6 | "net/http" | 9 | "net/http" |
7 | "path" | 10 | "os" |
8 | //"io/ioutil" | 11 | //"path" |
9 | //"os" | 12 | "io" |
13 | "path/filepath" | ||
10 | ) | 14 | ) |
11 | 15 | ||
12 | const APP_NAME = "Ninja Go Local Cloud" | 16 | const APP_NAME = "Ninja Go Local Cloud" |
@@ -29,32 +33,121 @@ const statusPathLen = len(statusPath) | |||
29 | 33 | ||
30 | //////// FILESYSTEM | 34 | //////// FILESYSTEM |
31 | 35 | ||
36 | func exist(path string) bool { | ||
37 | _, err := os.Stat(path) | ||
38 | if !os.IsNotExist(err) { | ||
39 | return true | ||
40 | } | ||
41 | return false | ||
42 | } | ||
43 | |||
32 | //// Files | 44 | //// Files |
33 | 45 | ||
34 | func writeFile() { | 46 | func writeFile(path string, content []byte, overwrite bool) (err error) { |
47 | if !overwrite { | ||
48 | if exist(path) { | ||
49 | err = os.ErrExist | ||
50 | return | ||
51 | } | ||
52 | } | ||
53 | err = ioutil.WriteFile(path, content, 0600) | ||
54 | return | ||
55 | } | ||
56 | |||
57 | func readFile(path string) (content []byte, err error) { | ||
58 | content, err = ioutil.ReadFile(path) | ||
59 | return | ||
35 | } | 60 | } |
36 | 61 | ||
37 | func readFile() { | 62 | func removeFile(path string) (err error) { |
63 | err = os.Remove(path) | ||
64 | return | ||
38 | } | 65 | } |
39 | 66 | ||
40 | func removeFile() { | 67 | func moveFile(source string, dest string) (err error) { |
68 | err = os.Rename(source, dest) | ||
69 | return | ||
41 | } | 70 | } |
42 | 71 | ||
43 | func copyFile() { | 72 | func copyFile(source string, dest string) (err error) { |
73 | // from https://gist.github.com/2876519 | ||
74 | sf, err := os.Open(source) | ||
75 | if err != nil { | ||
76 | return err | ||
77 | } | ||
78 | defer sf.Close() | ||
79 | df, err := os.Create(dest) | ||
80 | if err != nil { | ||
81 | return err | ||
82 | } | ||
83 | defer df.Close() | ||
84 | _, err = io.Copy(df, sf) | ||
85 | if err == nil { | ||
86 | si, err := os.Stat(source) | ||
87 | if err != nil { | ||
88 | err = os.Chmod(dest, si.Mode()) | ||
89 | } | ||
90 | |||
91 | } | ||
92 | return | ||
44 | } | 93 | } |
45 | 94 | ||
46 | //// Dirs | 95 | //// Dirs |
47 | 96 | ||
48 | func createDir() { | 97 | func createDir(path string) (err error) { |
98 | err = os.MkdirAll(path, 0600) | ||
99 | return | ||
49 | } | 100 | } |
50 | 101 | ||
51 | func removeDir() { | 102 | func removeDir(path string) (err error) { |
103 | err = os.RemoveAll(path) | ||
104 | return | ||
52 | } | 105 | } |
53 | 106 | ||
54 | func listDir() { | 107 | func listDir(path string) (list []os.FileInfo, err error) { |
108 | list, err = ioutil.ReadDir(path) | ||
109 | return | ||
55 | } | 110 | } |
56 | 111 | ||
57 | func copyDir() { | 112 | func moveDir(source string, dest string) (err error) { |
113 | err = os.Rename(source, dest) | ||
114 | return | ||
115 | } | ||
116 | |||
117 | func copyDir(source string, dest string) (err error) { | ||
118 | // from https://gist.github.com/2876519 | ||
119 | fi, err := os.Stat(source) | ||
120 | if err != nil { | ||
121 | return | ||
122 | } | ||
123 | if !fi.IsDir() { | ||
124 | return os.ErrInvalid | ||
125 | } | ||
126 | _, err = os.Open(dest) | ||
127 | if !os.IsNotExist(err) { | ||
128 | return os.ErrExist | ||
129 | } | ||
130 | err = os.MkdirAll(dest, fi.Mode()) | ||
131 | if err != nil { | ||
132 | return | ||
133 | } | ||
134 | entries, err := ioutil.ReadDir(source) | ||
135 | for _, entry := range entries { | ||
136 | sfp := source + "/" + entry.Name() | ||
137 | dfp := dest + "/" + entry.Name() | ||
138 | if entry.IsDir() { | ||
139 | err = copyDir(sfp, dfp) | ||
140 | if err != nil { | ||
141 | return | ||
142 | } | ||
143 | } else { | ||
144 | err = copyFile(sfp, dfp) | ||
145 | if err != nil { | ||
146 | return | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | return | ||
58 | } | 151 | } |
59 | 152 | ||
60 | //////// REQUEST HANDLERS | 153 | //////// REQUEST HANDLERS |
@@ -63,7 +156,7 @@ func copyDir() { | |||
63 | 156 | ||
64 | func fileHandler(w http.ResponseWriter, r *http.Request) { | 157 | func fileHandler(w http.ResponseWriter, r *http.Request) { |
65 | p := r.URL.Path[filePathLen:] | 158 | p := r.URL.Path[filePathLen:] |
66 | path.Clean(p) | 159 | filepath.Clean(p) |
67 | 160 | ||
68 | switch r.Method { | 161 | switch r.Method { |
69 | case "POST": | 162 | case "POST": |
@@ -86,7 +179,7 @@ func fileHandler(w http.ResponseWriter, r *http.Request) { | |||
86 | 179 | ||
87 | func dirHandler(w http.ResponseWriter, r *http.Request) { | 180 | func dirHandler(w http.ResponseWriter, r *http.Request) { |
88 | p := r.URL.Path[dirPathLen:] | 181 | p := r.URL.Path[dirPathLen:] |
89 | path.Clean(p) | 182 | filepath.Clean(p) |
90 | 183 | ||
91 | switch r.Method { | 184 | switch r.Method { |
92 | case "POST": | 185 | case "POST": |
@@ -111,6 +204,17 @@ func getDataHandler(w http.ResponseWriter, r *http.Request) { | |||
111 | 204 | ||
112 | // Get the cloud status JSON | 205 | // Get the cloud status JSON |
113 | func getStatusHandler(w http.ResponseWriter, r *http.Request) { | 206 | func getStatusHandler(w http.ResponseWriter, r *http.Request) { |
207 | cloudStatus := map[string]string{ | ||
208 | "name": APP_NAME, | ||
209 | "version": APP_VERSION, | ||
210 | "server-root": rootFlag, | ||
211 | "status": "running", | ||
212 | } | ||
213 | j, err := json.Marshal(cloudStatus) | ||
214 | if err != nil { | ||
215 | log.Println(err) | ||
216 | } | ||
217 | w.Write(j) | ||
114 | } | 218 | } |
115 | 219 | ||
116 | //////// INIT and MAIN | 220 | //////// INIT and MAIN |