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

Restore speed improvements #79

Merged
merged 6 commits into from
Jun 23, 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,6 +22,7 @@
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 All @@ -39,6 +40,10 @@
public class FileAttributes
implements PlexusIoResourceAttributes
{
public static final LinkOption[] FOLLOW_LINK_OPTIONS = new LinkOption[] { };

public static final LinkOption[] NOFOLLOW_LINK_OPTIONS = new LinkOption[] { LinkOption.NOFOLLOW_LINKS };
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MutablePublicArray: Non-empty arrays are mutable, so this public static final array is not a constant and can be modified by clients of this class. Prefer an ImmutableList, or provide an accessor method that returns a defensive copy.

(at-me in a reply with help or ignore)


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]


@Nullable
private final Integer groupId;

Expand All @@ -52,69 +57,101 @@ 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;

/**
* @deprecated use {@link #FileAttributes(File)} and remove the unused userCache and groupCache parameters
*/
@Deprecated
public FileAttributes( @Nonnull File file, @Nonnull Map<Integer, String> userCache,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

InlineMeSuggester: This deprecated API looks inlineable. If you'd like the body of the API to be inlined to its callers, please annotate it with @InlineMe.

Suggested change
public FileAttributes( @Nonnull File file, @Nonnull Map<Integer, String> userCache,
@InlineMe(replacement = "this(file)")

(at-me in a reply with help or ignore)


Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]

@Nonnull Map<Integer, String> groupCache )
throws IOException
{
this( file );
}

public FileAttributes( @Nonnull File file )
throws IOException
{
this( file, false );
}

public FileAttributes( @Nonnull File file, boolean followLinks )
throws IOException
{
LinkOption[] options = followLinks ? FOLLOW_LINK_OPTIONS : NOFOLLOW_LINK_OPTIONS;
Path path = file.toPath();
if ( AttributeUtils.isUnix( path ) )
Set<String> views = path.getFileSystem().supportedFileAttributeViews();
String names;
if ( views.contains( "unix" ) )
{
names = "unix:*";
}
else if ( views.contains( "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" );
names = "posix:*";
}
else
{
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 );
names = "basic:*";
}
Map<String, Object> attrs = Files.readAttributes( path, names, options );
if ( !attrs.containsKey( "group" ) && !attrs.containsKey( "owner" ) && views.contains( "owner" ) )
{
Map<String, Object> ownerAttrs = Files.readAttributes( path, "owner:*", options );
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
PlexusIoResourceAttributes uncached( @Nonnull File file )
throws IOException
{
return new FileAttributes( file, new HashMap<Integer, String>(), new HashMap<Integer, String>() );
return new FileAttributes( file );
}

@Nullable
Expand Down Expand Up @@ -290,4 +327,34 @@ 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 @@ -22,7 +22,6 @@
import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -43,69 +42,71 @@ public static PlexusIoResourceAttributes mergeAttributes( PlexusIoResourceAttrib
{
return base;
}
SimpleResourceAttributes result;
if ( base == null )
{
result = new SimpleResourceAttributes();
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() );
}
else
{
result = new SimpleResourceAttributes( base.getUserId(), base.getUserName(), base.getGroupId(),
base.getGroupName(), base.getOctalMode() );
result.setSymbolicLink( base.isSymbolicLink() );
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() );
}
}

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 Expand Up @@ -158,11 +159,16 @@ public static boolean isOctalModeEnabled( int mode, int targetMode )
return ( mode & targetMode ) != 0;
}

@SuppressWarnings( { "UnusedDeclaration" } )
public static PlexusIoResourceAttributes getFileAttributes( File file )
throws IOException
{
Map<String, PlexusIoResourceAttributes> byPath = getFileAttributesByPath( file, false );
return getFileAttributes( file, false );
}

public static PlexusIoResourceAttributes getFileAttributes( File file, boolean followLinks )
throws IOException
{
Map<String, PlexusIoResourceAttributes> byPath = getFileAttributesByPath( file, false, followLinks );
final PlexusIoResourceAttributes o = byPath.get( file.getAbsolutePath() );
if ( o == null )
{
Expand All @@ -184,8 +190,15 @@ Map<String, PlexusIoResourceAttributes> getFileAttributesByPath( @Nonnull File d
boolean recursive )
throws IOException
{
Map<Integer, String> userCache = new HashMap<>();
Map<Integer, String> groupCache = new HashMap<>();
return getFileAttributesByPath( dir, recursive, false );
}

public static @Nonnull
Map<String, PlexusIoResourceAttributes> getFileAttributesByPath( @Nonnull File dir,
boolean recursive,
boolean followLinks )
throws IOException
{
final List<String> fileAndDirectoryNames;
if ( recursive && dir.isDirectory() )
{
Expand All @@ -200,8 +213,7 @@ Map<String, PlexusIoResourceAttributes> getFileAttributesByPath( @Nonnull File d

for ( String fileAndDirectoryName : fileAndDirectoryNames )
{
attributesByPath.put( fileAndDirectoryName,
new FileAttributes( new File( fileAndDirectoryName ), userCache, groupCache ) );
attributesByPath.put( fileAndDirectoryName, new FileAttributes( new File( fileAndDirectoryName ), followLinks ) );
}
return attributesByPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public interface PlexusIoResourceAttributes
*/
int getOctalMode();

@Nonnull
//@Nonnull
//String getOctalModeString();

/**
Expand Down
Loading