annotate src/org/tmatesoft/hg/internal/FileSystemHelper.java @ 677:1c49c0cee540

Report line number at the first appearance, like 'hg annotate -l' does
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 18 Jul 2013 18:47:45 +0200
parents 4e6179bde4fc
children 661e77dc88ba
rev   line source
580
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
1 /*
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
2 * Copyright (c) 2013 TMate Software Ltd
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
3 *
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
4 * This program is free software; you can redistribute it and/or modify
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
5 * it under the terms of the GNU General Public License as published by
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
6 * the Free Software Foundation; version 2 of the License.
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
7 *
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
8 * This program is distributed in the hope that it will be useful,
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
11 * GNU General Public License for more details.
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
12 *
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
13 * For information on how to redistribute this software under
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
14 * the terms of a license other than GNU General Public License
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
15 * contact TMate Software at support@hg4j.com
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
16 */
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
17 package org.tmatesoft.hg.internal;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
18
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
19 import static org.tmatesoft.hg.util.LogFacility.Severity.Warn;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
20
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
21 import java.io.File;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
22 import java.io.IOException;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
23 import java.nio.ByteBuffer;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
24 import java.util.ArrayList;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
25 import java.util.Arrays;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
26 import java.util.Collections;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
27 import java.util.List;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
28
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
29 import org.tmatesoft.hg.core.SessionContext;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
30
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
31 /**
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
32 * TODO Merge with RegularFileStats
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
33 *
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
34 * @author Artem Tikhomirov
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
35 * @author TMate Software Ltd.
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
36 */
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
37 public class FileSystemHelper {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
38
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
39 private final SessionContext ctx;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
40 private final List<String> linkCmd, chmodCmd, statCmd;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
41 private final ProcessExecHelper execHelper;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
42
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
43 public FileSystemHelper(SessionContext sessionContext) {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
44 ctx = sessionContext;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
45 if (Internals.runningOnWindows()) {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
46 linkCmd = Arrays.asList("mklink", "%1", "%2");
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
47 chmodCmd = Collections.emptyList();
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
48 statCmd = Collections.emptyList();
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
49 } else {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
50 linkCmd = Arrays.asList("/bin/ln", "-s", "%2", "%1");
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
51 chmodCmd = Arrays.asList("/bin/chmod", "+x", "%1");
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
52 statCmd = Arrays.asList("stat", "--format=%a", "%1");
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
53 }
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
54 execHelper = new ProcessExecHelper();
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
55 }
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
56
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
57 public void createSymlink(File parentDir, String linkName, byte[] target) throws IOException {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
58 ArrayList<String> command = new ArrayList<String>(linkCmd);
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
59 command.set(command.indexOf("%1"), linkName);
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
60 String targetFilename = Internals.getFileEncoding(ctx).decode(ByteBuffer.wrap(target)).toString();
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
61 command.set(command.indexOf("%2"), targetFilename);
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
62 execHelper.cwd(parentDir);
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
63 try {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
64 execHelper.exec(command);
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
65 } catch (InterruptedException ex) {
622
4e6179bde4fc Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 580
diff changeset
66 IOException e = new IOException();
4e6179bde4fc Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 580
diff changeset
67 ex.initCause(ex); // XXX Java 1.5
4e6179bde4fc Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 580
diff changeset
68 throw e;
580
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
69 }
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
70 }
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
71
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
72 public void setExecutableBit(File parentDir, String fname) throws IOException {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
73 if (chmodCmd.isEmpty()) {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
74 return;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
75 }
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
76 ArrayList<String> command = new ArrayList<String>(chmodCmd);
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
77 command.set(command.indexOf("%1"), fname);
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
78 execHelper.cwd(parentDir);
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
79 try {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
80 execHelper.exec(command);
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
81 } catch (InterruptedException ex) {
622
4e6179bde4fc Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 580
diff changeset
82 IOException e = new IOException();
4e6179bde4fc Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 580
diff changeset
83 ex.initCause(ex); // XXX Java 1.5
4e6179bde4fc Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 580
diff changeset
84 throw e;
580
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
85 }
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
86 }
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
87
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
88 public int getFileMode(File file, int defaultValue) throws IOException {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
89 if (statCmd.isEmpty()) {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
90 return defaultValue;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
91 }
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
92 ArrayList<String> command = new ArrayList<String>(statCmd);
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
93 command.set(command.indexOf("%1"), file.getAbsolutePath());
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
94 String result = null;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
95 try {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
96 result = execHelper.exec(command).toString().trim();
622
4e6179bde4fc Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 580
diff changeset
97 if (result.length() == 0) { // XXX Java 1.5 isEmpty()
580
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
98 return defaultValue;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
99 }
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
100 return Integer.parseInt(result, 8);
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
101 } catch (InterruptedException ex) {
622
4e6179bde4fc Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 580
diff changeset
102 IOException e = new IOException();
4e6179bde4fc Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 580
diff changeset
103 ex.initCause(ex); // XXX Java 1.5
4e6179bde4fc Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents: 580
diff changeset
104 throw e;
580
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
105 } catch (NumberFormatException ex) {
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
106 ctx.getLog().dump(getClass(), Warn, ex, String.format("Bad value for access rights:%s", result));
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
107 return defaultValue;
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
108 }
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
109 }
bd5926e24aa3 Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff changeset
110 }