Mercurial > hg4j
annotate src/org/tmatesoft/hg/internal/FileSystemHelper.java @ 713:661e77dc88ba tip
Mac support: respect Mac alternatives of command-line arguments for common unix tools
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Sun, 03 Aug 2014 18:09:00 +0200 |
parents | 4e6179bde4fc |
children |
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(); |
713
661e77dc88ba
Mac support: respect Mac alternatives of command-line arguments for common unix tools
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
622
diff
changeset
|
49 } else if (Internals.runningOnMac()) { |
661e77dc88ba
Mac support: respect Mac alternatives of command-line arguments for common unix tools
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
622
diff
changeset
|
50 linkCmd = Arrays.asList("/bin/ln", "-s", "%2", "%1"); |
661e77dc88ba
Mac support: respect Mac alternatives of command-line arguments for common unix tools
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
622
diff
changeset
|
51 chmodCmd = Arrays.asList("/bin/chmod", "+x", "%1"); |
661e77dc88ba
Mac support: respect Mac alternatives of command-line arguments for common unix tools
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
622
diff
changeset
|
52 statCmd = Arrays.asList("stat", "-f", "%p", "%1"); |
580
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
53 } else { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
54 linkCmd = Arrays.asList("/bin/ln", "-s", "%2", "%1"); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
55 chmodCmd = Arrays.asList("/bin/chmod", "+x", "%1"); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
56 statCmd = Arrays.asList("stat", "--format=%a", "%1"); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
57 } |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
58 execHelper = new ProcessExecHelper(); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
59 } |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
60 |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
61 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
|
62 ArrayList<String> command = new ArrayList<String>(linkCmd); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
63 command.set(command.indexOf("%1"), linkName); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
64 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
|
65 command.set(command.indexOf("%2"), targetFilename); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 execHelper.cwd(parentDir); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 try { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
68 execHelper.exec(command); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
69 } catch (InterruptedException ex) { |
622
4e6179bde4fc
Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
580
diff
changeset
|
70 IOException e = new IOException(); |
4e6179bde4fc
Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
580
diff
changeset
|
71 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
|
72 throw e; |
580
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
73 } |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
74 } |
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 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
|
77 if (chmodCmd.isEmpty()) { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 return; |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
79 } |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 ArrayList<String> command = new ArrayList<String>(chmodCmd); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
81 command.set(command.indexOf("%1"), fname); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 execHelper.cwd(parentDir); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 try { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
84 execHelper.exec(command); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
85 } catch (InterruptedException ex) { |
622
4e6179bde4fc
Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
580
diff
changeset
|
86 IOException e = new IOException(); |
4e6179bde4fc
Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
580
diff
changeset
|
87 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
|
88 throw e; |
580
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
89 } |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
90 } |
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 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
|
93 if (statCmd.isEmpty()) { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
94 return defaultValue; |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
95 } |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
96 ArrayList<String> command = new ArrayList<String>(statCmd); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
97 command.set(command.indexOf("%1"), file.getAbsolutePath()); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
98 String result = null; |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
99 try { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
100 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
|
101 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
|
102 return defaultValue; |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
103 } |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
104 return Integer.parseInt(result, 8); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
105 } catch (InterruptedException ex) { |
622
4e6179bde4fc
Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
580
diff
changeset
|
106 IOException e = new IOException(); |
4e6179bde4fc
Update to comply with Java 1.5 target
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
580
diff
changeset
|
107 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
|
108 throw e; |
580
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
109 } catch (NumberFormatException ex) { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
110 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
|
111 return defaultValue; |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
112 } |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
113 } |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
114 } |