kitaev@213: /* kitaev@213: * Copyright (c) 2011 TMate Software Ltd kitaev@213: * kitaev@213: * This program is free software; you can redistribute it and/or modify kitaev@213: * it under the terms of the GNU General Public License as published by kitaev@213: * the Free Software Foundation; version 2 of the License. kitaev@213: * kitaev@213: * This program is distributed in the hope that it will be useful, kitaev@213: * but WITHOUT ANY WARRANTY; without even the implied warranty of kitaev@213: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the kitaev@213: * GNU General Public License for more details. kitaev@213: * kitaev@213: * For information on how to redistribute this software under kitaev@213: * the terms of a license other than GNU General Public License kitaev@213: * contact TMate Software at support@hg4j.com kitaev@213: */ kitaev@213: package org.tmatesoft.hg.internal; kitaev@213: kitaev@213: import java.util.Arrays; kitaev@213: import java.util.TreeSet; kitaev@213: kitaev@213: import org.tmatesoft.hg.util.PathRewrite; kitaev@213: kitaev@213: /** kitaev@213: * @see http://mercurial.selenic.com/wiki/CaseFoldingPlan kitaev@213: * @see http://mercurial.selenic.com/wiki/fncacheRepoFormat kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: class StoragePathHelper implements PathRewrite { kitaev@213: kitaev@213: private final boolean store; kitaev@213: private final boolean fncache; kitaev@213: private final boolean dotencode; kitaev@213: kitaev@213: public StoragePathHelper(boolean isStore, boolean isFncache, boolean isDotencode) { kitaev@213: store = isStore; kitaev@213: fncache = isFncache; kitaev@213: dotencode = isDotencode; kitaev@213: } kitaev@213: kitaev@213: // FIXME document what path argument is, whether it includes .i or .d, and whether it's 'normalized' (slashes) or not. kitaev@213: // since .hg/store keeps both .i files and files without extension (e.g. fncache), guees, for data == false kitaev@213: // we shall assume path has extension kitaev@213: public String rewrite(String path) { kitaev@213: final String STR_STORE = "store/"; kitaev@213: final String STR_DATA = "data/"; kitaev@213: final String STR_DH = "dh/"; kitaev@213: final String reservedChars = "\\:*?\"<>|"; kitaev@213: char[] hexByte = new char[2]; kitaev@213: kitaev@213: path = path.replace(".hg/", ".hg.hg/").replace(".i/", ".i.hg/").replace(".d/", ".d.hg/"); kitaev@213: StringBuilder sb = new StringBuilder(path.length() << 1); kitaev@213: if (store || fncache) { kitaev@213: // encodefilename kitaev@213: for (int i = 0; i < path.length(); i++) { kitaev@213: final char ch = path.charAt(i); kitaev@213: if (ch >= 'a' && ch <= 'z') { kitaev@213: sb.append(ch); // POIRAE kitaev@213: } else if (ch >= 'A' && ch <= 'Z') { kitaev@213: sb.append('_'); kitaev@213: sb.append(Character.toLowerCase(ch)); // Perhaps, (char) (((int) ch) + 32)? Even better, |= 0x20? kitaev@213: } else if (reservedChars.indexOf(ch) != -1) { kitaev@213: sb.append('~'); kitaev@213: sb.append(toHexByte(ch, hexByte)); kitaev@213: } else if ((ch >= '~' /*126*/ && ch <= 255) || ch < ' ' /*32*/) { kitaev@213: sb.append('~'); kitaev@213: sb.append(toHexByte(ch, hexByte)); kitaev@213: } else if (ch == '_') { kitaev@213: sb.append('_'); kitaev@213: sb.append('_'); kitaev@213: } else { kitaev@213: sb.append(ch); kitaev@213: } kitaev@213: } kitaev@213: // auxencode kitaev@213: if (fncache) { kitaev@213: encodeWindowsDeviceNames(sb); kitaev@213: } kitaev@213: } kitaev@213: final int MAX_PATH_LEN = 120; kitaev@213: if (fncache && (sb.length() + STR_DATA.length() + ".i".length() > MAX_PATH_LEN)) { kitaev@213: String digest = new DigestHelper().sha1(STR_DATA, path, ".i").asHexString(); kitaev@213: final int DIR_PREFIX_LEN = 8; kitaev@213: // not sure why (-4) is here. 120 - 40 = up to 80 for path with ext. dh/ + ext(.i) = 3+2 kitaev@213: final int MAX_DIR_PREFIX = 8 * (DIR_PREFIX_LEN + 1) - 4; kitaev@213: sb = new StringBuilder(MAX_PATH_LEN); kitaev@213: for (int i = 0; i < path.length(); i++) { kitaev@213: final char ch = path.charAt(i); kitaev@213: if (ch >= 'a' && ch <= 'z') { kitaev@213: sb.append(ch); kitaev@213: } else if (ch >= 'A' && ch <= 'Z') { kitaev@213: sb.append((char) (ch | 0x20)); // lowercase kitaev@213: } else if (reservedChars.indexOf(ch) != -1) { kitaev@213: sb.append('~'); kitaev@213: sb.append(toHexByte(ch, hexByte)); kitaev@213: } else if ((ch >= '~' /*126*/ && ch <= 255) || ch < ' ' /*32*/) { kitaev@213: sb.append('~'); kitaev@213: sb.append(toHexByte(ch, hexByte)); kitaev@213: } else { kitaev@213: sb.append(ch); kitaev@213: } kitaev@213: } kitaev@213: encodeWindowsDeviceNames(sb); kitaev@213: int fnameStart = sb.lastIndexOf("/"); // since we rewrite file names, it never ends with slash (for dirs, I'd pass length-2); kitaev@213: StringBuilder completeHashName = new StringBuilder(MAX_PATH_LEN); kitaev@213: completeHashName.append(STR_STORE); kitaev@213: completeHashName.append(STR_DH); kitaev@213: if (fnameStart == -1) { kitaev@213: // no dirs, just long filename kitaev@213: sb.setLength(MAX_PATH_LEN - 40 /*digest.length()*/ - STR_DH.length() - ".i".length()); kitaev@213: completeHashName.append(sb); kitaev@213: } else { kitaev@213: StringBuilder sb2 = new StringBuilder(MAX_PATH_LEN); kitaev@213: int x = 0; kitaev@213: do { kitaev@213: int i = sb.indexOf("/", x); kitaev@213: final int sb2Len = sb2.length(); kitaev@213: if (i-x <= DIR_PREFIX_LEN) { // a b c d e f g h / kitaev@213: sb2.append(sb, x, i + 1); // with slash kitaev@213: } else { kitaev@213: sb2.append(sb, x, x + DIR_PREFIX_LEN); kitaev@213: // may unexpectedly end with bad character kitaev@213: final int last = sb2.length()-1; kitaev@213: char lastChar = sb2.charAt(last); kitaev@213: assert lastChar == sb.charAt(x + DIR_PREFIX_LEN - 1); kitaev@213: if (lastChar == '.' || lastChar == ' ') { kitaev@213: sb2.setCharAt(last, '_'); kitaev@213: } kitaev@213: sb2.append('/'); kitaev@213: } kitaev@213: if (sb2.length()-1 > MAX_DIR_PREFIX) { kitaev@213: sb2.setLength(sb2Len); // strip off last segment, it's too much kitaev@213: break; kitaev@213: } kitaev@213: x = i+1; kitaev@213: } while (x < fnameStart); kitaev@213: assert sb2.charAt(sb2.length() - 1) == '/'; kitaev@213: int left = MAX_PATH_LEN - sb2.length() - 40 /*digest.length()*/ - STR_DH.length() - ".i".length(); kitaev@213: assert left >= 0; kitaev@213: fnameStart++; // move from / to actual name kitaev@213: sb2.append(sb, fnameStart, fnameStart + left > sb.length() ? sb.length() : fnameStart+left); kitaev@213: completeHashName.append(sb2); kitaev@213: } kitaev@213: completeHashName.append(digest); kitaev@213: sb = completeHashName; kitaev@213: } else if (store) { kitaev@213: sb.insert(0, STR_STORE + STR_DATA); kitaev@213: } kitaev@213: sb.append(".i"); kitaev@213: return sb.toString(); kitaev@213: } kitaev@213: kitaev@213: private void encodeWindowsDeviceNames(StringBuilder sb) { kitaev@213: char[] hexByte = new char[2]; kitaev@213: int x = 0; // last segment start kitaev@213: final TreeSet windowsReservedFilenames = new TreeSet(); kitaev@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(" "))); kitaev@213: do { kitaev@213: int i = sb.indexOf("/", x); kitaev@213: if (i == -1) { kitaev@213: i = sb.length(); kitaev@213: } kitaev@213: // windows reserved filenames are at least of length 3 kitaev@213: if (i - x >= 3) { kitaev@213: boolean found = false; kitaev@213: if (i-x == 3 || i-x == 4) { kitaev@213: found = windowsReservedFilenames.contains(sb.subSequence(x, i)); kitaev@213: } else if (sb.charAt(x+3) == '.') { // implicit i-x > 3 kitaev@213: found = windowsReservedFilenames.contains(sb.subSequence(x, x+3)); kitaev@213: } else if (i-x > 4 && sb.charAt(x+4) == '.') { kitaev@213: found = windowsReservedFilenames.contains(sb.subSequence(x, x+4)); kitaev@213: } kitaev@213: if (found) { kitaev@213: sb.insert(x+3, toHexByte(sb.charAt(x+2), hexByte)); kitaev@213: sb.setCharAt(x+2, '~'); kitaev@213: i += 2; kitaev@213: } kitaev@213: } kitaev@213: if (dotencode && (sb.charAt(x) == '.' || sb.charAt(x) == ' ')) { kitaev@213: sb.insert(x+1, toHexByte(sb.charAt(x), hexByte)); kitaev@213: sb.setCharAt(x, '~'); // setChar *after* charAt/insert to get ~2e, not ~7e for '.' kitaev@213: i += 2; kitaev@213: } kitaev@213: x = i+1; kitaev@213: } while (x < sb.length()); kitaev@213: } kitaev@213: kitaev@213: private static char[] toHexByte(int ch, char[] buf) { kitaev@213: assert buf.length > 1; kitaev@213: final String hexDigits = "0123456789abcdef"; kitaev@213: buf[0] = hexDigits.charAt((ch & 0x00F0) >>> 4); kitaev@213: buf[1] = hexDigits.charAt(ch & 0x0F); kitaev@213: return buf; kitaev@213: } kitaev@213: }