|
1 /* |
|
2 * Copyright 2007-2009 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Sun designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Sun in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
22 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
23 * have any questions. |
|
24 */ |
|
25 |
|
26 package java.nio.file.attribute; |
|
27 |
|
28 import java.nio.file.*; |
|
29 import java.io.IOException; |
|
30 import java.util.*; |
|
31 import java.util.concurrent.TimeUnit; |
|
32 |
|
33 /** |
|
34 * This class consists exclusively of static methods that operate on or return |
|
35 * the attributes of files or file stores. These methods provide for convenient |
|
36 * use of the {@link AttributeView attribute-views} defined in this package. |
|
37 * |
|
38 * @since 1.7 |
|
39 */ |
|
40 |
|
41 public final class Attributes { |
|
42 private Attributes() { |
|
43 } |
|
44 |
|
45 /** |
|
46 * Splits the given attribute name into the name of an attribute view and |
|
47 * the attribute. If the attribute view is not identified then it assumed |
|
48 * to be "basic". |
|
49 */ |
|
50 private static String[] split(String attribute) { |
|
51 String[] s = new String[2]; |
|
52 int pos = attribute.indexOf(':'); |
|
53 if (pos == -1) { |
|
54 s[0] = "basic"; |
|
55 s[1] = attribute; |
|
56 } else { |
|
57 s[0] = attribute.substring(0, pos++); |
|
58 s[1] = (pos == attribute.length()) ? "" : attribute.substring(pos); |
|
59 } |
|
60 return s; |
|
61 } |
|
62 |
|
63 /** |
|
64 * Sets the value of a file attribute. |
|
65 * |
|
66 * <p> The {@code attribute} parameter identifies the attribute to be set |
|
67 * and takes the form: |
|
68 * <blockquote> |
|
69 * [<i>view-name</i><b>:</b>]<i>attribute-name</i> |
|
70 * </blockquote> |
|
71 * where square brackets [...] delineate an optional component and the |
|
72 * character {@code ':'} stands for itself. |
|
73 * |
|
74 * <p> <i>view-name</i> is the {@link FileAttributeView#name name} of a {@link |
|
75 * FileAttributeView} that identifies a set of file attributes. If not |
|
76 * specified then it defaults to {@code "basic"}, the name of the file |
|
77 * attribute view that identifies the basic set of file attributes common to |
|
78 * many file systems. <i>attribute-name</i> is the name of the attribute |
|
79 * within the set. |
|
80 * |
|
81 * <p> <b>Usage Example:</b> |
|
82 * Suppose we want to set the DOS "hidden" attribute: |
|
83 * <pre> |
|
84 * Attributes.setAttribute(file, "dos:hidden", true); |
|
85 * </pre> |
|
86 * |
|
87 * @param file |
|
88 * A file reference that locates the file |
|
89 * @param attribute |
|
90 * The attribute to set |
|
91 * @param value |
|
92 * The attribute value |
|
93 * |
|
94 * @throws UnsupportedOperationException |
|
95 * If the attribute view is not available or it does not |
|
96 * support updating the attribute |
|
97 * @throws IllegalArgumentException |
|
98 * If the attribute value is of the correct type but has an |
|
99 * inappropriate value |
|
100 * @throws ClassCastException |
|
101 * If the attribute value is not of the expected type or is a |
|
102 * collection containing elements that are not of the expected |
|
103 * type |
|
104 * @throws IOException |
|
105 * If an I/O error occurs |
|
106 * @throws SecurityException |
|
107 * In the case of the default provider, and a security manager is |
|
108 * installed, its {@link SecurityManager#checkWrite(String) checkWrite} |
|
109 * method denies write access to the file. If this method is invoked |
|
110 * to set security sensitive attributes then the security manager |
|
111 * may be invoked to check for additional permissions. |
|
112 */ |
|
113 public static void setAttribute(FileRef file, String attribute, Object value) |
|
114 throws IOException |
|
115 { |
|
116 String[] s = split(attribute); |
|
117 FileAttributeView view = file.getFileAttributeView(s[0]); |
|
118 if (view == null) |
|
119 throw new UnsupportedOperationException("View '" + s[0] + "' not available"); |
|
120 view.setAttribute(s[1], value); |
|
121 } |
|
122 |
|
123 /** |
|
124 * Reads the value of a file attribute. |
|
125 * |
|
126 * <p> The {@code attribute} parameter identifies the attribute to be read |
|
127 * and takes the form: |
|
128 * <blockquote> |
|
129 * [<i>view-name</i><b>:</b>]<i>attribute-name</i> |
|
130 * </blockquote> |
|
131 * where square brackets [...] delineate an optional component and the |
|
132 * character {@code ':'} stands for itself. |
|
133 * |
|
134 * <p> <i>view-name</i> is the {@link FileAttributeView#name name} of a {@link |
|
135 * FileAttributeView} that identifies a set of file attributes. If not |
|
136 * specified then it defaults to {@code "basic"}, the name of the file |
|
137 * attribute view that identifies the basic set of file attributes common to |
|
138 * many file systems. <i>attribute-name</i> is the name of the attribute. |
|
139 * |
|
140 * <p> The {@code options} array may be used to indicate how symbolic links |
|
141 * are handled for the case that the file is a symbolic link. By default, |
|
142 * symbolic links are followed and the file attribute of the final target |
|
143 * of the link is read. If the option {@link LinkOption#NOFOLLOW_LINKS |
|
144 * NOFOLLOW_LINKS} is present then symbolic links are not followed and so |
|
145 * the method returns the file attribute of the symbolic link. |
|
146 * |
|
147 * <p> <b>Usage Example:</b> |
|
148 * Suppose we require the user ID of the file owner on a system that |
|
149 * supports a "{@code unix}" view: |
|
150 * <pre> |
|
151 * int uid = (Integer)Attributes.getAttribute(file, "unix:uid"); |
|
152 * </pre> |
|
153 * |
|
154 * @param file |
|
155 * A file reference that locates the file |
|
156 * @param attribute |
|
157 * The attribute to read |
|
158 * @param options |
|
159 * Options indicating how symbolic links are handled |
|
160 * |
|
161 * @return The attribute value, or {@code null} if the attribute view |
|
162 * is not available or it does not support reading the attribute |
|
163 * |
|
164 * @throws IOException |
|
165 * If an I/O error occurs |
|
166 * @throws SecurityException |
|
167 * In the case of the default provider, and a security manager is |
|
168 * installed, its {@link SecurityManager#checkRead(String) checkRead} |
|
169 * method denies read access to the file. If this method is invoked |
|
170 * to read security sensitive attributes then the security manager |
|
171 * may be invoked to check for additional permissions. |
|
172 */ |
|
173 public static Object getAttribute(FileRef file, |
|
174 String attribute, |
|
175 LinkOption... options) |
|
176 throws IOException |
|
177 { |
|
178 String[] s = split(attribute); |
|
179 FileAttributeView view = file.getFileAttributeView(s[0], options); |
|
180 if (view != null) |
|
181 return view.getAttribute(s[1]); |
|
182 // view not available |
|
183 return null; |
|
184 } |
|
185 |
|
186 /** |
|
187 * Reads a set of file attributes as a bulk operation. |
|
188 * |
|
189 * <p> The {@code attributes} parameter identifies the attributes to be read |
|
190 * and takes the form: |
|
191 * <blockquote> |
|
192 * [<i>view-name</i><b>:</b>]<i>attribute-list</i> |
|
193 * </blockquote> |
|
194 * where square brackets [...] delineate an optional component and the |
|
195 * character {@code ':'} stands for itself. |
|
196 * |
|
197 * <p> <i>view-name</i> is the {@link FileAttributeView#name name} of a {@link |
|
198 * FileAttributeView} that identifies a set of file attributes. If not |
|
199 * specified then it defaults to {@code "basic"}, the name of the file |
|
200 * attribute view that identifies the basic set of file attributes common to |
|
201 * many file systems. |
|
202 * |
|
203 * <p> The <i>attribute-list</i> component is a comma separated list of |
|
204 * zero or more names of attributes to read. If the list contains the value |
|
205 * {@code "*"} then all attributes are read. Attributes that are not supported |
|
206 * are ignored and will not be present in the returned map. It is |
|
207 * implementation specific if all attributes are read as an atomic operation |
|
208 * with respect to other file system operations. |
|
209 * |
|
210 * <p> The following examples demonstrate possible values for the {@code |
|
211 * attributes} parameter: |
|
212 * |
|
213 * <blockquote> |
|
214 * <table border="0"> |
|
215 * <tr> |
|
216 * <td> {@code "*"} </td> |
|
217 * <td> Read all {@link BasicFileAttributes basic-file-attributes}. </td> |
|
218 * </tr> |
|
219 * <tr> |
|
220 * <td> {@code "size,lastModifiedTime,lastAccessTime"} </td> |
|
221 * <td> Reads the file size, last modified time, and last access time |
|
222 * attributes. </td> |
|
223 * </tr> |
|
224 * <tr> |
|
225 * <td> {@code "posix:*"} </td> |
|
226 * <td> Read all {@link PosixFileAttributes POSIX-file-attributes}.. </td> |
|
227 * </tr> |
|
228 * <tr> |
|
229 * <td> {@code "posix:permissions,owner,size"} </td> |
|
230 * <td> Reads the POSX file permissions, owner, and file size. </td> |
|
231 * </tr> |
|
232 * </table> |
|
233 * </blockquote> |
|
234 * |
|
235 * <p> The {@code options} array may be used to indicate how symbolic links |
|
236 * are handled for the case that the file is a symbolic link. By default, |
|
237 * symbolic links are followed and the file attributes of the final target |
|
238 * of the link are read. If the option {@link LinkOption#NOFOLLOW_LINKS |
|
239 * NOFOLLOW_LINKS} is present then symbolic links are not followed and so |
|
240 * the method returns the file attributes of the symbolic link. |
|
241 * |
|
242 * @param file |
|
243 * A file reference that locates the file |
|
244 * @param attributes |
|
245 * The attributes to read |
|
246 * @param options |
|
247 * Options indicating how symbolic links are handled |
|
248 * |
|
249 * @return A map of the attributes returned; may be empty. The map's keys |
|
250 * are the attribute names, its values are the attribute values |
|
251 * |
|
252 * @throws IOException |
|
253 * If an I/O error occurs |
|
254 * @throws SecurityException |
|
255 * In the case of the default provider, and a security manager is |
|
256 * installed, its {@link SecurityManager#checkRead(String) checkRead} |
|
257 * method denies read access to the file. If this method is invoked |
|
258 * to read security sensitive attributes then the security manager |
|
259 * may be invoke to check for additional permissions. |
|
260 */ |
|
261 public static Map<String,?> readAttributes(FileRef file, |
|
262 String attributes, |
|
263 LinkOption... options) |
|
264 throws IOException |
|
265 { |
|
266 String[] s = split(attributes); |
|
267 FileAttributeView view = file.getFileAttributeView(s[0], options); |
|
268 if (view != null) { |
|
269 // further split attributes into the first and rest. |
|
270 String[] names = s[1].split(","); |
|
271 int rem = names.length-1; |
|
272 String first = names[0]; |
|
273 String[] rest = new String[rem]; |
|
274 if (rem > 0) System.arraycopy(names, 1, rest, 0, rem); |
|
275 |
|
276 return view.readAttributes(first, rest); |
|
277 } |
|
278 // view not available |
|
279 return Collections.emptyMap(); |
|
280 } |
|
281 |
|
282 /** |
|
283 * Reads the basic file attributes of a file. |
|
284 * |
|
285 * <p> The {@code options} array may be used to indicate how symbolic links |
|
286 * are handled for the case that the file is a symbolic link. By default, |
|
287 * symbolic links are followed and the file attributes of the final target |
|
288 * of the link are read. If the option {@link LinkOption#NOFOLLOW_LINKS |
|
289 * NOFOLLOW_LINKS} is present then symbolic links are not followed and so |
|
290 * the method returns the file attributes of the symbolic link. This option |
|
291 * should be used where there is a need to determine if a file is a |
|
292 * symbolic link: |
|
293 * <pre> |
|
294 * boolean isSymbolicLink = Attributes.readBasicFileAttributes(file, NOFOLLOW_LINKS).isSymbolicLink(); |
|
295 * </pre> |
|
296 * |
|
297 * <p> It is implementation specific if all file attributes are read as an |
|
298 * atomic operation with respect to other file system operations. |
|
299 * |
|
300 * @param file |
|
301 * A file reference that locates the file |
|
302 * @param options |
|
303 * Options indicating how symbolic links are handled |
|
304 * |
|
305 * @return The basic file attributes |
|
306 * |
|
307 * @throws IOException |
|
308 * If an I/O error occurs |
|
309 * @throws SecurityException |
|
310 * In the case of the default provider, the security manager's {@link |
|
311 * SecurityManager#checkRead(String) checkRead} method is invoked |
|
312 * to check read access to file |
|
313 * |
|
314 * @see BasicFileAttributeView#readAttributes |
|
315 */ |
|
316 public static BasicFileAttributes readBasicFileAttributes(FileRef file, |
|
317 LinkOption... options) |
|
318 throws IOException |
|
319 { |
|
320 return file.getFileAttributeView(BasicFileAttributeView.class, options) |
|
321 .readAttributes(); |
|
322 } |
|
323 |
|
324 /** |
|
325 * Reads the POSIX file attributes of a file. |
|
326 * |
|
327 * <p> The {@code file} parameter locates a file that supports the {@link |
|
328 * PosixFileAttributeView}. This file attribute view provides access to a |
|
329 * subset of the file attributes commonly associated with files on file |
|
330 * systems used by operating systems that implement the Portable Operating |
|
331 * System Interface (POSIX) family of standards. It is implementation |
|
332 * specific if all file attributes are read as an atomic operation with |
|
333 * respect to other file system operations. |
|
334 * |
|
335 * <p> The {@code options} array may be used to indicate how symbolic links |
|
336 * are handled for the case that the file is a symbolic link. By default, |
|
337 * symbolic links are followed and the file attributes of the final target |
|
338 * of the link are read. If the option {@link LinkOption#NOFOLLOW_LINKS |
|
339 * NOFOLLOW_LINKS} is present then symbolic links are not followed and so |
|
340 * the method returns the file attributes of the symbolic link. |
|
341 * |
|
342 * @param file |
|
343 * A file reference that locates the file |
|
344 * @param options |
|
345 * Options indicating how symbolic links are handled |
|
346 * |
|
347 * @return The POSIX file attributes |
|
348 * |
|
349 * @throws UnsupportedOperationException |
|
350 * If the {@code PosixFileAttributeView} is not available |
|
351 * @throws IOException |
|
352 * If an I/O error occurs |
|
353 * @throws SecurityException |
|
354 * In the case of the default provider, and a security manager is |
|
355 * installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt> |
|
356 * or its {@link SecurityManager#checkRead(String) checkRead} method |
|
357 * denies read access to the file. |
|
358 * |
|
359 * @see PosixFileAttributeView#readAttributes |
|
360 */ |
|
361 public static PosixFileAttributes readPosixFileAttributes(FileRef file, |
|
362 LinkOption... options) |
|
363 throws IOException |
|
364 { |
|
365 PosixFileAttributeView view = |
|
366 file.getFileAttributeView(PosixFileAttributeView.class, options); |
|
367 if (view == null) |
|
368 throw new UnsupportedOperationException(); |
|
369 return view.readAttributes(); |
|
370 } |
|
371 |
|
372 /** |
|
373 * Reads the DOS file attributes of a file. |
|
374 * |
|
375 * <p> The {@code file} parameter locates a file that supports the {@link |
|
376 * DosFileAttributeView}. This file attribute view provides access to |
|
377 * legacy "DOS" attributes supported by the file systems such as File |
|
378 * Allocation Table (FAT), commonly used in <em>consumer devices</em>. It is |
|
379 * implementation specific if all file attributes are read as an atomic |
|
380 * operation with respect to other file system operations. |
|
381 * |
|
382 * <p> The {@code options} array may be used to indicate how symbolic links |
|
383 * are handled for the case that the file is a symbolic link. By default, |
|
384 * symbolic links are followed and the file attributes of the final target |
|
385 * of the link are read. If the option {@link LinkOption#NOFOLLOW_LINKS |
|
386 * NOFOLLOW_LINKS} is present then symbolic links are not followed and so |
|
387 * the method returns the file attributes of the symbolic link. |
|
388 * |
|
389 * @param file |
|
390 * A file reference that locates the file |
|
391 * @param options |
|
392 * Options indicating how symbolic links are handled |
|
393 * |
|
394 * @return The DOS file attributes |
|
395 * |
|
396 * @throws UnsupportedOperationException |
|
397 * If the {@code DosFileAttributeView} is not available |
|
398 * @throws IOException |
|
399 * If an I/O error occurs |
|
400 * @throws SecurityException |
|
401 * In the case of the default provider, the security manager's {@link |
|
402 * SecurityManager#checkRead(String) checkRead} method is invoked |
|
403 * to check read access to file |
|
404 * |
|
405 * @see DosFileAttributeView#readAttributes |
|
406 */ |
|
407 public static DosFileAttributes readDosFileAttributes(FileRef file, |
|
408 LinkOption... options) |
|
409 throws IOException |
|
410 { |
|
411 DosFileAttributeView view = |
|
412 file.getFileAttributeView(DosFileAttributeView.class, options); |
|
413 if (view == null) |
|
414 throw new UnsupportedOperationException(); |
|
415 return view.readAttributes(); |
|
416 } |
|
417 |
|
418 /** |
|
419 * Returns the owner of a file. |
|
420 * |
|
421 * <p> The {@code file} parameter locates a file that supports the {@link |
|
422 * FileOwnerAttributeView}. This file attribute view provides access to |
|
423 * a file attribute that is the owner of the file. |
|
424 * |
|
425 * @param file |
|
426 * A file reference that locates the file |
|
427 * |
|
428 * @return A user principal representing the owner of the file |
|
429 * |
|
430 * @throws UnsupportedOperationException |
|
431 * If the {@code FileOwnerAttributeView} is not available |
|
432 * @throws IOException |
|
433 * If an I/O error occurs |
|
434 * @throws SecurityException |
|
435 * In the case of the default provider, and a security manager is |
|
436 * installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt> |
|
437 * or its {@link SecurityManager#checkRead(String) checkRead} method |
|
438 * denies read access to the file. |
|
439 * |
|
440 * @see FileOwnerAttributeView#getOwner |
|
441 */ |
|
442 public static UserPrincipal getOwner(FileRef file) throws IOException { |
|
443 FileOwnerAttributeView view = |
|
444 file.getFileAttributeView(FileOwnerAttributeView.class); |
|
445 if (view == null) |
|
446 throw new UnsupportedOperationException(); |
|
447 return view.getOwner(); |
|
448 } |
|
449 |
|
450 /** |
|
451 * Updates the file owner. |
|
452 * |
|
453 * <p> The {@code file} parameter locates a file that supports the {@link |
|
454 * FileOwnerAttributeView}. This file attribute view provides access to |
|
455 * a file attribute that is the owner of the file. |
|
456 * |
|
457 * @param file |
|
458 * A file reference that locates the file |
|
459 * @param owner |
|
460 * The new file owner |
|
461 * |
|
462 * @throws UnsupportedOperationException |
|
463 * If the {@code FileOwnerAttributeView} is not available |
|
464 * @throws IOException |
|
465 * If an I/O error occurs |
|
466 * @throws SecurityException |
|
467 * In the case of the default provider, and a security manager is |
|
468 * installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt> |
|
469 * or its {@link SecurityManager#checkWrite(String) checkWrite} |
|
470 * method denies write access to the file. |
|
471 * |
|
472 * @see FileOwnerAttributeView#setOwner |
|
473 */ |
|
474 public static void setOwner(FileRef file, UserPrincipal owner) |
|
475 throws IOException |
|
476 { |
|
477 FileOwnerAttributeView view = |
|
478 file.getFileAttributeView(FileOwnerAttributeView.class); |
|
479 if (view == null) |
|
480 throw new UnsupportedOperationException(); |
|
481 view.setOwner(owner); |
|
482 } |
|
483 |
|
484 /** |
|
485 * Reads a file's Access Control List (ACL). |
|
486 * |
|
487 * <p> The {@code file} parameter locates a file that supports the {@link |
|
488 * AclFileAttributeView}. This file attribute view provides access to ACLs |
|
489 * based on the ACL model specified in |
|
490 * <a href="http://www.ietf.org/rfc/rfc3530.txt"><i>RFC 3530</i></a>. |
|
491 * |
|
492 * @param file |
|
493 * A file reference that locates the file |
|
494 * |
|
495 * @return An ordered list of {@link AclEntry entries} representing the |
|
496 * ACL. The returned list is modifiable. |
|
497 * |
|
498 * @throws UnsupportedOperationException |
|
499 * If the {@code AclAttributeView} is not available |
|
500 * @throws IOException |
|
501 * If an I/O error occurs |
|
502 * @throws SecurityException |
|
503 * In the case of the default provider, and a security manager is |
|
504 * installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt> |
|
505 * or its {@link SecurityManager#checkRead(String) checkRead} method |
|
506 * denies read access to the file. |
|
507 * |
|
508 * @see AclFileAttributeView#getAcl |
|
509 */ |
|
510 public static List<AclEntry> getAcl(FileRef file) throws IOException { |
|
511 AclFileAttributeView view = |
|
512 file.getFileAttributeView(AclFileAttributeView.class); |
|
513 if (view == null) |
|
514 throw new UnsupportedOperationException(); |
|
515 return view.getAcl(); |
|
516 } |
|
517 |
|
518 /** |
|
519 * Updates a file's Access Control List (ACL). |
|
520 * |
|
521 * <p> The {@code file} parameter locates a file that supports the {@link |
|
522 * AclFileAttributeView}. This file attribute view provides access to ACLs |
|
523 * based on the ACL model specified in |
|
524 * <a href="http://www.ietf.org/rfc/rfc3530.txt"><i>RFC 3530</i></a>. |
|
525 * |
|
526 * @param file |
|
527 * A file reference that locates the file |
|
528 * @param acl |
|
529 * The new file ACL |
|
530 * |
|
531 * @throws UnsupportedOperationException |
|
532 * If the {@code AclFileAttributeView} is not available |
|
533 * @throws IOException |
|
534 * If an I/O error occurs |
|
535 * @throws SecurityException |
|
536 * In the case of the default provider, and a security manager is |
|
537 * installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt> |
|
538 * or its {@link SecurityManager#checkWrite(String) checkWrite} |
|
539 * method denies write access to the file. |
|
540 * |
|
541 * @see AclFileAttributeView#setAcl |
|
542 */ |
|
543 public static void setAcl(FileRef file, List<AclEntry> acl) |
|
544 throws IOException |
|
545 { |
|
546 AclFileAttributeView view = |
|
547 file.getFileAttributeView(AclFileAttributeView.class); |
|
548 if (view == null) |
|
549 throw new UnsupportedOperationException(); |
|
550 view.setAcl(acl); |
|
551 } |
|
552 |
|
553 /** |
|
554 * Updates the value of a file's last modified time attribute. |
|
555 * |
|
556 * <p> The time value is measured since the epoch |
|
557 * (00:00:00 GMT, January 1, 1970) and is converted to the precision supported |
|
558 * by the file system. Converting from finer to coarser granularities result |
|
559 * in precision loss. |
|
560 * |
|
561 * <p> If the file system does not support a last modified time attribute then |
|
562 * this method has no effect. |
|
563 * |
|
564 * @param file |
|
565 * A file reference that locates the file |
|
566 * |
|
567 * @param lastModifiedTime |
|
568 * The new last modified time, or {@code -1L} to update it to |
|
569 * the current time |
|
570 * @param unit |
|
571 * A {@code TimeUnit} determining how to interpret the |
|
572 * {@code lastModifiedTime} parameter |
|
573 * |
|
574 * @throws IllegalArgumentException |
|
575 * If the {@code lastModifiedime} parameter is a negative value other |
|
576 * than {@code -1L} |
|
577 * @throws IOException |
|
578 * If an I/O error occurs |
|
579 * @throws SecurityException |
|
580 * In the case of the default provider, the security manager's {@link |
|
581 * SecurityManager#checkWrite(String) checkWrite} method is invoked |
|
582 * to check write access to file |
|
583 * |
|
584 * @see BasicFileAttributeView#setTimes |
|
585 */ |
|
586 public static void setLastModifiedTime(FileRef file, |
|
587 long lastModifiedTime, |
|
588 TimeUnit unit) |
|
589 throws IOException |
|
590 { |
|
591 file.getFileAttributeView(BasicFileAttributeView.class) |
|
592 .setTimes(lastModifiedTime, null, null, unit); |
|
593 } |
|
594 |
|
595 /** |
|
596 * Updates the value of a file's last access time attribute. |
|
597 * |
|
598 * <p> The time value is measured since the epoch |
|
599 * (00:00:00 GMT, January 1, 1970) and is converted to the precision supported |
|
600 * by the file system. Converting from finer to coarser granularities result |
|
601 * in precision loss. |
|
602 * |
|
603 * <p> If the file system does not support a last access time attribute then |
|
604 * this method has no effect. |
|
605 * |
|
606 * @param lastAccessTime |
|
607 * The new last access time, or {@code -1L} to update it to |
|
608 * the current time |
|
609 * @param unit |
|
610 * A {@code TimeUnit} determining how to interpret the |
|
611 * {@code lastModifiedTime} parameter |
|
612 * |
|
613 * @throws IllegalArgumentException |
|
614 * If the {@code lastAccessTime} parameter is a negative value other |
|
615 * than {@code -1L} |
|
616 * @throws IOException |
|
617 * If an I/O error occurs |
|
618 * @throws SecurityException |
|
619 * In the case of the default provider, the security manager's {@link |
|
620 * SecurityManager#checkWrite(String) checkWrite} method is invoked |
|
621 * to check write access to file |
|
622 * |
|
623 * @see BasicFileAttributeView#setTimes |
|
624 */ |
|
625 public static void setLastAccessTime(FileRef file, |
|
626 long lastAccessTime, |
|
627 TimeUnit unit) |
|
628 throws IOException |
|
629 { |
|
630 file.getFileAttributeView(BasicFileAttributeView.class) |
|
631 .setTimes(null, lastAccessTime, null, unit); |
|
632 } |
|
633 |
|
634 /** |
|
635 * Sets a file's POSIX permissions. |
|
636 * |
|
637 * <p> The {@code file} parameter is a reference to an existing file. It |
|
638 * supports the {@link PosixFileAttributeView} that provides access to file |
|
639 * attributes commonly associated with files on file systems used by |
|
640 * operating systems that implement the Portable Operating System Interface |
|
641 * (POSIX) family of standards. |
|
642 * |
|
643 * @param file |
|
644 * A file reference that locates the file |
|
645 * @param perms |
|
646 * The new set of permissions |
|
647 * |
|
648 * @throws UnsupportedOperationException |
|
649 * If {@code PosixFileAttributeView} is not available |
|
650 * @throws ClassCastException |
|
651 * If the sets contains elements that are not of type {@code |
|
652 * PosixFilePermission} |
|
653 * @throws IOException |
|
654 * If an I/O error occurs |
|
655 * @throws SecurityException |
|
656 * In the case of the default provider, and a security manager is |
|
657 * installed, it denies {@link RuntimePermission}<tt>("accessUserInformation")</tt> |
|
658 * or its {@link SecurityManager#checkWrite(String) checkWrite} |
|
659 * method denies write access to the file. |
|
660 * |
|
661 * @see PosixFileAttributeView#setPermissions |
|
662 */ |
|
663 public static void setPosixFilePermissions(FileRef file, |
|
664 Set<PosixFilePermission> perms) |
|
665 throws IOException |
|
666 { |
|
667 PosixFileAttributeView view = |
|
668 file.getFileAttributeView(PosixFileAttributeView.class); |
|
669 if (view == null) |
|
670 throw new UnsupportedOperationException(); |
|
671 view.setPermissions(perms); |
|
672 } |
|
673 |
|
674 /** |
|
675 * Reads the space attributes of a file store. |
|
676 * |
|
677 * <p> The {@code store} parameter is a file store that supports the |
|
678 * {@link FileStoreSpaceAttributeView} providing access to the space related |
|
679 * attributes of the file store. It is implementation specific if all attributes |
|
680 * are read as an atomic operation with respect to other file system operations. |
|
681 * |
|
682 * @param store |
|
683 * The file store |
|
684 * |
|
685 * @return The file store space attributes |
|
686 * |
|
687 * @throws UnsupportedOperationException |
|
688 * If the file store space attribute view is not supported |
|
689 * @throws IOException |
|
690 * If an I/O error occurs |
|
691 * |
|
692 * @see FileStoreSpaceAttributeView#readAttributes() |
|
693 */ |
|
694 public static FileStoreSpaceAttributes readFileStoreSpaceAttributes(FileStore store) |
|
695 throws IOException |
|
696 { |
|
697 FileStoreSpaceAttributeView view = |
|
698 store.getFileStoreAttributeView(FileStoreSpaceAttributeView.class); |
|
699 if (view == null) |
|
700 throw new UnsupportedOperationException(); |
|
701 return view.readAttributes(); |
|
702 } |
|
703 } |