Mercurial > jhg
annotate src/org/tmatesoft/hg/internal/FileSystemHelper.java @ 608:e1b29756f901
Clean, organize and resolve some TODOs and FIXMEs: minor refactorings and comments
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Tue, 07 May 2013 21:27:51 +0200 |
parents | bd5926e24aa3 |
children | 4e6179bde4fc |
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) { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
66 throw new IOException(ex); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
67 } |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
68 } |
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 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
|
71 if (chmodCmd.isEmpty()) { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
72 return; |
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 ArrayList<String> command = new ArrayList<String>(chmodCmd); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
75 command.set(command.indexOf("%1"), fname); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
76 execHelper.cwd(parentDir); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
77 try { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
78 execHelper.exec(command); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
79 } catch (InterruptedException ex) { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
80 throw new IOException(ex); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
81 } |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
82 } |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
83 |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
84 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
|
85 if (statCmd.isEmpty()) { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
86 return defaultValue; |
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 ArrayList<String> command = new ArrayList<String>(statCmd); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
89 command.set(command.indexOf("%1"), file.getAbsolutePath()); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
90 String result = null; |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
91 try { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
92 result = execHelper.exec(command).toString().trim(); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
93 if (result.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 return Integer.parseInt(result, 8); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
97 } catch (InterruptedException ex) { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
98 throw new IOException(ex); |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
99 } catch (NumberFormatException ex) { |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
100 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
|
101 return defaultValue; |
bd5926e24aa3
Respect unix flags for checkout/revert
Artem Tikhomirov <tikhomirov.artem@gmail.com>
parents:
diff
changeset
|
102 } |
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 } |