Skip to content

Commit

Permalink
Don't follow symlinks for directory copies
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Jan 14, 2019
1 parent 622ef32 commit 118ac28
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 115 deletions.
Expand Up @@ -6,6 +6,7 @@
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;
Expand Down Expand Up @@ -267,7 +268,7 @@ public static void deleteDirectory(File folder) {
* @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();
}
Expand All @@ -281,37 +282,10 @@ public static void copyDirectory(File from, File to) {
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();
}
}
}
Expand Down
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.util.*;

/**
Expand Down Expand Up @@ -264,7 +265,7 @@ public static void deleteDirectory(File folder) {
* @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();
}
Expand All @@ -278,34 +279,10 @@ public static void copyDirectory(File from, File to) {
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();
}
}
}
Expand Down
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.util.*;

/**
Expand Down Expand Up @@ -264,7 +265,7 @@ public static void deleteDirectory(File folder) {
* @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();
}
Expand All @@ -278,34 +279,10 @@ public static void copyDirectory(File from, File to) {
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();
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion SubServers.Host/pom.xml
Expand Up @@ -20,7 +20,7 @@
<dependency>
<groupId>net.ME1312.Galaxi</groupId>
<artifactId>GalaxiEngine</artifactId>
<version>19w03a</version>
<version>19w03b</version>
<scope>compile</scope>
</dependency>
<dependency>
Expand Down
35 changes: 5 additions & 30 deletions SubServers.Sync/src/net/ME1312/SubServers/Sync/Library/Util.java
Expand Up @@ -6,6 +6,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.util.*;

/**
Expand Down Expand Up @@ -264,48 +265,22 @@ public static void deleteDirectory(File folder) {
* @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();
}
}
}
Expand Down

0 comments on commit 118ac28

Please sign in to comment.