1 /*
2 * Copyright (c) 2008, 2016, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
57 * Do not follow reparse points when opening an existing file. Do not fail
58 * if the file is a reparse point.
59 */
60 static final OpenOption OPEN_REPARSE_POINT = new OpenOption() { };
61
62 /**
63 * Represents the flags from a user-supplied set of open options.
64 */
65 private static class Flags {
66 boolean read;
67 boolean write;
68 boolean append;
69 boolean truncateExisting;
70 boolean create;
71 boolean createNew;
72 boolean deleteOnClose;
73 boolean sparse;
74 boolean overlapped;
75 boolean sync;
76 boolean dsync;
77
78 // non-standard
79 boolean shareRead = true;
80 boolean shareWrite = true;
81 boolean shareDelete = true;
82 boolean noFollowLinks;
83 boolean openReparsePoint;
84
85 static Flags toFlags(Set<? extends OpenOption> options) {
86 Flags flags = new Flags();
87 for (OpenOption option: options) {
88 if (option instanceof StandardOpenOption) {
89 switch ((StandardOpenOption)option) {
90 case READ : flags.read = true; break;
91 case WRITE : flags.write = true; break;
92 case APPEND : flags.append = true; break;
93 case TRUNCATE_EXISTING : flags.truncateExisting = true; break;
94 case CREATE : flags.create = true; break;
95 case CREATE_NEW : flags.createNew = true; break;
96 case DELETE_ON_CLOSE : flags.deleteOnClose = true; break;
104 if (option == LinkOption.NOFOLLOW_LINKS) {
105 flags.noFollowLinks = true;
106 continue;
107 }
108 if (option == OPEN_REPARSE_POINT) {
109 flags.openReparsePoint = true;
110 continue;
111 }
112 if (ExtendedOptions.NOSHARE_READ.matches(option)) {
113 flags.shareRead = false;
114 continue;
115 }
116 if (ExtendedOptions.NOSHARE_WRITE.matches(option)) {
117 flags.shareWrite = false;
118 continue;
119 }
120 if (ExtendedOptions.NOSHARE_DELETE.matches(option)) {
121 flags.shareDelete = false;
122 continue;
123 }
124 if (option == null)
125 throw new NullPointerException();
126 throw new UnsupportedOperationException();
127 }
128 return flags;
129 }
130 }
131
132 /**
133 * Open/creates file, returning FileChannel to access the file
134 *
135 * @param pathForWindows
136 * The path of the file to open/create
137 * @param pathToCheck
138 * The path used for permission checks (if security manager)
139 */
140 static FileChannel newFileChannel(String pathForWindows,
141 String pathToCheck,
142 Set<? extends OpenOption> options,
143 long pSecurityDescriptor)
144 throws WindowsException
145 {
146 Flags flags = Flags.toFlags(options);
147
148 // default is reading; append => writing
149 if (!flags.read && !flags.write) {
150 if (flags.append) {
151 flags.write = true;
152 } else {
153 flags.read = true;
154 }
155 }
156
157 // validation
158 if (flags.read && flags.append)
159 throw new IllegalArgumentException("READ + APPEND not allowed");
160 if (flags.append && flags.truncateExisting)
161 throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
162
163 FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor);
164 return FileChannelImpl.open(fdObj, pathForWindows, flags.read, flags.write, null);
165 }
166
167 /**
168 * Open/creates file, returning AsynchronousFileChannel to access the file
169 *
170 * @param pathForWindows
171 * The path of the file to open/create
172 * @param pathToCheck
173 * The path used for permission checks (if security manager)
174 * @param pool
175 * The thread pool that the channel is associated with
176 */
177 static AsynchronousFileChannel newAsynchronousFileChannel(String pathForWindows,
178 String pathToCheck,
179 Set<? extends OpenOption> options,
180 long pSecurityDescriptor,
181 ThreadPool pool)
182 throws IOException
183 {
184 Flags flags = Flags.toFlags(options);
|
1 /*
2 * Copyright (c) 2008, 2017, Oracle and/or its affiliates. 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
57 * Do not follow reparse points when opening an existing file. Do not fail
58 * if the file is a reparse point.
59 */
60 static final OpenOption OPEN_REPARSE_POINT = new OpenOption() { };
61
62 /**
63 * Represents the flags from a user-supplied set of open options.
64 */
65 private static class Flags {
66 boolean read;
67 boolean write;
68 boolean append;
69 boolean truncateExisting;
70 boolean create;
71 boolean createNew;
72 boolean deleteOnClose;
73 boolean sparse;
74 boolean overlapped;
75 boolean sync;
76 boolean dsync;
77 boolean direct;
78
79 // non-standard
80 boolean shareRead = true;
81 boolean shareWrite = true;
82 boolean shareDelete = true;
83 boolean noFollowLinks;
84 boolean openReparsePoint;
85
86 static Flags toFlags(Set<? extends OpenOption> options) {
87 Flags flags = new Flags();
88 for (OpenOption option: options) {
89 if (option instanceof StandardOpenOption) {
90 switch ((StandardOpenOption)option) {
91 case READ : flags.read = true; break;
92 case WRITE : flags.write = true; break;
93 case APPEND : flags.append = true; break;
94 case TRUNCATE_EXISTING : flags.truncateExisting = true; break;
95 case CREATE : flags.create = true; break;
96 case CREATE_NEW : flags.createNew = true; break;
97 case DELETE_ON_CLOSE : flags.deleteOnClose = true; break;
105 if (option == LinkOption.NOFOLLOW_LINKS) {
106 flags.noFollowLinks = true;
107 continue;
108 }
109 if (option == OPEN_REPARSE_POINT) {
110 flags.openReparsePoint = true;
111 continue;
112 }
113 if (ExtendedOptions.NOSHARE_READ.matches(option)) {
114 flags.shareRead = false;
115 continue;
116 }
117 if (ExtendedOptions.NOSHARE_WRITE.matches(option)) {
118 flags.shareWrite = false;
119 continue;
120 }
121 if (ExtendedOptions.NOSHARE_DELETE.matches(option)) {
122 flags.shareDelete = false;
123 continue;
124 }
125 if (ExtendedOptions.DIRECT.matches(option)) {
126 flags.direct = true;
127 continue;
128 }
129 if (option == null)
130 throw new NullPointerException();
131 throw new UnsupportedOperationException();
132 }
133 return flags;
134 }
135 }
136
137 /**
138 * Open/creates file, returning FileChannel to access the file
139 *
140 * @param pathForWindows
141 * The path of the file to open/create
142 * @param pathToCheck
143 * The path used for permission checks (if security manager)
144 */
145 static FileChannel newFileChannel(String pathForWindows,
146 String pathToCheck,
147 Set<? extends OpenOption> options,
148 long pSecurityDescriptor)
149 throws WindowsException
150 {
151 Flags flags = Flags.toFlags(options);
152
153 // default is reading; append => writing
154 if (!flags.read && !flags.write) {
155 if (flags.append) {
156 flags.write = true;
157 } else {
158 flags.read = true;
159 }
160 }
161
162 // validation
163 if (flags.read && flags.append)
164 throw new IllegalArgumentException("READ + APPEND not allowed");
165 if (flags.append && flags.truncateExisting)
166 throw new IllegalArgumentException("APPEND + TRUNCATE_EXISTING not allowed");
167
168 FileDescriptor fdObj = open(pathForWindows, pathToCheck, flags, pSecurityDescriptor);
169 return FileChannelImpl.open(fdObj, pathForWindows, flags.read,
170 flags.write, flags.direct, null);
171 }
172
173 /**
174 * Open/creates file, returning AsynchronousFileChannel to access the file
175 *
176 * @param pathForWindows
177 * The path of the file to open/create
178 * @param pathToCheck
179 * The path used for permission checks (if security manager)
180 * @param pool
181 * The thread pool that the channel is associated with
182 */
183 static AsynchronousFileChannel newAsynchronousFileChannel(String pathForWindows,
184 String pathToCheck,
185 Set<? extends OpenOption> options,
186 long pSecurityDescriptor,
187 ThreadPool pool)
188 throws IOException
189 {
190 Flags flags = Flags.toFlags(options);
|