Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix symbolic links to directories are not recognized as directories #75

Merged
merged 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.FileOwnerAttributeView;
import java.nio.file.attribute.FileTime;
import java.nio.file.attribute.PosixFilePermission;
import java.security.Principal;
import java.util.Collections;
Expand Down Expand Up @@ -53,77 +52,62 @@ public class FileAttributes

private final boolean symbolicLink;

private final boolean regularFile;

private final boolean directory;

private final boolean other;

private final int octalMode;

private final Set<PosixFilePermission> permissions;

private final long size;

private final FileTime lastModifiedTime;

public FileAttributes( @Nonnull File file, @Nonnull Map<Integer, String> userCache,
@Nonnull Map<Integer, String> groupCache )
throws IOException
{

Path path = file.toPath();
Set<String> views = path.getFileSystem().supportedFileAttributeViews();
String names;
if ( views.contains( "unix" ) )
{
names = "unix:*";
}
else if ( views.contains( "posix" ) )
if ( AttributeUtils.isUnix( path ) )
{
names = "posix:*";
Map<String, Object> attrs = Files.readAttributes( path, "unix:permissions,gid,uid,isSymbolicLink,mode", LinkOption.NOFOLLOW_LINKS );
this.permissions = (Set<PosixFilePermission>) attrs.get( "permissions" );

groupId = (Integer) attrs.get( "gid" );

String groupName = groupCache.get( groupId );
if ( groupName != null )
{
this.groupName = groupName;
}
else
{
Object group = Files.getAttribute( path, "unix:group", LinkOption.NOFOLLOW_LINKS );
this.groupName = ( (Principal) group ).getName();
groupCache.put( groupId, this.groupName );
}
userId = (Integer) attrs.get( "uid" );
String userName = userCache.get( userId );
if ( userName != null )
{
this.userName = userName;
}
else
{
Object owner = Files.getAttribute( path, "unix:owner", LinkOption.NOFOLLOW_LINKS );
this.userName = ( (Principal) owner ).getName();
userCache.put( userId, this.userName );
}
octalMode = (Integer) attrs.get( "mode" ) & 0xfff; // Mask off top bits for compatibilty. Maybe check if we
// can skip this
symbolicLink = (Boolean) attrs.get( "isSymbolicLink" );
}
else
{
names = "basic:*";
FileOwnerAttributeView fa = AttributeUtils.getFileOwnershipInfo( file );
this.userName = fa.getOwner().getName();
userId = null;
this.groupName = null;
this.groupId = null;
octalMode = PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE;
permissions = Collections.emptySet();
symbolicLink = Files.isSymbolicLink( path );
}
Map<String, Object> attrs = Files.readAttributes( path, names, LinkOption.NOFOLLOW_LINKS);
if ( !attrs.containsKey( "group" ) && !attrs.containsKey( "owner" ) && views.contains( "owner" ) )
{
Map<String, Object> ownerAttrs = Files.readAttributes( path, "owner:*", LinkOption.NOFOLLOW_LINKS);
Map<String, Object> newAttrs = new HashMap<>( attrs );
newAttrs.putAll( ownerAttrs );
attrs = newAttrs;
}
this.groupId = (Integer) attrs.get( "gid" );
this.groupName = attrs.containsKey( "group" ) ? ((Principal) attrs.get( "group" ) ).getName() : null;
this.userId = (Integer) attrs.get( "uid" );
this.userName = attrs.containsKey( "owner" ) ? ((Principal) attrs.get( "owner" ) ).getName() : null;
this.symbolicLink = (Boolean) attrs.get( "isSymbolicLink" );
this.regularFile = (Boolean) attrs.get( "isRegularFile" );
this.directory = (Boolean) attrs.get( "isDirectory" );
this.other = (Boolean) attrs.get( "isOther" );
this.octalMode = attrs.containsKey( "mode" ) ? (Integer) attrs.get( "mode" ) & 0xfff : PlexusIoResourceAttributes.UNKNOWN_OCTAL_MODE;
this.permissions = attrs.containsKey( "permissions" ) ? (Set<PosixFilePermission>) attrs.get( "permissions" ) : Collections.<PosixFilePermission>emptySet();
this.size = (Long) attrs.get( "size" );
this.lastModifiedTime = (FileTime) attrs.get( "lastModifiedTime" );
}

public FileAttributes( @Nullable Integer userId, String userName, @Nullable Integer groupId, @Nullable String groupName,
int octalMode, boolean symbolicLink, boolean regularFile, boolean directory, boolean other,
Set<PosixFilePermission> permissions, long size, FileTime lastModifiedTime) {
this.userId = userId;
this.userName = userName;
this.groupId = groupId;
this.groupName = groupName;
this.octalMode = octalMode;
this.symbolicLink = symbolicLink;
this.regularFile = regularFile;
this.directory = directory;
this.other = other;
this.permissions = permissions;
this.size = size;
this.lastModifiedTime = lastModifiedTime;
}

