annotate hg4j/src/main/java/org/tmatesoft/hg/internal/StoragePathHelper.java @ 213:6ec4af642ba8 gradle

Project uses Gradle for build - actual changes
author Alexander Kitaev <kitaev@gmail.com>
date Tue, 10 May 2011 10:52:53 +0200
parents
children
rev   line source
213
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
1 /*
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2011 TMate Software Ltd
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
3 *
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
7 *
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
12 *
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
16 */
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
18
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
19 import java.util.Arrays;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
20 import java.util.TreeSet;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
21
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
22 import org.tmatesoft.hg.util.PathRewrite;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
23
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
24 /**
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
25 * @see http://mercurial.selenic.com/wiki/CaseFoldingPlan
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
26 * @see http://mercurial.selenic.com/wiki/fncacheRepoFormat
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
27 *
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
28 * @author Artem Tikhomirov
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
29 * @author TMate Software Ltd.
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
30 */
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
31 class StoragePathHelper implements PathRewrite {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
32
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
33 private final boolean store;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
34 private final boolean fncache;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
35 private final boolean dotencode;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
36
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
37 public StoragePathHelper(boolean isStore, boolean isFncache, boolean isDotencode) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
38 store = isStore;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
39 fncache = isFncache;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
40 dotencode = isDotencode;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
41 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
42
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
43 // FIXME document what path argument is, whether it includes .i or .d, and whether it's 'normalized' (slashes) or not.
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
44 // since .hg/store keeps both .i files and files without extension (e.g. fncache), guees, for data == false
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
45 // we shall assume path has extension
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
46 public String rewrite(String path) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
47 final String STR_STORE = "store/";
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
48 final String STR_DATA = "data/";
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
49 final String STR_DH = "dh/";
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
50 final String reservedChars = "\\:*?\"<>|";
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
51 char[] hexByte = new char[2];
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
52
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
53 path = path.replace(".hg/", ".hg.hg/").replace(".i/", ".i.hg/").replace(".d/", ".d.hg/");
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
54 StringBuilder sb = new StringBuilder(path.length() << 1);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
55 if (store || fncache) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
56 // encodefilename
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
57 for (int i = 0; i < path.length(); i++) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
58 final char ch = path.charAt(i);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
59 if (ch >= 'a' && ch <= 'z') {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
60 sb.append(ch); // POIRAE
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
61 } else if (ch >= 'A' && ch <= 'Z') {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
62 sb.append('_');
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
63 sb.append(Character.toLowerCase(ch)); // Perhaps, (char) (((int) ch) + 32)? Even better, |= 0x20?
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
64 } else if (reservedChars.indexOf(ch) != -1) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
65 sb.append('~');
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
66 sb.append(toHexByte(ch, hexByte));
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
67 } else if ((ch >= '~' /*126*/ && ch <= 255) || ch < ' ' /*32*/) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
68 sb.append('~');
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
69 sb.append(toHexByte(ch, hexByte));
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
70 } else if (ch == '_') {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
71 sb.append('_');
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
72 sb.append('_');
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
73 } else {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
74 sb.append(ch);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
75 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
76 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
77 // auxencode
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
78 if (fncache) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
79 encodeWindowsDeviceNames(sb);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
80 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
81 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
82 final int MAX_PATH_LEN = 120;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
83 if (fncache && (sb.length() + STR_DATA.length() + ".i".length() > MAX_PATH_LEN)) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
84 String digest = new DigestHelper().sha1(STR_DATA, path, ".i").asHexString();
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
85 final int DIR_PREFIX_LEN = 8;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
86 // not sure why (-4) is here. 120 - 40 = up to 80 for path with ext. dh/ + ext(.i) = 3+2
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
87 final int MAX_DIR_PREFIX = 8 * (DIR_PREFIX_LEN + 1) - 4;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
88 sb = new StringBuilder(MAX_PATH_LEN);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
89 for (int i = 0; i < path.length(); i++) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
90 final char ch = path.charAt(i);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
91 if (ch >= 'a' && ch <= 'z') {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
92 sb.append(ch);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
93 } else if (ch >= 'A' && ch <= 'Z') {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
94 sb.append((char) (ch | 0x20)); // lowercase
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
95 } else if (reservedChars.indexOf(ch) != -1) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
96 sb.append('~');
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
97 sb.append(toHexByte(ch, hexByte));
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
98 } else if ((ch >= '~' /*126*/ && ch <= 255) || ch < ' ' /*32*/) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
99 sb.append('~');
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
100 sb.append(toHexByte(ch, hexByte));
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
101 } else {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
102 sb.append(ch);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
103 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
104 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
105 encodeWindowsDeviceNames(sb);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
106 int fnameStart = sb.lastIndexOf("/"); // since we rewrite file names, it never ends with slash (for dirs, I'd pass length-2);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
107 StringBuilder completeHashName = new StringBuilder(MAX_PATH_LEN);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
108 completeHashName.append(STR_STORE);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
109 completeHashName.append(STR_DH);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
110 if (fnameStart == -1) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
111 // no dirs, just long filename
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
112 sb.setLength(MAX_PATH_LEN - 40 /*digest.length()*/ - STR_DH.length() - ".i".length());
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
113 completeHashName.append(sb);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
114 } else {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
115 StringBuilder sb2 = new StringBuilder(MAX_PATH_LEN);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
116 int x = 0;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
117 do {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
118 int i = sb.indexOf("/", x);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
119 final int sb2Len = sb2.length();
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
120 if (i-x <= DIR_PREFIX_LEN) { // a b c d e f g h /
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
121 sb2.append(sb, x, i + 1); // with slash
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
122 } else {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
123 sb2.append(sb, x, x + DIR_PREFIX_LEN);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
124 // may unexpectedly end with bad character
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
125 final int last = sb2.length()-1;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
126 char lastChar = sb2.charAt(last);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
127 assert lastChar == sb.charAt(x + DIR_PREFIX_LEN - 1);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
128 if (lastChar == '.' || lastChar == ' ') {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
129 sb2.setCharAt(last, '_');
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
130 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
131 sb2.append('/');
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
132 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
133 if (sb2.length()-1 > MAX_DIR_PREFIX) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
134 sb2.setLength(sb2Len); // strip off last segment, it's too much
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
135 break;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
136 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
137 x = i+1;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
138 } while (x < fnameStart);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
139 assert sb2.charAt(sb2.length() - 1) == '/';
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
140 int left = MAX_PATH_LEN - sb2.length() - 40 /*digest.length()*/ - STR_DH.length() - ".i".length();
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
141 assert left >= 0;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
142 fnameStart++; // move from / to actual name
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
143 sb2.append(sb, fnameStart, fnameStart + left > sb.length() ? sb.length() : fnameStart+left);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
144 completeHashName.append(sb2);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
145 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
146 completeHashName.append(digest);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
147 sb = completeHashName;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
148 } else if (store) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
149 sb.insert(0, STR_STORE + STR_DATA);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
150 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
151 sb.append(".i");
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
152 return sb.toString();
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
153 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
154
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
155 private void encodeWindowsDeviceNames(StringBuilder sb) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
156 char[] hexByte = new char[2];
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
157 int x = 0; // last segment start
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
158 final TreeSet<String> windowsReservedFilenames = new TreeSet<String>();
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
159 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(" ")));
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
160 do {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
161 int i = sb.indexOf("/", x);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
162 if (i == -1) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
163 i = sb.length();
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
164 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
165 // windows reserved filenames are at least of length 3
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
166 if (i - x >= 3) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
167 boolean found = false;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
168 if (i-x == 3 || i-x == 4) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
169 found = windowsReservedFilenames.contains(sb.subSequence(x, i));
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
170 } else if (sb.charAt(x+3) == '.') { // implicit i-x > 3
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
171 found = windowsReservedFilenames.contains(sb.subSequence(x, x+3));
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
172 } else if (i-x > 4 && sb.charAt(x+4) == '.') {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
173 found = windowsReservedFilenames.contains(sb.subSequence(x, x+4));
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
174 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
175 if (found) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
176 sb.insert(x+3, toHexByte(sb.charAt(x+2), hexByte));
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
177 sb.setCharAt(x+2, '~');
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
178 i += 2;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
179 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
180 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
181 if (dotencode && (sb.charAt(x) == '.' || sb.charAt(x) == ' ')) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
182 sb.insert(x+1, toHexByte(sb.charAt(x), hexByte));
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
183 sb.setCharAt(x, '~'); // setChar *after* charAt/insert to get ~2e, not ~7e for '.'
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
184 i += 2;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
185 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
186 x = i+1;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
187 } while (x < sb.length());
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
188 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
189
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
190 private static char[] toHexByte(int ch, char[] buf) {
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
191 assert buf.length > 1;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
192 final String hexDigits = "0123456789abcdef";
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
193 buf[0] = hexDigits.charAt((ch & 0x00F0) >>> 4);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
194 buf[1] = hexDigits.charAt(ch & 0x0F);
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
195 return buf;
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
196 }
6ec4af642ba8 Project uses Gradle for build - actual changes
Alexander Kitaev <kitaev@gmail.com>
parents:
diff changeset
197 }