comparison src/org/tmatesoft/hg/internal/FNCacheFile.java @ 539:9edfd5a223b8

Commit: handle empty repository case
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 13 Feb 2013 18:44:58 +0100
parents
children 6ca3d0c5b4bc
comparison
equal deleted inserted replaced
538:dd4f6311af52 539:9edfd5a223b8
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.nio.charset.Charset;
23 import java.util.ArrayList;
24
25 import org.tmatesoft.hg.util.Path;
26
27 /**
28 * <blockquote>
29 * 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).
30 * </blockquote>
31 * @see http://mercurial.selenic.com/wiki/fncacheRepoFormat
32 * @author Artem Tikhomirov
33 * @author TMate Software Ltd.
34 */
35 public class FNCacheFile {
36
37 private final Internals repo;
38 private final ArrayList<Path> files;
39
40 public FNCacheFile(Internals internalRepo) {
41 repo = internalRepo;
42 files = new ArrayList<Path>();
43 }
44
45 public void read(Path.Source pathFactory) throws IOException {
46 File f = fncacheFile();
47 files.clear();
48 if (!f.exists()) {
49 return;
50 }
51 ArrayList<String> entries = new ArrayList<String>();
52 // names in fncache are in local encoding, shall translate to unicode
53 new LineReader(f, repo.getSessionContext().getLog(), repo.getFilenameEncoding()).read(new LineReader.SimpleLineCollector(), entries);
54 for (String e : entries) {
55 files.add(pathFactory.path(e));
56 }
57 }
58
59 public void write() throws IOException {
60 if (files.isEmpty()) {
61 return;
62 }
63 File f = fncacheFile();
64 f.getParentFile().mkdirs();
65 final Charset filenameEncoding = repo.getFilenameEncoding();
66 FileOutputStream fncacheFile = new FileOutputStream(f);
67 for (Path p : files) {
68 String s = "data/" + p.toString() + ".i"; // TODO post-1.0 this is plain wrong. (a) likely need .d files, too; (b) what about dh/ location?
69 fncacheFile.write(s.getBytes(filenameEncoding));
70 fncacheFile.write(0x0A); // http://mercurial.selenic.com/wiki/fncacheRepoFormat
71 }
72 fncacheFile.close();
73 }
74
75 public void add(Path p) {
76 files.add(p);
77 }
78
79 private File fncacheFile() {
80 return repo.getFileFromStoreDir("fncache");
81 }
82 }