File: stat.go

package info (click to toggle)
golang-github-colinmarc-hdfs 2.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,760 kB
  • sloc: sh: 130; xml: 40; makefile: 31
file content (108 lines) | stat: -rw-r--r-- 2,538 bytes parent folder | download
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
package hdfs

import (
	"os"
	"path"
	"time"

	hdfs "github.com/colinmarc/hdfs/v2/internal/protocol/hadoop_hdfs"
	"google.golang.org/protobuf/proto"
)

// FileInfo implements os.FileInfo, and provides information about a file or
// directory in HDFS.
type FileInfo struct {
	name   string
	status *FileStatus
}

type FileStatus = 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, interpretException(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 {
		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)
}