1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.vfs2.util;
19
20 import java.util.EnumMap;
21 import java.util.Map;
22
23
24
25
26
27
28 public class PosixPermissions {
29
30
31
32
33 public enum Type {
34
35
36
37
38 UserReadable(0x100),
39
40
41
42
43 UserWritable(0x080),
44
45
46
47
48 UserExecutable(0x040),
49
50
51
52
53 GroupReadable(0x020),
54
55
56
57
58 GroupWritable(0x010),
59
60
61
62
63 GroupExecutable(0x008),
64
65
66
67
68 OtherReadable(0x004),
69
70
71
72
73 OtherWritable(0x002),
74
75
76
77
78 OtherExecutable(0x001);
79
80 private final int mask;
81
82
83
84
85 Type(final int mask) {
86 this.mask = mask;
87 }
88
89
90
91
92
93
94 public int getMask() {
95 return mask;
96 }
97
98 }
99
100
101
102
103 private final int permissions;
104
105
106
107
108 private final boolean isOwner;
109
110
111
112
113 private final boolean isInGroup;
114
115
116
117
118
119
120
121
122 public PosixPermissions(final int permissions, final boolean isOwner, final boolean isInGroup) {
123 this.permissions = permissions;
124 this.isOwner = isOwner;
125 this.isInGroup = isInGroup;
126 }
127
128
129
130
131
132
133
134 private int computeNewPermissions(final Map<Type, Boolean> values) {
135 int newPerms = permissions;
136 for (final Map.Entry<Type, Boolean> entry : values.entrySet()) {
137 final Type type = entry.getKey();
138 if (entry.getValue()) {
139 newPerms |= type.getMask();
140 } else {
141 newPerms &= ~type.getMask();
142 }
143 }
144 return newPerms;
145 }
146
147
148
149
150
151
152 private boolean get(final Type type) {
153 return (type.getMask() & permissions) != 0;
154 }
155
156
157
158
159
160
161 public int getPermissions() {
162 return permissions;
163 }
164
165
166
167
168
169
170 public boolean isExecutable() {
171 if (isOwner) {
172 return get(Type.UserExecutable);
173 }
174 if (isInGroup) {
175 return get(Type.GroupExecutable);
176 }
177 return get(Type.OtherExecutable);
178 }
179
180
181
182
183
184
185 public boolean isReadable() {
186 if (isOwner) {
187 return get(Type.UserReadable);
188 }
189 if (isInGroup) {
190 return get(Type.GroupReadable);
191 }
192 return get(Type.OtherReadable);
193 }
194
195
196
197
198
199
200 public boolean isWritable() {
201 if (isOwner) {
202 return get(Type.UserWritable);
203 }
204 if (isInGroup) {
205 return get(Type.GroupWritable);
206 }
207 return get(Type.OtherWritable);
208 }
209
210
211
212
213
214
215
216
217 public int makeExecutable(final boolean executable, final boolean ownerOnly) {
218 final EnumMap<Type, Boolean> map = new EnumMap<>(Type.class);
219 map.put(Type.UserExecutable, executable);
220 if (!ownerOnly) {
221 map.put(Type.GroupExecutable, executable);
222 map.put(Type.OtherExecutable, executable);
223 }
224 return computeNewPermissions(map);
225 }
226
227
228
229
230
231
232
233
234 public Integer makeReadable(final boolean readable, final boolean ownerOnly) {
235 final EnumMap<Type, Boolean> map = new EnumMap<>(Type.class);
236 map.put(Type.UserReadable, readable);
237 if (!ownerOnly) {
238 map.put(Type.GroupReadable, readable);
239 map.put(Type.OtherReadable, readable);
240 }
241 return computeNewPermissions(map);
242 }
243
244
245
246
247
248
249
250
251 public Integer makeWritable(final boolean writable, final boolean ownerOnly) {
252 final EnumMap<Type, Boolean> map = new EnumMap<>(Type.class);
253 map.put(Type.UserWritable, writable);
254 if (!ownerOnly) {
255 map.put(Type.GroupWritable, writable);
256 map.put(Type.OtherWritable, writable);
257 }
258 return computeNewPermissions(map);
259 }
260 }