23 * questions.
24 */
25
26 package sun.nio.fs;
27
28 import java.nio.file.*;
29 import java.nio.file.attribute.*;
30 import java.nio.channels.SeekableByteChannel;
31 import java.util.*;
32 import java.util.concurrent.TimeUnit;
33 import java.io.IOException;
34
35 import static sun.nio.fs.UnixNativeDispatcher.*;
36 import static sun.nio.fs.UnixConstants.*;
37
38 /**
39 * Unix implementation of SecureDirectoryStream.
40 */
41
42 class UnixSecureDirectoryStream
43 extends SecureDirectoryStream<Path>
44 {
45 private final UnixDirectoryStream ds;
46 private final int dfd;
47
48 UnixSecureDirectoryStream(UnixPath dir,
49 long dp,
50 int dfd,
51 DirectoryStream.Filter<? super Path> filter)
52 {
53 this.ds = new UnixDirectoryStream(dir, dp, filter);
54 this.dfd = dfd;
55 }
56
57 @Override
58 public void close()
59 throws IOException
60 {
61 ds.writeLock().lock();
62 try {
63 if (ds.closeImpl()) {
64 UnixNativeDispatcher.close(dfd);
65 }
66 } finally {
67 ds.writeLock().unlock();
68 }
69 }
70
71 @Override
72 public Iterator<Path> iterator() {
73 return ds.iterator(this);
74 }
75
76 private UnixPath getName(Path obj) {
77 if (obj == null)
78 throw new NullPointerException();
79 if (!(obj instanceof UnixPath))
80 throw new ProviderMismatchException();
81 return (UnixPath)obj;
82 }
83
84 /**
85 * Opens sub-directory in this directory
86 */
87 @Override
88 public SecureDirectoryStream<Path> newDirectoryStream(Path obj,
89 LinkOption... options)
90 throws IOException
91 {
92 UnixPath file = getName(obj);
93 UnixPath child = ds.directory().resolve(file);
94 boolean followLinks = file.getFileSystem().followLinks(options);
95
96 // permission check using name resolved against original path of directory
97 SecurityManager sm = System.getSecurityManager();
98 if (sm != null) {
99 child.checkRead();
100 }
101
102 ds.readLock().lock();
103 try {
104 if (!ds.isOpen())
105 throw new ClosedDirectoryStreamException();
106
107 // open directory and create new secure directory stream
108 int newdfd1 = -1;
109 int newdfd2 = -1;
110 long ptr = 0L;
111 try {
112 int flags = O_RDONLY;
113 if (!followLinks)
114 flags |= O_NOFOLLOW;
285 return (V) null;
286 }
287
288 /**
289 * Returns file attribute view bound to this directory
290 */
291 @Override
292 public <V extends FileAttributeView> V getFileAttributeView(Class<V> type) {
293 return getFileAttributeViewImpl(null, type, false);
294 }
295
296 /**
297 * Returns file attribute view bound to dfd/filename.
298 */
299 @Override
300 public <V extends FileAttributeView> V getFileAttributeView(Path obj,
301 Class<V> type,
302 LinkOption... options)
303 {
304 UnixPath file = getName(obj);
305 boolean followLinks = file.getFileSystem().followLinks(options);
306 return getFileAttributeViewImpl(file, type, followLinks);
307 }
308
309 /**
310 * A BasicFileAttributeView implementation that using a dfd/name pair.
311 */
312 private class BasicFileAttributeViewImpl
313 implements BasicFileAttributeView
314 {
315 final UnixPath file;
316 final boolean followLinks;
317
318 BasicFileAttributeViewImpl(UnixPath file, boolean followLinks)
319 {
320 this.file = file;
321 this.followLinks = followLinks;
322 }
323
324 int open() throws IOException {
325 int oflags = O_RDONLY;
326 if (!followLinks)
327 oflags |= O_NOFOLLOW;
328 try {
329 return openat(dfd, file.asByteArray(), oflags, 0);
330 } catch (UnixException x) {
331 x.rethrowAsIOException(file);
332 return -1; // keep compiler happy
333 }
334 }
335
336 private void checkWriteAccess() {
337 SecurityManager sm = System.getSecurityManager();
338 if (sm != null) {
339 ds.directory().resolve(file).checkWrite();
340 }
341 }
342
343 @Override
344 public String name() {
345 return "basic";
346 }
347
348 @Override
349 public BasicFileAttributes readAttributes() throws IOException {
350 ds.readLock().lock();
351 try {
352 if (!ds.isOpen())
353 throw new ClosedDirectoryStreamException();
354
355 SecurityManager sm = System.getSecurityManager();
356 if (sm != null) {
357 if (file == null) {
358 ds.directory().checkRead();
359 } else {
360 ds.directory().resolve(file).checkRead();
361 }
|
23 * questions.
24 */
25
26 package sun.nio.fs;
27
28 import java.nio.file.*;
29 import java.nio.file.attribute.*;
30 import java.nio.channels.SeekableByteChannel;
31 import java.util.*;
32 import java.util.concurrent.TimeUnit;
33 import java.io.IOException;
34
35 import static sun.nio.fs.UnixNativeDispatcher.*;
36 import static sun.nio.fs.UnixConstants.*;
37
38 /**
39 * Unix implementation of SecureDirectoryStream.
40 */
41
42 class UnixSecureDirectoryStream
43 implements SecureDirectoryStream<Path>
44 {
45 private final UnixDirectoryStream ds;
46 private final int dfd;
47
48 UnixSecureDirectoryStream(UnixPath dir,
49 long dp,
50 int dfd,
51 DirectoryStream.Filter<? super Path> filter)
52 {
53 this.ds = new UnixDirectoryStream(dir, dp, filter);
54 this.dfd = dfd;
55 }
56
57 @Override
58 public void close()
59 throws IOException
60 {
61 ds.writeLock().lock();
62 try {
63 if (ds.closeImpl()) {
64 UnixNativeDispatcher.close(dfd);
65 }
66 } finally {
67 ds.writeLock().unlock();
68 }
69 }
70
71 @Override
72 public Iterator<Path> iterator() {
73 return ds.iterator(this);
74 }
75
76 private UnixPath getName(Path obj) {
77 if (obj == null)
78 throw new NullPointerException();
79 if (!(obj instanceof UnixPath))
80 throw new ProviderMismatchException();
81 return (UnixPath)obj;
82 }
83
84 private boolean followLinks(LinkOption... options) {
85 boolean followLinks = true;
86 for (LinkOption option: options) {
87 if (option == LinkOption.NOFOLLOW_LINKS) {
88 followLinks = false;
89 continue;
90 }
91 if (option == null)
92 throw new NullPointerException();
93 throw new AssertionError("Should not get here");
94 }
95 return followLinks;
96 }
97
98 /**
99 * Opens sub-directory in this directory
100 */
101 @Override
102 public SecureDirectoryStream<Path> newDirectoryStream(Path obj,
103 LinkOption... options)
104 throws IOException
105 {
106 UnixPath file = getName(obj);
107 UnixPath child = ds.directory().resolve(file);
108 boolean followLinks = followLinks(options);
109
110 // permission check using name resolved against original path of directory
111 SecurityManager sm = System.getSecurityManager();
112 if (sm != null) {
113 child.checkRead();
114 }
115
116 ds.readLock().lock();
117 try {
118 if (!ds.isOpen())
119 throw new ClosedDirectoryStreamException();
120
121 // open directory and create new secure directory stream
122 int newdfd1 = -1;
123 int newdfd2 = -1;
124 long ptr = 0L;
125 try {
126 int flags = O_RDONLY;
127 if (!followLinks)
128 flags |= O_NOFOLLOW;
299 return (V) null;
300 }
301
302 /**
303 * Returns file attribute view bound to this directory
304 */
305 @Override
306 public <V extends FileAttributeView> V getFileAttributeView(Class<V> type) {
307 return getFileAttributeViewImpl(null, type, false);
308 }
309
310 /**
311 * Returns file attribute view bound to dfd/filename.
312 */
313 @Override
314 public <V extends FileAttributeView> V getFileAttributeView(Path obj,
315 Class<V> type,
316 LinkOption... options)
317 {
318 UnixPath file = getName(obj);
319 boolean followLinks = followLinks(options);
320 return getFileAttributeViewImpl(file, type, followLinks);
321 }
322
323 /**
324 * A BasicFileAttributeView implementation that using a dfd/name pair.
325 */
326 private class BasicFileAttributeViewImpl
327 implements BasicFileAttributeView
328 {
329 final UnixPath file;
330 final boolean followLinks;
331
332 BasicFileAttributeViewImpl(UnixPath file, boolean followLinks)
333 {
334 this.file = file;
335 this.followLinks = followLinks;
336 }
337
338 int open() throws IOException {
339 int oflags = O_RDONLY;
340 if (!followLinks)
341 oflags |= O_NOFOLLOW;
342 try {
343 return openat(dfd, file.asByteArray(), oflags, 0);
344 } catch (UnixException x) {
345 x.rethrowAsIOException(file);
346 return -1; // keep compiler happy
347 }
348 }
349
350 private void checkWriteAccess() {
351 SecurityManager sm = System.getSecurityManager();
352 if (sm != null) {
353 if (file == null) {
354 ds.directory().checkWrite();
355 } else {
356 ds.directory().resolve(file).checkWrite();
357 }
358 }
359 }
360
361 @Override
362 public String name() {
363 return "basic";
364 }
365
366 @Override
367 public BasicFileAttributes readAttributes() throws IOException {
368 ds.readLock().lock();
369 try {
370 if (!ds.isOpen())
371 throw new ClosedDirectoryStreamException();
372
373 SecurityManager sm = System.getSecurityManager();
374 if (sm != null) {
375 if (file == null) {
376 ds.directory().checkRead();
377 } else {
378 ds.directory().resolve(file).checkRead();
379 }
|