Skip to content

Commit bf86860

Browse files
committed
Revert "Speed improvements (#33)"
The commit intorduces regression. Symlinks to directories are no longer considered directories. See #71 This reverts commit 5b79b54.
1 parent ddfdd3d commit bf86860

File tree

7 files changed

+100
-231
lines changed

7 files changed

+100
-231
lines changed

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

+40-86
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
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;
2625
import java.nio.file.attribute.PosixFilePermission;
2726
import java.security.Principal;
2827
import java.util.Collections;
@@ -53,77 +52,62 @@ public class FileAttributes
5352

5453
private final boolean symbolicLink;
5554

56-
private final boolean regularFile;
57-
58-
private final boolean directory;
59-
60-
private final boolean other;
61-
6255
private final int octalMode;
6356

6457
private final Set<PosixFilePermission> permissions;
6558

66-
private final long size;
67-
68-
private final FileTime lastModifiedTime;
69-
7059
public FileAttributes( @Nonnull File file, @Nonnull Map<Integer, String> userCache,
7160
@Nonnull Map<Integer, String> groupCache )
7261
throws IOException
7362
{
7463

7564
Path path = file.toPath();
76-
Set<String> views = path.getFileSystem().supportedFileAttributeViews();
77-
String names;
78-
if ( views.contains( "unix" ) )
79-
{
80-
names = "unix:*";
81-
}
82-
else if ( views.contains( "posix" ) )
65+
if ( AttributeUtils.isUnix( path ) )
8366
{
84-
names = "posix:*";
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" );
8598
}
8699
else
87100
{
88-
names = "basic:*";
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 );
89109
}
90-
Map<String, Object> attrs = Files.readAttributes( path, names, LinkOption.NOFOLLOW_LINKS);
91-
if ( !attrs.containsKey( "group" ) && !attrs.containsKey( "owner" ) && views.contains( "owner" ) )
92-
{
93-
Map<String, Object> ownerAttrs = Files.readAttributes( path, "owner:*", LinkOption.NOFOLLOW_LINKS);
94-
Map<String, Object> newAttrs = new HashMap<>( attrs );
95-
newAttrs.putAll( ownerAttrs );
96-
attrs = newAttrs;
97-
}
98-
this.groupId = (Integer) attrs.get( "gid" );
99-
this.groupName = attrs.containsKey( "group" ) ? ((Principal) attrs.get( "group" ) ).getName() : null;
100-
this.userId = (Integer) attrs.get( "uid" );
101-
this.userName = attrs.containsKey( "owner" ) ? ((Principal) attrs.get( "owner" ) ).getName() : null;
102-
this.symbolicLink = (Boolean) attrs.get( "isSymbolicLink" );
103-
this.regularFile = (Boolean) attrs.get( "isRegularFile" );
104-
this.directory = (Boolean) attrs.get( "isDirectory" );
105-
this.other = (Boolean) attrs.get( "isOther" );
106-
this.octalMode = attrs.containsKey( "mode" ) ? (Integer) attrs.get( "mode" ) & 0xfff : PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE;
107-
this.permissions = attrs.containsKey( "permissions" ) ? (Set<PosixFilePermission>) attrs.get( "permissions" ) : Collections.<PosixFilePermission>emptySet();
108-
this.size = (Long) attrs.get( "size" );
109-
this.lastModifiedTime = (FileTime) attrs.get( "lastModifiedTime" );
110-
}
111110

112-
public FileAttributes( @Nullable Integer userId, String userName, @Nullable Integer groupId, @Nullable String groupName,
113-
int octalMode, boolean symbolicLink, boolean regularFile, boolean directory, boolean other,
114-
Set<PosixFilePermission> permissions, long size, FileTime lastModifiedTime) {
115-
this.userId = userId;
116-
this.userName = userName;
117-
this.groupId = groupId;
118-
this.groupName = groupName;
119-
this.octalMode = octalMode;
120-
this.symbolicLink = symbolicLink;
121-
this.regularFile = regularFile;
122-
this.directory = directory;
123-
this.other = other;
124-
this.permissions = permissions;
125-
this.size = size;
126-
this.lastModifiedTime = lastModifiedTime;
127111
}
128112

