diff --git a/platform/bootstrap/src/com/intellij/ide/startup/StartupActionScriptManager.java b/platform/bootstrap/src/com/intellij/ide/startup/StartupActionScriptManager.java index 690ded71d307..3a21db3d568a 100644 --- a/platform/bootstrap/src/com/intellij/ide/startup/StartupActionScriptManager.java +++ b/platform/bootstrap/src/com/intellij/ide/startup/StartupActionScriptManager.java @@ -13,15 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -/** - * @author cdr - */ package com.intellij.ide.startup; import com.intellij.openapi.application.PathManager; import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.util.io.FileUtilRt; import com.intellij.util.io.ZipUtil; import org.jetbrains.annotations.NonNls; @@ -31,6 +27,9 @@ import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; +/** + * @author cdr + */ @SuppressWarnings({"HardCodedStringLiteral"}) public class StartupActionScriptManager { private static final Logger LOG = Logger.getInstance("#com.intellij.ide.startup.StartupActionScriptManager"); @@ -109,7 +108,7 @@ public class StartupActionScriptManager { } private static boolean canCreateFile(File file) { - return FileUtil.ensureCanCreateFile(file); + return FileUtilRt.ensureCanCreateFile(file); } public interface ActionCommand { @@ -156,7 +155,7 @@ public class StartupActionScriptManager { "Installing Plugin", JOptionPane.ERROR_MESSAGE); } else { - FileUtil.copy(mySource, myDestination); + FileUtilRt.copy(mySource, myDestination); } } @@ -223,7 +222,7 @@ public class StartupActionScriptManager { } public void execute() throws IOException { - if (mySource != null && mySource.exists() && !FileUtil.delete(mySource)) { + if (mySource != null && mySource.exists() && !FileUtilRt.delete(mySource)) { //noinspection HardCodedStringLiteral System.err.println("Action " + this + " failed."); JOptionPane.showMessageDialog(JOptionPane.getRootFrame(), diff --git a/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java b/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java index 3b8b2bd9ab3f..2d07237cea51 100644 --- a/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java +++ b/platform/util-rt/src/com/intellij/openapi/util/io/FileUtilRt.java @@ -24,6 +24,7 @@ import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.TestOnly; import java.io.*; +import java.nio.channels.FileChannel; import java.util.UUID; /** @@ -36,7 +37,10 @@ import java.util.UUID; public class FileUtilRt { public static final int MEGABYTE = 1024 * 1024; public static final int LARGE_FOR_CONTENT_LOADING = 20 * MEGABYTE; + private static final LoggerRt LOG = LoggerRt.getInstance("#com.intellij.openapi.util.io.FileUtilLight"); + private static final int MAX_FILE_DELETE_ATTEMPTS = 10; + private static final boolean USE_FILE_CHANNELS = "true".equalsIgnoreCase(System.getProperty("idea.fs.useChannels")); protected static final ThreadLocal BUFFER = new ThreadLocal() { @Override @@ -408,4 +412,122 @@ public class FileUtilRt { return parentFile; } } + + public static boolean delete(@NotNull File file) { + if (file.isDirectory()) { + if (!deleteChildren(file)) return false; + } + return deleteFile(file); + } + + protected static boolean deleteChildren(@NotNull File file) { + File[] files = file.listFiles(); + if (files != null) { + for (File child : files) { + if (!delete(child)) return false; + } + } + return true; + } + + protected static boolean deleteFile(@NotNull File file) { + for (int i = 0; i < MAX_FILE_DELETE_ATTEMPTS; i++) { + if (file.delete() || !file.exists()) return true; + try { + //noinspection BusyWait + Thread.sleep(10); + } + catch (InterruptedException ignored) { } + } + return false; + } + + public static boolean ensureCanCreateFile(@NotNull File file) { + if (file.exists()) return file.canWrite(); + if (!createIfNotExists(file)) return false; + return delete(file); + } + + public static boolean createIfNotExists(@NotNull File file) { + if (file.exists()) return true; + try { + if (!createParentDirs(file)) return false; + + OutputStream s = new FileOutputStream(file); + s.close(); + return true; + } + catch (IOException e) { + LOG.info(e); + return false; + } + } + + public static boolean createParentDirs(@NotNull File file) { + if (!file.exists()) { + final File parentFile = file.getParentFile(); + if (parentFile != null) { + return createDirectory(parentFile); + } + } + return true; + } + + public static boolean createDirectory(@NotNull File path) { + return path.isDirectory() || path.mkdirs(); + } + + public static void copy(@NotNull File fromFile, @NotNull File toFile) throws IOException { + if (!ensureCanCreateFile(toFile)) { + return; + } + + FileOutputStream fos = new FileOutputStream(toFile); + try { + FileInputStream fis = new FileInputStream(fromFile); + try { + copy(fis, fos); + } + finally { + fis.close(); + } + } + finally { + fos.close(); + } + + long timeStamp = fromFile.lastModified(); + if (timeStamp < 0) { + LOG.warn("Invalid timestamp " + timeStamp + " of '" + fromFile + "'"); + } + else if (!toFile.setLastModified(timeStamp)) { + LOG.warn("Unable to set timestamp " + timeStamp + " to '" + toFile + "'"); + } + } + + public static void copy(@NotNull InputStream inputStream, @NotNull OutputStream outputStream) throws IOException { + if (USE_FILE_CHANNELS && inputStream instanceof FileInputStream && outputStream instanceof FileOutputStream) { + final FileChannel fromChannel = ((FileInputStream)inputStream).getChannel(); + try { + final FileChannel toChannel = ((FileOutputStream)outputStream).getChannel(); + try { + fromChannel.transferTo(0, Long.MAX_VALUE, toChannel); + } + finally { + toChannel.close(); + } + } + finally { + fromChannel.close(); + } + } + else { + final byte[] buffer = BUFFER.get(); + while (true) { + int read = inputStream.read(buffer); + if (read < 0) break; + outputStream.write(buffer, 0, read); + } + } + } } diff --git a/platform/util/src/com/intellij/openapi/util/io/FileUtil.java b/platform/util/src/com/intellij/openapi/util/io/FileUtil.java index 39af02b48e4d..373ce9972316 100644 --- a/platform/util/src/com/intellij/openapi/util/io/FileUtil.java +++ b/platform/util/src/com/intellij/openapi/util/io/FileUtil.java @@ -33,7 +33,6 @@ import org.jetbrains.annotations.TestOnly; import java.io.*; import java.lang.reflect.Method; -import java.nio.channels.FileChannel; import java.util.*; import java.util.concurrent.*; import java.util.regex.Pattern; @@ -65,10 +64,6 @@ public class FileUtil extends FileUtilRt { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.util.io.FileUtil"); - private static final int MAX_FILE_DELETE_ATTEMPTS = 10; - - private static final boolean USE_FILE_CHANNELS = SystemProperties.getBooleanProperty("idea.fs.useChannels", false); - @NotNull public static String join(@NotNull final String... parts) { return StringUtil.join(parts, File.separator); @@ -419,59 +414,30 @@ public class FileUtil extends FileUtilRt { } public static boolean delete(@NotNull File file) { - if (file.isDirectory() && !FileSystemUtil.isSymLink(file)) { - File[] files = file.listFiles(); - if (files != null) { - for (File child : files) { - if (!delete(child)) return false; - } - } + FileAttributes attributes = FileSystemUtil.getAttributes(file); + if (attributes == null) return true; + + if (attributes.isDirectory() && !attributes.isSymLink()) { + if (!deleteChildren(file)) return false; } - for (int i = 0; i < MAX_FILE_DELETE_ATTEMPTS; i++) { - if (file.delete() || !file.exists()) return true; - try { - //noinspection BusyWait - Thread.sleep(10); - } - catch (InterruptedException ignored) { } - } - return false; + return deleteFile(file); } public static boolean createParentDirs(@NotNull File file) { - if (!file.exists()) { - final File parentFile = file.getParentFile(); - if (parentFile != null) { - return createDirectory(parentFile); - } - } - return true; + return FileUtilRt.createParentDirs(file); } public static boolean createDirectory(@NotNull File path) { - return path.isDirectory() || path.mkdirs(); + return FileUtilRt.createDirectory(path); } public static boolean createIfDoesntExist(@NotNull File file) { - if (file.exists()) return true; - try { - if (!createParentDirs(file)) return false; - - OutputStream s = new FileOutputStream(file); - s.close(); - return true; - } - catch (IOException e) { - LOG.info(e); - return false; - } + return FileUtilRt.createIfNotExists(file); } public static boolean ensureCanCreateFile(@NotNull File file) { - if (file.exists()) return file.canWrite(); - if (!createIfDoesntExist(file)) return false; - return delete(file); + return FileUtilRt.ensureCanCreateFile(file); } public static void copy(@NotNull File fromFile, @NotNull File toFile) throws IOException { @@ -542,29 +508,7 @@ public class FileUtil extends FileUtilRt { } public static void copy(@NotNull InputStream inputStream, @NotNull OutputStream outputStream) throws IOException { - if (USE_FILE_CHANNELS && inputStream instanceof FileInputStream && outputStream instanceof FileOutputStream) { - final FileChannel fromChannel = ((FileInputStream)inputStream).getChannel(); - try { - final FileChannel toChannel = ((FileOutputStream)outputStream).getChannel(); - try { - fromChannel.transferTo(0, Long.MAX_VALUE, toChannel); - } - finally { - toChannel.close(); - } - } - finally { - fromChannel.close(); - } - } - else { - final byte[] buffer = BUFFER.get(); - while (true) { - int read = inputStream.read(buffer); - if (read < 0) break; - outputStream.write(buffer, 0, read); - } - } + FileUtilRt.copy(inputStream, outputStream); } public static void copy(@NotNull InputStream inputStream, int maxSize, @NotNull OutputStream outputStream) throws IOException {