comparison src/org/tmatesoft/hg/internal/FileSystemHelper.java @ 580:bd5926e24aa3

Respect unix flags for checkout/revert
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Fri, 19 Apr 2013 20:30:34 +0200
parents
children 4e6179bde4fc
comparison
equal deleted inserted replaced
579:36e36b926747 580:bd5926e24aa3
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 static org.tmatesoft.hg.util.LogFacility.Severity.Warn;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.nio.ByteBuffer;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28
29 import org.tmatesoft.hg.core.SessionContext;
30
31 /**
32 * TODO Merge with RegularFileStats
33 *
34 * @author Artem Tikhomirov
35 * @author TMate Software Ltd.
36 */
37 public class FileSystemHelper {
38
39 private final SessionContext ctx;
40 private final List<String> linkCmd, chmodCmd, statCmd;
41 private final ProcessExecHelper execHelper;
42
43 public FileSystemHelper(SessionContext sessionContext) {
44 ctx = sessionContext;
45 if (Internals.runningOnWindows()) {
46 linkCmd = Arrays.asList("mklink", "%1", "%2");
47 chmodCmd = Collections.emptyList();
48 statCmd = Collections.emptyList();
49 } else {
50 linkCmd = Arrays.asList("/bin/ln", "-s", "%2", "%1");
51 chmodCmd = Arrays.asList("/bin/chmod", "+x", "%1");
52 statCmd = Arrays.asList("stat", "--format=%a", "%1");
53 }
54 execHelper = new ProcessExecHelper();
55 }
56
57 public void createSymlink(File parentDir, String linkName, byte[] target) throws IOException {
58 ArrayList<String> command = new ArrayList<String>(linkCmd);
59 command.set(command.indexOf("%1"), linkName);
60 String targetFilename = Internals.getFileEncoding(ctx).decode(ByteBuffer.wrap(target)).toString();
61 command.set(command.indexOf("%2"), targetFilename);
62 execHelper.cwd(parentDir);
63 try {
64 execHelper.exec(command);
65 } catch (InterruptedException ex) {
66 throw new IOException(ex);
67 }
68 }
69
70 public void setExecutableBit(File parentDir, String fname) throws IOException {
71 if (chmodCmd.isEmpty()) {
72 return;
73 }
74 ArrayList<String> command = new ArrayList<String>(chmodCmd);
75 command.set(command.indexOf("%1"), fname);
76 execHelper.cwd(parentDir);
77 try {
78 execHelper.exec(command);
79 } catch (InterruptedException ex) {
80 throw new IOException(ex);
81 }
82 }
83
84 public int getFileMode(File file, int defaultValue) throws IOException {
85 if (statCmd.isEmpty()) {
86 return defaultValue;
87 }
88 ArrayList<String> command = new ArrayList<String>(statCmd);
89 command.set(command.indexOf("%1"), file.getAbsolutePath());
90 String result = null;
91 try {
92 result = execHelper.exec(command).toString().trim();
93 if (result.isEmpty()) {
94 return defaultValue;
95 }
96 return Integer.parseInt(result, 8);
97 } catch (InterruptedException ex) {
98 throw new IOException(ex);
99 } catch (NumberFormatException ex) {
100 ctx.getLog().dump(getClass(), Warn, ex, String.format("Bad value for access rights:%s", result));
101 return defaultValue;
102 }
103 }
104 }