annotate src/org/tmatesoft/hg/internal/StoragePathHelper.java @ 418:528b6780a8bd

A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 22 Mar 2012 21:02:20 +0100
parents 464b4404e75d
children 48f993aa2f41
rev   line source
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
2 * Copyright (c) 2011-2012 TMate Software Ltd
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
102
a3a2e5deb320 Updated contact address to support@hg4j.com
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 83
diff changeset
15 * contact TMate Software at support@hg4j.com
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
19 import java.nio.ByteBuffer;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
20 import java.nio.CharBuffer;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
21 import java.nio.charset.Charset;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
22 import java.nio.charset.CharsetEncoder;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.util.Arrays;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.util.TreeSet;
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
25 import java.util.regex.Matcher;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
26 import java.util.regex.Pattern;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28 import org.tmatesoft.hg.util.PathRewrite;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30 /**
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 * @see http://mercurial.selenic.com/wiki/CaseFoldingPlan
80
4222b04f34ee Follow history of a file
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 74
diff changeset
32 * @see http://mercurial.selenic.com/wiki/fncacheRepoFormat
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
33 * @see http://mercurial.selenic.com/wiki/EncodingStrategy
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 *
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 * @author Artem Tikhomirov
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 * @author TMate Software Ltd.
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 */
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38 class StoragePathHelper implements PathRewrite {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 private final boolean store;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 private final boolean fncache;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42 private final boolean dotencode;
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
43 private final Pattern suffix2replace;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
44 private final CharsetEncoder csEncoder;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
45 private final char[] hexEncodedByte = new char[] {'~', '0', '0'};
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
46 private final ByteBuffer byteEncodingBuf;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
47 private final CharBuffer charEncodingBuf;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
48
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
49 public StoragePathHelper(boolean isStore, boolean isFncache, boolean isDotencode) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
50 this(isStore, isFncache, isDotencode, Charset.defaultCharset());
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
51 }
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
53 public StoragePathHelper(boolean isStore, boolean isFncache, boolean isDotencode, Charset fsEncoding) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
54 assert fsEncoding != null;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 store = isStore;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56 fncache = isFncache;
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 dotencode = isDotencode;
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
58 suffix2replace = Pattern.compile("\\.([id]|hg)/");
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
59 csEncoder = fsEncoding.newEncoder(); // FIXME catch exception and rethrow as our's RT
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
60 byteEncodingBuf = ByteBuffer.allocate(Math.round(csEncoder.maxBytesPerChar()) + 1/*in fact, need ceil, hence +1*/);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
61 charEncodingBuf = CharBuffer.allocate(1);
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 }
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63
418
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 411
diff changeset
64 /**
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 411
diff changeset
65 * path argument is repository-relative name of the user's file.
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 411
diff changeset
66 * It has to be normalized (slashes) and shall not include extension .i or .d.
528b6780a8bd A bit of FIXME cleanup (mostly degraded to TODO post 1.0), comments and javadoc
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 411
diff changeset
67 */
292
a415fe296a50 Refactor PathRewrite to accept any char sequence, not only string
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 138
diff changeset
68 public CharSequence rewrite(CharSequence p) {
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 final String STR_STORE = "store/";
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 final String STR_DATA = "data/";
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71 final String STR_DH = "dh/";
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
72 final String reservedChars = "\\:*?\"<>|";
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
74 Matcher suffixMatcher = suffix2replace.matcher(p);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
75 CharSequence path;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
76 // Matcher.replaceAll, but without extra toString
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
77 boolean found = suffixMatcher.find();
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
78 if (found) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
79 StringBuffer sb = new StringBuffer(p.length() + 20);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
80 do {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
81 suffixMatcher.appendReplacement(sb, ".$1.hg/");
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
82 } while (found = suffixMatcher.find());
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
83 suffixMatcher.appendTail(sb);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
84 path = sb;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
85 } else {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
86 path = p;
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
87 }
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
88
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 StringBuilder sb = new StringBuilder(path.length() << 1);
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 if (store || fncache) {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 for (int i = 0; i < path.length(); i++) {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 final char ch = path.charAt(i);
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 if (ch >= 'a' && ch <= 'z') {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 sb.append(ch); // POIRAE
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 } else if (ch >= 'A' && ch <= 'Z') {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 sb.append('_');
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
97 sb.append(Character.toLowerCase(ch)); // Perhaps, (char) (((int) ch) + 32)? Even better, |= 0x20?
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
98 } else if (reservedChars.indexOf(ch) != -1) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
99 sb.append(toHexByte(ch));
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 } else if ((ch >= '~' /*126*/ && ch <= 255) || ch < ' ' /*32*/) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
101 sb.append(toHexByte(ch));
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 } else if (ch == '_') {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
103 sb.append('_');
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
104 sb.append('_');
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 } else {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
106 // either ASCII char that doesn't require special handling, or an Unicode character to get encoded
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
107 // according to filesystem/native encoding, see http://mercurial.selenic.com/wiki/EncodingStrategy
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
108 // despite of what the page says, use of native encoding seems worst solution to me (repositories
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
109 // can't be easily shared between OS'es with different encodings then, e.g. Win1251 and Linux UTF8).
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
110 // If the ease of sharing was not the point, what's the reason to mangle with names at all then (
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
111 // lowercase and exclude reserved device names).
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
112 if (ch < '~' /*126*/ || !csEncoder.canEncode(ch)) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
113 sb.append(ch);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
114 } else {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
115 appendEncoded(sb, ch);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
116 }
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
117 }
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
118 }
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 // auxencode
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
120 if (fncache) {
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
121 encodeWindowsDeviceNames(sb);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
122 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
123 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
124 final int MAX_PATH_LEN = 120;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
125 if (fncache && (sb.length() + STR_DATA.length() + ".i".length() > MAX_PATH_LEN)) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
126 // TODO [post-1.0] Mercurial uses system encoding for paths, hence we need to pass bytes to DigestHelper
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
127 // to ensure our sha1 value (default encoding of unicode string if one looks into DH impl) match that
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
128 // produced by Mercurial (based on native string).
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
129 String digest = new DigestHelper().sha1(STR_DATA, path, ".i").asHexString();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
130 final int DIR_PREFIX_LEN = 8;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
131 // not sure why (-4) is here. 120 - 40 = up to 80 for path with ext. dh/ + ext(.i) = 3+2
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
132 final int MAX_DIR_PREFIX = 8 * (DIR_PREFIX_LEN + 1) - 4;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
133 sb = new StringBuilder(MAX_PATH_LEN);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
134 for (int i = 0; i < path.length(); i++) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
135 final char ch = path.charAt(i);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
136 if (ch >= 'a' && ch <= 'z') {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
137 sb.append(ch);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
138 } else if (ch >= 'A' && ch <= 'Z') {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
139 sb.append((char) (ch | 0x20)); // lowercase
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
140 } else if (reservedChars.indexOf(ch) != -1) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
141 sb.append(toHexByte(ch));
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
142 } else if ((ch >= '~' /*126*/ && ch <= 255) || ch < ' ' /*32*/) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
143 sb.append(toHexByte(ch));
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
144 } else {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
145 if (ch < '~' /*126*/ || !csEncoder.canEncode(ch)) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
146 sb.append(ch);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
147 } else {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
148 appendEncoded(sb, ch);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
149 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
150 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
151 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
152 encodeWindowsDeviceNames(sb);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
153 int fnameStart = sb.lastIndexOf("/"); // since we rewrite file names, it never ends with slash (for dirs, I'd pass length-2);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
154 StringBuilder completeHashName = new StringBuilder(MAX_PATH_LEN);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
155 completeHashName.append(STR_STORE);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
156 completeHashName.append(STR_DH);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
157 if (fnameStart == -1) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
158 // no dirs, just long filename
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
159 sb.setLength(MAX_PATH_LEN - 40 /*digest.length()*/ - STR_DH.length() - ".i".length());
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
160 completeHashName.append(sb);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
161 } else {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
162 StringBuilder sb2 = new StringBuilder(MAX_PATH_LEN);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
163 int x = 0;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
164 do {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
165 int i = sb.indexOf("/", x);
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
166 final int sb2Len = sb2.length();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
167 if (i-x <= DIR_PREFIX_LEN) { // a b c d e f g h /
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
168 sb2.append(sb, x, i + 1); // with slash
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
169 } else {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
170 sb2.append(sb, x, x + DIR_PREFIX_LEN);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
171 // may unexpectedly end with bad character
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
172 final int last = sb2.length()-1;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
173 char lastChar = sb2.charAt(last);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
174 assert lastChar == sb.charAt(x + DIR_PREFIX_LEN - 1);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
175 if (lastChar == '.' || lastChar == ' ') {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
176 sb2.setCharAt(last, '_');
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
177 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
178 sb2.append('/');
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
179 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
180 if (sb2.length()-1 > MAX_DIR_PREFIX) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
181 sb2.setLength(sb2Len); // strip off last segment, it's too much
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
182 break;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
183 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
184 x = i+1;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
185 } while (x < fnameStart);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
186 assert sb2.charAt(sb2.length() - 1) == '/';
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
187 int left = MAX_PATH_LEN - sb2.length() - 40 /*digest.length()*/ - STR_DH.length() - ".i".length();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
188 assert left >= 0;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
189 fnameStart++; // move from / to actual name
346
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
190 if (fnameStart + left > sb.length()) {
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
191 // there left less chars in the mangled name that we can fit
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
192 sb2.append(sb, fnameStart, sb.length());
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
193 int stillAvailable = (fnameStart+left) - sb.length();
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
194 // stillAvailable > 0;
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
195 sb2.append(".i", 0, stillAvailable > 2 ? 2 : stillAvailable);
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
196 } else {
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
197 // add as much as we can
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
198 sb2.append(sb, fnameStart, fnameStart+left);
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
199 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
200 completeHashName.append(sb2);
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
201 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
202 completeHashName.append(digest);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
203 sb = completeHashName;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
204 } else if (store) {
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
205 sb.insert(0, STR_STORE + STR_DATA);
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
206 }
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
207 sb.append(".i");
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
208 return sb.toString();
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
209 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
210
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
211 private void encodeWindowsDeviceNames(StringBuilder sb) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
212 int x = 0; // last segment start
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
213 final TreeSet<String> windowsReservedFilenames = new TreeSet<String>();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
214 windowsReservedFilenames.addAll(Arrays.asList("con prn aux nul com1 com2 com3 com4 com5 com6 com7 com8 com9 lpt1 lpt2 lpt3 lpt4 lpt5 lpt6 lpt7 lpt8 lpt9".split(" ")));
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
215 do {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
216 int i = sb.indexOf("/", x);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
217 if (i == -1) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
218 i = sb.length();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
219 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
220 // windows reserved filenames are at least of length 3
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
221 if (i - x >= 3) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
222 boolean found = false;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
223 if (i-x == 3 || i-x == 4) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
224 found = windowsReservedFilenames.contains(sb.subSequence(x, i));
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
225 } else if (sb.charAt(x+3) == '.') { // implicit i-x > 3
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
226 found = windowsReservedFilenames.contains(sb.subSequence(x, x+3));
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
227 } else if (i-x > 4 && sb.charAt(x+4) == '.') {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
228 found = windowsReservedFilenames.contains(sb.subSequence(x, x+4));
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
229 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
230 if (found) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
231 // x+2 as we change the third letter in device name
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
232 replace(sb, x+2, toHexByte(sb.charAt(x+2)));
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
233 i += 2;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
234 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
235 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
236 if (dotencode && (sb.charAt(x) == '.' || sb.charAt(x) == ' ')) {
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
237 char dotOrSpace = sb.charAt(x); // beware, replace() below changes charAt(x), rather get a copy
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
238 // not to get ~7e for '.' instead of ~2e, if later refactoring changes the logic
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
239 replace(sb, x, toHexByte(dotOrSpace));
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
240 i += 2;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
241 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
242 x = i+1;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
243 } while (x < sb.length());
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
244 }
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
245
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
246 // shall be synchronized in case of multithreaded use
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
247 private void appendEncoded(StringBuilder sb, char ch) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
248 charEncodingBuf.clear();
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
249 byteEncodingBuf.clear();
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
250 charEncodingBuf.put(ch).flip();
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
251 csEncoder.encode(charEncodingBuf, byteEncodingBuf, false);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
252 byteEncodingBuf.flip();
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
253 while (byteEncodingBuf.hasRemaining()) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
254 sb.append(toHexByte(byteEncodingBuf.get()));
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
255 }
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
256 }
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
257
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
258 /**
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
259 * replace char at sb[index] with a sequence
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
260 */
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
261 private static void replace(StringBuilder sb, int index, char[] with) {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
262 // there's StringBuilder.replace(int, int+1, String), but with char[] - I don't want to make a string out of hexEncodedByte
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
263 sb.setCharAt(index, with[0]);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
264 sb.insert(index+1, with, 1, with.length - 1);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
265 }
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
266
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
267 /**
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
268 * put hex representation of byte ch into buf from specified offset
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
269 */
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
270 private char[] toHexByte(int ch) {
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
271 final String hexDigits = "0123456789abcdef";
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
272 hexEncodedByte[1] = hexDigits.charAt((ch & 0x00F0) >>> 4);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
273 hexEncodedByte[2] = hexDigits.charAt(ch & 0x0F);
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
274 return hexEncodedByte;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
275 }
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
276 }