-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from all commits
d19f770
274d04b
d7bbc79
89339a3
46245e4
56f13b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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; | ||||||
|
@@ -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 }; | ||||||
|
||||||
@Nullable | ||||||
private final Integer groupId; | ||||||
|
||||||
|
@@ -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, | ||||||
gnodet marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
(at-me in a reply with Was this a good recommendation? |
||||||
@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" ); | ||||||
michael-o marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
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 | ||||||
|
@@ -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; | ||||||
} | ||||||
} |
There was a problem hiding this comment.
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
orignore
)Was this a good recommendation?
[ 🙁 Not relevant ] - [ 😕 Won't fix ] - [ 😑 Not critical, will fix ] - [ 🙂 Critical, will fix ] - [ 😊 Critical, fixing now ]