From 118ac28e4c170949f0e49271738a531d181bcf4c Mon Sep 17 00:00:00 2001 From: ME1312 Date: Mon, 14 Jan 2019 18:47:43 -0500 Subject: [PATCH] Don't follow symlinks for directory copies --- .../SubServers/Bungee/Library/Util.java | 34 +++--------------- .../Client/Bukkit/Library/Util.java | 31 +++------------- .../Client/Sponge/Library/Util.java | 31 +++------------- SubServers.Host/pom.xml | 2 +- .../ME1312/SubServers/Sync/Library/Util.java | 35 +++---------------- 5 files changed, 18 insertions(+), 115 deletions(-) diff --git a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Util.java b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Util.java index 163ba236..e5d73d7e 100644 --- a/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Util.java +++ b/SubServers.Bungee/src/net/ME1312/SubServers/Bungee/Library/Util.java @@ -6,6 +6,7 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.file.Files; +import java.nio.file.LinkOption; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -267,7 +268,7 @@ public final class Util { * @param to Destination */ public static void copyDirectory(File from, File to) { - if (from.isDirectory()) { + if (from.isDirectory() && !Files.isSymbolicLink(from.toPath())) { if (!to.exists()) { to.mkdirs(); } @@ -281,37 +282,10 @@ public final class Util { copyDirectory(srcFile, destFile); } } else { - if (to.exists()) { - to.delete(); - } - InputStream in = null; - OutputStream out = null; - try { - in = new FileInputStream(from); - out = new FileOutputStream(to, false); - - byte[] buffer = new byte[1024]; - - int length; - while ((length = in.read(buffer)) > 0) { - out.write(buffer, 0, length); - } - - in.close(); - out.close(); + Files.copy(from.toPath(), to.toPath(), LinkOption.NOFOLLOW_LINKS); } catch (Exception e) { - try { - if (in != null) in.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - - try { - if (out != null) out.close(); - } catch (IOException e2) { - e2.printStackTrace(); - } + e.printStackTrace(); } } } diff --git a/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Library/Util.java b/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Library/Util.java index 7f719d2e..1e2a82e9 100644 --- a/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Library/Util.java +++ b/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Library/Util.java @@ -6,6 +6,7 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.file.Files; +import java.nio.file.LinkOption; import java.util.*; /** @@ -264,7 +265,7 @@ public final class Util { * @param to Destination */ public static void copyDirectory(File from, File to) { - if (from.isDirectory()) { + if (from.isDirectory() && !Files.isSymbolicLink(from.toPath())) { if (!to.exists()) { to.mkdirs(); } @@ -278,34 +279,10 @@ public final class Util { copyDirectory(srcFile, destFile); } } else { - InputStream in = null; - OutputStream out = null; - try { - in = new FileInputStream(from); - out = new FileOutputStream(to); - - byte[] buffer = new byte[1024]; - - int length; - while ((length = in.read(buffer)) > 0) { - out.write(buffer, 0, length); - } - - in.close(); - out.close(); + Files.copy(from.toPath(), to.toPath(), LinkOption.NOFOLLOW_LINKS); } catch (Exception e) { - try { - if (in != null) in.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - - try { - if (out != null) out.close(); - } catch (IOException e2) { - e2.printStackTrace(); - } + e.printStackTrace(); } } } diff --git a/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/Library/Util.java b/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/Library/Util.java index 4b247d7c..f61bcdd2 100644 --- a/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/Library/Util.java +++ b/SubServers.Client/Sponge/src/net/ME1312/SubServers/Client/Sponge/Library/Util.java @@ -6,6 +6,7 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.file.Files; +import java.nio.file.LinkOption; import java.util.*; /** @@ -264,7 +265,7 @@ public final class Util { * @param to Destination */ public static void copyDirectory(File from, File to) { - if (from.isDirectory()) { + if (from.isDirectory() && !Files.isSymbolicLink(from.toPath())) { if (!to.exists()) { to.mkdirs(); } @@ -278,34 +279,10 @@ public final class Util { copyDirectory(srcFile, destFile); } } else { - InputStream in = null; - OutputStream out = null; - try { - in = new FileInputStream(from); - out = new FileOutputStream(to); - - byte[] buffer = new byte[1024]; - - int length; - while ((length = in.read(buffer)) > 0) { - out.write(buffer, 0, length); - } - - in.close(); - out.close(); + Files.copy(from.toPath(), to.toPath(), LinkOption.NOFOLLOW_LINKS); } catch (Exception e) { - try { - if (in != null) in.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - - try { - if (out != null) out.close(); - } catch (IOException e2) { - e2.printStackTrace(); - } + e.printStackTrace(); } } } diff --git a/SubServers.Host/pom.xml b/SubServers.Host/pom.xml index 38250f35..26d1047d 100644 --- a/SubServers.Host/pom.xml +++ b/SubServers.Host/pom.xml @@ -20,7 +20,7 @@ net.ME1312.Galaxi GalaxiEngine - 19w03a + 19w03b compile diff --git a/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Util.java b/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Util.java index 2a6be18d..300f27f2 100644 --- a/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Util.java +++ b/SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Util.java @@ -6,6 +6,7 @@ import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.nio.file.Files; +import java.nio.file.LinkOption; import java.util.*; /** @@ -264,48 +265,22 @@ public final class Util { * @param to Destination */ public static void copyDirectory(File from, File to) { - if (from.isDirectory()) { + if (from.isDirectory() && !Files.isSymbolicLink(from.toPath())) { if (!to.exists()) { to.mkdirs(); } - String files[] = from.list(); - - for (String file : files) { + for (String file : from.list()) { File srcFile = new File(from, file); File destFile = new File(to, file); copyDirectory(srcFile, destFile); } } else { - InputStream in = null; - OutputStream out = null; - try { - in = new FileInputStream(from); - out = new FileOutputStream(to); - - byte[] buffer = new byte[1024]; - - int length; - while ((length = in.read(buffer)) > 0) { - out.write(buffer, 0, length); - } - - in.close(); - out.close(); + Files.copy(from.toPath(), to.toPath(), LinkOption.NOFOLLOW_LINKS); } catch (Exception e) { - try { - if (in != null) in.close(); - } catch (IOException e1) { - e1.printStackTrace(); - } - - try { - if (out != null) out.close(); - } catch (IOException e2) { - e2.printStackTrace(); - } + e.printStackTrace(); } } }