diff options
author | Pacien TRAN-GIRARD | 2012-09-23 15:03:28 +0200 |
---|---|---|
committer | Pacien | 2015-12-07 22:48:27 +0100 |
commit | 2ba379a218dcef842b5b46b10d12130757a6dc3f (patch) | |
tree | 20571a25dc8d07e77cb89b4c6fcd68b92aa0db1e /src | |
parent | 22c4609e303301f991a2e95f01853a56beb564bb (diff) | |
download | ninja-go-local-cloud-2ba379a218dcef842b5b46b10d12130757a6dc3f.tar.gz |
Optimize code (using pointers).
Diffstat (limited to 'src')
-rw-r--r-- | src/ninjacloud.go | 232 |
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 | ||
35 | const APP_NAME = "Ninja Go Local Cloud" | 35 | const APP_NAME = "Ninja Go Local Cloud" |
36 | const APP_VERSION = "0.1 Draft" | 36 | const APP_VERSION = "0.1" |
37 | 37 | ||
38 | var versionFlag bool | 38 | var versionFlag bool |
39 | var interfaceFlag string | 39 | var interfaceFlag string |
@@ -50,13 +50,10 @@ const dirPath = "/directory/" | |||
50 | const webPath = "/web?url=" | 50 | const webPath = "/web?url=" |
51 | const statusPath = "/cloudstatus/" | 51 | const statusPath = "/cloudstatus/" |
52 | 52 | ||
53 | const drivePrefixLen = len(drivePrefix) - 1 | ||
54 | const filePathLen = len(filePath) | 53 | const filePathLen = len(filePath) |
55 | const dirPathLen = len(dirPath) | 54 | const dirPathLen = len(dirPath) |
56 | const webPathLen = len(webPath) | 55 | const webPathLen = len(webPath) |
57 | 56 | ||
58 | //const statusPathLen = len(statusPath) | ||
59 | |||
60 | func sliceContains(s []string, c string) bool { | 57 | func 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 | ||
71 | func properties(path string) (infos os.FileInfo, err error) { | 68 | func 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 | ||
76 | func modifiedSince(path string, since string) bool { | 73 | func 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 | ||
90 | func exist(path string) bool { | 87 | func 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 | ||
98 | func isInRoot(path string) bool { | 95 | func 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 | ||
104 | func writeFile(path string, content []byte, overwrite bool) (err error) { | 101 | func 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 | ||
120 | func readFile(path string) (content []byte, err error) { | 117 | func 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 | ||
125 | func removeFile(path string) (err error) { | 122 | func removeFile(path string) (err error) { |
126 | err = os.Remove(path) | 123 | err = os.Remove(*&path) |
127 | return | 124 | return |
128 | } | 125 | } |
129 | 126 | ||
130 | func moveFile(source string, dest string) (err error) { | 127 | func 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 | ||
135 | func copyFile(source string, dest string) (err error) { | 132 | func 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 | ||
160 | func createDir(path string) (err error) { | 157 | func 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 | ||
165 | func removeDir(path string) (err error) { | 162 | func 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 | ||
175 | func moveDir(source string, dest string) (err error) { | 172 | func 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 | ||
180 | func copyDir(source string, dest string) (err error) { | 177 | func 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 == "" |