Don't follow symlinks for directory copies

This commit is contained in:
ME1312 2019-01-14 18:47:43 -05:00
parent 622ef32b4e
commit 118ac28e4c
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
5 changed files with 18 additions and 115 deletions

View File

@ -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();
}
}
}

View File

@ -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();
}
}
}

View File

@ -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();
}
}
}

View File

@ -20,7 +20,7 @@
<dependency>
<groupId>net.ME1312.Galaxi</groupId>
<artifactId>GalaxiEngine</artifactId>
<version>19w03a</version>
<version>19w03b</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@ -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();
}
}
}