|
23 | 23 | import java.io.BufferedInputStream; |
24 | 24 | import java.io.ByteArrayInputStream; |
25 | 25 | import java.io.ByteArrayOutputStream; |
| 26 | +import java.io.FileNotFoundException; |
26 | 27 | import java.io.IOException; |
27 | 28 | import java.io.InputStream; |
28 | 29 | import java.io.OutputStream; |
@@ -1213,32 +1214,42 @@ public void write(IFile target, InputStream content, IFileInfo fileInfo, int upd |
1213 | 1214 | IFileStore store = getStore(target); |
1214 | 1215 | prepareWrite(target, fileInfo, updateFlags, append, targetResource, store); |
1215 | 1216 |
|
1216 | | - // On Windows an attempt to open an output stream on a hidden file results in FileNotFoundException. |
1217 | | - // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=194216 |
1218 | | - boolean restoreHiddenAttribute = false; |
1219 | | - if (fileInfo.exists() && fileInfo.getAttribute(EFS.ATTRIBUTE_HIDDEN) && Platform.getOS().equals(Platform.OS_WIN32)) { |
1220 | | - fileInfo.setAttribute(EFS.ATTRIBUTE_HIDDEN, false); |
1221 | | - store.putInfo(fileInfo, EFS.SET_ATTRIBUTES, subMonitor.split(1)); |
1222 | | - restoreHiddenAttribute = true; |
1223 | | - } else { |
1224 | | - subMonitor.split(1); |
1225 | | - } |
1226 | 1217 | int options = append ? EFS.APPEND : EFS.NONE; |
1227 | | - try (OutputStream out = store.openOutputStream(options, subMonitor.split(1))) { |
1228 | | - if (restoreHiddenAttribute) { |
1229 | | - fileInfo.setAttribute(EFS.ATTRIBUTE_HIDDEN, true); |
| 1218 | + try { |
| 1219 | + boolean opened = false; |
| 1220 | + try (OutputStream out = store.openOutputStream(options, subMonitor.split(1))) { |
| 1221 | + opened = true; |
| 1222 | + FileUtil.transferStreams(content, out, store.toString(), subMonitor.split(1)); |
| 1223 | + } catch (CoreException e) { |
| 1224 | + // On Windows an attempt to open an output stream on a hidden file results in |
| 1225 | + // FileNotFoundException. |
| 1226 | + // See https://bugs.eclipse.org/bugs/show_bug.cgi?id=194216 |
| 1227 | + if (opened || !(e.getCause() instanceof FileNotFoundException) |
| 1228 | + || !Platform.getOS().equals(Platform.OS_WIN32)) { |
| 1229 | + throw e; |
| 1230 | + } |
| 1231 | + fileInfo = store.fetchInfo(); |
| 1232 | + if (!(fileInfo.exists() && fileInfo.getAttribute(EFS.ATTRIBUTE_HIDDEN))) { |
| 1233 | + throw e; |
| 1234 | + } |
| 1235 | + // set hidden=false and retry: |
| 1236 | + fileInfo.setAttribute(EFS.ATTRIBUTE_HIDDEN, false); |
1230 | 1237 | store.putInfo(fileInfo, EFS.SET_ATTRIBUTES, subMonitor.split(1)); |
1231 | | - } else { |
1232 | | - subMonitor.split(1); |
| 1238 | + try (OutputStream out = store.openOutputStream(options, null)) { |
| 1239 | + // restore Hidden Attribute: |
| 1240 | + fileInfo.setAttribute(EFS.ATTRIBUTE_HIDDEN, true); |
| 1241 | + store.putInfo(fileInfo, EFS.SET_ATTRIBUTES, subMonitor.split(1)); |
| 1242 | + FileUtil.transferStreams(content, out, store.toString(), subMonitor.split(1)); |
| 1243 | + } |
1233 | 1244 | } |
1234 | | - FileUtil.transferStreams(content, out, store.toString(), subMonitor.split(1)); |
1235 | 1245 | } catch (IOException e) { |
| 1246 | + // Exception on OutputStream.close() |
1236 | 1247 | String msg = NLS.bind(Messages.localstore_couldNotWrite, store.toString()); |
1237 | 1248 | throw new ResourceException(IResourceStatus.FAILED_WRITE_LOCAL, IPath.fromOSString(store.toString()), msg, e); |
1238 | 1249 | } |
1239 | 1250 | finishWrite(targetResource, store); |
1240 | 1251 | } catch (IOException streamCloseIgnored) { |
1241 | | - // ignore; |
| 1252 | + // ignore Exception on InputStream.close() |
1242 | 1253 | } |
1243 | 1254 | } |
1244 | 1255 |
|
|
0 commit comments