Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
Armin Schrenk committed Mar 5, 2019
1 parent 662c388 commit 8a977f4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
32 changes: 31 additions & 1 deletion src/main/java/org/cryptomator/frontend/dokany/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,43 @@ public static void setAttribute(DosFileAttributeView attrView, FileAttribute att
}
}

/**
* Converts search string expression from the windows kernel to valid glob search patterns
* <p>
* TODO: maybe both methods should be merged for speed up?
*
* @param rawWindowsPattern a String that may contain unescaped glob patterns or windows kernel specific search patterns
* @return the corresponding glob search pattern for a {@link java.nio.file.PathMatcher}
*/
public static String convertToGlobPattern(String rawWindowsPattern) {
String javaPattern = convertToJavaPattern(rawWindowsPattern);
return addEscapeSequencesForPathPattern(javaPattern);
}

/**
* Replaces specific characters that are windows kernel specific search patterns. It is no problem to replace them directly, since all three are invalid filename characters (see <a href="https://docs.microsoft.com/de-de/windows/desktop/FileIO/naming-a-file"> Microsoft documentation</a>)
* 1. '>' is replaced by '?'
* 2. '<' is replaced by '*'
* 3. '"' is replaced by '.'
*
* @param rawWindowsPattern a String that may contain windows kernel specific search patterns
* @return
*/
private static String convertToJavaPattern(String rawWindowsPattern) {
String tmp1 = rawWindowsPattern.replace('>', '?');
String tmp2 = tmp1.replace('<', '*');
String tmp3 = tmp2.replace('"', '.');
return tmp3;
}

/**
* Method for preprocessing a string containing glob patterns for a {@link java.nio.file.PathMatcher}. These characters must be escaped to not cause a different matching expression.
* This method escapes the characters defined in {@link FileUtil#globOperatorsToEscapeCodePoints}.
*
* @param rawPattern a string possibly containing unwanted glob operators
* @return a String where some glob operators are escaped
*/
public static String addEscapeSequencesForPathPattern(String rawPattern) {
private static String addEscapeSequencesForPathPattern(String rawPattern) {
return rawPattern.codePoints().flatMap(c -> {
if (Character.isBmpCodePoint(c) && globOperatorsToEscapeCodePoints.contains(c)) {
return IntStream.of((int) '\\', c);
Expand All @@ -102,6 +131,7 @@ public static String addEscapeSequencesForPathPattern(String rawPattern) {
}).collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append).toString();
}


public static Set<OpenOption> buildOpenOptions(EnumIntegerSet<AccessMask> accessMasks, EnumIntegerSet<FileAccessMask> fileAccessMasks, EnumIntegerSet<FileAttribute> fileAttributes, EnumIntegerSet<CreateOptions> createOptions, CreationDisposition creationDisposition, boolean append, boolean fileExists) {
Set<OpenOption> openOptions = Sets.newHashSet();
if (accessMasks.contains(AccessMask.GENERIC_WRITE) || accessMasks.contains(AccessMask.DELETE) || fileAccessMasks.contains(FileAccessMask.READ_DATA)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public int findFilesWithPattern(WString fileName, WString searchPattern, DokanyO
} else {
// we want to filter by glob
// since the Java API does NOT specify on which string representation a pathMatcher compares a path to a given expression, we assume NFC
String nfcSearchPattern = Normalizer.normalize(FileUtil.addEscapeSequencesForPathPattern(searchPattern.toString()), Normalizer.Form.NFC);
String nfcSearchPattern = Normalizer.normalize(FileUtil.convertToGlobPattern(searchPattern.toString()), Normalizer.Form.NFC);
PathMatcher matcher = path.getFileSystem().getPathMatcher("glob:" + nfcSearchPattern);
filter = (Path p) -> matcher.matches(p.getFileName());
}
Expand Down

0 comments on commit 8a977f4

Please sign in to comment.