22
22
import java .nio .file .LinkOption ;
23
23
import java .nio .file .Path ;
24
24
import java .nio .file .attribute .FileOwnerAttributeView ;
25
+ import java .nio .file .attribute .FileTime ;
25
26
import java .nio .file .attribute .PosixFilePermission ;
26
27
import java .security .Principal ;
27
28
import java .util .Collections ;
39
40
public class FileAttributes
40
41
implements PlexusIoResourceAttributes
41
42
{
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
+
42
47
@ Nullable
43
48
private final Integer groupId ;
44
49
@@ -52,69 +57,101 @@ public class FileAttributes
52
57
53
58
private final boolean symbolicLink ;
54
59
60
+ private final boolean regularFile ;
61
+
62
+ private final boolean directory ;
63
+
64
+ private final boolean other ;
65
+
55
66
private final int octalMode ;
56
67
57
68
private final Set <PosixFilePermission > permissions ;
58
69
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
59
78
public FileAttributes ( @ Nonnull File file , @ Nonnull Map <Integer , String > userCache ,
60
79
@ Nonnull Map <Integer , String > groupCache )
61
80
throws IOException
62
81
{
82
+ this ( file );
83
+ }
84
+
85
+ public FileAttributes ( @ Nonnull File file )
86
+ throws IOException
87
+ {
88
+ this ( file , false );
89
+ }
63
90
91
+ public FileAttributes ( @ Nonnull File file , boolean followLinks )
92
+ throws IOException
93
+ {
94
+ LinkOption [] options = followLinks ? FOLLOW_LINK_OPTIONS : NOFOLLOW_LINK_OPTIONS ;
64
95
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" ) )
66
103
{
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:*" ;
98
105
}
99
106
else
100
107
{
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:*" ;
109
109
}
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
+ }
110
131
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 ;
111
148
}
112
149
113
150
public static @ Nonnull
114
151
PlexusIoResourceAttributes uncached ( @ Nonnull File file )
115
152
throws IOException
116
153
{
117
- return new FileAttributes ( file , new HashMap < Integer , String >(), new HashMap < Integer , String >() );
154
+ return new FileAttributes ( file );
118
155
}
119
156
120
157
@ Nullable
@@ -290,4 +327,34 @@ public boolean isSymbolicLink()
290
327
{
291
328
return symbolicLink ;
292
329
}
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