annotate src/org/tmatesoft/hg/internal/StoragePathHelper.java @ 411:464b4404e75d smartgit3

Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Tue, 20 Mar 2012 17:56:50 +0100
parents 6d2c6b2469fc
children 528b6780a8bd
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
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 // FIXME document what path argument is, whether it includes .i or .d, and whether it's 'normalized' (slashes) or not.
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 // since .hg/store keeps both .i files and files without extension (e.g. fncache), guees, for data == false
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
66 // we shall assume path has extension
292
a415fe296a50 Refactor PathRewrite to accept any char sequence, not only string
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 138
diff changeset
67 public CharSequence rewrite(CharSequence p) {
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
68 final String STR_STORE = "store/";
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 final String STR_DATA = "data/";
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 final String STR_DH = "dh/";
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
71 final String reservedChars = "\\:*?\"<>|";
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
73 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
74 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
75 // 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
76 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
77 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
78 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
79 do {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
80 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
81 } 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
82 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
83 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
84 } else {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
85 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
86 }
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
87
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 StringBuilder sb = new StringBuilder(path.length() << 1);
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 if (store || fncache) {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 for (int i = 0; i < path.length(); i++) {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 final char ch = path.charAt(i);
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 if (ch >= 'a' && ch <= 'z') {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 sb.append(ch); // POIRAE
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 } else if (ch >= 'A' && ch <= 'Z') {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 sb.append('_');
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 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
97 } 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
98 sb.append(toHexByte(ch));
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 } 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
100 sb.append(toHexByte(ch));
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 } else if (ch == '_') {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
102 sb.append('_');
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 } 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
105 // 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
106 // 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
107 // 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
108 // 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
109 // 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
110 // 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
111 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
112 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
113 } else {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
114 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
115 }
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
116 }
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 // auxencode
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
119 if (fncache) {
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
120 encodeWindowsDeviceNames(sb);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
121 }
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 final int MAX_PATH_LEN = 120;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
124 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
125 // 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
126 // 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
127 // 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
128 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
129 final int DIR_PREFIX_LEN = 8;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
130 // 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
131 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
132 sb = new StringBuilder(MAX_PATH_LEN);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
133 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
134 final char ch = path.charAt(i);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
135 if (ch >= 'a' && ch <= 'z') {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
136 sb.append(ch);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
137 } else if (ch >= 'A' && ch <= 'Z') {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
138 sb.append((char) (ch | 0x20)); // lowercase
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
139 } 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
140 sb.append(toHexByte(ch));
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
141 } 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
142 sb.append(toHexByte(ch));
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
143 } 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
144 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
145 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
146 } else {
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
147 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
148 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
149 }
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 encodeWindowsDeviceNames(sb);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
152 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
153 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
154 completeHashName.append(STR_STORE);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
155 completeHashName.append(STR_DH);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
156 if (fnameStart == -1) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
157 // no dirs, just long filename
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
158 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
159 completeHashName.append(sb);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
160 } else {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
161 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
162 int x = 0;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
163 do {
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
164 int i = sb.indexOf("/", x);
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
165 final int sb2Len = sb2.length();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
166 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
167 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
168 } else {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
169 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
170 // may unexpectedly end with bad character
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
171 final int last = sb2.length()-1;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
172 char lastChar = sb2.charAt(last);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
173 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
174 if (lastChar == '.' || lastChar == ' ') {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
175 sb2.setCharAt(last, '_');
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
176 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
177 sb2.append('/');
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
178 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
179 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
180 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
181 break;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
182 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
183 x = i+1;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
184 } while (x < fnameStart);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
185 assert sb2.charAt(sb2.length() - 1) == '/';
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
186 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
187 assert left >= 0;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
188 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
189 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
190 // 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
191 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
192 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
193 // stillAvailable > 0;
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
194 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
195 } else {
6d2c6b2469fc Issue 18: Invalid storage path for certain long names
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 292
diff changeset
196 // 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
197 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
198 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
199 completeHashName.append(sb2);
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
200 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
201 completeHashName.append(digest);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
202 sb = completeHashName;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
203 } else if (store) {
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
204 sb.insert(0, STR_STORE + STR_DATA);
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
205 }
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
206 sb.append(".i");
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
207 return sb.toString();
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
208 }
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
209
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
210 private void encodeWindowsDeviceNames(StringBuilder sb) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
211 int x = 0; // last segment start
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
212 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
213 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
214 do {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
215 int i = sb.indexOf("/", x);
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
216 if (i == -1) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
217 i = sb.length();
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
218 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
219 // 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
220 if (i - x >= 3) {
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
221 boolean found = false;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
222 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
223 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
224 } 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
225 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
226 } 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
227 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
228 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
229 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
230 // 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
231 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
232 i += 2;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
233 }
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 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
236 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
237 // 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
238 replace(sb, x, toHexByte(dotOrSpace));
83
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
239 i += 2;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
240 }
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
241 x = i+1;
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
242 } while (x < sb.length());
a5275143664c Complete path hash calculation of fncache requirement
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 80
diff changeset
243 }
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
244
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
245 // 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
246 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
247 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
248 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
249 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
250 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
251 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
252 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
253 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
254 }
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
255 }
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
256
411
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
257 /**
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
258 * 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
259 */
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
260 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
261 // 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
262 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
263 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
264 }
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 * 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
268 */
464b4404e75d Issue 29: Bad storage path translation - translate Unicode chars to filesystem encoding
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 346
diff changeset
269 private char[] toHexByte(int ch) {
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
270 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
271 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
272 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
273 return hexEncodedByte;
74
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
274 }
6f1b88693d48 Complete refactoring to org.tmatesoft
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
275 }