forked from colinmarc/hdfs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
227 lines (211 loc) · 6.17 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
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
package hdfs
import (
"os"
"os/user"
"path/filepath"
"time"
)
type IClient interface {
Append(name string) (*FileWriter, error)
Chmod(name string, perm os.FileMode) error
Chown(name string, user, group string) error
Chtimes(name string, atime time.Time, mtime time.Time) error
Close() error
CopyToLocal(src string, dst string) error
CopyToRemote(src string, dst string) error
Create(name string) (*FileWriter, error)
CreateEmptyFile(name string) error
CreateFile(name string, replication int, blockSize int64, perm os.FileMode) (*FileWriter, error)
GetContentSummary(name string) (*ContentSummary, error)
Mkdir(dirname string, perm os.FileMode) error
MkdirAll(dirname string, perm os.FileMode) error
Open(name string) (*FileReader, error)
ReadDir(dirname string) ([]os.FileInfo, error)
ReadFile(filename string) ([]byte, error)
Remove(name string) error
Rename(oldpath, newpath string) error
Stat(name string) (os.FileInfo, error)
StatFs() (FsInfo, error)
Walk(root string, walkFn filepath.WalkFunc) error
}
/// Client is a proxy to IClient, for compat reason
type Client struct {
realc IClient
}
// ClientOptions
type ClientOptions struct {
// the conf, if missing, load default conf from external file
Conf HadoopConf
// the root nsid, if missing, use defaultFS from conf
RootNameServiceID string
// the user name, if missing, use HADOOP_USER_NAME env var
User string
// force connect to this addresses instead of smart choice
Addresses []string
}
var _ IClient = Client{}
// Username returns the value of HADOOP_USER_NAME in the environment, or
// the current system user if it is not set.
func Username() (string, error) {
username := os.Getenv("HADOOP_USER_NAME")
if username != "" {
return username, nil
}
currentUser, err := user.Current()
if err != nil {
return "", err
}
return currentUser.Username, nil
}
// NewForUser returns a connected Client with the user specified, or an error if
// it can't connect.
//
// Deprecated: Use NewClient with SimpleClientOptions instead.
func NewForUser(address string, user string) (*Client, error) {
t, err := NewSimpleClientForUser(address, user)
if err != nil {
return nil, err
} else {
return &Client{t}, nil
}
}
// New returns a connected Client, or an error if it can't connect. The user
// will be the user the code is running under. If address is an empty string
// it will try and get the namenode address from the hadoop configuration
// files. If address is a NameServiceID in conf file, it will connect to one
// of its HA node, If address is NameServiceID with Viewfs mount links, it
// will return a ViewfsClient to handle links
func New(maybe_addr string) (*Client, error) {
if maybe_addr != "" {
conf := LoadHadoopConf("")
o := conf.CheckTypeOfNameAddressString(maybe_addr)
switch o {
case TNAS_SimpleAddress, TNAS_SimpleNameServiceID:
{
t, err := NewSimpleClientForAddress(maybe_addr)
if err != nil {
return nil, err
} else {
return &Client{t}, nil
}
}
case TNAS_ViewfsNameServiceID:
{
t, err := NewViewfsClientForRootNSID(conf, maybe_addr)
if err != nil {
return nil, err
} else {
return &Client{t}, nil
}
}
default:
return nil, errUnresolvedNamenode
}
} else {
t, err := NewViewfsClientDefault()
if err != nil {
return nil, err
} else {
return &Client{t}, nil
}
}
}
func NewClient(options ClientOptions) (*Client, error) {
var err error
if options.User == "" {
options.User, err = Username()
if err != nil {
return nil, err
}
}
if options.Addresses != nil && len(options.Addresses) > 0 {
opt2 := SimpleClientOptions{
User: options.User,
Addresses: options.Addresses,
}
t, err := NewSimpleClient(opt2)
if err != nil {
return nil, err
} else {
return &Client{t}, nil
}
} else {
opt2 := ViewfsClientOptions{
Conf: options.Conf,
RootNameServiceID: options.RootNameServiceID,
User: options.User,
}
t, err := NewViewfsClient(opt2)
if err != nil {
return nil, err
} else {
return &Client{t}, nil
}
}
}
// Client is a proxy to IClient
//====================================
func (c Client) Append(name string) (*FileWriter, error) {
return c.realc.Append(name)
}
func (c Client) Chmod(name string, perm os.FileMode) error {
return c.realc.Chmod(name, perm)
}
func (c Client) Chown(name string, user, group string) error {
return c.realc.Chown(name, user, group)
}
func (c Client) Chtimes(name string, atime time.Time, mtime time.Time) error {
return c.realc.Chtimes(name, atime, mtime)
}
func (c Client) Close() error {
return c.realc.Close()
}
func (c Client) CopyToLocal(src string, dst string) error {
return c.realc.CopyToLocal(src, dst)
}
func (c Client) CopyToRemote(src string, dst string) error {
return c.realc.CopyToRemote(src, dst)
}
func (c Client) Create(name string) (*FileWriter, error) {
return c.realc.Create(name)
}
func (c Client) CreateEmptyFile(name string) error {
return c.realc.CreateEmptyFile(name)
}
func (c Client) CreateFile(name string, replication int, blockSize int64, perm os.FileMode) (*FileWriter, error) {
return c.realc.CreateFile(name, replication, blockSize, perm)
}
func (c Client) GetContentSummary(name string) (*ContentSummary, error) {
return c.realc.GetContentSummary(name)
}
func (c Client) Mkdir(dirname string, perm os.FileMode) error {
return c.realc.Mkdir(dirname, perm)
}
func (c Client) MkdirAll(dirname string, perm os.FileMode) error {
return c.realc.MkdirAll(dirname, perm)
}
func (c Client) Open(name string) (*FileReader, error) {
return c.realc.Open(name)
}
func (c Client) ReadDir(dirname string) ([]os.FileInfo, error) {
return c.realc.ReadDir(dirname)
}
func (c Client) ReadFile(filename string) ([]byte, error) {
return c.realc.ReadFile(filename)
}
func (c Client) Remove(name string) error {
return c.realc.Remove(name)
}
func (c Client) Rename(oldpath, newpath string) error {
return c.realc.Rename(oldpath, newpath)
}
func (c Client) Stat(name string) (os.FileInfo, error) {
return c.realc.Stat(name)
}
func (c Client) StatFs() (FsInfo, error) {
return c.realc.StatFs()
}
func (c Client) Walk(root string, walkFn filepath.WalkFunc) error {
return c.realc.Walk(root, walkFn)
}
//====================================