129113
public static @Nonnull
@@ -306,34 +290,4 @@ public boolean isSymbolicLink()
306290
{
307291
return symbolicLink;
308292
}
309-
310-
public boolean isRegularFile()
311-
{
312-
return regularFile;
313-
}
314-
315-
public boolean isDirectory()
316-
{
317-
return directory;
318-
}
319-
320-
public boolean isOther()
321-
{
322-
return other;
323-
}
324-
325-
public long getSize()
326-
{
327-
return size;
328-
}
329-
330-
public FileTime getLastModifiedTime()
331-
{
332-
return lastModifiedTime;
333-
}
334-
335-
protected Set<PosixFilePermission> getPermissions()
336-
{
337-
return permissions;
338-
}
339293
}

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

+57-59
Original file line numberDiff line numberDiff line change
@@ -43,71 +43,69 @@ public static PlexusIoResourceAttributes mergeAttributes( PlexusIoResourceAttrib
4343
{
4444
return base;
4545
}
46+
SimpleResourceAttributes result;
4647
if ( base == null )
4748
{
48-
return new SimpleResourceAttributes(
49-
override.getUserId() != null && override.getUserId() != -1
50-
? override.getUserId()
51-
: def != null && def.getUserId() != null && def.getUserId() != -1
52-
? def.getUserId()
53-
: null,
54-
override.getUserName() != null
55-
? override.getUserName()
56-
: def != null
57-
? def.getUserName()
58-
: null,
59-
override.getGroupId() != null && override.getGroupId() != -1
60-
? override.getGroupId()
61-
: def != null && def.getGroupId() != null && def.getGroupId() != -1
62-
? def.getGroupId()
63-
: null,
64-
override.getGroupName() != null
65-
? override.getGroupName()
66-
: def != null
67-
? def.getGroupName()
68-
: null,
69-
override.getOctalMode() );
49+
result = new SimpleResourceAttributes();
7050
}
7151
else
7252
{
73-
Integer uid = override.getUserId() != null && override.getUserId() != -1
74-
? override.getUserId()
75-
: base.getUserId() != null && base.getUserId() != -1
76-
? base.getUserId()
77-
: def.getUserId() != null && def.getUserId() != -1
78-
? def.getUserId()
79-
: null;
80-
String uname = override.getUserName() != null
81-
? override.getUserName()
82-
: base.getUserName() != null
83-
? base.getUserName()
84-
: def.getUserName();
85-
Integer gid = override.getGroupId() != null && override.getGroupId() != -1
86-
? override.getGroupId()
87-
: base.getGroupId() != null && base.getGroupId() != -1
88-
? base.getGroupId()
89-
: def.getGroupId() != null && def.getGroupId() != -1
90-
? def.getGroupId()
91-
: null;
92-
String gname = override.getGroupName() != null
93-
? override.getGroupName()
94-
: base.getGroupName() != null
95-
? base.getGroupName()
96-
: def.getGroupName();
97-
int mode = override.getOctalMode() > 0
98-
? override.getOctalMode()
99-
: base.getOctalMode() >= 0
100-
? base.getOctalMode()
101-
: def.getOctalMode();
102-
if ( base instanceof FileAttributes )
103-
{
104-
return new UserGroupModeFileAttributes( uid, uname, gid, gname, mode, (FileAttributes) base );
105-
}
106-
else
107-
{
108-
return new SimpleResourceAttributes( uid, uname, gid, gname, mode, base.isSymbolicLink() );
109-
}
53+
result = new SimpleResourceAttributes( base.getUserId(), base.getUserName(), base.getGroupId(),
54+
base.getGroupName(), base.getOctalMode() );
55+
result.setSymbolicLink( base.isSymbolicLink() );
11056
}
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;
111109
}
112110

113111
public static boolean isGroupExecutableInOctal( int mode )

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

-10
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,6 @@ public SimpleResourceAttributes( Integer uid, String userName, Integer gid, Stri
4646
this.mode = mode;
4747
}
4848

49-
public SimpleResourceAttributes( Integer uid, String userName, Integer gid, String groupName, int mode, boolean isSymbolicLink )
50-
{
51-
this.uid = uid;
52-
this.userName = userName;
53-
this.gid = gid;
54-
this.groupName = groupName;
55-
this.mode = mode;
56-
this.isSymbolicLink = isSymbolicLink;
57-
}
58-
5949
public static PlexusIoResourceAttributes lastResortDummyAttributesForBrokenOS()
6050
{
6151
return new SimpleResourceAttributes();

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

-47
This file was deleted.

0 commit comments

Comments
 (0)