summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2012-09-23 15:03:28 +0200
committerPacien2015-12-07 22:48:27 +0100
commit2ba379a218dcef842b5b46b10d12130757a6dc3f (patch)
tree20571a25dc8d07e77cb89b4c6fcd68b92aa0db1e /src
parent22c4609e303301f991a2e95f01853a56beb564bb (diff)
downloadninja-go-local-cloud-2ba379a218dcef842b5b46b10d12130757a6dc3f.tar.gz
Optimize code (using pointers).
Diffstat (limited to 'src')
-rw-r--r--src/ninjacloud.go232
1 files changed, 110 insertions, 122 deletions
diff --git a/src/ninjacloud.go b/src/ninjacloud.go
index a600ee7..f2d31d7 100644
--- a/src/ninjacloud.go
+++ b/src/ninjacloud.go
@@ -33,7 +33,7 @@ import (
33) 33)
34 34
35const APP_NAME = "Ninja Go Local Cloud" 35const APP_NAME = "Ninja Go Local Cloud"
36const APP_VERSION = "0.1 Draft" 36const APP_VERSION = "0.1"
37 37
38var versionFlag bool 38var versionFlag bool
39var interfaceFlag string 39var interfaceFlag string
@@ -50,13 +50,10 @@ const dirPath = "/directory/"
50const webPath = "/web?url=" 50const webPath = "/web?url="
51const statusPath = "/cloudstatus/" 51const statusPath = "/cloudstatus/"
52 52
53const drivePrefixLen = len(drivePrefix) - 1
54const filePathLen = len(filePath) 53const filePathLen = len(filePath)
55const dirPathLen = len(dirPath) 54const dirPathLen = len(dirPath)
56const webPathLen = len(webPath) 55const webPathLen = len(webPath)
57 56
58//const statusPathLen = len(statusPath)
59
60func sliceContains(s []string, c string) bool { 57func sliceContains(s []string, c string) bool {
61 for _, e := range s { 58 for _, e := range s {
62 if c == e { 59 if c == e {
@@ -69,13 +66,13 @@ func sliceContains(s []string, c string) bool {
69//////// FILESYSTEM 66//////// FILESYSTEM
70 67
71func properties(path string) (infos os.FileInfo, err error) { 68func properties(path string) (infos os.FileInfo, err error) {
72 infos, err = os.Stat(path) 69 infos, err = os.Stat(*&path)
73 return 70 return
74} 71}
75 72
76func modifiedSince(path string, since string) bool { 73func modifiedSince(path string, since string) bool {
77 s, err := strconv.ParseInt(since, 10, 64) 74 s, err := strconv.ParseInt(*&since, 10, 64)
78 infos, err := properties(path) 75 infos, err := properties(*&path)
79 if err != nil { 76 if err != nil {
80 return false 77 return false
81 } 78 }
@@ -88,67 +85,67 @@ func modifiedSince(path string, since string) bool {
88} 85}
89 86
90func exist(path string) bool { 87func exist(path string) bool {
91 _, err := os.Stat(path) 88 _, err := os.Stat(*&path)
92 if !os.IsNotExist(err) { 89 if !os.IsNotExist(*&err) {
93 return true 90 return true
94 } 91 }
95 return false 92 return false
96} 93}
97 94
98func isInRoot(path string) bool { 95func isInRoot(path string) bool {
99 return filepath.HasPrefix(path, rootFlag) 96 return filepath.HasPrefix(*&path, *&rootFlag)
100} 97}
101 98
102//// Files 99//// Files
103 100
104func writeFile(path string, content []byte, overwrite bool) (err error) { 101func writeFile(path string, content []byte, overwrite bool) (err error) {
105 if !overwrite { 102 if !overwrite {
106 if exist(path) { 103 if exist(*&path) {
107 err = os.ErrExist 104 err = os.ErrExist
108 return 105 return
109 } 106 }
110 } else { 107 } else {
111 if !exist(path) { 108 if !exist(*&path) {
112 err = os.ErrNotExist 109 err = os.ErrNotExist
113 return 110 return
114 } 111 }
115 } 112 }
116 err = ioutil.WriteFile(path, content, 0777) 113 err = ioutil.WriteFile(*&path, *&content, 0777)
117 return 114 return
118} 115}
119 116
120func readFile(path string) (content []byte, err error) { 117func readFile(path string) (content []byte, err error) {
121 content, err = ioutil.ReadFile(path) 118 content, err = ioutil.ReadFile(*&path)
122 return 119 return
123} 120}
124 121
125func removeFile(path string) (err error) { 122func removeFile(path string) (err error) {
126 err = os.Remove(path) 123 err = os.Remove(*&path)
127 return 124 return
128} 125}
129 126
130func moveFile(source string, dest string) (err error) { 127func moveFile(source string, dest string) (err error) {
131 err = os.Rename(source, dest) 128 err = os.Rename(*&source, *&dest)
132 return 129 return
133} 130}
134 131
135func copyFile(source string, dest string) (err error) { 132func copyFile(source string, dest string) (err error) {
136 // from https://gist.github.com/2876519 133 // from https://gist.github.com/2876519
137 sf, err := os.Open(source) 134 sf, err := os.Open(*&source)
138 if err != nil { 135 if err != nil {
139 return err 136 return err
140 } 137 }
141 defer sf.Close() 138 defer sf.Close()
142 df, err := os.Create(dest) 139 df, err := os.Create(*&dest)
143 if err != nil { 140 if err != nil {
144 return err 141 return err
145 } 142 }
146 defer df.Close() 143 defer df.Close()
147 _, err = io.Copy(df, sf) 144 _, err = io.Copy(*&df, *&sf)
148 if err == nil { 145 if err == nil {
149 si, err := os.Stat(source) 146 si, err := os.Stat(*&source)
150 if err != nil { 147 if err != nil {
151 err = os.Chmod(dest, si.Mode()) 148 err = os.Chmod(*&dest, si.Mode())
152 } 149 }
153 150
154 } 151 }
@@ -158,53 +155,53 @@ func copyFile(source string, dest string) (err error) {
158//// Dirs 155//// Dirs
159 156
160func createDir(path string) (err error) { 157func createDir(path string) (err error) {
161 err = os.MkdirAll(path, 0777) 158 err = os.MkdirAll(*&path, 0777)
162 return 159 return
163} 160}
164 161
165func removeDir(path string) (err error) { 162func removeDir(path string) (err error) {
166 err = os.RemoveAll(path) 163 err = os.RemoveAll(*&path)
167 return 164 return
168} 165}
169 166
170/*func listDir(path string) (list []os.FileInfo, err error) { 167/*func listDir(path string) (list []os.FileInfo, err error) {
171 list, err = ioutil.ReadDir(path) 168 list, err = ioutil.ReadDir(*&path)
172 return 169 return
173}*/ 170}*/
174 171
175func moveDir(source string, dest string) (err error) { 172func moveDir(source string, dest string) (err error) {
176 err = os.Rename(source, dest) 173 err = os.Rename(*&source, *&dest)
177 return 174 return
178} 175}
179 176
180func copyDir(source string, dest string) (err error) { 177func copyDir(source string, dest string) (err error) {
181 // from https://gist.github.com/2876519 178 // from https://gist.github.com/2876519
182 fi, err := os.Stat(source) 179 fi, err := os.Stat(*&source)
183 if err != nil { 180 if err != nil {
184 return 181 return
185 } 182 }
186 if !fi.IsDir() { 183 if !fi.IsDir() {
187 return os.ErrInvalid 184 return os.ErrInvalid
188 } 185 }
189 _, err = os.Open(dest) 186 _, err = os.Open(*&dest)
190 if !os.IsNotExist(err) { 187 if !os.IsNotExist(*&err) {
191 return os.ErrExist 188 return os.ErrExist
192 } 189 }
193 err = os.MkdirAll(dest, fi.Mode()) 190 err = os.MkdirAll(*&dest, fi.Mode())
194 if err != nil { 191 if err != nil {
195 return 192 return
196 } 193 }
197 entries, err := ioutil.ReadDir(source) 194 entries, err := ioutil.ReadDir(*&source)
198 for _, entry := range entries { 195 for _, entry := range entries {
199 sfp := source + "/" + entry.Name() 196 sfp := source + "/" + entry.Name()
200 dfp := dest + "/" + entry.Name() 197 dfp := dest + "/" + entry.Name()
201 if entry.IsDir() { 198 if entry.IsDir() {
202 err = copyDir(sfp, dfp) 199 err = copyDir(*&sfp, *&dfp)
203 if err != nil { 200 if err != nil {
204 return 201 return
205 } 202 }
206 } else { 203 } else {
207 err = copyFile(sfp, dfp) 204 err = copyFile(*&sfp, *&dfp)
208 if err != nil { 205 if err != nil {
209 return 206 return
210 } 207 }
@@ -228,15 +225,15 @@ func listDir(path string, recursive bool, filter []string, returnType string) (l
228 returnAll := returnType == "all" || returnType == "" 225 returnAll := returnType == "all" || returnType == ""
229 returnFiles := returnType == "files" || returnAll 226 returnFiles := returnType == "files" || returnAll
230 returnDirs := returnType == "directories" || returnAll 227 returnDirs := returnType == "directories" || returnAll
231 currentDir, err := ioutil.ReadDir(path) 228 currentDir, err := ioutil.ReadDir(*&path)
232 for _, d := range currentDir { 229 for _, d := range currentDir {
233 if d.IsDir() && returnDirs { 230 if d.IsDir() && returnDirs {
234 var e element 231 var e element
235 modTime := strconv.FormatInt(d.ModTime().UnixNano(), 10) 232 modTime := strconv.FormatInt(d.ModTime().UnixNano(), 10)
236 modTime = modTime[:len(modTime)-6] 233 modTime = modTime[:len(*&modTime)-6]
237 uri := path + "/" + d.Name() 234 uri := drivePrefix + projectsDir + "/" + path + "/" + d.Name()
238 uri = filepath.Clean(drivePrefix + projectsDir + "/" + uri) 235 uri = filepath.Clean(*&uri)
239 uri = filepath.ToSlash(uri) 236 uri = filepath.ToSlash(*&uri)
240 e.Type = "directory" 237 e.Type = "directory"
241 e.Name = d.Name() 238 e.Name = d.Name()
242 e.Uri = uri 239 e.Uri = uri
@@ -245,25 +242,26 @@ func listDir(path string, recursive bool, filter []string, returnType string) (l
245 e.Size = strconv.FormatInt(d.Size(), 10) 242 e.Size = strconv.FormatInt(d.Size(), 10)
246 e.Writable = "true" // TODO 243 e.Writable = "true" // TODO
247 if recursive { 244 if recursive {
248 e.Children, err = listDir(uri, recursive, filter, returnType) 245 e.Children, err = listDir(*&uri, *&recursive, *&filter, *&returnType)
249 if err != nil { 246 if err != nil {
250 return 247 return
251 } 248 }
252 } else { 249 } else {
253 e.Children = nil 250 e.Children = nil
254 } 251 }
255 list = append(list, e) 252 list = append(*&list, *&e)
256 } else if !d.IsDir() && returnFiles { 253 } else if !d.IsDir() && returnFiles {
257 ext := filepath.Ext(d.Name()) 254 ext := filepath.Ext(d.Name())
258 if ext != "" { 255 if ext != "" {
259 ext = ext[1:] 256 ext = ext[1:]
260 } 257 }
261 if cap(filter) == 1 || sliceContains(filter, ext) { 258 if cap(*&filter) == 1 || sliceContains(*&filter, *&ext) {
262 var e element 259 var e element
263 modTime := strconv.FormatInt(d.ModTime().UnixNano(), 10) 260 modTime := strconv.FormatInt(d.ModTime().UnixNano(), 10)
264 modTime = modTime[:len(modTime)-6] 261 modTime = modTime[:len(*&modTime)-6]
265 uri := filepath.Clean(drivePrefix + projectsDir + "/" + path + "/" + d.Name()) 262 uri := drivePrefix + projectsDir + "/" + path + "/" + d.Name()
266 uri = filepath.ToSlash(uri) 263 uri = filepath.Clean(*&uri)
264 uri = filepath.ToSlash(*&uri)
267 e.Type = "file" 265 e.Type = "file"
268 e.Name = d.Name() 266 e.Name = d.Name()
269 e.Uri = uri 267 e.Uri = uri
@@ -272,7 +270,7 @@ func listDir(path string, recursive bool, filter []string, returnType string) (l
272 e.Size = strconv.FormatInt(d.Size(), 10) 270 e.Size = strconv.FormatInt(d.Size(), 10)
273 e.Writable = "true" // TODO 271 e.Writable = "true" // TODO
274 e.Children = nil 272 e.Children = nil
275 list = append(list, e) 273 list = append(*&list, *&e)
276 } 274 }
277 } 275 }
278 } 276 }
@@ -290,9 +288,9 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
290 w.Header().Add("Access-Control-Allow-Origin", "*/*") 288 w.Header().Add("Access-Control-Allow-Origin", "*/*")
291 w.Header().Add("Access-Control-Max-Age", "86400") 289 w.Header().Add("Access-Control-Max-Age", "86400")
292 p := filepath.Clean(r.URL.Path[filePathLen:]) 290 p := filepath.Clean(r.URL.Path[filePathLen:])
293 p = filepath.ToSlash(p) 291 p = filepath.ToSlash(*&p)
294 p = strings.TrimLeft(p, driveName+"/"+projectsDir+"/") 292 p = strings.TrimLeft(*&p, driveName+"/"+projectsDir+"/")
295 if filepath.IsAbs(p) { 293 if filepath.IsAbs(*&p) {
296 w.WriteHeader(http.StatusForbidden) 294 w.WriteHeader(http.StatusForbidden)
297 return 295 return
298 } 296 }
@@ -300,19 +298,19 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
300 switch r.Method { 298 switch r.Method {
301 case "POST": 299 case "POST":
302 // Create a new file 300 // Create a new file
303 content, err := ioutil.ReadAll(r.Body) 301 content, err := ioutil.ReadAll(*&r.Body)
304 if err != nil { 302 if err != nil {
305 log.Println(err) 303 log.Println(*&err)
306 w.WriteHeader(http.StatusInternalServerError) 304 w.WriteHeader(http.StatusInternalServerError)
307 return 305 return
308 } 306 }
309 err = writeFile(p, *&content, false) 307 err = writeFile(*&p, *&content, false)
310 if err == os.ErrExist { 308 if err == os.ErrExist {
311 log.Println(err) 309 log.Println(*&err)
312 w.WriteHeader(http.StatusBadRequest) 310 w.WriteHeader(http.StatusBadRequest)
313 return 311 return
314 } else if err != nil { 312 } else if err != nil {
315 log.Println(err) 313 log.Println(*&err)
316 w.WriteHeader(http.StatusInternalServerError) 314 w.WriteHeader(http.StatusInternalServerError)
317 return 315 return
318 } 316 }
@@ -322,19 +320,19 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
322 source := r.Header.Get("sourceURI") 320 source := r.Header.Get("sourceURI")
323 if source == "" { 321 if source == "" {
324 // Update an existing file (save over existing file) 322 // Update an existing file (save over existing file)
325 content, err := ioutil.ReadAll(r.Body) 323 content, err := ioutil.ReadAll(*&r.Body)
326 if err != nil { 324 if err != nil {
327 log.Println(err) 325 log.Println(*&err)
328 w.WriteHeader(http.StatusInternalServerError) 326 w.WriteHeader(http.StatusInternalServerError)
329 return 327 return
330 } 328 }
331 err = writeFile(p, *&content, true) 329 err = writeFile(*&p, *&content, true)
332 if err == os.ErrNotExist { 330 if err == os.ErrNotExist {
333 log.Println(err) 331 log.Println(*&err)
334 w.WriteHeader(http.StatusNotFound) 332 w.WriteHeader(http.StatusNotFound)
335 return 333 return
336 } else if err != nil { 334 } else if err != nil {
337 log.Println(err) 335 log.Println(*&err)
338 w.WriteHeader(http.StatusInternalServerError) 336 w.WriteHeader(http.StatusInternalServerError)
339 return 337 return
340 } 338 }
@@ -343,40 +341,30 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
343 } else { 341 } else {
344 // Copy, Move of an existing file 342 // Copy, Move of an existing file
345 if r.Header.Get("overwrite-destination") != "true" { 343 if r.Header.Get("overwrite-destination") != "true" {
346 if exist(p) { 344 if exist(*&p) {
347 w.WriteHeader(http.StatusInternalServerError) 345 w.WriteHeader(http.StatusInternalServerError)
348 return 346 return
349 } 347 }
350 /*err := removeFile(p)
351 if err == os.ErrNotExist {
352 log.Println(err)
353 w.WriteHeader(http.StatusNotFound)
354 return
355 } else if err != nil {
356 log.Println(err)
357 w.WriteHeader(http.StatusInternalServerError)
358 return
359 }*/
360 } 348 }
361 if r.Header.Get("delete-source") == "true" { 349 if r.Header.Get("delete-source") == "true" {
362 err := moveFile(source, p) 350 err := moveFile(*&source, *&p)
363 if err == os.ErrNotExist { 351 if err == os.ErrNotExist {
364 log.Println(err) 352 log.Println(*&err)
365 w.WriteHeader(http.StatusNotFound) 353 w.WriteHeader(http.StatusNotFound)
366 return 354 return
367 } else if err != nil { 355 } else if err != nil {
368 log.Println(err) 356 log.Println(*&err)
369 w.WriteHeader(http.StatusInternalServerError) 357 w.WriteHeader(http.StatusInternalServerError)
370 return 358 return
371 } 359 }
372 } else { 360 } else {
373 err := copyFile(source, p) 361 err := copyFile(*&source, *&p)
374 if err == os.ErrNotExist { 362 if err == os.ErrNotExist {
375 log.Println(err) 363 log.Println(*&err)
376 w.WriteHeader(http.StatusNotFound) 364 w.WriteHeader(http.StatusNotFound)
377 return 365 return
378 } else if err != nil { 366 } else if err != nil {
379 log.Println(err) 367 log.Println(*&err)
380 w.WriteHeader(http.StatusInternalServerError) 368 w.WriteHeader(http.StatusInternalServerError)
381 return 369 return
382 } 370 }
@@ -386,17 +374,17 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
386 } 374 }
387 case "DELETE": 375 case "DELETE":
388 // Delete an existing file 376 // Delete an existing file
389 if !exist(p) { 377 if !exist(*&p) {
390 w.WriteHeader(http.StatusNotFound) 378 w.WriteHeader(http.StatusNotFound)
391 return 379 return
392 } 380 }
393 err := removeFile(p) 381 err := removeFile(*&p)
394 if err == os.ErrNotExist { 382 if err == os.ErrNotExist {
395 log.Println(err) 383 log.Println(*&err)
396 w.WriteHeader(http.StatusNotFound) 384 w.WriteHeader(http.StatusNotFound)
397 return 385 return
398 } else if err != nil { 386 } else if err != nil {
399 log.Println(err) 387 log.Println(*&err)
400 w.WriteHeader(http.StatusInternalServerError) 388 w.WriteHeader(http.StatusInternalServerError)
401 return 389 return
402 } 390 }
@@ -407,7 +395,7 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
407 modSince := r.Header.Get("If-modified-since") 395 modSince := r.Header.Get("If-modified-since")
408 getInfo := r.Header.Get("get-file-info") 396 getInfo := r.Header.Get("get-file-info")
409 if modSince != "" && modSince != "false" && modSince != "none" { 397 if modSince != "" && modSince != "false" && modSince != "none" {
410 if modifiedSince(p, modSince) { 398 if modifiedSince(*&p, *&modSince) {
411 w.WriteHeader(http.StatusOK) 399 w.WriteHeader(http.StatusOK)
412 return 400 return
413 } else { 401 } else {
@@ -423,14 +411,14 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
423 return 411 return
424 } 412 }
425 } else if getInfo != "" && getInfo != "false" { 413 } else if getInfo != "" && getInfo != "false" {
426 infos, err := properties(p) 414 infos, err := properties(*&p)
427 if err != nil { 415 if err != nil {
428 log.Println(err) 416 log.Println(*&err)
429 w.WriteHeader(http.StatusInternalServerError) 417 w.WriteHeader(http.StatusInternalServerError)
430 return 418 return
431 } 419 }
432 modDate := strconv.FormatInt(infos.ModTime().UnixNano(), 10) 420 modDate := strconv.FormatInt(infos.ModTime().UnixNano(), 10)
433 modDate = modDate[:len(modDate)-6] 421 modDate = modDate[:len(*&modDate)-6]
434 size := strconv.FormatInt(infos.Size(), 10) 422 size := strconv.FormatInt(infos.Size(), 10)
435 fileInfo := map[string]string{ 423 fileInfo := map[string]string{
436 "creationDate": modDate, // TODO 424 "creationDate": modDate, // TODO
@@ -438,16 +426,16 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
438 "size": size, 426 "size": size,
439 "readOnly": "false", // TODO 427 "readOnly": "false", // TODO
440 } 428 }
441 j, err := json.MarshalIndent(fileInfo, "", " ") 429 j, err := json.MarshalIndent(*&fileInfo, "", " ")
442 if err != nil { 430 if err != nil {
443 log.Println(err) 431 log.Println(*&err)
444 w.WriteHeader(http.StatusInternalServerError) 432 w.WriteHeader(http.StatusInternalServerError)
445 return 433 return
446 } 434 }
447 w.Write(j) 435 w.Write(j)
448 return 436 return
449 } else { 437 } else {
450 ext := filepath.Ext(p) 438 ext := filepath.Ext(*&p)
451 switch ext { 439 switch ext {
452 case ".htm": 440 case ".htm":
453 case ".html": 441 case ".html":
@@ -456,14 +444,14 @@ func fileHandler(w http.ResponseWriter, r *http.Request) {
456 case ".json": 444 case ".json":
457 w.Header().Set("Content-Type", "application/json") 445 w.Header().Set("Content-Type", "application/json")
458 } 446 }
459 file, err := readFile(p) 447 file, err := readFile(*&p)
460 if err != nil { 448 if err != nil {
461 log.Println(err) 449 log.Println(*&err)
462 w.WriteHeader(http.StatusInternalServerError) 450 w.WriteHeader(http.StatusInternalServerError)
463 return 451 return
464 } 452 }
465 w.WriteHeader(http.StatusOK) 453 w.WriteHeader(http.StatusOK)
466 w.Write(file) 454 w.Write(*&file)
467 return 455 return
468 } 456 }
469 } 457 }
@@ -478,9 +466,9 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
478 w.Header().Add("Access-Control-Allow-Origin", "*/*") 466 w.Header().Add("Access-Control-Allow-Origin", "*/*")
479 w.Header().Add("Access-Control-Max-Age", "86400") 467 w.Header().Add("Access-Control-Max-Age", "86400")
480 p := filepath.Clean(r.URL.Path[dirPathLen:]) 468 p := filepath.Clean(r.URL.Path[dirPathLen:])
481 p = filepath.ToSlash(p) 469 p = filepath.ToSlash(*&p)
482 p = strings.TrimLeft(p, driveName+"/"+projectsDir+"/") 470 p = strings.TrimLeft(*&p, driveName+"/"+projectsDir+"/")
483 if filepath.IsAbs(p) { 471 if filepath.IsAbs(*&p) {
484 w.WriteHeader(http.StatusForbidden) 472 w.WriteHeader(http.StatusForbidden)
485 return 473 return
486 } 474 }
@@ -488,9 +476,9 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
488 switch r.Method { 476 switch r.Method {
489 case "POST": 477 case "POST":
490 // Create a new directory 478 // Create a new directory
491 err := createDir(p) 479 err := createDir(*&p)
492 if err != nil { 480 if err != nil {
493 log.Println(err) 481 log.Println(*&err)
494 w.WriteHeader(http.StatusBadRequest) 482 w.WriteHeader(http.StatusBadRequest)
495 return 483 return
496 } 484 }
@@ -498,17 +486,17 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
498 return 486 return
499 case "DELETE": 487 case "DELETE":
500 // Delete an existing directory 488 // Delete an existing directory
501 if !exist(p) { 489 if !exist(*&p) {
502 w.WriteHeader(http.StatusNotFound) 490 w.WriteHeader(http.StatusNotFound)
503 return 491 return
504 } 492 }
505 err := removeDir(p) 493 err := removeDir(*&p)
506 if err == os.ErrNotExist { 494 if err == os.ErrNotExist {
507 log.Println(err) 495 log.Println(*&err)
508 w.WriteHeader(http.StatusNotFound) 496 w.WriteHeader(http.StatusNotFound)
509 return 497 return
510 } else if err != nil { 498 } else if err != nil {
511 log.Println(err) 499 log.Println(*&err)
512 w.WriteHeader(http.StatusInternalServerError) 500 w.WriteHeader(http.StatusInternalServerError)
513 return 501 return
514 } 502 }
@@ -518,11 +506,11 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
518 // List the contents of an existing directory 506 // List the contents of an existing directory
519 modSince := r.Header.Get("If-modified-since") 507 modSince := r.Header.Get("If-modified-since")
520 if modSince != "" && modSince != "false" && modSince != "none" { 508 if modSince != "" && modSince != "false" && modSince != "none" {
521 if !exist(p) { 509 if !exist(*&p) {
522 w.WriteHeader(http.StatusNotFound) 510 w.WriteHeader(http.StatusNotFound)
523 return 511 return
524 } 512 }
525 if modifiedSince(p, modSince) { 513 if modifiedSince(*&p, *&modSince) {
526 w.WriteHeader(http.StatusOK) 514 w.WriteHeader(http.StatusOK)
527 return 515 return
528 } else { 516 } else {
@@ -530,7 +518,7 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
530 return 518 return
531 } 519 }
532 } else if r.Header.Get("check-existence-only") == "true" { 520 } else if r.Header.Get("check-existence-only") == "true" {
533 if exist(p) { 521 if exist(*&p) {
534 w.WriteHeader(http.StatusNoContent) 522 w.WriteHeader(http.StatusNoContent)
535 return 523 return
536 } else { 524 } else {
@@ -551,31 +539,31 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
551 n.Type = "directory" 539 n.Type = "directory"
552 n.Name = projectsDir 540 n.Name = projectsDir
553 n.Uri = drivePrefix + projectsDir 541 n.Uri = drivePrefix + projectsDir
554 c = append(c, n) 542 c = append(*&c, *&n)
555 e.Type = "directory" 543 e.Type = "directory"
556 e.Children = c 544 e.Children = c
557 } else { 545 } else {
558 if p == "" { 546 if p == "" {
559 p = "." 547 p = "."
560 } 548 }
561 fileInfo, err := listDir(p, recursive, filter, returnType) 549 fileInfo, err := listDir(*&p, *&recursive, *&filter, *&returnType)
562 if err == os.ErrNotExist { 550 if err == os.ErrNotExist {
563 log.Println(err) 551 log.Println(*&err)
564 w.WriteHeader(http.StatusNotFound) 552 w.WriteHeader(http.StatusNotFound)
565 return 553 return
566 } else if err != nil { 554 } else if err != nil {
567 log.Println(err) 555 log.Println(*&err)
568 w.WriteHeader(http.StatusInternalServerError) 556 w.WriteHeader(http.StatusInternalServerError)
569 return 557 return
570 } 558 }
571 rootDir, err := properties(p) 559 rootDir, err := properties(*&p)
572 if err != nil { 560 if err != nil {
573 log.Println(err) 561 log.Println(*&err)
574 w.WriteHeader(http.StatusInternalServerError) 562 w.WriteHeader(http.StatusInternalServerError)
575 return 563 return
576 } 564 }
577 modTime := strconv.FormatInt(rootDir.ModTime().UnixNano(), 10) 565 modTime := strconv.FormatInt(rootDir.ModTime().UnixNano(), 10)
578 modTime = modTime[:len(modTime)-6] 566 modTime = modTime[:len(*&modTime)-6]
579 e.Type = "directory" 567 e.Type = "directory"
580 e.Name = rootDir.Name() 568 e.Name = rootDir.Name()
581 e.Uri = drivePrefix + p 569 e.Uri = drivePrefix + p
@@ -586,9 +574,9 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
586 e.Children = fileInfo 574 e.Children = fileInfo
587 } 575 }
588 576
589 j, err := json.MarshalIndent(e, "", " ") 577 j, err := json.MarshalIndent(*&e, "", " ")
590 if err != nil { 578 if err != nil {
591 log.Println(err) 579 log.Println(*&err)
592 w.WriteHeader(http.StatusInternalServerError) 580 w.WriteHeader(http.StatusInternalServerError)
593 return 581 return
594 } 582 }
@@ -604,24 +592,24 @@ func dirHandler(w http.ResponseWriter, r *http.Request) {
604 } 592 }
605 operation := r.Header.Get("operation") 593 operation := r.Header.Get("operation")
606 if operation == "move" { 594 if operation == "move" {
607 err := moveDir(source, p) 595 err := moveDir(*&source, *&p)
608 if err == os.ErrNotExist { 596 if err == os.ErrNotExist {
609 log.Println(err) 597 log.Println(*&err)
610 w.WriteHeader(http.StatusNotFound) 598 w.WriteHeader(http.StatusNotFound)
611 return 599 return
612 } else if err != nil { 600 } else if err != nil {
613 log.Println(err) 601 log.Println(*&err)
614 w.WriteHeader(http.StatusInternalServerError) 602 w.WriteHeader(http.StatusInternalServerError)
615 return 603 return
616 } 604 }
617 } else if operation == "copy" { 605 } else if operation == "copy" {
618 err := copyDir(source, p) 606 err := copyDir(*&source, *&p)
619 if err == os.ErrNotExist { 607 if err == os.ErrNotExist {
620 log.Println(err) 608 log.Println(*&err)
621 w.WriteHeader(http.StatusNotFound) 609 w.WriteHeader(http.StatusNotFound)
622 return 610 return
623 } else if err != nil { 611 } else if err != nil {
624 log.Println(err) 612 log.Println(*&err)
625 w.WriteHeader(http.StatusInternalServerError) 613 w.WriteHeader(http.StatusInternalServerError)
626 return 614 return
627 } 615 }
@@ -650,9 +638,9 @@ func getStatusHandler(w http.ResponseWriter, r *http.Request) {
650 "server-root": drivePrefix + projectsDir, 638 "server-root": drivePrefix + projectsDir,
651 "status": "running", 639 "status": "running",
652 } 640 }
653 j, err := json.MarshalIndent(cloudStatus, "", " ") 641 j, err := json.MarshalIndent(*&cloudStatus, "", " ")
654 if err != nil { 642 if err != nil {
655 log.Println(err) 643 log.Println(*&err)
656 } 644 }
657 w.Write(j) 645 w.Write(j)
658} 646}
@@ -676,16 +664,16 @@ func main() {
676 664
677 root := filepath.Clean((rootFlag + "/" + projectsDir)) 665 root := filepath.Clean((rootFlag + "/" + projectsDir))
678 666
679 err := createDir(root) 667 err := createDir(*&root)
680 if err != nil { 668 if err != nil {
681 log.Println(err) 669 log.Println(*&err)
682 return 670 return
683 } 671 }
684 672
685 err = os.Chdir(root) 673 err = os.Chdir(*&root)
686 currentDir, err := os.Getwd() 674 currentDir, err := os.Getwd()
687 if err != nil { 675 if err != nil {
688 log.Println(err) 676 log.Println(*&err)
689 return 677 return
690 } 678 }
691 679
@@ -700,7 +688,7 @@ func main() {
700 688
701 err = http.ListenAndServe(interfaceFlag+":"+portFlag, nil) 689 err = http.ListenAndServe(interfaceFlag+":"+portFlag, nil)
702 if err != nil { 690 if err != nil {
703 log.Println(err) 691 log.Println(*&err)
704 return 692 return
705 } 693 }
706} 694}