-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
189 lines (167 loc) · 4.09 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package httpfs
// client-side API
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"strings"
)
var wd = "" // working directory, see SetWD
// SetWD sets a "working directory" for the client side,
// prefixed to all relative local paths passed to client functions (Mkdir, Touch, Remove, ...).
// dir may start with "http://", turning local relative client paths into remote paths.
// E.g.:
// http://path -> http://path
// path/file -> wd/path/file
// /path/file -> /path/file
func SetWD(dir string) {
if dir != "" && !strings.HasSuffix(dir, "/") {
dir = dir + "/"
}
wd = dir
}
// Mkdir creates a directory at specified URL.
func Mkdir(URL string) error {
URL = addWorkDir(URL)
if isRemote(URL) {
return httpMkdir(URL)
} else {
return localMkdir(URL)
}
}
// Touch creates an empty file at the specified URL.
func Touch(URL string) error {
URL = addWorkDir(URL)
if isRemote(URL) {
return httpTouch(URL)
} else {
return localTouch(URL)
}
}
// ReadDir reads and returns all file names in the directory at URL.
func ReadDir(URL string) ([]string, error) {
URL = addWorkDir(URL)
if isRemote(URL) {
return httpLs(URL)
} else {
return localLs(URL)
}
}
// Remove removes the file or directory at URL, and all children it may contain.
// Similar to os.RemoveAll.
func Remove(URL string) error {
URL = addWorkDir(URL)
if isRemote(URL) {
return httpRemove(URL)
} else {
return localRemove(URL)
}
}
// Read the entire file and return its contents.
func Read(URL string) ([]byte, error) {
URL = addWorkDir(URL)
if isRemote(URL) {
return httpRead(URL)
} else {
return localRead(URL)
}
}
// Append p to the file given by URL,
// but first assure that the file had the expected size.
// Used to avoid accidental concurrent writes by two processes to the same file.
// Size < 0 disables size check.
func AppendSize(URL string, p []byte, size int64) error {
URL = addWorkDir(URL)
if isRemote(URL) {
return httpAppend(URL, p, size)
} else {
return localAppend(URL, p, size)
}
}
// Append p to the file given by URL.
func Append(URL string, p []byte) error {
return AppendSize(URL, p, -1)
}
// Create file given by URL and put data from p there.
func Put(URL string, p []byte) error {
URL = addWorkDir(URL)
if isRemote(URL) {
return httpPut(URL, p)
} else {
return localPut(URL, p)
}
}
func isRemote(URL string) bool {
return strings.HasPrefix(URL, "http://")
}
// prefix wd to URL if URL is a relative file path
// does not start with "/", "http://"
func addWorkDir(URL string) string {
if isRemote(URL) {
return URL
}
if !path.IsAbs(URL) {
return wd + URL
}
return URL
}
func httpMkdir(URL string) error {
_, err := do(MKDIR, URL, nil, nil)
return err
}
func httpTouch(URL string) error {
_, err := do(TOUCH, URL, nil, nil)
return err
}
func httpLs(URL string) (ls []string, err error) {
r, errHTTP := do(LS, URL, nil, nil)
if errHTTP != nil {
return nil, errHTTP
}
errJSON := json.Unmarshal(r, &ls)
if errJSON != nil {
return nil, mkErr(LS, URL, errJSON)
}
return ls, nil
}
func httpAppend(URL string, data []byte, size int64) error {
var query map[string][]string
if size >= 0 {
query = map[string][]string{"size": {fmt.Sprint(size)}}
}
_, err := do(APPEND, URL, data, query)
return err
}
func httpPut(URL string, data []byte) error {
_, err := do(PUT, URL, data, nil)
return err
}
func httpRead(URL string) ([]byte, error) {
return do(READ, URL, nil, nil)
}
func httpRemove(URL string) error {
_, err := do(RM, URL, nil, nil)
return err
}
// do a http request.
func do(a action, URL string, body []byte, query url.Values) (resp []byte, err error) {
u, err := url.Parse(URL)
u.Path = string(a) + path.Clean("/"+u.Path)
u.RawQuery = query.Encode()
response, errR := http.Post(u.String(), "data", bytes.NewReader(body))
if errR != nil {
return nil, mkErr(a, URL, errR)
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return nil, errors.New("do " + u.String() + ":" + response.Status + ":" + readBody(response.Body))
}
resp, err = ioutil.ReadAll(response.Body)
err = mkErr(a, URL, err)
return
}