View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.util;
45  
46  import static java.nio.charset.StandardCharsets.UTF_8;
47  
48  import java.io.File;
49  import java.io.PrintStream;
50  import java.nio.file.Files;
51  import java.nio.file.Path;
52  import java.security.AccessController;
53  import java.security.PrivilegedAction;
54  import java.util.ArrayList;
55  import java.util.Arrays;
56  import java.util.List;
57  
58  import org.eclipse.jgit.api.errors.JGitInternalException;
59  import org.eclipse.jgit.errors.CommandFailedException;
60  import org.eclipse.jgit.lib.Constants;
61  import org.eclipse.jgit.lib.Repository;
62  import org.slf4j.Logger;
63  import org.slf4j.LoggerFactory;
64  
65  /**
66   * FS implementation for Cygwin on Windows
67   *
68   * @since 3.0
69   */
70  public class FS_Win32_Cygwin extends FS_Win32 {
71  	private final static Logger LOG = LoggerFactory
72  			.getLogger(FS_Win32_Cygwin.class);
73  
74  	private static String cygpath;
75  
76  	/**
77  	 * Whether cygwin is found
78  	 *
79  	 * @return true if cygwin is found
80  	 */
81  	public static boolean isCygwin() {
82  		final String path = AccessController
83  				.doPrivileged(new PrivilegedAction<String>() {
84  					@Override
85  					public String run() {
86  						return System.getProperty("java.library.path"); //$NON-NLS-1$
87  					}
88  				});
89  		if (path == null)
90  			return false;
91  		File found = FS.searchPath(path, "cygpath.exe"); //$NON-NLS-1$
92  		if (found != null)
93  			cygpath = found.getPath();
94  		return cygpath != null;
95  	}
96  
97  	/**
98  	 * Constructor
99  	 */
100 	public FS_Win32_Cygwin() {
101 		super();
102 	}
103 
104 	/**
105 	 * Constructor
106 	 *
107 	 * @param src
108 	 *            instance whose attributes to copy
109 	 */
110 	protected FS_Win32_Cygwin(FS src) {
111 		super(src);
112 	}
113 
114 	/** {@inheritDoc} */
115 	@Override
116 	public FS newInstance() {
117 		return new FS_Win32_Cygwin(this);
118 	}
119 
120 	/** {@inheritDoc} */
121 	@Override
122 	public File resolve(File dir, String pn) {
123 		String useCygPath = System.getProperty("jgit.usecygpath"); //$NON-NLS-1$
124 		if (useCygPath != null && useCygPath.equals("true")) { //$NON-NLS-1$
125 			String w;
126 			try {
127 				w = readPipe(dir, //
128 					new String[] { cygpath, "--windows", "--absolute", pn }, // //$NON-NLS-1$ //$NON-NLS-2$
129 					UTF_8.name());
130 			} catch (CommandFailedException e) {
131 				LOG.warn(e.getMessage());
132 				return null;
133 			}
134 			if (!StringUtils.isEmptyOrNull(w)) {
135 				return new File(w);
136 			}
137 		}
138 		return super.resolve(dir, pn);
139 	}
140 
141 	/** {@inheritDoc} */
142 	@Override
143 	protected File userHomeImpl() {
144 		final String home = AccessController
145 				.doPrivileged(new PrivilegedAction<String>() {
146 					@Override
147 					public String run() {
148 						return System.getenv("HOME"); //$NON-NLS-1$
149 					}
150 				});
151 		if (home == null || home.length() == 0)
152 			return super.userHomeImpl();
153 		return resolve(new File("."), home); //$NON-NLS-1$
154 	}
155 
156 	/** {@inheritDoc} */
157 	@Override
158 	public ProcessBuilder runInShell(String cmd, String[] args) {
159 		List<String> argv = new ArrayList<>(4 + args.length);
160 		argv.add("sh.exe"); //$NON-NLS-1$
161 		argv.add("-c"); //$NON-NLS-1$
162 		argv.add(cmd + " \"$@\""); //$NON-NLS-1$
163 		argv.add(cmd);
164 		argv.addAll(Arrays.asList(args));
165 		ProcessBuilder proc = new ProcessBuilder();
166 		proc.command(argv);
167 		return proc;
168 	}
169 
170 	/** {@inheritDoc} */
171 	@Override
172 	public String relativize(String base, String other) {
173 		final String relativized = super.relativize(base, other);
174 		return relativized.replace(File.separatorChar, '/');
175 	}
176 
177 	/** {@inheritDoc} */
178 	@Override
179 	public ProcessResult runHookIfPresent(Repository repository, String hookName,
180 			String[] args, PrintStream outRedirect, PrintStream errRedirect,
181 			String stdinArgs) throws JGitInternalException {
182 		return internalRunHookIfPresent(repository, hookName, args, outRedirect,
183 				errRedirect, stdinArgs);
184 	}
185 
186 	/** {@inheritDoc} */
187 	@Override
188 	public File findHook(Repository repository, String hookName) {
189 		final File gitdir = repository.getDirectory();
190 		if (gitdir == null) {
191 			return null;
192 		}
193 		final Path hookPath = gitdir.toPath().resolve(Constants.HOOKS)
194 				.resolve(hookName);
195 		if (Files.isExecutable(hookPath))
196 			return hookPath.toFile();
197 		return null;
198 	}
199 }