forked from vladimirvivien/gowfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsadmin.go
297 lines (248 loc) · 8.17 KB
/
fsadmin.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package webhdfs
import "fmt"
import "os"
import "net/http"
import "strconv"
// Renames the specified path resource to a new name.
// See HDFS FileSystem.rename()
func (fs *FileSystem) Rename(source Path, destination Path) (bool, error) {
if source.Name == "" || destination.Name == "" {
return false, fmt.Errorf("Rename() - params source and destination cannot be empty.")
}
params := map[string]string{"op": OP_RENAME, "destination": destination.Name}
u, err := buildRequestUrl(fs.Config, &source, ¶ms)
if err != nil {
return false, err
}
req, _ := http.NewRequest("PUT", u.String(), nil)
hdfsData, err := requestHdfsData(fs.client, *req)
if err != nil {
return false, err
}
return hdfsData.Boolean, nil
}
//Deletes the specified path.
//See HDFS FileSystem.delete()
func (fs *FileSystem) Delete(path Path, recursive bool) (bool, error) {
if path.Name == "" {
return false, fmt.Errorf("Delete() - param path cannot be empty.")
}
params := map[string]string{
"op": OP_DELETE,
"recursive": strconv.FormatBool(recursive)}
u, err := buildRequestUrl(fs.Config, &path, ¶ms)
if err != nil {
return false, err
}
req, _ := http.NewRequest("DELETE", u.String(), nil)
hdfsData, err := requestHdfsData(fs.client, *req)
if err != nil {
return false, err
}
return hdfsData.Boolean, nil
}
// Sets the permission for the specified path.
// See FileSystem.setPermission()
func (fs *FileSystem) SetPermission(path Path, permission os.FileMode) (bool, error) {
if path.Name == "" {
return false, fmt.Errorf("SetPermission() - param path cannot be empty.")
}
if permission < 0 || permission > 1777 {
return false, fmt.Errorf("SetPermission() - permission is invalid.")
}
params := map[string]string{
"op": OP_SETPERMISSION,
"permission": strconv.FormatInt(int64(permission), 8)}
u, err := buildRequestUrl(fs.Config, &path, ¶ms)
if err != nil {
return false, err
}
req, _ := http.NewRequest("PUT", u.String(), nil)
rsp, err := fs.client.Do(req)
if err != nil {
return false, err
}
if rsp.StatusCode != http.StatusOK {
return false, fmt.Errorf("SetPermission() - server returned unexpected value, permission not set.")
}
return true, nil
}
//Sets owner for the specified path.
//See HDFS FileSystem.setOwner()
func (fs *FileSystem) SetOwner(path Path, owner string, group string) (bool, error) {
if path.Name == "" {
return false, fmt.Errorf("SetOwner() - param path cannot be empty.")
}
params := map[string]string{
"op": OP_SETOWNER,
"owner": owner,
"group": group}
u, err := buildRequestUrl(fs.Config, &path, ¶ms)
if err != nil {
return false, err
}
req, _ := http.NewRequest("PUT", u.String(), nil)
rsp, err := fs.client.Do(req)
if err != nil {
return false, err
}
if rsp.StatusCode != http.StatusOK {
return false, fmt.Errorf("SetOwner() - server returned unexpected value, owner not set.")
}
return true, nil
}
// Sets replication factor for given path.
// See HDFS FileSystem.setReplication()
func (fs *FileSystem) SetReplication(path Path, replication uint16) (bool, error) {
if path.Name == "" {
return false, fmt.Errorf("SetReplication() - param path cannot be empty.")
}
if replication <= 0 {
return false, fmt.Errorf("SetReplication() - replication is invalid.")
}
params := map[string]string{
"op": OP_SETREPLICATION,
"replication": strconv.FormatInt(int64(replication), 8)}
u, err := buildRequestUrl(fs.Config, &path, ¶ms)
if err != nil {
return false, err
}
req, _ := http.NewRequest("PUT", u.String(), nil)
hdfsData, err := requestHdfsData(fs.client, *req)
if err != nil {
return false, err
}
return hdfsData.Boolean, nil
}
// Sets access or modification time for specified resource
// See HDFS FileSystem.setTimes
func (fs *FileSystem) SetTimes(path Path, accesstime int64, modificationtime int64) (bool, error) {
if path.Name == "" {
return false, fmt.Errorf("SetTimes() - Path cannot be empty.")
}
params := map[string]string{
"op": OP_SETTIMES,
"accesstime": strconv.FormatInt(int64(accesstime), 10),
"modificationtime": strconv.FormatInt(int64(modificationtime), 10)}
u, err := buildRequestUrl(fs.Config, &path, ¶ms)
if err != nil {
return false, err
}
req, _ := http.NewRequest("PUT", u.String(), nil)
rsp, err := fs.client.Do(req)
if err != nil {
return false, err
}
if rsp.StatusCode != http.StatusOK {
return false, fmt.Errorf("SetTimes() - server returned unexpected value, resource times not modified.")
}
return true, nil
}
// Creates the specified directory(ies).
// See HDFS FileSystem.mkdirs()
func (fs *FileSystem) MkDirs(p Path, fm os.FileMode) (bool, error) {
params := map[string]string{"op": OP_MKDIRS}
if fm < 0 || fm > 1777 {
params["permission"] = "0700"
} else {
params["permission"] = strconv.FormatInt(int64(fm), 8)
}
u, err := buildRequestUrl(fs.Config, &p, ¶ms)
if err != nil {
return false, err
}
req, _ := http.NewRequest("PUT", u.String(), nil)
hdfsData, err := requestHdfsData(fs.client, *req)
if err != nil {
return false, err
}
return hdfsData.Boolean, nil
}
// Creates a symlink where link -> destination
// See HDFS FileSystem.createSymlink()
// dest - the full path of the original resource
// link - the symlink path to create
// createParent - when true, parent dirs are created if they don't exist
// See http://hadoop.apache.org/docs/r2.2.0/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#HTTP_Query_Parameter_Dictionary
func (fs *FileSystem) CreateSymlink(dest Path, link Path, createParent bool) (bool, error) {
params := map[string]string{"op": OP_CREATESYMLINK}
if dest.Name == "" || link.Name == "" {
return false, fmt.Errorf("CreateSymlink - param dest and link cannot be empty.")
}
params["destination"] = dest.Name
params["createParent"] = strconv.FormatBool(createParent)
u, err := buildRequestUrl(fs.Config, &link, ¶ms)
if err != nil {
return false, err
}
req, _ := http.NewRequest("PUT", u.String(), nil)
rsp, err := fs.client.Do(req)
defer rsp.Body.Close()
if err != nil {
return false, err
}
return true, nil
}
// Returns status for a given file. The Path must represent a FILE
// on the remote system. (see HDFS FileSystem.getFileStatus())
func (fs *FileSystem) GetFileStatus(p Path) (FileStatus, error) {
params := map[string]string{"op": OP_GETFILESTATUS}
u, err := buildRequestUrl(fs.Config, &p, ¶ms)
if err != nil {
return FileStatus{}, err
}
req, _ := http.NewRequest("GET", u.String(), nil)
hdfsData, err := requestHdfsData(fs.client, *req)
if err != nil {
return FileStatus{}, err
}
return hdfsData.FileStatus, nil
}
// Returns an array of FileStatus for a given file directory.
// For details, see HDFS FileSystem.listStatus()
func (fs *FileSystem) ListStatus(p Path) ([]FileStatus, error) {
params := map[string]string{"op": OP_LISTSTATUS}
u, err := buildRequestUrl(fs.Config, &p, ¶ms)
if err != nil {
return nil, err
}
req, _ := http.NewRequest("GET", u.String(), nil)
hdfsData, err := requestHdfsData(fs.client, *req)
if err != nil {
return nil, err
}
return hdfsData.FileStatuses.FileStatus, nil
}
//Returns ContentSummary for the given path.
//For detail, see HDFS FileSystem.getContentSummary()
func (fs *FileSystem) GetContentSummary(p Path) (ContentSummary, error) {
params := map[string]string{"op": OP_GETCONTENTSUMMARY}
u, err := buildRequestUrl(fs.Config, &p, ¶ms)
if err != nil {
return ContentSummary{}, err
}
req, _ := http.NewRequest("GET", u.String(), nil)
hdfsData, err := requestHdfsData(fs.client, *req)
if err != nil {
return ContentSummary{}, err
}
return hdfsData.ContentSummary, nil
}
func (fs *FileSystem) GetHomeDirectory() (Path, error) {
return Path{}, fmt.Errorf("Method GetHomeDirectory(), not implemented yet.")
}
// Returns HDFS file checksum.
// For detail, see HDFS FileSystem.getFileChecksum()
func (fs *FileSystem) GetFileChecksum(p Path) (FileChecksum, error) {
params := map[string]string{"op": OP_GETFILECHECKSUM}
u, err := buildRequestUrl(fs.Config, &p, ¶ms)
if err != nil {
return FileChecksum{}, err
}
req, _ := http.NewRequest("GET", u.String(), nil)
hdfsData, err := requestHdfsData(fs.client, *req)
if err != nil {
return FileChecksum{}, err
}
return hdfsData.FileChecksum, nil
}