public static @Nonnull
Expand Down Expand Up @@ -306,34 +290,4 @@ public boolean isSymbolicLink()
{
return symbolicLink;
}

public boolean isRegularFile()
{
return regularFile;
}

public boolean isDirectory()
{
return directory;
}

public boolean isOther()
{
return other;
}

public long getSize()
{
return size;
}

public FileTime getLastModifiedTime()
{
return lastModifiedTime;
}

protected Set<PosixFilePermission> getPermissions()
{
return permissions;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,71 +43,69 @@ public static PlexusIoResourceAttributes mergeAttributes( PlexusIoResourceAttrib
{
return base;
}
SimpleResourceAttributes result;
if ( base == null )
{
return new SimpleResourceAttributes(
override.getUserId() != null && override.getUserId() != -1
? override.getUserId()
: def != null && def.getUserId() != null && def.getUserId() != -1
? def.getUserId()
: null,
override.getUserName() != null
? override.getUserName()
: def != null
? def.getUserName()
: null,
override.getGroupId() != null && override.getGroupId() != -1
? override.getGroupId()
: def != null && def.getGroupId() != null && def.getGroupId() != -1
? def.getGroupId()
: null,
override.getGroupName() != null
? override.getGroupName()
: def != null
? def.getGroupName()
: null,
override.getOctalMode() );
result = new SimpleResourceAttributes();
}
else
{
Integer uid = override.getUserId() != null && override.getUserId() != -1
? override.getUserId()
: base.getUserId() != null && base.getUserId() != -1
? base.getUserId()
: def.getUserId() != null && def.getUserId() != -1
? def.getUserId()
: null;
String uname = override.getUserName() != null
? override.getUserName()
: base.getUserName() != null
? base.getUserName()
: def.getUserName();
Integer gid = override.getGroupId() != null && override.getGroupId() != -1
? override.getGroupId()
: base.getGroupId() != null && base.getGroupId() != -1
? base.getGroupId()
: def.getGroupId() != null && def.getGroupId() != -1
? def.getGroupId()
: null;
String gname = override.getGroupName() != null
? override.getGroupName()
: base.getGroupName() != null
? base.getGroupName()
: def.getGroupName();
int mode = override.getOctalMode() > 0
? override.getOctalMode()
: base.getOctalMode() >= 0
? base.getOctalMode()
: def.getOctalMode();
if ( base instanceof FileAttributes )
{
return new UserGroupModeFileAttributes( uid, uname, gid, gname, mode, (FileAttributes) base );
}
else
{
return new SimpleResourceAttributes( uid, uname, gid, gname, mode, base.isSymbolicLink() );
}
result = new SimpleResourceAttributes( base.getUserId(), base.getUserName(), base.getGroupId(),
base.getGroupName(), base.getOctalMode() );
result.setSymbolicLink( base.isSymbolicLink() );
}

if ( override.getGroupId() != null && override.getGroupId() != -1 )
{
result.setGroupId( override.getGroupId() );
}

if ( def != null && def.getGroupId() >= 0 && ( result.getGroupId() == null || result.getGroupId() < 0 ) )
{
result.setGroupId( def.getGroupId() );
}

if ( override.getGroupName() != null )
{
result.setGroupName( override.getGroupName() );
}

if ( def != null && result.getGroupName() == null )
{
result.setGroupName( def.getGroupName() );
}

if ( override.getUserId() != null && override.getUserId() != -1 )
{
result.setUserId( override.getUserId() );
}

if ( def != null && def.getUserId() >= 0 && ( result.getUserId() == null || result.getUserId() < 0 ) )
{
result.setUserId( def.getUserId() );
}

if ( override.getUserName() != null )
{
result.setUserName( override.getUserName() );
}

if ( def != null && result.getUserName() == null )
{
result.setUserName( def.getUserName() );
}

if ( override.getOctalMode() > 0 )
{
result.setOctalMode( override.getOctalMode() );
}

if ( def != null && result.getOctalMode() < 0 )
{
result.setOctalMode( def.getOctalMode() );
}

return result;
}

public static boolean isGroupExecutableInOctal( int mode )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ public SimpleResourceAttributes( Integer uid, String userName, Integer gid, Stri
this.mode = mode;
}

public SimpleResourceAttributes( Integer uid, String userName, Integer gid, String groupName, int mode, boolean isSymbolicLink )
{
this.uid = uid;
this.userName = userName;
this.gid = gid;
this.groupName = groupName;
this.mode = mode;
this.isSymbolicLink = isSymbolicLink;
}

public static PlexusIoResourceAttributes lastResortDummyAttributesForBrokenOS()
{
return new SimpleResourceAttributes();
Expand Down

This file was deleted.

Loading