Skip to content

Commit 6ffc902

Browse files
gnodetcstamas
andauthored
Restore speed improvements (#79)
Reimplement speed improvements (that were once reverted). Co-authored-by: Tamas Cservenak <tamas@cservenak.net>
1 parent cd1712f commit 6ffc902

14 files changed

+424
-135
lines changed

src/main/java/org/codehaus/plexus/components/io/attributes/FileAttributes.java

+109-42
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.nio.file.LinkOption;
2323
import java.nio.file.Path;
2424
import java.nio.file.attribute.FileOwnerAttributeView;
25+
import java.nio.file.attribute.FileTime;
2526
import java.nio.file.attribute.PosixFilePermission;
2627
import java.security.Principal;
2728
import java.util.Collections;
@@ -39,6 +40,10 @@
3940
public class FileAttributes
4041
implements PlexusIoResourceAttributes
4142
{
43+
public static final LinkOption[] FOLLOW_LINK_OPTIONS = new LinkOption[] { };
44+
45+
public static final LinkOption[] NOFOLLOW_LINK_OPTIONS = new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
46+
4247
@Nullable
4348
private final Integer groupId;
4449

@@ -52,69 +57,101 @@ public class FileAttributes
5257

5358
private final boolean symbolicLink;
5459

60+
private final boolean regularFile;
61+
62+
private final boolean directory;
63+
64+
private final boolean other;
65+
5566
private final int octalMode;
5667

5768
private final Set<PosixFilePermission> permissions;
5869

70+
private final long size;
71+
72+
private final FileTime lastModifiedTime;
73+
74+
/**
75+
* @deprecated use {@link #FileAttributes(File)} and remove the unused userCache and groupCache parameters
76+
*/
77+
@Deprecated
5978
public FileAttributes( @Nonnull File file, @Nonnull Map<Integer, String> userCache,
6079
@Nonnull Map<Integer, String> groupCache )
6180
throws IOException
6281
{
82+
this( file );
83+
}
84+
85+
public FileAttributes( @Nonnull File file )
86+
throws IOException
87+
{
88+
this( file, false );
89+
}
6390

91+
public FileAttributes( @Nonnull File file, boolean followLinks )
92+
throws IOException
93+
{
94+
LinkOption[] options = followLinks ? FOLLOW_LINK_OPTIONS : NOFOLLOW_LINK_OPTIONS;
6495
Path path = file.toPath();
65-
if ( AttributeUtils.isUnix( path ) )
96+
Set<String> views = path.getFileSystem().supportedFileAttributeViews();
97+
String names;
98+
if ( views.contains( "unix" ) )
99+
{
100+
names = "unix:*";
101+
}
102+
else if ( views.contains( "posix" ) )
66103
{
67-
Map<String, Object> attrs = Files.readAttributes( path, "unix:permissions,gid,uid,isSymbolicLink,mode", LinkOption.NOFOLLOW_LINKS );
68-
this.permissions = (Set<PosixFilePermission>) attrs.get( "permissions" );
69-
70-
groupId = (Integer) attrs.get( "gid" );
71-
72-
String groupName = groupCache.get( groupId );
73-
if ( groupName != null )
74-
{
75-
this.groupName = groupName;
76-
}
77-
else
78-
{
79-
Object group = Files.getAttribute( path, "unix:group", LinkOption.NOFOLLOW_LINKS );
80-
this.groupName = ( (Principal) group ).getName();
81-
groupCache.put( groupId, this.groupName );
82-
}
83-
userId = (Integer) attrs.get( "uid" );
84-
String userName = userCache.get( userId );
85-
if ( userName != null )
86-
{
87-
this.userName = userName;
88-
}
89-
else
90-
{
91-
Object owner = Files.getAttribute( path, "unix:owner", LinkOption.NOFOLLOW_LINKS );
92-
this.userName = ( (Principal) owner ).getName();
93-
userCache.put( userId, this.userName );
94-
}
95-
octalMode = (Integer) attrs.get( "mode" ) & 0xfff; // Mask off top bits for compatibilty. Maybe check if we
96-
// can skip this
97-
symbolicLink = (Boolean) attrs.get( "isSymbolicLink" );
104+
names = "posix:*";
98105
}
99106
else
100107
{
101-
FileOwnerAttributeView fa = AttributeUtils.getFileOwnershipInfo( file );
102-
this.userName = fa.getOwner().getName();
103-
userId = null;
104-
this.groupName = null;
105-
this.groupId = null;
106-
octalMode = PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE;
107-
permissions = Collections.emptySet();
108-
symbolicLink = Files.isSymbolicLink( path );
108+
names = "basic:*";
109109
}
110+
Map<String, Object> attrs = Files.readAttributes( path, names, options );
111+
if ( !attrs.containsKey( "group" ) && !attrs.containsKey( "owner" ) && views.contains( "owner" ) )
112+
{
113+
Map<String, Object> ownerAttrs = Files.readAttributes( path, "owner:*", options );
114+
Map<String, Object> newAttrs = new HashMap<>( attrs );
115+
newAttrs.putAll( ownerAttrs );
116+
attrs = newAttrs;
117+
}
118+
this.groupId = (Integer) attrs.get( "gid" );
119+
this.groupName = attrs.containsKey( "group" ) ? ((Principal) attrs.get( "group" ) ).getName() : null;
120+
this.userId = (Integer) attrs.get( "uid" );
121+
this.userName = attrs.containsKey( "owner" ) ? ((Principal) attrs.get( "owner" ) ).getName() : null;
122+
this.symbolicLink = (Boolean) attrs.get( "isSymbolicLink" );
123+
this.regularFile = (Boolean) attrs.get( "isRegularFile" );
124+
this.directory = (Boolean) attrs.get( "isDirectory" );
125+
this.other = (Boolean) attrs.get( "isOther" );
126+
this.octalMode = attrs.containsKey( "mode" ) ? (Integer) attrs.get( "mode" ) & 0xfff : PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE;
127+
this.permissions = attrs.containsKey( "permissions" ) ? (Set<PosixFilePermission>) attrs.get( "permissions" ) : Collections.<PosixFilePermission>emptySet();
128+
this.size = (Long) attrs.get( "size" );
129+
this.lastModifiedTime = (FileTime) attrs.get( "lastModifiedTime" );
130+
}
110131

132+
public FileAttributes( @Nullable Integer userId, String userName, @Nullable Integer groupId, @Nullable String groupName,
133+
int octalMode, boolean symbolicLink, boolean regularFile, boolean directory, boolean other,
134+
Set<PosixFilePermission> permissions, long size, FileTime lastModifiedTime )
135+
{
136+
this.userId = userId;
137+
this.userName = userName;
138+
this.groupId = groupId;
139+
this.groupName = groupName;
140+
this.octalMode = octalMode;
141+
this.symbolicLink = symbolicLink;
142+
this.regularFile = regularFile;
143+
this.directory = directory;
144+
this.other = other;
145+
this.permissions = permissions;
146+
this.size = size;
147+
this.lastModifiedTime = lastModifiedTime;
111148
}
112149

113150
public static @Nonnull
114151
PlexusIoResourceAttributes uncached( @Nonnull File file )
115152
throws IOException
116153
{
117-
return new FileAttributes( file, new HashMap<Integer, String>(), new HashMap<Integer, String>() );
154+
return new FileAttributes( file );
118155
}
119156

120157
@Nullable
@@ -290,4 +327,34 @@ public boolean isSymbolicLink()
290327
{
291328
return symbolicLink;
292329
}
293-
}
330+
331+
public boolean isRegularFile()
332+
{
333+
return regularFile;
334+
}
335+
336+
public boolean isDirectory()
337+
{
338+
return directory;
339+
}
340+
341+
public boolean isOther()
342+
{
343+
return other;
344+
}
345+
346+
public long getSize()
347+
{
348+
return size;
349+
}
350+
351+
public FileTime getLastModifiedTime()
352+
{
353+
return lastModifiedTime;
354+
}
355+
356+
protected Set<PosixFilePermission> getPermissions()
357+
{
358+
return permissions;
359+
}
360+
}

