Mercurial > hg4j
annotate src/org/tmatesoft/hg/internal/FNCacheFile.java @ 616:5e0313485eef
encode directories as demanded by fncache format
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 14 May 2013 17:31:35 +0200 |
parents | 6ca3d0c5b4bc |
children | 507602cb4fb3 |
rev | line source |
---|---|
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
1 /* |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2013 TMate Software Ltd |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
3 * |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
4 * This program is free software; you can redistribute it and/or modify |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
5 * it under the terms of the GNU General Public License as published by |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
6 * the Free Software Foundation; version 2 of the License. |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
7 * |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
8 * This program is distributed in the hope that it will be useful, |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
11 * GNU General Public License for more details. |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
12 * |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
13 * For information on how to redistribute this software under |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
14 * the terms of a license other than GNU General Public License |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
15 * contact TMate Software at support@hg4j.com |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
16 */ |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
17 package org.tmatesoft.hg.internal; |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
18 |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
19 import java.io.File; |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
20 import java.io.FileOutputStream; |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
21 import java.io.IOException; |
616
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
22 import java.nio.ByteBuffer; |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
23 import java.nio.CharBuffer; |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
24 import java.nio.channels.FileChannel; |
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
25 import java.nio.charset.Charset; |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
26 import java.util.ArrayList; |
559
6ca3d0c5b4bc
Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
539
diff
changeset
|
27 import java.util.List; |
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
28 |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
29 import org.tmatesoft.hg.util.Path; |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
30 |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
31 /** |
559
6ca3d0c5b4bc
Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
539
diff
changeset
|
32 * Append-only fncache support |
6ca3d0c5b4bc
Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
539
diff
changeset
|
33 * |
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
34 * <blockquote> |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
35 * The fncache file contains the paths of all filelog files in the store as encoded by mercurial.filelog.encodedir. The paths are separated by '\n' (LF). |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
36 * </blockquote> |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
37 * @see http://mercurial.selenic.com/wiki/fncacheRepoFormat |
559
6ca3d0c5b4bc
Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
539
diff
changeset
|
38 * |
6ca3d0c5b4bc
Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
539
diff
changeset
|
39 * |
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
40 * @author Artem Tikhomirov |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
41 * @author TMate Software Ltd. |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
42 */ |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
43 public class FNCacheFile { |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
44 |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
45 private final Internals repo; |
559
6ca3d0c5b4bc
Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
539
diff
changeset
|
46 // private final List<Path> files; |
616
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
47 private final List<Path> addedDotI; |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
48 private final List<Path> addedDotD; |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
49 private final FNCachePathHelper pathHelper; |
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
50 |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
51 public FNCacheFile(Internals internalRepo) { |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
52 repo = internalRepo; |
559
6ca3d0c5b4bc
Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
539
diff
changeset
|
53 // files = new ArrayList<Path>(); |
616
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
54 pathHelper = new FNCachePathHelper(); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
55 addedDotI = new ArrayList<Path>(5); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
56 addedDotD = new ArrayList<Path>(5); |
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 } |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 |
559
6ca3d0c5b4bc
Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
539
diff
changeset
|
59 /* |
6ca3d0c5b4bc
Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
539
diff
changeset
|
60 * For append-only option, we don't care reading the original content |
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
61 public void read(Path.Source pathFactory) throws IOException { |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
62 File f = fncacheFile(); |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63 files.clear(); |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 if (!f.exists()) { |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
65 return; |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 } |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 ArrayList<String> entries = new ArrayList<String>(); |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
68 // names in fncache are in local encoding, shall translate to unicode |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
69 new LineReader(f, repo.getSessionContext().getLog(), repo.getFilenameEncoding()).read(new LineReader.SimpleLineCollector(), entries); |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
70 for (String e : entries) { |
559
6ca3d0c5b4bc
Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
539
diff
changeset
|
71 // FIXME plain wrong, need either to decode paths and strip off .i/.d or (if keep names as is) change write() |
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 files.add(pathFactory.path(e)); |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
73 } |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 } |
559
6ca3d0c5b4bc
Commit: tests and fixes for defects discovered
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
539
diff
changeset
|
75 */ |
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
76 |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 public void write() throws IOException { |
616
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
78 if (addedDotI.isEmpty() && addedDotD.isEmpty()) { |
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
79 return; |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 } |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
81 File f = fncacheFile(); |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 f.getParentFile().mkdirs(); |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 final Charset filenameEncoding = repo.getFilenameEncoding(); |
616
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
84 ArrayList<CharBuffer> added = new ArrayList<CharBuffer>(); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
85 for (Path p : addedDotI) { |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
86 added.add(CharBuffer.wrap(pathHelper.rewrite(p))); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
87 } |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
88 for (Path p : addedDotD) { |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
89 // XXX FNCachePathHelper always return name of an index file, need to change it into a name of data file, |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
90 // although the approach (to replace last char) is depressingly awful |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
91 CharSequence cs = pathHelper.rewrite(p); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
92 CharBuffer cb = CharBuffer.allocate(cs.length()); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
93 cb.append(cs); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
94 cb.put(cs.length()-1, 'd'); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
95 cb.flip(); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
96 added.add(cb); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
97 } |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
98 FileChannel fncacheFile = new FileOutputStream(f, true).getChannel(); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
99 ByteBuffer lf = ByteBuffer.wrap(new byte[] { 0x0A }); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
100 for (CharBuffer b : added) { |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
101 fncacheFile.write(filenameEncoding.encode(b)); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
102 fncacheFile.write(lf); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
103 lf.rewind(); |
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
104 } |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
105 fncacheFile.close(); |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
106 } |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
107 |
616
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
108 public void addIndex(Path p) { |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
109 addedDotI.add(p); |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
110 } |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
111 |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
112 public void addData(Path p) { |
5e0313485eef
encode directories as demanded by fncache format
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
559
diff
changeset
|
113 addedDotD.add(p); |
539
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
114 } |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
115 |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
116 private File fncacheFile() { |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
117 return repo.getFileFromStoreDir("fncache"); |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
118 } |
9edfd5a223b8
Commit: handle empty repository case
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
119 } |