Skip to content

Commit

Permalink
Code Cleanup, Fixed Overwrite issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
EATSTEAK committed Nov 27, 2016
1 parent cc0083b commit 512dd67
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 41 deletions.
7 changes: 3 additions & 4 deletions src/me/macjuul/chfiles/Functions/copy_file.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.annotations.api;
import com.laytonsmith.core.Security;
import com.laytonsmith.core.constructs.CBoolean;
import com.laytonsmith.core.constructs.CVoid;
import com.laytonsmith.core.constructs.Construct;
import com.laytonsmith.core.constructs.Target;
Expand Down Expand Up @@ -48,13 +47,13 @@ public Boolean runAsync() {
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
File fromLoc = new File(t.file().getParentFile(), args[0].val());
File toLoc = new File(t.file().getParentFile(), args[1].val());
if(!Security.CheckSecurity(fromLoc) || !Security.CheckSecurity(toLoc)) {
if (!Security.CheckSecurity(fromLoc) || !Security.CheckSecurity(toLoc)) {
throw new CRESecurityException("You do not have access to some of the files", t);
}
try {
if(fromLoc.isDirectory()) {
if (fromLoc.isDirectory()) {
FileUtils.copyDirectory(fromLoc, toLoc);
} else if(fromLoc.isFile()) {
} else if (fromLoc.isFile()) {
FileUtils.copyFile(fromLoc, toLoc);
}
return CVoid.VOID;
Expand Down
6 changes: 2 additions & 4 deletions src/me/macjuul/chfiles/Functions/create_dir.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.macjuul.chfiles.Functions;

import com.laytonsmith.PureUtilities.Common.FileUtil;
import com.laytonsmith.PureUtilities.SimpleVersion;
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.annotations.api;
Expand All @@ -16,7 +15,6 @@
import com.laytonsmith.core.functions.AbstractFunction;

import java.io.File;
import java.io.IOException;

/**
* Created by bexco on 2016-03-26.
Expand Down Expand Up @@ -44,11 +42,11 @@ public Boolean runAsync() {
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
File loc = new File(t.file().getParentFile(), args[0].val());
if(!Security.CheckSecurity(loc)) {
if (!Security.CheckSecurity(loc)) {
throw new CRESecurityException("You do not have permission to access the file '" + loc.getAbsolutePath() + "'", t);
}
try {
if(loc.exists()) {
if (loc.exists()) {
throw new CREIOException(loc.getAbsolutePath() + "Already Exists", t);
}
loc.mkdir();
Expand Down
5 changes: 2 additions & 3 deletions src/me/macjuul/chfiles/Functions/create_file.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.macjuul.chfiles.Functions;

import com.laytonsmith.PureUtilities.Common.FileUtil;
import com.laytonsmith.PureUtilities.SimpleVersion;
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.annotations.api;
Expand Down Expand Up @@ -44,11 +43,11 @@ public Boolean runAsync() {
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
File loc = new File(t.file().getParentFile(), args[0].val());
if(!Security.CheckSecurity(loc)) {
if (!Security.CheckSecurity(loc)) {
throw new CRESecurityException("You do not have permission to access the file '" + loc.getAbsolutePath() + "'", t);
}
try {
if(loc.exists()) {
if (loc.exists()) {
throw new CREIOException(loc.getAbsolutePath() + "Already Exists", t);
}
loc.createNewFile();
Expand Down
9 changes: 4 additions & 5 deletions src/me/macjuul/chfiles/Functions/delete_file.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.macjuul.chfiles.Functions;

import com.laytonsmith.PureUtilities.Common.FileUtil;
import com.laytonsmith.PureUtilities.SimpleVersion;
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.annotations.api;
Expand Down Expand Up @@ -45,16 +44,16 @@ public Boolean runAsync() {
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
File loc = new File(t.file().getParentFile(), args[0].val());
if(!Security.CheckSecurity(loc)) {
if (!Security.CheckSecurity(loc)) {
throw new CRESecurityException("You do not have permission to access the file '" + loc.getAbsolutePath() + "'", t);
}
try {
if(!loc.exists()) {
if (!loc.exists()) {
throw new CREIOException(loc.getAbsolutePath() + "Doesn't exists", t);
}
if(loc.isDirectory()) {
if (loc.isDirectory()) {
FileUtils.deleteDirectory(loc);
} else if(loc.isFile()) {
} else if (loc.isFile()) {
FileUtils.forceDelete(loc);
}
return CVoid.VOID;
Expand Down
7 changes: 1 addition & 6 deletions src/me/macjuul/chfiles/Functions/file_exists.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,15 @@
import com.laytonsmith.annotations.api;
import com.laytonsmith.core.Security;
import com.laytonsmith.core.constructs.CBoolean;
import com.laytonsmith.core.constructs.CVoid;
import com.laytonsmith.core.constructs.Construct;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.environments.Environment;
import com.laytonsmith.core.exceptions.CRE.CREIOException;
import com.laytonsmith.core.exceptions.CRE.CRESecurityException;
import com.laytonsmith.core.exceptions.CRE.CREThrowable;
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
import com.laytonsmith.core.functions.AbstractFunction;
import com.laytonsmith.libs.org.apache.commons.io.FileUtils;
import org.bukkit.Bukkit;

import java.io.File;
import java.io.IOException;

/**
* Created by bexco on 2016-03-16.
Expand All @@ -45,7 +40,7 @@ public Boolean runAsync() {
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
File loc = new File(t.file().getParentFile(), args[0].val());
if(!Security.CheckSecurity(loc)) {
if (!Security.CheckSecurity(loc)) {
throw new CRESecurityException("You do not have permission to access the file '" + loc.getAbsolutePath() + "'", t);
}
return CBoolean.get(loc.exists());
Expand Down
2 changes: 1 addition & 1 deletion src/me/macjuul/chfiles/Functions/is_dir.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Boolean runAsync() {
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
File loc = new File(t.file().getParentFile(), args[0].val());
if(!Security.CheckSecurity(loc)) {
if (!Security.CheckSecurity(loc)) {
throw new CRESecurityException("You do not have permission to access the file '" + loc.getAbsolutePath() + "'", t);
}
return CBoolean.get(loc.isDirectory());
Expand Down
2 changes: 1 addition & 1 deletion src/me/macjuul/chfiles/Functions/is_file.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public Boolean runAsync() {
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
File loc = new File(t.file().getParentFile(), args[0].val());
if(!Security.CheckSecurity(loc)) {
if (!Security.CheckSecurity(loc)) {
throw new CRESecurityException("You do not have permission to access the file '" + loc.getAbsolutePath() + "'", t);
}
return CBoolean.get(loc.isFile());
Expand Down
11 changes: 7 additions & 4 deletions src/me/macjuul/chfiles/Functions/list_files.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import com.laytonsmith.PureUtilities.Version;
import com.laytonsmith.annotations.api;
import com.laytonsmith.core.Security;
import com.laytonsmith.core.constructs.*;
import com.laytonsmith.core.constructs.CArray;
import com.laytonsmith.core.constructs.CString;
import com.laytonsmith.core.constructs.Construct;
import com.laytonsmith.core.constructs.Target;
import com.laytonsmith.core.environments.Environment;
import com.laytonsmith.core.exceptions.CRE.CREIOException;
import com.laytonsmith.core.exceptions.CRE.CRESecurityException;
Expand Down Expand Up @@ -40,13 +43,13 @@ public Boolean runAsync() {
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
File loc = new File(t.file().getParentFile(), args[0].val());
if(!Security.CheckSecurity(loc)) {
if (!Security.CheckSecurity(loc)) {
throw new CRESecurityException("You do not have permission to access the file '" + loc.getAbsolutePath() + "'", t);
}
CArray ret = new CArray(t);
if(loc.exists() && loc.isDirectory()) {
if (loc.exists() && loc.isDirectory()) {
String[] list = loc.list();
for(String file:list) {
for (String file : list) {
ret.push(new CString(file, t), t);
}
} else {
Expand Down
10 changes: 4 additions & 6 deletions src/me/macjuul/chfiles/Functions/rename_file.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
import com.laytonsmith.core.exceptions.CRE.CREThrowable;
import com.laytonsmith.core.exceptions.ConfigRuntimeException;
import com.laytonsmith.core.functions.AbstractFunction;
import com.laytonsmith.libs.org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;

/**
* Created by bexco on 2016-03-16.
Expand Down Expand Up @@ -44,15 +42,15 @@ public Boolean runAsync() {
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
File loc = new File(t.file().getParentFile(), args[0].val());
if(!Security.CheckSecurity(loc)) {
if (!Security.CheckSecurity(loc)) {
throw new CRESecurityException("You do not have permission to access the file '" + loc.getAbsolutePath() + "'", t);
}
if(!loc.exists()) {
if (!loc.exists()) {
throw new CREIOException(loc.getAbsolutePath() + "Doesn't exists", t);
}
if(loc.isDirectory()) {
if (loc.isDirectory()) {
loc.renameTo(new File(loc.getParent() + File.pathSeparator + args[1].val() + File.pathSeparator));
} else if(loc.isFile()) {
} else if (loc.isFile()) {
loc.renameTo(new File(loc.getParent() + File.pathSeparator + args[1].val()));
}
return CVoid.VOID;
Expand Down
14 changes: 7 additions & 7 deletions src/me/macjuul/chfiles/Functions/write_file.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ public Boolean runAsync() {
@Override
public Construct exec(Target t, Environment env, Construct... args) throws ConfigRuntimeException {
File loc = new File(t.file().getParentFile(), args[0].val());
if(!Security.CheckSecurity(loc)) {
if (!Security.CheckSecurity(loc)) {
throw new CRESecurityException("You do not have permission to access the file '" + loc.getAbsolutePath() + "'", t);
}
try {
if(!loc.exists()) {
if (!loc.exists()) {
throw new CREIOException(loc.getAbsolutePath() + "Doesn't exists", t);
}
if(args.length == 3 && args[2].val().toUpperCase().equals("OVERWRITE")) {
FileUtil.write(args[1].val(), loc, 0);
} else {
FileUtil.write(args[1].val(), loc, 1);
}
if (args.length == 3 && args[2].val().toUpperCase().equals("OVERWRITE")) {
FileUtil.write(args[1].val(), loc, 0);
} else {
FileUtil.write(args[1].val(), loc, 1);
}

return CVoid.VOID;
} catch (IOException e) {
Expand Down

0 comments on commit 512dd67

Please sign in to comment.