AdoptOpenJDK / AdoptOpenJDK/IcedTea-Web

Writing to a file via CreateRestrictedFile/

Abierto
#862 1 comentario 1 reacción 0 asignados Ver en GitHub
Lenguaje dominante
Java
Estrellas
268
Forks
96
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

Writing to a file fails if the file does not already exist on Windows - not an issue on Linux. Here is the stack trace of the error I get when writing preferences to a non-existent file:

at com.sun.proxy.$Proxy7.saveFileDialog(Unknown Source)
at net.sourceforge.jnlp.services.ServiceUtil$PrivilegedHandler.invoke(ServiceUtil.java:206)
at java.security.AccessController.doPrivileged(Native Method)
at net.sourceforge.jnlp.services.ServiceUtil$PrivilegedHandler$1.run(ServiceUtil.java:201)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at net.sourceforge.jnlp.services.XFileSaveService.saveFileDialog(XFileSaveService.java:70)
at net.sourceforge.jnlp.services.XFileSaveService.writeToFile(XFileSaveService.java:120)
at net.adoptopenjdk.icedteaweb.io.FileUtils.createRestrictedFile(FileUtils.java:182)
at net.adoptopenjdk.icedteaweb.io.FileUtils.createRestrictedFile(FileUtils.java:345)
**java.io.IOException: Cannot rename C:\Temp\ppp.temp to C:\Temp\ppp**

One workaround is to create an empty text file first then write to it - the file MUST exist for it to be written to. This is not ideal.

Upon close inspection/debugging I found in method writeTofile in class XFileSaveService.class:

/**

Writes actual file to disk.
*/
private void writeToFile(InputStream stream, File file) throws IOException {
**if (!file.createNewFile())** { //file exists boolean replace = (JOptionPane.showConfirmDialog(null, file.getAbsolutePath() + " already exists.\n" + "Do you want to replace it?", "Warning - File Exists", JOptionPane.YES_NO_OPTION) == 0); if (!replace) return; }
else

{ RestrictedFileUtils.createRestrictedFile(file); }
if (file.canWrite())

{ FileOutputStream out = new FileOutputStream(file); byte[] b = new byte[256]; int read = 0; while ((read = stream.read(b)) > 0) out.write(b, 0, read); out.flush(); out.close(); }
else

{ throw new IOException("Unable to open file for writing"); }
}
}
This method uses !file.createNewFile() in an if to check if the file exists - this causes a file to be created such that when a rename attempt is made the file already exists so can not be created. A rewrite of the open source code is being attempted as a way to remedy this issue.

Is this an actual problem - has anyone else experienced this on Windows (or even linux). I took a copy of the writeToFile method and replaced the (!file.createNewFile()) with file.exists() and this sorted the problem out.

Please enlighten me.

Thanks,
Shinal

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.