Skip to content

Commit 0b1473e

Browse files
committed
[PLXCOMP-179] Java 1.7 file attribute support
1 parent c860779 commit 0b1473e

File tree

5 files changed

+649
-8
lines changed

5 files changed

+649
-8
lines changed
Lines changed: 353 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,353 @@
1+
package org.codehaus.plexus.components.io.attributes;
2+
3+
/*
4+
* Copyright 2007 The Codehaus Foundation.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
22+
public class Java7FileAttributes
23+
implements PlexusIoResourceAttributes
24+
{
25+
26+
protected static final char VALUE_DISABLED_MODE = '-';
27+
28+
protected static final char VALUE_WRITABLE_MODE = 'w';
29+
30+
protected static final char VALUE_READABLE_MODE = 'r';
31+
32+
protected static final char VALUE_EXECUTABLE_MODE = 'x';
33+
34+
protected static final int INDEX_WORLD_EXECUTE = 9;
35+
36+
protected static final int INDEX_WORLD_WRITE = 8;
37+
38+
protected static final int INDEX_WORLD_READ = 7;
39+
40+
protected static final int INDEX_GROUP_EXECUTE = 6;
41+
42+
protected static final int INDEX_GROUP_WRITE = 5;
43+
44+
protected static final int INDEX_GROUP_READ = 4;
45+
46+
protected static final int INDEX_OWNER_EXECUTE = 3;
47+
48+
protected static final int INDEX_OWNER_WRITE = 2;
49+
50+
protected static final int INDEX_OWNER_READ = 1;
51+
52+
private int groupId = -1;
53+
54+
private String groupName;
55+
56+
private int userId = -1;
57+
58+
private String userName;
59+
60+
private char[] mode;
61+
62+
public Java7FileAttributes(File file)
63+
throws IOException
64+
{
65+
Object posixFileAttributes = Java7Reflector.getPosixFileAttributes( file );
66+
67+
this.userName = Java7Reflector.getOwnerUserName( posixFileAttributes );
68+
this.groupName = Java7Reflector.getOwnerGroupName( posixFileAttributes );
69+
mode = Java7Reflector.getPermissions( posixFileAttributes ).toCharArray();
70+
}
71+
72+
protected char[] getLsModeParts()
73+
{
74+
return mode;
75+
}
76+
77+
protected void setLsModeParts( char[] mode )
78+
{
79+
if( mode.length < 10 )
80+
{
81+
this.mode = new char[10];
82+
System.arraycopy( mode, 0, this.mode, 0, mode.length );
83+
for ( int i = mode.length; i < 10; i++ )
84+
{
85+
this.mode[i] = VALUE_DISABLED_MODE;
86+
}
87+
}
88+
else
89+
{
90+
this.mode = mode;
91+
}
92+
}
93+
94+
public Integer getGroupId()
95+
{
96+
return groupId;
97+
}
98+
99+
public boolean hasGroupId()
100+
{
101+
return false;
102+
}
103+
104+
public boolean hasUserId()
105+
{
106+
return false;
107+
}
108+
109+
public String getGroupName()
110+
{
111+
return groupName;
112+
}
113+
114+
public Integer getUserId()
115+
{
116+
return userId;
117+
}
118+
119+
public String getUserName()
120+
{
121+
return userName;
122+
}
123+
124+
public boolean isGroupExecutable()
125+
{
126+
return checkFlag( '-', INDEX_GROUP_EXECUTE );
127+
}
128+
129+
private boolean checkFlag( char disabledValue, int idx )
130+
{
131+
return mode != null && mode[idx] != disabledValue;
132+
}
133+
134+
public boolean isGroupReadable()
135+
{
136+
return checkFlag( '-', INDEX_GROUP_READ );
137+
}
138+
139+
public boolean isGroupWritable()
140+
{
141+
return checkFlag( '-', INDEX_GROUP_WRITE );
142+
}
143+
144+
public boolean isOwnerExecutable()
145+
{
146+
return checkFlag( '-', INDEX_OWNER_EXECUTE );
147+
}
148+
149+
public boolean isOwnerReadable()
150+
{
151+
return checkFlag( '-', INDEX_OWNER_READ );
152+
}
153+
154+
public boolean isOwnerWritable()
155+
{
156+
return checkFlag( '-', INDEX_OWNER_WRITE );
157+
}
158+
159+
public boolean isWorldExecutable()
160+
{
161+
return checkFlag( '-', INDEX_WORLD_EXECUTE );
162+
}
163+
164+
public boolean isWorldReadable()
165+
{
166+
return checkFlag( '-', INDEX_WORLD_READ );
167+
}
168+
169+
public boolean isWorldWritable()
170+
{
171+
return checkFlag( '-', INDEX_WORLD_WRITE );
172+
}
173+
174+
public String toString()
175+
{
176+
StringBuilder sb = new StringBuilder();
177+
sb.append( "\nFile Attributes:\n------------------------------\nuser: " );
178+
sb.append( userName == null ? "" : userName );
179+
sb.append( "\ngroup: " );
180+
sb.append( groupName == null ? "" : groupName );
181+
sb.append( "\nuid: " );
182+
sb.append( hasUserId() ? Integer.toString( userId ) : "" );
183+
sb.append( "\ngid: " );
184+
sb.append( hasGroupId() ? Integer.toString( groupId ) : "" );
185+
sb.append( "\nmode: " );
186+
sb.append( mode == null ? "" : String.valueOf( mode ) );
187+
188+
return sb.toString();
189+
}
190+
191+
public int getOctalMode()
192+
{
193+
int result = 0;
194+
195+
if ( isOwnerReadable() )
196+
{
197+
result |= AttributeConstants.OCTAL_OWNER_READ;
198+
}
199+
200+
if ( isOwnerWritable() )
201+
{
202+
result |= AttributeConstants.OCTAL_OWNER_WRITE;
203+
}
204+
205+
if ( isOwnerExecutable() )
206+
{
207+
result |= AttributeConstants.OCTAL_OWNER_EXECUTE;
208+
}
209+
210+
if ( isGroupReadable() )
211+
{
212+
result |= AttributeConstants.OCTAL_GROUP_READ;
213+
}
214+
215+
if ( isGroupWritable() )
216+
{
217+
result |= AttributeConstants.OCTAL_GROUP_WRITE;
218+
}
219+
220+
if ( isGroupExecutable() )
221+
{
222+
result |= AttributeConstants.OCTAL_GROUP_EXECUTE;
223+
}
224+
225+
if ( isWorldReadable() )
226+
{
227+
result |= AttributeConstants.OCTAL_WORLD_READ;
228+
}
229+
230+
if ( isWorldWritable() )
231+
{
232+
result |= AttributeConstants.OCTAL_WORLD_WRITE;
233+
}
234+
235+
if ( isWorldExecutable() )
236+
{
237+
result |= AttributeConstants.OCTAL_WORLD_EXECUTE;
238+
}
239+
240+
return result;
241+
}
242+
243+
public String getOctalModeString()
244+
{
245+
return Integer.toString( getOctalMode(), 8 );
246+
}
247+
248+
public PlexusIoResourceAttributes setGroupExecutable( boolean flag )
249+
{
250+
setMode( flag ? VALUE_EXECUTABLE_MODE : VALUE_DISABLED_MODE, INDEX_GROUP_EXECUTE );
251+
return this;
252+
}
253+
254+
public PlexusIoResourceAttributes setGroupId( Integer gid )
255+
{
256+
this.groupId = gid;
257+
return this;
258+
}
259+
260+
public PlexusIoResourceAttributes setGroupName( String name )
261+
{
262+
this.groupName = name;
263+
return this;
264+
}
265+
266+
public PlexusIoResourceAttributes setGroupReadable( boolean flag )
267+
{
268+
setMode( flag ? VALUE_READABLE_MODE : VALUE_DISABLED_MODE, INDEX_GROUP_READ );
269+
return this;
270+
}
271+
272+
public PlexusIoResourceAttributes setGroupWritable( boolean flag )
273+
{
274+
setMode( flag ? VALUE_WRITABLE_MODE : VALUE_DISABLED_MODE, INDEX_GROUP_WRITE );
275+
return this;
276+
}
277+
278+
public PlexusIoResourceAttributes setOwnerExecutable( boolean flag )
279+
{
280+
setMode( flag ? VALUE_EXECUTABLE_MODE : VALUE_DISABLED_MODE, INDEX_OWNER_EXECUTE );
281+
return this;
282+
}
283+
284+
public PlexusIoResourceAttributes setOwnerReadable( boolean flag )
285+
{
286+
setMode( flag ? VALUE_READABLE_MODE : VALUE_DISABLED_MODE, INDEX_OWNER_READ );
287+
return this;
288+
}
289+
290+
public PlexusIoResourceAttributes setOwnerWritable( boolean flag )
291+
{
292+
setMode( flag ? VALUE_WRITABLE_MODE : VALUE_DISABLED_MODE, INDEX_OWNER_WRITE );
293+
return this;
294+
}
295+
296+
public PlexusIoResourceAttributes setUserId( Integer uid )
297+
{
298+
this.userId = uid;
299+
return this;
300+
}
301+
302+
public PlexusIoResourceAttributes setUserName( String name )
303+
{
304+
this.userName = name;
305+
return this;
306+
}
307+
308+
public PlexusIoResourceAttributes setWorldExecutable( boolean flag )
309+
{
310+
setMode( flag ? VALUE_EXECUTABLE_MODE : VALUE_DISABLED_MODE, INDEX_WORLD_EXECUTE );
311+
return this;
312+
}
313+
314+
public PlexusIoResourceAttributes setWorldReadable( boolean flag )
315+
{
316+
setMode( flag ? VALUE_READABLE_MODE : VALUE_DISABLED_MODE, INDEX_WORLD_READ );
317+
return this;
318+
}
319+
320+
public PlexusIoResourceAttributes setWorldWritable( boolean flag )
321+
{
322+
setMode( flag ? VALUE_WRITABLE_MODE : VALUE_DISABLED_MODE, INDEX_WORLD_WRITE );
323+
return this;
324+
}
325+
326+
private void setMode( char value, int modeIdx )
327+
{
328+
char[] mode = getLsModeParts();
329+
mode[modeIdx] = value;
330+
331+
setLsModeParts( mode );
332+
}
333+
334+
public PlexusIoResourceAttributes setOctalMode( int mode )
335+
{
336+
setGroupExecutable( PlexusIoResourceAttributeUtils.isGroupExecutableInOctal( mode ) );
337+
setGroupReadable( PlexusIoResourceAttributeUtils.isGroupReadableInOctal( mode ) );
338+
setGroupWritable( PlexusIoResourceAttributeUtils.isGroupWritableInOctal( mode ) );
339+
setOwnerExecutable( PlexusIoResourceAttributeUtils.isOwnerExecutableInOctal( mode ) );
340+
setOwnerReadable( PlexusIoResourceAttributeUtils.isOwnerReadableInOctal( mode ) );
341+
setOwnerWritable( PlexusIoResourceAttributeUtils.isOwnerWritableInOctal( mode ) );
342+
setWorldExecutable( PlexusIoResourceAttributeUtils.isWorldExecutableInOctal( mode ) );
343+
setWorldReadable( PlexusIoResourceAttributeUtils.isWorldReadableInOctal( mode ) );
344+
setWorldWritable( PlexusIoResourceAttributeUtils.isWorldWritableInOctal( mode ) );
345+
return this;
346+
}
347+
348+
public PlexusIoResourceAttributes setOctalModeString( String mode )
349+
{
350+
setOctalMode( Integer.parseInt( mode, 8 ) );
351+
return this;
352+
}
353+
}

0 commit comments

Comments
 (0)