37
37
* Immutable
38
38
*/
39
39
public class FileAttributes
40
- implements PlexusIoResourceAttributes {
40
+ implements PlexusIoResourceAttributes
41
+ {
41
42
@ Nullable
42
43
private final Integer groupId ;
43
44
@@ -55,197 +56,236 @@ public class FileAttributes
55
56
56
57
private final Set <PosixFilePermission > permissions ;
57
58
58
- public FileAttributes (@ Nonnull File file , @ Nonnull Map <Integer , String > userCache ,
59
- @ Nonnull Map <Integer , String > groupCache )
60
- throws IOException {
61
-
59
+ public FileAttributes ( @ Nonnull File file , @ Nonnull Map <Integer , String > userCache ,
60
+ @ Nonnull Map <Integer , String > groupCache )
61
+ throws IOException
62
+ {
62
63
63
64
Path path = file .toPath ();
64
- if (AttributeUtils .isUnix (path )) {
65
- Map <String , Object > attrs = Files .readAttributes (path , "unix:*" , LinkOption .NOFOLLOW_LINKS );
66
- this .permissions = (Set <PosixFilePermission >) attrs .get ("permissions" );
65
+ if ( AttributeUtils .isUnix ( path ) )
66
+ {
67
+ Map <String , Object > attrs = Files .readAttributes ( path , "unix:*" , LinkOption .NOFOLLOW_LINKS );
68
+ this .permissions = (Set <PosixFilePermission >) attrs .get ( "permissions" );
67
69
68
- groupId = (Integer ) attrs .get ("gid" );
70
+ groupId = (Integer ) attrs .get ( "gid" );
69
71
70
- String groupName = groupCache .get (groupId );
71
- if (groupName != null ) {
72
+ String groupName = groupCache .get ( groupId );
73
+ if ( groupName != null )
74
+ {
72
75
this .groupName = groupName ;
73
- } else {
74
- this .groupName = ((Principal ) attrs .get ("group" )).getName ();
75
- groupCache .put (groupId , this .groupName );
76
76
}
77
- userId = (Integer ) attrs .get ("uid" );
78
- String userName = userCache .get (userId );
79
- if (userName != null ) {
77
+ else
78
+ {
79
+ this .groupName = ( (Principal ) attrs .get ( "group" ) ).getName ();
80
+ groupCache .put ( groupId , this .groupName );
81
+ }
82
+ userId = (Integer ) attrs .get ( "uid" );
83
+ String userName = userCache .get ( userId );
84
+ if ( userName != null )
85
+ {
80
86
this .userName = userName ;
81
- } else {
82
- this .userName = ((Principal ) attrs .get ("owner" )).getName ();
83
- userCache .put (userId , this .userName );
84
87
}
85
- octalMode = (Integer ) attrs .get ("mode" ) & 0xfff ; // Mask off top bits for compatibilty. Maybe check if we can skip this
86
- symbolicLink = (Boolean ) attrs .get ("isSymbolicLink" );
87
- } else {
88
- FileOwnerAttributeView fa = AttributeUtils .getFileOwnershipInfo (file );
88
+ else
89
+ {
90
+ this .userName = ( (Principal ) attrs .get ( "owner" ) ).getName ();
91
+ userCache .put ( userId , this .userName );
92
+ }
93
+ octalMode = (Integer ) attrs .get ( "mode" ) & 0xfff ; // Mask off top bits for compatibilty. Maybe check if we
94
+ // can skip this
95
+ symbolicLink = (Boolean ) attrs .get ( "isSymbolicLink" );
96
+ }
97
+ else
98
+ {
99
+ FileOwnerAttributeView fa = AttributeUtils .getFileOwnershipInfo ( file );
89
100
this .userName = fa .getOwner ().getName ();
90
101
userId = null ;
91
102
this .groupName = null ;
92
103
this .groupId = null ;
93
104
octalMode = PlexusIoResourceAttributes .UNKNOWN_OCTAL_MODE ;
94
105
permissions = Collections .emptySet ();
95
- symbolicLink = Files .isSymbolicLink (path );
106
+ symbolicLink = Files .isSymbolicLink ( path );
96
107
}
97
108
98
109
}
99
110
100
- public static
101
- @ Nonnull
102
- PlexusIoResourceAttributes uncached ( @ Nonnull File file )
103
- throws IOException {
104
- return new FileAttributes (file , new HashMap <Integer , String >(), new HashMap <Integer , String >());
111
+ public static @ Nonnull
112
+ PlexusIoResourceAttributes uncached ( @ Nonnull File file )
113
+ throws IOException
114
+ {
115
+ return new FileAttributes ( file , new HashMap <Integer , String >(), new HashMap <Integer , String >() );
105
116
}
106
117
107
-
108
118
@ Nullable
109
- public Integer getGroupId () {
119
+ public Integer getGroupId ()
120
+ {
110
121
111
122
return groupId ;
112
123
}
113
124
114
- public boolean hasGroupId () {
125
+ public boolean hasGroupId ()
126
+ {
115
127
return false ;
116
128
}
117
129
118
- public boolean hasUserId () {
130
+ public boolean hasUserId ()
131
+ {
119
132
return false ;
120
133
}
121
134
122
135
@ Nullable
123
- public String getGroupName () {
136
+ public String getGroupName ()
137
+ {
124
138
return groupName ;
125
139
}
126
140
127
- public Integer getUserId () {
141
+ public Integer getUserId ()
142
+ {
128
143
return userId ;
129
144
}
130
145
131
- public String getUserName () {
146
+ public String getUserName ()
147
+ {
132
148
return userName ;
133
149
}
134
150
135
- public boolean isGroupExecutable () {
136
- return containsPermission (PosixFilePermission .GROUP_EXECUTE );
151
+ public boolean isGroupExecutable ()
152
+ {
153
+ return containsPermission ( PosixFilePermission .GROUP_EXECUTE );
137
154
}
138
155
139
- private boolean containsPermission (PosixFilePermission groupExecute ) {
140
- return permissions .contains (groupExecute );
156
+ private boolean containsPermission ( PosixFilePermission groupExecute )
157
+ {
158
+ return permissions .contains ( groupExecute );
141
159
}
142
160
143
- public boolean isGroupReadable () {
144
- return containsPermission (PosixFilePermission .GROUP_READ );
161
+ public boolean isGroupReadable ()
162
+ {
163
+ return containsPermission ( PosixFilePermission .GROUP_READ );
145
164
}
146
165
147
- public boolean isGroupWritable () {
148
- return containsPermission (PosixFilePermission .GROUP_WRITE );
166
+ public boolean isGroupWritable ()
167
+ {
168
+ return containsPermission ( PosixFilePermission .GROUP_WRITE );
149
169
}
150
170
151
- public boolean isOwnerExecutable () {
152
- return containsPermission (PosixFilePermission .OWNER_EXECUTE );
171
+ public boolean isOwnerExecutable ()
172
+ {
173
+ return containsPermission ( PosixFilePermission .OWNER_EXECUTE );
153
174
}
154
175
155
- public boolean isOwnerReadable () {
156
- return containsPermission (PosixFilePermission .OWNER_READ );
176
+ public boolean isOwnerReadable ()
177
+ {
178
+ return containsPermission ( PosixFilePermission .OWNER_READ );
157
179
}
158
180
159
- public boolean isOwnerWritable () {
160
- return containsPermission (PosixFilePermission .OWNER_WRITE );
181
+ public boolean isOwnerWritable ()
182
+ {
183
+ return containsPermission ( PosixFilePermission .OWNER_WRITE );
161
184
}
162
185
163
- public boolean isWorldExecutable () {
164
- return containsPermission (PosixFilePermission .OTHERS_EXECUTE );
186
+ public boolean isWorldExecutable ()
187
+ {
188
+ return containsPermission ( PosixFilePermission .OTHERS_EXECUTE );
165
189
166
190
}
167
191
168
- public boolean isWorldReadable () {
169
- return containsPermission (PosixFilePermission .OTHERS_READ );
192
+ public boolean isWorldReadable ()
193
+ {
194
+ return containsPermission ( PosixFilePermission .OTHERS_READ );
170
195
}
171
196
172
- public boolean isWorldWritable () {
173
- return containsPermission (PosixFilePermission .OTHERS_WRITE );
197
+ public boolean isWorldWritable ()
198
+ {
199
+ return containsPermission ( PosixFilePermission .OTHERS_WRITE );
174
200
}
175
201
176
- public String toString () {
202
+ public String toString ()
203
+ {
177
204
StringBuilder sb = new StringBuilder ();
178
- sb .append (System .lineSeparator ());
179
- sb .append ("File Attributes:" );
180
- sb .append (System .lineSeparator ());
181
- sb .append ("------------------------------" );
182
- sb .append (System .lineSeparator ());
183
- sb .append ("user: " );
184
- sb .append (userName == null ? "" : userName );
185
- sb .append (System .lineSeparator ());
186
- sb .append ("group: " );
187
- sb .append (groupName == null ? "" : groupName );
188
- sb .append (System .lineSeparator ());
189
- sb .append ("uid: " );
190
- sb .append (hasUserId () ? Integer .toString (userId ) : "" );
191
- sb .append (System .lineSeparator ());
192
- sb .append ("gid: " );
193
- sb .append (hasGroupId () ? Integer .toString (groupId ) : "" );
205
+ sb .append ( System .lineSeparator () );
206
+ sb .append ( "File Attributes:" );
207
+ sb .append ( System .lineSeparator () );
208
+ sb .append ( "------------------------------" );
209
+ sb .append ( System .lineSeparator () );
210
+ sb .append ( "user: " );
211
+ sb .append ( userName == null ? "" : userName );
212
+ sb .append ( System .lineSeparator () );
213
+ sb .append ( "group: " );
214
+ sb .append ( groupName == null ? "" : groupName );
215
+ sb .append ( System .lineSeparator () );
216
+ sb .append ( "uid: " );
217
+ sb .append ( hasUserId () ? Integer .toString ( userId ) : "" );
218
+ sb .append ( System .lineSeparator () );
219
+ sb .append ( "gid: " );
220
+ sb .append ( hasGroupId () ? Integer .toString ( groupId ) : "" );
194
221
195
222
return sb .toString ();
196
223
}
197
224
198
- public int getOctalMode () {
225
+ public int getOctalMode ()
226
+ {
199
227
return octalMode ;
200
228
}
201
229
202
- public int calculatePosixOctalMode () {
230
+ public int calculatePosixOctalMode ()
231
+ {
203
232
int result = 0 ;
204
233
205
- if (isOwnerReadable ()) {
234
+ if ( isOwnerReadable () )
235
+ {
206
236
result |= AttributeConstants .OCTAL_OWNER_READ ;
207
237
}
208
238
209
- if (isOwnerWritable ()) {
239
+ if ( isOwnerWritable () )
240
+ {
210
241
result |= AttributeConstants .OCTAL_OWNER_WRITE ;
211
242
}
212
243
213
- if (isOwnerExecutable ()) {
244
+ if ( isOwnerExecutable () )
245
+ {
214
246
result |= AttributeConstants .OCTAL_OWNER_EXECUTE ;
215
247
}
216
248
217
- if (isGroupReadable ()) {
249
+ if ( isGroupReadable () )
250
+ {
218
251
result |= AttributeConstants .OCTAL_GROUP_READ ;
219
252
}
220
253
221
- if (isGroupWritable ()) {
254
+ if ( isGroupWritable () )
255
+ {
222
256
result |= AttributeConstants .OCTAL_GROUP_WRITE ;
223
257
}
224
258
225
- if (isGroupExecutable ()) {
259
+ if ( isGroupExecutable () )
260
+ {
226
261
result |= AttributeConstants .OCTAL_GROUP_EXECUTE ;
227
262
}
228
263
229
- if (isWorldReadable ()) {
264
+ if ( isWorldReadable () )
265
+ {
230
266
result |= AttributeConstants .OCTAL_WORLD_READ ;
231
267
}
232
268
233
- if (isWorldWritable ()) {
269
+ if ( isWorldWritable () )
270
+ {
234
271
result |= AttributeConstants .OCTAL_WORLD_WRITE ;
235
272
}
236
273
237
- if (isWorldExecutable ()) {
274
+ if ( isWorldExecutable () )
275
+ {
238
276
result |= AttributeConstants .OCTAL_WORLD_EXECUTE ;
239
277
}
240
278
241
279
return result ;
242
280
}
243
281
244
- public String getOctalModeString () {
245
- return Integer .toString (getOctalMode (), 8 );
282
+ public String getOctalModeString ()
283
+ {
284
+ return Integer .toString ( getOctalMode (), 8 );
246
285
}
247
286
248
- public boolean isSymbolicLink () {
287
+ public boolean isSymbolicLink ()
288
+ {
249
289
return symbolicLink ;
250
290
}
251
291
}
0 commit comments