-
Notifications
You must be signed in to change notification settings - Fork 340
/
stat.go
111 lines (88 loc) · 2.55 KB
/
stat.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
package hdfs
import (
"os"
"path"
"time"
hdfs "github.com/colinmarc/hdfs/protocol/hadoop_hdfs"
"github.com/colinmarc/hdfs/rpc"
"github.com/golang/protobuf/proto"
)
// FileInfo implements os.FileInfo, and provides information about a file or
// directory in HDFS.
type FileInfo struct {
name string
status *hdfs.HdfsFileStatusProto
}
// Stat returns an os.FileInfo describing the named file or directory.
func (c *Client) Stat(name string) (os.FileInfo, error) {
fi, err := c.getFileInfo(name)
if err != nil {
err = &os.PathError{"stat", name, err}
}
return fi, err
}
func (c *Client) getFileInfo(name string) (os.FileInfo, error) {
req := &hdfs.GetFileInfoRequestProto{Src: proto.String(name)}
resp := &hdfs.GetFileInfoResponseProto{}
err := c.namenode.Execute("getFileInfo", req, resp)
if err != nil {
if nnErr, ok := err.(*rpc.NamenodeError); ok {
err = interpretException(nnErr.Exception, err)
}
return nil, err
}
if resp.GetFs() == nil {
return nil, os.ErrNotExist
}
return newFileInfo(resp.GetFs(), name), nil
}
func newFileInfo(status *hdfs.HdfsFileStatusProto, name string) *FileInfo {
fi := &FileInfo{status: status}
var fullName string
if string(status.GetPath()) != "" {
fullName = string(status.GetPath())
} else {
fullName = name
}
fi.name = path.Base(fullName)
return fi
}
func (fi *FileInfo) Name() string {
return fi.name
}
func (fi *FileInfo) Size() int64 {
return int64(fi.status.GetLength())
}
func (fi *FileInfo) Mode() os.FileMode {
mode := os.FileMode(fi.status.GetPermission().GetPerm())
if fi.IsDir() {
mode |= os.ModeDir
}
return mode
}
func (fi *FileInfo) ModTime() time.Time {
return time.Unix(0, int64(fi.status.GetModificationTime())*int64(time.Millisecond))
}
func (fi *FileInfo) IsDir() bool {
return fi.status.GetFileType() == hdfs.HdfsFileStatusProto_IS_DIR
}
// Sys returns the raw *hadoop_hdfs.HdfsFileStatusProto message from the
// namenode.
func (fi *FileInfo) Sys() interface{} {
return fi.status
}
// Owner returns the name of the user that owns the file or directory. It's not
// part of the os.FileInfo interface.
func (fi *FileInfo) Owner() string {
return fi.status.GetOwner()
}
// OwnerGroup returns the name of the group that owns the file or directory.
// It's not part of the os.FileInfo interface.
func (fi *FileInfo) OwnerGroup() string {
return fi.status.GetGroup()
}
// AccessTime returns the last time the file was accessed. It's not part of the
// os.FileInfo interface.
func (fi *FileInfo) AccessTime() time.Time {
return time.Unix(int64(fi.status.GetAccessTime())/1000, 0)
}