File: info.go

package info (click to toggle)
golang-github-tredoe-osutil 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: makefile: 4
file content (172 lines) | stat: -rw-r--r-- 3,618 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
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
// Copyright 2012 Jonas mg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

package file

import "os"

// flags got in: `man 2 stat`
const (
	modeROwner = 00400 // owner has read permission
	modeWOwner = 00200 // owner has write permission
	modeXOwner = 00100 // owner has execute permission

	modeRGroup = 00040 // group has read permission
	modeWGroup = 00020 // group has write permission
	modeXGroup = 00010 // group has execute permission

	modeROthers = 00004 // others have read permission
	modeWOthers = 00002 // others have write permission
	modeXOthers = 00001 // others have execute permission
)

type perm uint8

// permissions
const (
	_ perm = iota
	R      // read
	W      // write
	X      // execute
)

// info represents a wrapper about os.FileInfo to append some functions.
type info struct{ fi os.FileInfo }

// NewInfo returns a info describing the named file.
func NewInfo(name string) (*info, error) {
	i, err := os.Stat(name)
	if err != nil {
		return nil, err
	}
	return &info{i}, nil
}

// IsDir reports whether if it is a directory.
func (i *info) IsDir() bool {
	return i.fi.IsDir()
}

// IsFile reports whether it is a regular file.
func (i *info) IsFile() bool {
	return i.fi.Mode()&os.ModeType == 0
}

// OwnerHas reports whether the owner has all given permissions.
func (i *info) OwnerHas(p ...perm) bool {
	mode := i.fi.Mode()

	for _, v := range p {
		switch v {
		case R:
			if mode&modeROwner == 0 {
				return false
			}
		case W:
			if mode&modeWOwner == 0 {
				return false
			}
		case X:
			if mode&modeXOwner == 0 {
				return false
			}
		}
	}
	return true
}

// GroupHas reports whether the group has all given permissions.
func (i *info) GroupHas(p ...perm) bool {
	mode := i.fi.Mode()

	for _, v := range p {
		switch v {
		case R:
			if mode&modeRGroup == 0 {
				return false
			}
		case W:
			if mode&modeWGroup == 0 {
				return false
			}
		case X:
			if mode&modeXGroup == 0 {
				return false
			}
		}
	}
	return true
}

// OthersHave reports whether the others have all given permissions.
func (i *info) OthersHave(p ...perm) bool {
	mode := i.fi.Mode()

	for _, v := range p {
		switch v {
		case R:
			if mode&modeROthers == 0 {
				return false
			}
		case W:
			if mode&modeWOthers == 0 {
				return false
			}
		case X:
			if mode&modeXOthers == 0 {
				return false
			}
		}
	}
	return true
}

// * * *

// IsDir reports whether if the named file is a directory.
func IsDir(name string) (bool, error) {
	i, err := NewInfo(name)
	if err != nil {
		return false, err
	}
	return i.IsDir(), nil
}

// IsFile reports whether the named file is a regular file.
func IsFile(name string) (bool, error) {
	i, err := NewInfo(name)
	if err != nil {
		return false, err
	}
	return i.IsFile(), nil
}

// OwnerHas reports whether the named file has all given permissions for the owner.
func OwnerHas(name string, p ...perm) (bool, error) {
	i, err := NewInfo(name)
	if err != nil {
		return false, err
	}
	return i.OwnerHas(p...), nil
}

// GroupHas reports whether the named file has all given permissions for the group.
func GroupHas(name string, p ...perm) (bool, error) {
	i, err := NewInfo(name)
	if err != nil {
		return false, err
	}
	return i.GroupHas(p...), nil
}

// OthersHave reports whether the named file have all given permissions for the others.
func OthersHave(name string, p ...perm) (bool, error) {
	i, err := NewInfo(name)
	if err != nil {
		return false, err
	}
	return i.OthersHave(p...), nil
}