forked from colinmarc/hdfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove.go
52 lines (43 loc) · 1.15 KB
/
remove.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
package hdfs
import (
"errors"
"os"
"github.com/golang/protobuf/proto"
hdfs "github.com/reborn-go/hdfs/internal/protocol/hadoop_hdfs"
)
// Remove removes the named file or (empty) directory.
func (c *Client) Remove(name string) error {
return delete(c, name, false)
}
// RemoveAll removes path and any children it contains. It removes everything it
// can but returns the first error it encounters. If the path does not exist,
// RemoveAll returns nil (no error).
func (c *Client) RemoveAll(name string) error {
err := delete(c, name, true)
if os.IsNotExist(err) {
return nil
}
return err
}
func delete(c *Client, name string, recursive bool) error {
_, err := c.getFileInfo(name)
if err != nil {
return &os.PathError{"remove", name, err}
}
req := &hdfs.DeleteRequestProto{
Src: proto.String(name),
Recursive: proto.Bool(recursive),
}
resp := &hdfs.DeleteResponseProto{}
err = c.namenode.Execute("delete", req, resp)
if err != nil {
return &os.PathError{"remove", name, interpretException(err)}
} else if resp.Result == nil {
return &os.PathError{
"remove",
name,
errors.New("unexpected empty response"),
}
}
return nil
}