2222import java .nio .file .LinkOption ;
2323import java .nio .file .Path ;
2424import java .nio .file .attribute .FileOwnerAttributeView ;
25+ import java .nio .file .attribute .FileTime ;
2526import java .nio .file .attribute .PosixFilePermission ;
2627import java .security .Principal ;
2728import java .util .Collections ;
3940public 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+ }
0 commit comments