diff --git a/src/main/java/caosdb/server/jobs/core/InsertFilesInDir.java b/src/main/java/caosdb/server/jobs/core/InsertFilesInDir.java index 7db04f124aa45b81d3484db3a9d0ea33ca74a5ba..ea8836e61f042d5b5fadae340fd23cd3a520eec2 100644 --- a/src/main/java/caosdb/server/jobs/core/InsertFilesInDir.java +++ b/src/main/java/caosdb/server/jobs/core/InsertFilesInDir.java @@ -62,6 +62,9 @@ public class InsertFilesInDir extends FlagJob { private Pattern include = null; private Pattern exclude = null; private boolean forceSymLinks = false; + private Pattern valueParser = + Pattern.compile( + "(?:(?:-p\\s*([^\\s]*?)\\s+)|(?:-i\\s*([^\\s]*?)\\s+)|(?:-e\\s*([^\\s]*?)\\s+)|(--force-allow-symlinks\\s+))|([^-].*)"); /** * @return a List of directories which subdirs are allowed to be batch-added. Needs to be @@ -86,14 +89,10 @@ public class InsertFilesInDir extends FlagJob { return ret; } - @Override - protected void job(final String value) { + public String parseValue(String value) { - String dirStr = value; - final Pattern pattern = - Pattern.compile( - "(?:(?:-p\\s*([^\\s]*?)\\s+)|(?:-i\\s*([^\\s]*?)\\s+)|(?:-e\\s*([^\\s]*?)\\s+)|(--force-allow-symlinks\\s+))|([^-].*)"); - final Matcher matcher = pattern.matcher(value); + String ret = value; + final Matcher matcher = valueParser.matcher(value); while (matcher.find()) { if (matcher.group(1) != null) { this.prefix = matcher.group(1).replaceFirst("/$", "") + "/"; @@ -108,9 +107,16 @@ public class InsertFilesInDir extends FlagJob { this.forceSymLinks = true; } if (matcher.group(5) != null) { - dirStr = matcher.group(5); + ret = matcher.group(5); } } + return ret; + } + + @Override + protected void job(final String value) { + + String dirStr = parseValue(value); final File dir = new File(dirStr); @@ -247,8 +253,16 @@ public class InsertFilesInDir extends FlagJob { return i; } + boolean isExcluded(File f) throws IOException { + return this.exclude != null && this.exclude.matcher(f.getCanonicalPath()).find(); + } + + boolean isNotIncluded(File f) throws IOException { + return this.include != null && !this.include.matcher(f.getCanonicalPath()).find(); + } + private boolean shouldBeProcessed(final File sub) throws IOException { - if (this.include != null && !this.include.matcher(sub.getCanonicalPath()).matches()) { + if (this.isNotIncluded(sub)) { getContainer() .addMessage( new Message( @@ -257,7 +271,7 @@ public class InsertFilesInDir extends FlagJob { "Not explicitly included directory or file: " + sub.getCanonicalPath())); return false; } - if (this.exclude != null && this.exclude.matcher(sub.getCanonicalPath()).matches()) { + if (this.isExcluded(sub)) { getContainer() .addMessage( new Message( diff --git a/src/test/java/caosdb/server/jobs/core/TestInsertFilesInDir.java b/src/test/java/caosdb/server/jobs/core/TestInsertFilesInDir.java new file mode 100644 index 0000000000000000000000000000000000000000..37cad0eca28a50b006985a73edb07e45b60fdf9e --- /dev/null +++ b/src/test/java/caosdb/server/jobs/core/TestInsertFilesInDir.java @@ -0,0 +1,19 @@ +package caosdb.server.jobs.core; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; +import org.junit.Test; + +public class TestInsertFilesInDir { + + @Test + public void testExclude() throws IOException { + InsertFilesInDir job = new InsertFilesInDir(); + job.init(null, null, null); + job.parseValue("-e ^.*test.*$ test"); + File testFile = new File("test.dat"); + assertTrue(job.isExcluded(testFile)); + } +}