src/main/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributeUtils.java

+76-64
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import java.io.File;
2323
import java.io.IOException;
2424
import java.util.Collections;
25-
import java.util.HashMap;
2625
import java.util.LinkedHashMap;
2726
import java.util.List;
2827
import java.util.Map;
@@ -43,69 +42,71 @@ public static PlexusIoResourceAttributes mergeAttributes( PlexusIoResourceAttrib
4342
{
4443
return base;
4544
}
46-
SimpleResourceAttributes result;
4745
if ( base == null )
4846
{
49-
result = new SimpleResourceAttributes();
47+
return new SimpleResourceAttributes(
48+
override.getUserId() != null && override.getUserId() != -1
49+
? override.getUserId()
50+
: def != null && def.getUserId() != null && def.getUserId() != -1
51+
? def.getUserId()
52+
: null,
53+
override.getUserName() != null
54+
? override.getUserName()
55+
: def != null
56+
? def.getUserName()
57+
: null,
58+
override.getGroupId() != null && override.getGroupId() != -1
59+
? override.getGroupId()
60+
: def != null && def.getGroupId() != null && def.getGroupId() != -1
61+
? def.getGroupId()
62+
: null,
63+
override.getGroupName() != null
64+
? override.getGroupName()
65+
: def != null
66+
? def.getGroupName()
67+
: null,
68+
override.getOctalMode() );
5069
}
5170
else
5271
{
53-
result = new SimpleResourceAttributes( base.getUserId(), base.getUserName(), base.getGroupId(),
54-
base.getGroupName(), base.getOctalMode() );
55-
result.setSymbolicLink( base.isSymbolicLink() );
72+
Integer uid = override.getUserId() != null && override.getUserId() != -1
73+
? override.getUserId()
74+
: base.getUserId() != null && base.getUserId() != -1
75+
? base.getUserId()
76+
: def.getUserId() != null && def.getUserId() != -1
77+
? def.getUserId()
78+
: null;
79+
String uname = override.getUserName() != null
80+
? override.getUserName()
81+
: base.getUserName() != null
82+
? base.getUserName()
83+
: def.getUserName();
84+
Integer gid = override.getGroupId() != null && override.getGroupId() != -1
85+
? override.getGroupId()
86+
: base.getGroupId() != null && base.getGroupId() != -1
87+
? base.getGroupId()
88+
: def.getGroupId() != null && def.getGroupId() != -1
89+
? def.getGroupId()
90+
: null;
91+
String gname = override.getGroupName() != null
92+
? override.getGroupName()
93+
: base.getGroupName() != null
94+
? base.getGroupName()
95+
: def.getGroupName();
96+
int mode = override.getOctalMode() > 0
97+
? override.getOctalMode()
98+
: base.getOctalMode() >= 0
99+
? base.getOctalMode()
100+
: def.getOctalMode();
101+
if ( base instanceof FileAttributes )
102+
{
103+
return new UserGroupModeFileAttributes( uid, uname, gid, gname, mode, (FileAttributes) base );
104+
}
105+
else
106+
{
107+
return new SimpleResourceAttributes( uid, uname, gid, gname, mode, base.isSymbolicLink() );
108+
}
56109
}
57-
58-
if ( override.getGroupId() != null && override.getGroupId() != -1 )
59-
{
60-
result.setGroupId( override.getGroupId() );
61-
}
62-
63-
if ( def != null && def.getGroupId() >= 0 && ( result.getGroupId() == null || result.getGroupId() < 0 ) )
64-
{
65-
result.setGroupId( def.getGroupId() );
66-
}
67-
68-
if ( override.getGroupName() != null )
69-
{
70-
result.setGroupName( override.getGroupName() );
71-
}
72-
73-
if ( def != null && result.getGroupName() == null )
74-
{
75-
result.setGroupName( def.getGroupName() );
76-
}
77-
78-
if ( override.getUserId() != null && override.getUserId() != -1 )
79-
{
80-
result.setUserId( override.getUserId() );
81-
}
82-
83-
if ( def != null && def.getUserId() >= 0 && ( result.getUserId() == null || result.getUserId() < 0 ) )
84-
{
85-
result.setUserId( def.getUserId() );
86-
}
87-
88-
if ( override.getUserName() != null )
89-
{
90-
result.setUserName( override.getUserName() );
91-
}
92-
93-
if ( def != null && result.getUserName() == null )
94-
{
95-
result.setUserName( def.getUserName() );
96-
}
97-
98-
if ( override.getOctalMode() > 0 )
99-
{
100-
result.setOctalMode( override.getOctalMode() );
101-
}
102-
103-
if ( def != null && result.getOctalMode() < 0 )
104-
{
105-
result.setOctalMode( def.getOctalMode() );
106-
}
107-
108-
return result;
109110
}
110111

111112
public static boolean isGroupExecutableInOctal( int mode )
@@ -158,11 +159,16 @@ public static boolean isOctalModeEnabled( int mode, int targetMode )
158159
return ( mode & targetMode ) != 0;
159160
}
160161

161-
@SuppressWarnings( { "UnusedDeclaration" } )
162162
public static PlexusIoResourceAttributes getFileAttributes( File file )
163163
throws IOException
164164
{
165-
Map<String, PlexusIoResourceAttributes> byPath = getFileAttributesByPath( file, false );
165+
return getFileAttributes( file, false );
166+
}
167+
168+
public static PlexusIoResourceAttributes getFileAttributes( File file, boolean followLinks )
169+
throws IOException
170+
{
171+
Map<String, PlexusIoResourceAttributes> byPath = getFileAttributesByPath( file, false, followLinks );
166172
final PlexusIoResourceAttributes o = byPath.get( file.getAbsolutePath() );
167173
if ( o == null )
168174
{
@@ -184,8 +190,15 @@ Map<String, PlexusIoResourceAttributes> getFileAttributesByPath( @Nonnull File d
184190
boolean recursive )
185191
throws IOException
186192
{
187-
Map<Integer, String> userCache = new HashMap<>();
188-
Map<Integer, String> groupCache = new HashMap<>();
193+
return getFileAttributesByPath( dir, recursive, false );
194+
}
195+
196+
public static @Nonnull
197+
Map<String, PlexusIoResourceAttributes> getFileAttributesByPath( @Nonnull File dir,
198+
boolean recursive,
199+
boolean followLinks )
200+
throws IOException
201+
{
189202
final List<String> fileAndDirectoryNames;
190203
if ( recursive && dir.isDirectory() )
191204
{
@@ -200,8 +213,7 @@ Map<String, PlexusIoResourceAttributes> getFileAttributesByPath( @Nonnull File d
200213

201214
for ( String fileAndDirectoryName : fileAndDirectoryNames )
202215
{
203-
attributesByPath.put( fileAndDirectoryName,
204-
new FileAttributes( new File( fileAndDirectoryName ), userCache, groupCache ) );
216+
attributesByPath.put( fileAndDirectoryName, new FileAttributes( new File( fileAndDirectoryName ), followLinks ) );
205217
}
206218
return attributesByPath;
207219
}

src/main/java/org/codehaus/plexus/components/io/attributes/PlexusIoResourceAttributes.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public interface PlexusIoResourceAttributes
7474
*/
7575
int getOctalMode();
7676

77-
@Nonnull
77+
//@Nonnull
7878
//String getOctalModeString();
7979

8080
/**

0 commit comments

Comments
 (0)